path_url = Ithemes_Sync_Functions::get_url( $GLOBALS['ithemes_sync_path'] ); list( $this->self_url ) = explode( '?', $_SERVER['REQUEST_URI'] ); $this->self_url .= '?page=' . $this->page_name; add_action( 'ithemes_sync_settings_page_index', array( $this, 'index' ) ); add_action( 'admin_print_styles', array( $this, 'add_styles' ) ); add_action( 'admin_print_scripts', array( $this, 'add_scripts' ) ); } public function add_styles() { wp_enqueue_style( 'ithemes-updater-settings-page-style', "{$this->path_url}/css/settings-page.css" ); } public function add_scripts() { $var = 'ithemes-updater-settings-page-script'; $translations = array( 'confirm_dialog_text' => __( 'Are you sure that you wish to unsync this user?', 'it-l10n-ithemes-sync' ), ); wp_enqueue_script( $var, "{$this->path_url}/js/settings-page.js", array( 'jquery' ) ); wp_localize_script( $var, 'ithemes_sync_settings', $translations ); } public function index() { $this->options = $GLOBALS['ithemes-sync-settings']->get_options(); $this->handle_post_action(); $this->show_settings(); } private function handle_post_action() { $post_data = Ithemes_Sync_Functions::get_post_data( array( 'username', 'password', 'action', 'user' ), true, true ); $action = $post_data['action']; if ( 'authenticate' == $action ) $this->authenticate( $post_data ); else if ( 'deauthenticate' == $action ) $this->deauthenticate( $post_data ); $this->options = $GLOBALS['ithemes-sync-settings']->get_options(); } private function authenticate( $data ) { check_admin_referer( 'authenticate-user' ); require_once( $GLOBALS['ithemes_sync_path'] . '/server.php' ); $result = Ithemes_Sync_Server::authenticate( $data['username'], $data['password'] ); if ( is_wp_error( $result ) ) { $heading = __( 'The user could not be synced.', 'it-l10n-ithemes-sync' ); $code = $result->get_error_code(); if ( 'http_request_failed' == $code ) $message = sprintf( __( '
The iThemes Sync server was unable to be contacted. WordPress returned the following error when trying to contact the server:
%1$s
If you continue to experience problems, please contact iThemes support.
', 'it-l10n-ithemes-sync' ), $result->get_error_message(), 'http://ithemes.com/support/' ); else if ( 'ithemes-sync-server-failed-request' == $code ) $message = sprintf( __( 'The iThemes Sync server was unable to process the request at this time. Please try again in a few minutes.
If you continue to experience problems, please contact iThemes support.
', 'it-l10n-ithemes-sync' ), 'http://ithemes.com/support/' ); else $message = $result->get_error_message(); $this->add_error_message( $heading, $message ); return; } if ( empty( $result['key'] ) ) { $heading = __( 'The user could not be synced.', 'it-l10n-ithemes-sync' ); $message = __( 'The sync request failed due to a server communication error. The server sent a response that did not include a authentication key.', 'it-l10n-ithemes-sync' ); $this->add_error_message( $heading, $message ); return; } if ( empty( $result['user_id'] ) ) { $heading = __( 'The user could not be synced.', 'it-l10n-ithemes-sync' ); $message = __( 'The sync request failed due to a server communication error. The server sent a response that did not include a user ID.', 'it-l10n-ithemes-sync' ); $this->add_error_message( $heading, $message ); return; } $add_result = $GLOBALS['ithemes-sync-settings']->add_authentication( $result['user_id'], $data['username'], $result['key'] ); if ( is_wp_error( $add_result ) ) { $heading = __( 'The user could not be synced.', 'it-l10n-ithemes-sync' ); $message = $add_result->get_error_message(); $this->add_error_message( $heading, $message ); return; } $heading = __( 'Woohoo! Your site has been synced.', 'it-l10n-ithemes-sync' ); $messages = array(); if ( 0 == $result['quota']['available'] ) $messages[] = sprintf( __( 'Your user has now used all of the sites available to be added to Sync. More information can be found on the iThemes membership panel.', 'it-l10n-ithemes-sync' ), $this->sync_dashboard_url ); else $messages[] = sprintf( _n( 'Your user can Sync 1 additional site.', 'Your user can sync %d additional sites.', $result['quota']['available'], 'it-l10n-ithemes-sync' ), $result['quota']['available'] ); $this->add_success_message( $heading, $messages ); } private function deauthenticate( $data ) { require_once( $GLOBALS['ithemes_sync_path'] . '/server.php' ); $options = $GLOBALS['ithemes-sync-settings']->get_options(); $user_details = $GLOBALS['ithemes-sync-settings']->get_authentication_details( $data['user'] ); $result = Ithemes_Sync_Server::deauthenticate( $data['user'], $user_details['username'], $user_details['key'] ); if ( is_wp_error( $result ) && ( 'authentication' != $result->get_error_code() ) ) { $heading = __( 'The user could not be unsynced.', 'it-l10n-ithemes-sync' ); $message = $result->get_error_message(); $this->add_error_message( $heading, $message ); return; } $result = $GLOBALS['ithemes-sync-settings']->remove_authentication( $data['user'], $user_details['username'] ); if ( is_wp_error( $result ) ) { $heading = __( 'The user could not be unsynced.', 'it-l10n-ithemes-sync' ); $message = $result->get_error_message(); $this->add_error_message( $heading, $message ); return; } $heading = __( 'The user was successfully unsynced.', 'it-l10n-ithemes-sync' ); $this->add_success_message( $heading ); } private function show_messages() { foreach ( $this->messages as $class => $messages ) { foreach ( $messages as $message ) $this->show_message( $message['heading'], $message['messages'], $class ); } } private function show_message( $heading, $messages, $class ) { ?> messages[$class][] = compact( 'heading', 'messages' ); } private function add_success_message( $heading, $messages = array() ) { $this->add_message( $heading, $messages, 'success' ); } private function add_error_message( $heading, $messages = array() ) { $this->add_message( $heading, $messages, 'error' ); $this->had_error = true; } private function save_settings() { check_admin_referer( 'save_settings', 'ithemes_sync_nonce' ); $settings_defaults = array(); $settings = array(); foreach ( $settings_defaults as $var => $val ) { if ( isset( $_POST[$var] ) ) $settings[$var] = $_POST[$var]; else $settings[$var] = $val; } $GLOBALS['ithemes-sync-settings']->update_options( $settings ); $this->messages[] = __( 'Settings saved', 'it-l10n-ithemes-sync' ); } public function show_settings() { $post_data = Ithemes_Sync_Functions::get_post_data( array( 'username', 'password' ), true ); if ( ! is_multisite() ) { $validations = $GLOBALS['ithemes-sync-settings']->validate_authentications(); } $valid_users = array(); $invalid_users = array(); uksort( $this->options['authentications'], array( $this, 'sort_usernames' ) ); foreach ( array_keys( $this->options['authentications'] ) as $user_id ) { if ( ! isset( $validations ) || $validations[$user_id] ) { $valid_users[] = $user_id; } else { $invalid_users[] = $user_id; } } ?>View the list of synced users below, unsync users if needed, or add additional users below.', 'it-l10n-ithemes-sync' ); ?>