150 static $instance = array();
151 static $busy =
false;
159 $preferences = isset( $instance[
$name] ) ? $instance[
$name] : ( empty(
$user_id ) ? get_option( $name ) : get_user_meta(
$user_id, $name,
true ) );
161 if ( !is_object( $preferences ) || !is_a( $preferences, $class_name ) ) {
162 $preferences =
new $class_name( $name,
$user_id );
167 if ( version_compare( $preferences->version,
$version ) < 0 ) {
170 $preferences->update();
176 if (
false === $busy && isset( $_POST[
'wpdk_preferences_class'] ) && !empty( $_POST[
'wpdk_preferences_class'] ) &&
177 $_POST[
'wpdk_preferences_class'] == get_class( $preferences )
180 if ( isset( $_POST[
'wpdk_preferences_branch'] ) && !empty( $_POST[
'wpdk_preferences_branch'] ) ) {
181 $branch = $_POST[
'wpdk_preferences_branch'];
184 if ( isset( $_POST[
'reset-to-default-preferences'] ) ) {
185 add_action(
'wpdk_preferences_feedback-' . $branch, array(
187 'wpdk_preferences_feedback_reset'
189 $preferences->$branch->defaults();
190 $preferences->update();
194 elseif ( isset( $_POST[
'update-preferences'] ) ) {
195 add_action(
'wpdk_preferences_feedback-' . $branch, array(
197 'wpdk_preferences_feedback_update'
199 $preferences->$branch->update();
200 $preferences->update();
205 elseif ( isset( $_POST[
'wpdk_preferences_reset_all'] ) ) {
206 $preferences->defaults();
207 $preferences->update();
218 $instance[
$name] = $preferences;
220 return $instance[
$name];
233 $this->name = sanitize_title(
$name );
245 $message = __(
'Your preferences were successfully restored to defaults values!', WPDK_TEXTDOMAIN );
246 $alert =
new WPDKTwitterBootstrapAlert(
'info', $message, WPDKTwitterBootstrapAlertType::SUCCESS );
257 $message = __(
'Your preferences values were successfully updated!', WPDK_TEXTDOMAIN );
258 $alert =
new WPDKTwitterBootstrapAlert(
'info', $message, WPDKTwitterBootstrapAlertType::SUCCESS );
269 public function get()
271 return empty( $this->user_id ) ? get_option( $this->name ) : get_user_meta( $this->user_id, $this->name,
true );
282 die( __METHOD__ .
' must be override in your subclass' );
296 $store_version = $this->
get();
299 $subclass_name = get_class( $this );
302 $delta = $instance =
new $subclass_name( $this->name );
304 if ( !empty( $store_version ) ) {
307 if ( !is_a( $store_version, $subclass_name ) ) {
327 if ( empty( $this->user_id ) ) {
328 update_option( $this->name, $this );
331 update_user_meta( $this->user_id, $this->name, $this );
340 public function delete()
342 if ( empty( $this->user_id ) ) {
343 delete_option( $this->name );
346 delete_user_meta( $this->user_id, $this->name );
383 die( __METHOD__ .
' must be override in your subclass' );
465 return $import_export->preferences;
481 $this->error =
false;
484 if ( isset( $_POST[
'wpdk_preferences_export'] ) ) {
488 elseif ( isset( $_POST[
'wpdk_preferences_import'] ) ) {
490 add_action(
'wpdk_header_view_' .
$preferences->name .
'-header-view_after_title', array(
492 'wpdk_preferences_import_export_feedback'
495 if ( $_FILES[
'file'][
'error'] > 0 ) {
496 $this->error = self::ERROR_READ_FILE;
499 $this->
import( $_FILES[
'file'][
'tmp_name'] );
511 private function import( $filename )
513 $this->
import = unserialize( gzinflate( file_get_contents( $filename ) ) );
516 if ( !is_object( $this->
import ) || !is_a( $this->
import, get_class( $this->preferences ) ) ) {
517 $this->error = self::ERROR_MALFORMED_FILE;
522 if ( version_compare( $this->import->version, $this->preferences->version ) > 0 ) {
523 $this->error = self::ERROR_VERSION;
537 $this->preferences = $this->import;
540 $this->preferences->update();
548 private function download()
551 $filename = sprintf(
'%s.wpx', $this->preferences->name );
552 $filename = apply_filters(
'wpdk_preferences_export_filename', $filename, $this->preferences );
555 $buffer = gzdeflate( serialize( $this->preferences ) );
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' );
577 $title = __(
'Warning!', WPDK_TEXTDOMAIN );
580 switch ( $this->error ) {
583 case self::ERROR_NONE;
584 $title = __(
'Successfully!', WPDK_TEXTDOMAIN );
585 $content = __(
'Import complete.', WPDK_TEXTDOMAIN );
588 case self::ERROR_READ_FILE:
589 $content = sprintf(
'%s %s', __(
'Error while read file! Error code:', WPDK_TEXTDOMAIN ), $_FILES[
'file'][
'error'] );
592 case self::ERROR_MALFORMED_FILE:
593 $content = __(
'Malformed file.', WPDK_TEXTDOMAIN );
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 );
601 $alert =
new WPDKTwitterBootstrapAlert(
'feedback', $content, empty( $this->error ) ? WPDKTwitterBootstrapAlertType::SUCCESS : WPDKTwitterBootstrapAlertType::WARNING, $title );