plugin_dir . '/classes/class-addnow-admin-panel-base.php'; if ( ! class_exists( 'AddNow_Admin_Panel_Settings_View' ) ) { class AddNow_Admin_Panel_Settings_View extends AddNow_Admin_Panel_View_Base { CONST API_URL = 'http://api.addnow.com/'; var $action = ''; var $action_messages = array(); var $action_errors = array(); var $admin_url = ''; var $settings_page = 'addnow-plugin'; var $placement_options = array(); var $textarea_rows = 0; var $current_widget; var $list_table; /** * * @author Paul Menard * * @since 1.0 * */ function __construct() { parent::__construct(); $this->tabs = array( 'widgets' => array( 'tab' => __( 'Widgets', 'addnow' ), 'title' => __( 'Define the AddNow Sharing Buttons Widgets you will use on this site', 'addnow' ), ), 'settings' => array( 'tab' => __( 'Settings', 'addnow' ), 'title' => __( 'The AddNow plugin Settings', 'addnow' ), ), 'setup' => array( 'tab' => __( 'Setup', 'addnow' ), 'title' => __( 'How to setup and use this plugin', 'addnow' ), ), ); $this->set_default_tab(); $this->placement_options = array( 'top' => __( 'Top', 'addnow' ), 'bottom' => __( 'Bottom', 'addnow' ), ); $this->placement_options_singular = array( 'before_title' => __( 'Before Title', 'addnow' ), 'after_title' => __( 'After Title', 'addnow' ), 'top' => __( 'Before Content', 'addnow' ), 'bottom' => __( 'After Content', 'addnow' ), ); } /** * This function is called by WordPress when our Settings menu within the custom post type is * loaded. Here we register a custom stylesheet to effect some screen elements. * * @since 1.0.0 * @param none * @return none */ function on_load_panel() { global $addnow_plugin; if ( ( isset( $_GET['tab'] ) ) && ( isset( $this->tabs[ $_GET['tab'] ] ) ) ) { $this->tab_current = esc_attr( $_GET['tab'] ); } $this->admin_url = add_query_arg( array( 'page' => $_GET['page'], 'tab' => $this->tab_current, ) ); wp_enqueue_style( 'addnow-admin-styles', $addnow_plugin->plugin_url .'styles/addnow-admin-styles.css', array(), $addnow_plugin->version ); wp_enqueue_style( 'jquery-ui-smmothness-1.11.2', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css', array(), '1.11.2' ); wp_enqueue_script( 'jquery-ui-accordion' ); wp_enqueue_script( 'addnow-admin-settings-view', $addnow_plugin->plugin_url .'/scripts/addnow-admin-settings-view.js', array( 'jquery', 'jquery-ui-accordion' ), $addnow_plugin->version, true ); $this->admin_help(); $this->on_process_actions(); if (empty($addnow_plugin->settings['tracking_key'])) { $this->action_errors[$this->action] = ['The Api key must not be empty.']; } } /** * This function is called to show the Setting page we registered in the admin_menu() * function. This function handles all the HTML settings field output. * * @since 1.0.0 * @param none * @return none */ function on_show_panel() { global $addnow_plugin; ?>

action_messages[ $this->action ] ) ) { ?>

action_messages[ $this->action ] ?>

action_errors[ $this->action ] ) ) { ?>
action_errors[ $this->action ] as $key => $error) { if (is_array($error)) { foreach ($error as $e) { echo '
'.$key.': '.$e.'
'; } } else { echo '
'.$error.'
'; } } ?>
settings_page .'-'. $this->action .'-'. $this->tab_current .'-nonce', $this->settings_page .'-'. $this->action .'-'. $this->tab_current .'-nonce' ); ?> tab_current ) { case 'setup': $this->show_content_tab_setup(); break; case 'widgets': if ( ! empty( $this->current_widget ) || ! empty( $this->action_errors[ $this->action ] ) ) { $this->show_content_tab_widgets(); } else { ?>

list_table->prepare_items(); $this->list_table->display(); } break; case 'settings': default: $this->show_content_tab_settings(); break; } ?>
* * @since 1.0 * */ function show_content_tab_setup() { global $addnow_plugin; ?>

tabs[$this->tab_current]['title'] ?>

  1. AddNow.com.', 'addnow' ) ?>
  2. Sharing Buttons Widgets to be used on your website.', 'addnow' ) ?>

  1. AddNow then to the 'Widgets' tab. Click the 'Add New' button.'", 'addnow' ) ?>
* * @since 1.0 * */ function show_content_tab_widgets() { global $addnow_plugin; $this->getWidget(); ?>

tabs[$this->tab_current]['title'] ?>

settings['entity'])) { ?> current_widget['id'] ) ) { ?>
get_help_item( 'widget_name', 'tip' ); ?>
get_help_item( 'widget_active', 'tip' ); ?>
get_help_item( 'widget_entity', 'tip' ); ?>
get_help_item( 'widget_shortcode', 'tip' ); ?> [addnow id="' . stripslashes( $this->current_widget['id'] ) . '"]'; ?>

get_help_item( 'where', 'tip' ); ?>

placement_options as $placement_key => $placement_label ) { ?> placement_options as $placement_key => $placement_label ) { ?> placement_options as $placement_key => $placement_label ) { ?> placement_options as $placement_key => $placement_label ) { ?>
current_widget['placement']['is_home'][ $placement_key ] ) ) { echo ' checked="checked" '; } ?> />
current_widget['placement']['is_home']['page_on_front'][ $placement_key ] ) ) { echo ' checked="checked" '; } ?> />
current_widget['placement']['is_home']['page_for_posts'][ $placement_key ] ) ) { echo ' checked="checked" '; } ?> />
current_widget['placement']['is_search'][ $placement_key ] ) ) { echo ' checked="checked" '; } ?> />
true, 'show_ui' => true ), 'objects' ); if ( isset( $post_types['attachment'] ) ) { unset( $post_types['attachment'] ); } if ( ! empty( $post_types ) ) { ?>

$post_type ) { ?> placement_options_singular as $placement_key => $placement_label ) { ?>
current_widget['placement']['is_single'][ $post_type_slug ][ $placement_key ] ) ) { echo ' checked="checked" '; } ?>>
true, 'has_archive' => true ), 'objects' ); if ( isset( $post_types['attachment'] ) ) { unset( $post_types['attachment'] ); } if ( ! empty( $post_types ) ) { ?>

$post_type ) { ?> placement_options as $placement_key => $placement_label ) { ?>
current_widget['placement']['is_archive'][ $post_type_slug ][ $placement_key ] ) ) { echo ' checked="checked" '; } ?>>
true, 'show_ui' => true ), 'objects' ); if ( ! empty( $taxonomies ) ) { ?>

$taxonomy ) { if ( $taxonomy->public == true ) { ?> placement_options as $placement_key => $placement_label ) { ?>
current_widget['placement']['is_archive'][ $taxonomy_slug ][ $placement_key ] ) ) { echo ' checked="checked" '; } ?>>
__( 'Date', 'addnow' ), ); if ( ! empty( $archive_types ) ) { ?>

$archive_label ) { if ( $taxonomy->public === true ) { ?> placement_options as $placement_key => $placement_label ) { ?>
current_widget['placement']['is_archive'][ $archive_slug ][ $placement_key ] ) ) { echo ' checked="checked" '; } ?>>

settings['tracking_key'])) { _e( 'Set in settings Api key from AddNow', 'addnow' ); } else { _e( 'Add widgets to AddNow', 'addnow' ); } ?>
* * @since 1.0 * */ function show_content_tab_settings() { global $addnow_plugin; ?>

tabs[ $this->tab_current]['title'] ?>

get_help_item('tracking_key', 'tip'); ?>

* * @since 1.0 * */ function delete_widget( $widget_id = '' ) { global $addnow_plugin; if ( ! empty( $widget_id ) ) { if ( isset( $addnow_plugin->settings['widgets'][$widget_id] ) ) { unset( $addnow_plugin->settings['widgets'][$widget_id] ); return true; } } } /** * * @author Paul Menard * * @since 1.0 * */ function bulk_delete_widgets() { $delete_count = 0; if ((isset($_POST['addnow-widgets-bulk-items'])) && (!empty($_POST['addnow-widgets-bulk-items']))) { foreach($_POST['addnow-widgets-bulk-items'] as $widget_id) { $return_status = $this->delete_widget($widget_id); if ($return_status == true) $delete_count += 1; } } return $delete_count; } function getWidget() { global $addnow_plugin; $response = wp_remote_get( self::API_URL.'api/v1/sites', array('headers' => 'Authorization: api-key '. $addnow_plugin->settings['tracking_key'] )); if(! empty($response->errors)) { $this->action_errors[$this->action] = $response->errors; return; } if($response['response']['code'] !== 200) { $this->action_errors[$this->action] = json_decode( $response['body'] ); return; } $sites = json_decode( $response['body'] ); $widgets = []; foreach ($sites as $site) { $response = wp_remote_get( self::API_URL.'api/v1/sites/'.$site->id.'/widgets', array('headers' => 'Authorization: api-key '. $addnow_plugin->settings['tracking_key'] )); if(! empty($response->errors)) { $this->action_errors[$this->action] = $response->errors; return; } if($response['response']['code'] !== 200) { $this->action_errors[$this->action] = json_decode( $response['body'] ); return; } $response = array_map(function ($item) use ($site){ $item->hash_id = $site->hash_id; $item->domain = $site->domain; return $item; }, json_decode( $response['body'] )); $widgets = array_merge($widgets, $response); } $addnow_plugin->settings['entity'] = $widgets; $addnow_plugin->update_settings(); } /** * This function is called from admin_menu_settings() and handles processing the settings form updates * * @since 1.0.0 * @param none * @return none */ function on_process_actions() { global $addnow_plugin; if ( ( isset( $_POST['action'] ) ) && ( !empty( $_POST['action'] ) ) && ( $_POST['action'] != '-1' ) ) { $this->action = esc_attr( $_POST['action'] ); } else if ( ( isset( $_POST['action2'] ) ) && ( !empty( $_POST['action2'] ) ) && ( $_POST['action2'] != '-1' ) ) { $this->action = esc_attr( $_POST['action2'] ); } else if (( isset( $_GET['action'] ) ) && ( !empty( $_GET['action'] ) ) ) { $this->action = esc_attr( $_GET['action'] ); } if ($this->tab_current == 'widgets') { if ( ( $this->action == 'edit' ) && ( isset( $_GET['widget_id'] ) ) && ( isset( $addnow_plugin->settings['widgets'][$_GET['widget_id']] ) ) ) { $this->current_widget = $addnow_plugin->settings['widgets'][$_GET['widget_id']]; if (!isset($this->current_widget['active'])) $this->current_widget['active'] = 'yes'; $this->action = 'update'; } else if ( $this->action == 'new' ) { $this->current_widget['id'] = ''; $this->current_widget['name'] = ''; $this->current_widget['active'] = 'yes'; $this->current_widget['entity'] = ''; $this->current_widget['code'] = ''; $this->current_widget['where'] = array(); $this->current_widget['placement'] = array(); $this->action = 'update'; return; } elseif ($this->action == 'update') { if ( ( isset( $_POST['addnow_form']['widget'] ) ) && ( !empty( $_POST['addnow_form']['widget'] ) ) ) { if ( ( empty( $_POST['addnow_form']['widget']['name'] ) ) ) { $this->action_errors[$this->action] = ['Widget Name' => [0 => 'This field can not be empty.']]; return; } $nonce_key = $this->settings_page .'-'. $this->action .'-'. $this->tab_current .'-nonce'; if ( !isset( $_POST[$nonce_key] ) ) { return; } if ( !wp_verify_nonce( $_POST[$nonce_key], $nonce_key ) ) { return; } $widget = $_POST['addnow_form']['widget']; if ( ( !isset( $widget['id'] ) ) || ( empty( $widget['id'] ) ) ) { $widget_action = 'new'; $widget['id'] = sanitize_title_with_dashes($widget['name'], '', 'save'); $widget_id = $widget['id']; $_iter = 1; while(true) { $existing_widget = $addnow_plugin->get_setting( $widget_id, 'widgets' ); if (!empty($existing_widget)) { $_iter += 1; $widget_id = $widget['id'] .'-'. $_iter; } else { $widget['id'] = $widget_id; break; } } } if ((isset($widget['id'])) && (!empty($widget['id']))) { $SETTINGS_CHANGED = true; $addnow_plugin->settings['widgets'][$widget['id']] = $widget; $this->current_widget = $widget; if ( ( isset( $_POST['addnow_form']['widget']['entity'] ) ) && ( !empty( $_POST['addnow_form']['widget']['entity'] ) ) ) { $this->getWidget(); foreach ($addnow_plugin->settings['entity'] as $entity) { if ($entity->id == $_POST['addnow_form']['widget']['entity']) { $this->current_widget['entity'] = $entity; $this->current_widget['code'] = '
'; } } $addnow_plugin->settings['widgets'][$widget['id']] = $this->current_widget; } $this->action_messages[$this->action] = __('Widget Saved', 'addnow'); $addnow_plugin->update_settings(); } } } else { if ( $this->action == 'delete' ) { if ( ! isset($_GET['widget_id']) ) { return; } $widget_id = esc_attr( $_GET['widget_id'] ); if ( ! isset( $_GET['addnow-nonce'] ) ) { return; } $nonce_value = 'addnow-plugin-delete-widget-nonce-'. $widget_id; if ( !wp_verify_nonce( $_GET['addnow-nonce'], $nonce_value ) ) { return; } $SETTINGS_CHANGED = $this->delete_widget($widget_id); if ( $SETTINGS_CHANGED === true ) { $this->action_messages[$this->action] = __('Widget Deleted', 'addnow'); $addnow_plugin->update_settings(); } } else if ($this->action == 'delete-widget') { $delete_count = $this->bulk_delete_widgets(); if ($delete_count > 0) { $this->action_messages[$this->action] = sprintf(__('%d Widget(s) Deleted', 'addnow'), intval($delete_count)); $addnow_plugin->update_settings(); } } if ( isset( $addnow_plugin->settings['widgets'] ) && ! empty( $addnow_plugin->settings['widgets'] ) ) { include dirname( __FILE__ ) . "/class-addnow-admin-panel-widgets-list-table.php"; $this->list_table = new AddNow_Admin_Panel_Widgets_View_List_Table(); $screen = get_current_screen(); $screen_per_page_option = str_replace( '-', '_', $screen->id ."_per_page" ); if ((isset($_POST['wp_screen_options']['option'])) && ($_POST['wp_screen_options']['option'] == $screen_per_page_option)) { if (isset($_POST['wp_screen_options']['value'])) { $per_page = intval($_POST['wp_screen_options']['value']); if ((!$per_page) || ($per_page < 1)) { $per_page = 20; } update_user_meta( get_current_user_id(), $screen_per_page_option, $per_page ); } } $per_page = get_user_meta( get_current_user_id(), $screen_per_page_option, true ); if ( empty( $per_page ) || $per_page < 1 ) { $per_page = 20; } $this->list_table->per_page = $per_page; add_screen_option( 'per_page', array( 'label' => __( 'per Page', 'wp-product' ), 'default' => $per_page ) ); } else { $this->current_widget['id'] = ''; $this->current_widget['name'] = ''; $this->current_widget['active'] = 'yes'; $this->current_widget['entity'] = ''; $this->current_widget['code'] = ''; $this->current_widget['placement'] = array(); $this->action = 'update'; } return; } } else if ( $this->tab_current == 'settings' ) { if ( isset( $_POST['addnow_form']['tracking_key'] ) ) { $nonce_key = $this->settings_page .'-'. $this->action .'-'. $this->tab_current .'-nonce'; if ( ! isset( $_POST[ $nonce_key ] ) ) { return; } if ( ! wp_verify_nonce( $_POST[$nonce_key], $nonce_key ) ) { return; } if ( ! empty($_POST['addnow_form']['tracking_key']) ) { $response = wp_remote_get( self::API_URL.'api/v1/sites', array('headers' => 'Authorization: api-key '. $_POST['addnow_form']['tracking_key'] )); if(! empty($response->errors)) { $this->action_errors[$this->action] = $response->errors; $addnow_plugin->settings['tracking_key'] = ''; $addnow_plugin->settings['entity'] = []; $addnow_plugin->update_settings(); return; } if($response['response']['code'] !== 200) { $this->action_errors[$this->action] = json_decode( $response['body'] ); $addnow_plugin->settings['tracking_key'] = ''; $addnow_plugin->settings['entity'] = []; $addnow_plugin->update_settings(); return; } $addnow_plugin->settings['tracking_key'] = urlencode( $_POST['addnow_form']['tracking_key'] ); } else { $addnow_plugin->settings['tracking_key'] = ''; $addnow_plugin->settings['entity'] = []; } $this->action_messages[$this->action] = __( 'Settings Saved', 'addnow' ); $addnow_plugin->update_settings(); } } return; } } }