WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-preferences.php
Go to the documentation of this file.
1 <?php
2 
91 
99  public $name;
100 
108  public $version;
109 
117  public $user_id;
118 
148  public static function init( $name, $class_name, $version = false, $user_id = false )
149  {
150  static $instance = array();
151  static $busy = false;
152 
156  $preferences = null;
157 
158  $name = sanitize_title( $name );
159  $preferences = isset( $instance[$name] ) ? $instance[$name] : ( empty( $user_id ) ? get_option( $name ) : get_user_meta( $user_id, $name, true ) );
160 
161  if ( !is_object( $preferences ) || !is_a( $preferences, $class_name ) ) {
162  $preferences = new $class_name( $name, $user_id );
163  }
164 
165  if ( !empty( $version ) ) {
166  /* Or if the onfly version is different from stored version. */
167  if ( version_compare( $preferences->version, $version ) < 0 ) {
168  /* For i.e. you would like update the version property. */
169  $preferences->version = $version;
170  $preferences->update();
171  }
172  }
173 
174  /* Check for post data. */
175  if ( !isset( $instance[$name] ) && !wpdk_is_ajax() ) {
176  if ( false === $busy && isset( $_POST['wpdk_preferences_class'] ) && !empty( $_POST['wpdk_preferences_class'] ) &&
177  $_POST['wpdk_preferences_class'] == get_class( $preferences )
178  ) {
179  $busy = true;
180  if ( isset( $_POST['wpdk_preferences_branch'] ) && !empty( $_POST['wpdk_preferences_branch'] ) ) {
181  $branch = $_POST['wpdk_preferences_branch'];
182 
183  /* Reset to default a specified branch. */
184  if ( isset( $_POST['reset-to-default-preferences'] ) ) {
185  add_action( 'wpdk_preferences_feedback-' . $branch, array(
186  $preferences,
187  'wpdk_preferences_feedback_reset'
188  ) );
189  $preferences->$branch->defaults();
190  $preferences->update();
191  }
192 
193  /* Update a specified branch. */
194  elseif ( isset( $_POST['update-preferences'] ) ) {
195  add_action( 'wpdk_preferences_feedback-' . $branch, array(
196  $preferences,
197  'wpdk_preferences_feedback_update'
198  ) );
199  $preferences->$branch->update();
200  $preferences->update();
201  }
202  }
203 
204  /* Reset all preferences. */
205  elseif ( isset( $_POST['wpdk_preferences_reset_all'] ) ) {
206  $preferences->defaults();
207  $preferences->update();
208  }
209 
210  /* Try for import/export. */
211  else {
212  $preferences = WPDKPreferencesImportExport::init( $preferences );
213  }
214  }
215  $busy = false;
216  }
217 
218  $instance[$name] = $preferences;
219 
220  return $instance[$name];
221  }
222 
231  protected function __construct( $name, $user_id = false )
232  {
233  $this->name = sanitize_title( $name );
234  $this->user_id = $user_id;
235  $this->defaults();
236  }
237 
244  {
245  $message = __( 'Your preferences were successfully restored to defaults values!', WPDK_TEXTDOMAIN );
246  $alert = new WPDKTwitterBootstrapAlert( 'info', $message, WPDKTwitterBootstrapAlertType::SUCCESS );
247  $alert->display();
248  }
249 
256  {
257  $message = __( 'Your preferences values were successfully updated!', WPDK_TEXTDOMAIN );
258  $alert = new WPDKTwitterBootstrapAlert( 'info', $message, WPDKTwitterBootstrapAlertType::SUCCESS );
259  $alert->display();
260  }
261 
269  public function get()
270  {
271  return empty( $this->user_id ) ? get_option( $this->name ) : get_user_meta( $this->user_id, $this->name, true );
272  }
273 
274 
280  public function defaults()
281  {
282  die( __METHOD__ . ' must be override in your subclass' );
283  }
284 
292  public function delta()
293  {
294 
295  /* Check if exists a store version. */
296  $store_version = $this->get();
297 
298  /* Get subclass name. */
299  $subclass_name = get_class( $this );
300 
301  /* Prepare an onfly instance. */
302  $delta = $instance = new $subclass_name( $this->name );
303 
304  if ( !empty( $store_version ) ) {
305 
306  /* In rare case could happen that the stored class is different from onfly class. */
307  if ( !is_a( $store_version, $subclass_name ) ) {
308  $this->delete();
309  $instance->update();
310  }
311  else {
312  /* Do delta. */
313  $delta = WPDKObject::__delta( $instance, $store_version );
314  $delta->update();
315  }
316  }
317  return $delta;
318  }
319 
325  public function update()
326  {
327  if ( empty( $this->user_id ) ) {
328  update_option( $this->name, $this );
329  }
330  else {
331  update_user_meta( $this->user_id, $this->name, $this );
332  }
333  }
334 
340  public function delete()
341  {
342  if ( empty( $this->user_id ) ) {
343  delete_option( $this->name );
344  }
345  else {
346  delete_user_meta( $this->user_id, $this->name );
347  }
348  }
349 
350 }
351 
363 
371  public function __construct()
372  {
373  $this->defaults();
374  }
375 
381  public function defaults()
382  {
383  die( __METHOD__ . ' must be override in your subclass' );
384  }
385 
398  public function update()
399  {
400  // Override to process post data
401  }
402 
403 }
404 
405 
419 
420  const ERROR_NONE = false;
421  const ERROR_READ_FILE = 1;
423  const ERROR_VERSION = 3;
424  const ERROR_USER_ID = 4; // Not used at this momnent
425 
433  private $error;
434 
442  private $import;
443 
451  public $preferences;
452 
462  public static function init( $preferences )
463  {
464  $import_export = new WPDKPreferencesImportExport( $preferences );
465  return $import_export->preferences;
466  }
467 
477  private function __construct( $preferences )
478  {
479  $this->preferences = $preferences;
480  $this->import = '';
481  $this->error = false;
482 
483  /* Check post data. */
484  if ( isset( $_POST['wpdk_preferences_export'] ) ) {
485  $this->download();
486  }
487  /* Import. */
488  elseif ( isset( $_POST['wpdk_preferences_import'] ) ) {
489  //add_filter( 'wpdk_preferences_import_export_feedback', array( $this, 'wpdk_preferences_import_export_feedback' ) );
490  add_action( 'wpdk_header_view_' . $preferences->name . '-header-view_after_title', array(
491  $this,
492  'wpdk_preferences_import_export_feedback'
493  ), 99 );
494 
495  if ( $_FILES['file']['error'] > 0 ) {
496  $this->error = self::ERROR_READ_FILE;
497  }
498  else {
499  $this->import( $_FILES['file']['tmp_name'] );
500  }
501  }
502  }
503 
511  private function import( $filename )
512  {
513  $this->import = unserialize( gzinflate( file_get_contents( $filename ) ) );
514 
515  /* Check for error in file structure. */
516  if ( !is_object( $this->import ) || !is_a( $this->import, get_class( $this->preferences ) ) ) {
517  $this->error = self::ERROR_MALFORMED_FILE;
518  return;
519  }
520 
521  /* Check for wrong version. */
522  if ( version_compare( $this->import->version, $this->preferences->version ) > 0 ) {
523  $this->error = self::ERROR_VERSION;
524  return;
525  }
526 
527  /* @todo Check for import preferences from other users
528  * if ( !empty( $this->import->user_id ) ) {
529  * $user_id = get_current_user_id();
530  * if ( $user_id !== $this->import->user_id ) {
531  * $this->error = self::ERROR_USER_ID;
532  * return;
533  * }
534  * }
535  */
536 
537  $this->preferences = $this->import;
538 
539  /* Apply. */
540  $this->preferences->update();
541  }
542 
548  private function download()
549  {
550  /* Create a filtrable filename. Default `name-preferences.wpx`. */
551  $filename = sprintf( '%s.wpx', $this->preferences->name );
552  $filename = apply_filters( 'wpdk_preferences_export_filename', $filename, $this->preferences );
553 
554  /* GZIP the object. */
555  $buffer = gzdeflate( serialize( $this->preferences ) );
556 
557  header( 'Content-Type: application/download' );
558  header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
559  header( 'Cache-Control: public' );
560  header( "Content-Length: " . strlen( $buffer ) );
561  header( 'Pragma: no-cache' );
562  header( 'Expires: 0' );
563 
564  echo $buffer;
565  exit;
566  }
567 
576  {
577  $title = __( 'Warning!', WPDK_TEXTDOMAIN );
578  $content = '';
579 
580  switch ( $this->error ) {
581 
582  /* All ok. */
583  case self::ERROR_NONE;
584  $title = __( 'Successfully!', WPDK_TEXTDOMAIN );
585  $content = __( 'Import complete.', WPDK_TEXTDOMAIN );
586  break;
587  /* Error while reading upload file. */
588  case self::ERROR_READ_FILE:
589  $content = sprintf( '%s %s', __( 'Error while read file! Error code:', WPDK_TEXTDOMAIN ), $_FILES['file']['error'] );
590  break;
591  /* Error while uncompress upload file. */
592  case self::ERROR_MALFORMED_FILE:
593  $content = __( 'Malformed file.', WPDK_TEXTDOMAIN );
594  break;
595  /* Version export error. */
596  case self::ERROR_VERSION:
597  $content = __( 'Wrong file version! You are try to import a most recent of export file. Please update your plugin before continue.', WPDK_TEXTDOMAIN );
598  break;
599  }
600 
601  $alert = new WPDKTwitterBootstrapAlert( 'feedback', $content, empty( $this->error ) ? WPDKTwitterBootstrapAlertType::SUCCESS : WPDKTwitterBootstrapAlertType::WARNING, $title );
602  $alert->display();
603  }
604 
605 }