| @@ -1,143 +1,158 @@ | ||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 2 | + | |
| 3 | +defined( 'ABSPATH' ) || exit; | |
| 4 | + | |
| 3 | 5 | if ( ! class_exists( 'Dfrapi_Initialize' ) ) { |
| 4 | 6 | |
| 7 | + class Dfrapi_Initialize { | |
| 5 | 8 | |
| 6 | -class Dfrapi_Initialize { | |
| 9 | + public function __construct() { | |
| 7 | 10 | |
| 8 | - public function __construct() { | |
| 11 | + // Actions | |
| 12 | + add_action( 'plugins_loaded', [ $this, 'initialize_classes' ] ); | |
| 13 | + add_action( 'admin_menu', [ $this, 'admin_menu' ] ); | |
| 14 | + add_action( 'admin_menu', [ $this, 'networks_menu' ], 20 ); | |
| 15 | + add_action( 'admin_menu', [ $this, 'merchants_menu' ], 30 ); | |
| 16 | + add_action( 'wp_ajax_search_form', [ $this, 'ajax_search_form' ] ); | |
| 9 | 17 | |
| 10 | - // Core admin functions. | |
| 11 | - require_once( DFRAPI_PATH . 'functions/admin-functions.php' ); | |
| 12 | - | |
| 13 | - // Load required classes. | |
| 14 | - require_once( DFRAPI_PATH . 'classes/class-dfrapi-env.php' ); // Checks environment for any problems. | |
| 15 | - require_once( DFRAPI_PATH . 'classes/class-dfrapi-configuration.php' ); // Configuration page. | |
| 16 | - require_once( DFRAPI_PATH . 'classes/class-dfrapi-networks.php' ); // Networks page. | |
| 17 | - require_once( DFRAPI_PATH . 'classes/class-dfrapi-merchants.php' ); // Merchants page. | |
| 18 | - require_once( DFRAPI_PATH . 'classes/class-dfrapi-tools.php' ); // Tools page. | |
| 19 | - require_once( DFRAPI_PATH . 'classes/class-dfrapi-export.php' ); // Export page. | |
| 20 | - require_once( DFRAPI_PATH . 'classes/class-dfrapi-import.php' ); // Import page. | |
| 21 | - require_once( DFRAPI_PATH . 'classes/class-dfrapi-account.php' ); // Account page. | |
| 22 | - require_once( DFRAPI_PATH . 'classes/class-dfrapi-help.php' ); // Help tabs. | |
| 23 | - | |
| 24 | - // Hooks | |
| 25 | - add_action( 'admin_enqueue_scripts', array( $this, 'load_css' ) ); | |
| 26 | - add_action( 'admin_enqueue_scripts', array( $this, 'load_js' ) ); | |
| 27 | - add_action( 'plugins_loaded', array( $this, 'initialize_classes' ) ); | |
| 28 | - add_action( 'admin_notices', array( $this, 'admin_notices' ) ); | |
| 29 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); | |
| 30 | - add_action( 'wp_ajax_search_form', array( $this, 'ajax_search_form' ) ); | |
| 18 | + // Datafeedr Admin Pages have been loaded. Broadcast "loaded" action. | |
| 19 | + do_action( 'dfrapi_loaded' ); | |
| 20 | + } | |
| 31 | 21 | |
| 32 | - add_filter( 'plugin_action_links_' . DFRAPI_BASENAME, array( $this, 'action_links' ) ); | |
| 33 | - add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 ); | |
| 22 | + function ajax_search_form() { | |
| 34 | 23 | |
| 35 | - do_action( 'dfrapi_loaded' ); | |
| 36 | - | |
| 37 | - } | |
| 24 | + check_ajax_referer( 'dfrapi_search_form', 'dfrapi_security' ); | |
| 38 | 25 | |
| 39 | - function ajax_search_form() { | |
| 40 | - $sform = new Dfrapi_SearchForm(); | |
| 41 | - echo $sform->ajaxHandler(); | |
| 42 | - die; | |
| 43 | - } | |
| 26 | + /** | |
| 27 | + * Filters the capability required to use the search form's AJAX handler. | |
| 28 | + * | |
| 29 | + * @since 1.4.3 | |
| 30 | + * | |
| 31 | + * @param string $capability Default 'edit_posts'. | |
| 32 | + */ | |
| 33 | + $capability = apply_filters( 'dfrapi_search_form_capability', 'edit_posts' ); | |
| 44 | 34 | |
| 45 | - function admin_menu() { | |
| 46 | - add_menu_page( | |
| 47 | - __( 'Datafeedr API', DFRAPI_DOMAIN ), | |
| 48 | - __( 'Datafeedr API', DFRAPI_DOMAIN ), | |
| 49 | - 'manage_options', | |
| 50 | - 'dfrapi', | |
| 51 | - '', | |
| 52 | - null, | |
| 53 | - 42 | |
| 54 | - ); | |
| 55 | - } | |
| 56 | - | |
| 57 | - function initialize_classes() { | |
| 58 | - | |
| 59 | - new Dfrapi_Configuration(); | |
| 60 | - | |
| 61 | - // Show "Networks" page if API keys are present. | |
| 62 | - if ( Dfrapi_Env::api_keys_exist() ) { | |
| 63 | - new Dfrapi_Networks(); | |
| 35 | + if ( ! current_user_can( $capability ) ) { | |
| 36 | + wp_die( -1, 403 ); | |
| 37 | + } | |
| 38 | + | |
| 39 | + $sform = new Dfrapi_SearchForm(); | |
| 40 | + echo $sform->ajaxHandler(); | |
| 41 | + die; | |
| 64 | 42 | } |
| 65 | - | |
| 66 | - // Show "Merchants" page if API keys are present AND a network is selected. | |
| 67 | - if ( Dfrapi_Env::api_keys_exist() && Dfrapi_Env::network_is_selected() ) { | |
| 68 | - new Dfrapi_Merchants(); | |
| 43 | + | |
| 44 | + function admin_menu() { | |
| 45 | + add_menu_page( | |
| 46 | + __( 'Datafeedr API', 'datafeedr-api' ), | |
| 47 | + __( 'Datafeedr API', 'datafeedr-api' ), | |
| 48 | + 'manage_options', | |
| 49 | + 'dfrapi', | |
| 50 | + '', | |
| 51 | + null, | |
| 52 | + 42 | |
| 53 | + ); | |
| 69 | 54 | } |
| 70 | - | |
| 71 | - // Show Tools, Export and Import pages if API keys are present. | |
| 72 | - if ( Dfrapi_Env::api_keys_exist() ) { | |
| 73 | - new Dfrapi_Tools(); | |
| 74 | - new Dfrapi_Export(); | |
| 75 | - new Dfrapi_Import(); | |
| 76 | - new Dfrapi_Account(); | |
| 55 | + | |
| 56 | + public function networks_menu() { | |
| 57 | + if ( dfrapi_datafeedr_api_keys_exist() ) { | |
| 58 | + add_submenu_page( | |
| 59 | + 'dfrapi', | |
| 60 | + __( 'Networks — Datafeedr API', 'datafeedr-api' ), | |
| 61 | + __( 'Networks', 'datafeedr-api' ), | |
| 62 | + 'manage_options', | |
| 63 | + 'dfrapi_networks', | |
| 64 | + array( $this, 'networks_output' ) | |
| 65 | + ); | |
| 66 | + } | |
| 77 | 67 | } |
| 78 | - } | |
| 79 | - | |
| 80 | - function load_css() { | |
| 81 | - | |
| 82 | - // Basic styling for API pages. | |
| 83 | - wp_register_style( 'dfrapi_css', DFRAPI_URL . 'css/style.css', false, DFRAPI_VERSION ); | |
| 84 | - wp_enqueue_style( 'dfrapi_css' ); | |
| 85 | - | |
| 86 | - // Basic styling for API pages. | |
| 87 | - wp_register_style( 'dfrapi_searchform', DFRAPI_URL . 'css/searchform.css', false, DFRAPI_VERSION ); | |
| 88 | - wp_enqueue_style( 'dfrapi_searchform' ); | |
| 89 | - } | |
| 90 | - | |
| 91 | - function load_js() { | |
| 92 | - | |
| 93 | - wp_register_script( 'dfrapi_general_js', DFRAPI_URL.'js/general.js', array( 'jquery' ), DFRAPI_VERSION, true ); | |
| 94 | - wp_enqueue_script( 'dfrapi_general_js' ); | |
| 95 | - | |
| 96 | - wp_register_script( 'dfrapi_searchfilter_js', DFRAPI_URL.'js/searchfilter.js', array( 'jquery' ), DFRAPI_VERSION, false ); | |
| 97 | - wp_enqueue_script( 'dfrapi_searchfilter_js' ); | |
| 98 | - | |
| 99 | - wp_register_script( 'dfrapi_merchants_js', DFRAPI_URL.'js/merchants.js', array( 'jquery' ), DFRAPI_VERSION, false ); | |
| 100 | - wp_enqueue_script( 'dfrapi_merchants_js' ); | |
| 101 | - | |
| 102 | - wp_register_script( 'dfrapi_searchform_js', DFRAPI_URL.'js/searchform.js', array( 'jquery' ), DFRAPI_VERSION, false ); | |
| 103 | - wp_enqueue_script( 'dfrapi_searchform_js' ); | |
| 104 | 68 | |
| 105 | - wp_register_script( 'dfrapi_jquery_reveal_js', DFRAPI_URL.'js/jquery.reveal.js', array( 'jquery' ), DFRAPI_VERSION, false ); | |
| 106 | - wp_enqueue_script( 'dfrapi_jquery_reveal_js' ); | |
| 107 | - } | |
| 69 | + public function networks_output() { | |
| 70 | + $key = 'dfrapi_networks'; | |
| 71 | + $page = 'dfrapi-networks'; | |
| 72 | + echo '<div class="wrap" id="' . $key . '">'; | |
| 73 | + echo '<h2>' . dfrapi_setting_pages( $page ) . ' — Datafeedr API</h2>'; | |
| 74 | + echo '<form method="post" action="options.php">'; | |
| 75 | + submit_button(); | |
| 76 | + wp_nonce_field( 'update-options' ); | |
| 77 | + settings_fields( $page ); | |
| 78 | + do_settings_sections( $page ); | |
| 79 | + submit_button(); | |
| 80 | + echo '</form>'; | |
| 81 | + echo '<div id="dfr_unload_message" style="display:none">' . | |
| 82 | + __( 'You have unsaved changes', 'datafeedr-api' ) . | |
| 83 | + '</div>'; | |
| 84 | + echo '</div>'; | |
| 85 | + } | |
| 108 | 86 | |
| 109 | - function admin_notices() { | |
| 110 | - $dfrapi_env = new Dfrapi_Env(); | |
| 111 | - if ( $notices = get_option( 'dfrapi_admin_notices' ) ) { | |
| 112 | - foreach ( $notices as $key => $message ) { | |
| 113 | - $button = ( $message['url'] != '' ) ? dfrapi_fix_button( $message['url'], $message['button_text'] ) : ''; | |
| 114 | - $upgrade_account = ( $key == 'usage_over_90_percent' ) ? ' | <a href="' . dfrapi_user_pages( 'change' ) . '?utm_source=plugin&utm_medium=link&utm_campaign=upgradenag">' . __( 'Upgrade', DFRAPI_DOMAIN ) . '</a>' : ''; | |
| 115 | - echo '<div class="'.$message['class'].'">'.$message['message'].$button.$upgrade_account.'</div>'; | |
| 87 | + public function merchants_menu() { | |
| 88 | + if ( dfrapi_datafeedr_api_keys_exist() && dfrapi_selected_network_count() > 0 ) { | |
| 89 | + add_submenu_page( | |
| 90 | + 'dfrapi', | |
| 91 | + __( 'Merchants — Datafeedr API', 'datafeedr-api' ), | |
| 92 | + __( 'Merchants', 'datafeedr-api' ), | |
| 93 | + 'manage_options', | |
| 94 | + 'dfrapi_merchants', | |
| 95 | + array( $this, 'merchants_output' ) | |
| 96 | + ); | |
| 116 | 97 | } |
| 117 | - delete_option( 'dfrapi_admin_notices' ); | |
| 118 | 98 | } |
| 119 | - } | |
| 120 | - | |
| 121 | - function plugin_row_meta( $links, $plugin_file ) { | |
| 122 | - if ( $plugin_file == DFRAPI_BASENAME ) { | |
| 123 | - /* $links[] = sprintf( '<a href="' . admin_url( 'plugin-install.php?tab=search&type=tag&s=dfrapi' ) . '">%s</a>', __( 'Integration Plugins', DFRAPI_DOMAIN ) ); */ | |
| 124 | - $links[] = sprintf( '<a href="' . DFRAPI_HELP_URL . '">%s</a>', __( 'Support', DFRAPI_DOMAIN ) ); | |
| 125 | - return $links; | |
| 99 | + | |
| 100 | + function merchants_output() { | |
| 101 | + $key = 'dfrapi_merchants'; | |
| 102 | + $page = 'dfrapi-merchants'; | |
| 103 | + echo '<div class="wrap" id="' . $key . '">'; | |
| 104 | + echo '<h2>' . dfrapi_setting_pages( $page ) . ' — Datafeedr API</h2>'; | |
| 105 | + echo '<form method="post" action="options.php">'; | |
| 106 | + submit_button(); | |
| 107 | + wp_nonce_field( 'update-options' ); | |
| 108 | + settings_fields( $page ); | |
| 109 | + do_settings_sections( $page ); | |
| 110 | + submit_button(); | |
| 111 | + echo '</form>'; | |
| 112 | + echo '<div id="dfr_unload_message" style="display:none">' . | |
| 113 | + __( 'You have unsaved changes', 'datafeedr-api' ) . '</div>'; | |
| 114 | + echo '</div>'; | |
| 126 | 115 | } |
| 127 | - return $links; | |
| 128 | - } | |
| 129 | 116 | |
| 130 | - function action_links( $links ) { | |
| 131 | - return array_merge( | |
| 132 | - $links, | |
| 133 | - array( | |
| 134 | - 'config' => '<a href="' . admin_url( 'admin.php?page=dfrapi' ) . '">' . __( 'API Settings', DFRAPI_DOMAIN ) . '</a>', | |
| 135 | - ) | |
| 136 | - ); | |
| 117 | + function initialize_classes() { | |
| 118 | + | |
| 119 | + new Dfrapi_Configuration(); | |
| 120 | + | |
| 121 | + // Show "Networks" page if API keys are present. | |
| 122 | + if ( dfrapi_datafeedr_api_keys_exist() ) { | |
| 123 | + | |
| 124 | + if ( isset( $_GET['page'] ) && 'dfrapi_networks' === $_GET['page'] && is_admin() ) { | |
| 125 | + new Dfrapi_Networks(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + if ( isset( $_POST['option_page'] ) && ( 'dfrapi-networks' === $_POST['option_page'] ) && is_admin() ) { | |
| 129 | + new Dfrapi_Networks(); | |
| 130 | + } | |
| 131 | + } | |
| 132 | + | |
| 133 | + // Show "Merchants" page if API keys are present AND a network is selected. | |
| 134 | + if ( dfrapi_datafeedr_api_keys_exist() && dfrapi_user_has_selected_networks() ) { | |
| 135 | + | |
| 136 | + if ( is_admin() && isset( $_GET['page'] ) && 'dfrapi_merchants' === $_GET['page'] ) { | |
| 137 | + new Dfrapi_Merchants(); | |
| 138 | + } | |
| 139 | + | |
| 140 | + if ( is_admin() && isset( $_POST['option_page'] ) && ( 'dfrapi-merchants' === $_POST['option_page'] ) ) { | |
| 141 | + new Dfrapi_Merchants(); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + | |
| 145 | + // Show Tools, Export and Import pages if API keys are present. | |
| 146 | + if ( dfrapi_datafeedr_api_keys_exist() ) { | |
| 147 | + new Dfrapi_Tools(); | |
| 148 | + new Dfrapi_Export(); | |
| 149 | + new Dfrapi_Import(); | |
| 150 | + new Dfrapi_Account(); | |
| 151 | + } | |
| 152 | + } | |
| 153 | + | |
| 137 | 154 | } |
| 138 | 155 | |
| 139 | -} | |
| 140 | - | |
| 141 | -$dfrapi_initialize = new Dfrapi_Initialize(); | |
| 156 | + $dfrapi_initialize = new Dfrapi_Initialize(); | |
| 142 | 157 | |
| 143 | 158 | } // class_exists check |