diff --git a/pmpro-toolkit.php b/pmpro-toolkit.php index a08c678..b1cabfc 100755 --- a/pmpro-toolkit.php +++ b/pmpro-toolkit.php @@ -11,443 +11,457 @@ defined( 'ABSPATH' ) || exit; -// If Paid Membership Pro is not active do nothing -if ( ! is_plugin_active( 'paid-memberships-pro/paid-memberships-pro.php' ) ) { - return; -} - -/* -* Globals -*/ -global $pmprodev_options, $gateway; -$default_options = array( - 'expire_memberships' => '', - 'expiration_warnings' => '', - 'payment_reminders' => '', - 'ipn_debug' => '', - 'authnet_silent_post_debug' => '', - 'stripe_webhook_debug' => '', - 'ins_debug' => '', - 'redirect_email' => '', - 'checkout_debug_email' => '', - 'checkout_debug_when' => '', - 'generate_info' => false, - 'performance_endpoints' => 'no', - 'ip_throttling' => false, - 'enable_cli_commands' => false, -); - -$pmprodev_options = get_option( 'pmprodev_options' ); - -if ( empty( $pmprodev_options ) ) { - $pmprodev_options = $default_options; -} else { - $pmprodev_options = array_merge( $default_options, $pmprodev_options ); -} - -// intialize options in the DB -function pmprodev_init_options() { - global $pmprodev_options, $default_options; - if ( ! $pmprodev_options || empty( $pmprodev_options ) ) { - $pmprodev_options = $default_options; - update_option( 'pmprodev_options', $pmprodev_options ); - } -} - -add_action( 'admin_init', 'pmprodev_init_options' ); - -/* -* Gateway Debug Constants -*/ -define( 'PMPRODEV_DIR', __DIR__ ); -require_once PMPRODEV_DIR . '/classes/class-pmprodev-migration-assistant.php'; - /** - * API LOADER - * Load the API Loader and any API endpoint files if performance tracking is enabled. - * - * @return void - * @since 1.1 - */ -if ( ! empty( $pmprodev_options['performance_endpoints'] ) && $pmprodev_options['performance_endpoints'] !== 'no' ) { - // Explicitly load the API Loader class. - require_once plugin_dir_path( __FILE__ ) . 'classes/class-api-loader.php'; - // Load the API Performance Tracking Trait. - require_once plugin_dir_path( __FILE__ ) . 'classes/traits/Performance_Tracking_Trait.php'; - - // Autoload API endpoint files in /classes/api/. - $api_dir = plugin_dir_path( __FILE__ ) . 'classes/api/'; - foreach ( glob( $api_dir . '*.php' ) as $api_file ) { - require_once $api_file; - } - - // Initialize the namespaced API Loader. - new PMPro_Toolkit\API_Loader(); -} - -/** - * Register WP-CLI commands for Toolkit scripts. + * Initialize the plugin after PMPro is loaded. * - * @return void - * @since 1.1 - */ -if ( defined( 'WP_CLI' ) && WP_CLI && ! empty( $pmprodev_options['enable_cli_commands'] ) ) { - // Load CLI command class only if enabled via Toolkit setting. - require_once plugin_dir_path( __FILE__ ) . 'cli/Toolkit_Commands.php'; - \WP_CLI::add_command( 'pmpro-toolkit', 'PMPro_Toolkit\\Toolkit_Commands' ); -} - -/** - * Remove the cron jobs/action schedules for expiration warnings and expiring credit cards if the options are set. + * Uses defined( 'PMPRO_VERSION' ) instead of is_plugin_active() so the check + * works regardless of PMPro's folder name (e.g. release candidates) and + * regardless of plugin load order. * * @return void - * @since 1.0 + * @since 1.1.1 */ -function pmprodev_gateway_debug_setup() { - - global $pmprodev_options; - - // define IPN/webhook debug emails - if ( ! empty( $pmprodev_options['ipn_debug'] ) && ! defined( 'PMPRO_IPN_DEBUG' ) ) { - define( 'PMPRO_IPN_DEBUG', $pmprodev_options['ipn_debug'] ); +function pmprodev_plugins_loaded() { + if ( ! defined( 'PMPRO_VERSION' ) ) { + return; } - if ( ! empty( $pmprodev_options['ipn_debug'] ) && ! defined( 'PMPRO_AUTHNET_SILENT_POST_DEBUG' ) ) { - define( 'PMPRO_AUTHNET_SILENT_POST_DEBUG', $pmprodev_options['ipn_debug'] ); - } - if ( ! empty( $pmprodev_options['ipn_debug'] ) && ! defined( 'PMPRO_STRIPE_WEBHOOK_DEBUG' ) ) { - define( 'PMPRO_STRIPE_WEBHOOK_DEBUG', $pmprodev_options['ipn_debug'] ); - } + /* + * Globals + */ + global $pmprodev_options, $gateway; + $default_options = array( + 'expire_memberships' => '', + 'expiration_warnings' => '', + 'payment_reminders' => '', + 'ipn_debug' => '', + 'authnet_silent_post_debug' => '', + 'stripe_webhook_debug' => '', + 'ins_debug' => '', + 'redirect_email' => '', + 'checkout_debug_email' => '', + 'checkout_debug_when' => '', + 'generate_info' => false, + 'performance_endpoints' => 'no', + 'ip_throttling' => false, + 'enable_cli_commands' => false, + ); - if ( ! empty( $pmprodev_options['ipn_debug'] ) && ! defined( 'PMPRO_INS_DEBUG' ) ) { - define( 'PMPRO_INS_DEBUG', $pmprodev_options['ipn_debug'] ); - } + $pmprodev_options = get_option( 'pmprodev_options' ); - // Unhook crons or Action Scheduler actions - if( !empty( $pmprodev_options['expire_memberships'] ) ) { - if ( class_exists( 'PMPro_Recurring_Actions' ) ) { - remove_action( 'pmpro_schedule_daily', array( PMPro_Recurring_Actions::instance(), 'check_for_expired_memberships' ) ); - } else { - remove_action( "pmpro_cron_expire_memberships", "pmpro_cron_expire_memberships" ); - } + if ( empty( $pmprodev_options ) ) { + $pmprodev_options = $default_options; + } else { + $pmprodev_options = array_merge( $default_options, $pmprodev_options ); } - if( !empty( $pmprodev_options['expiration_warnings'] ) ){ - if ( class_exists( 'PMPro_Recurring_Actions' ) ) { - remove_action( 'pmpro_schedule_daily', array( PMPro_Recurring_Actions::instance(), 'membership_expiration_reminders' ), 99 ); - } else { - remove_action( "pmpro_cron_expiration_warnings", "pmpro_cron_expiration_warnings" ); + // intialize options in the DB + function pmprodev_init_options() { + global $pmprodev_options, $default_options; + if ( ! $pmprodev_options || empty( $pmprodev_options ) ) { + $pmprodev_options = $default_options; + update_option( 'pmprodev_options', $pmprodev_options ); } } - if( !empty( $pmprodev_options['payment_reminders'] ) ){ - if ( class_exists( 'PMPro_Recurring_Actions' ) ) { - remove_action( 'pmpro_schedule_daily', array( PMPro_Recurring_Actions::instance(), 'recurring_payment_reminders' ) ); - } else { - remove_action( "pmpro_cron_recurring_payment_reminders", "pmpro_cron_recurring_payment_reminders" ); + add_action( 'admin_init', 'pmprodev_init_options' ); + + /* + * Gateway Debug Constants + */ + define( 'PMPRODEV_DIR', __DIR__ ); + require_once PMPRODEV_DIR . '/classes/class-pmprodev-migration-assistant.php'; + + /** + * API LOADER + * Load the API Loader and any API endpoint files if performance tracking is enabled. + * + * @return void + * @since 1.1 + */ + if ( ! empty( $pmprodev_options['performance_endpoints'] ) && $pmprodev_options['performance_endpoints'] !== 'no' ) { + // Explicitly load the API Loader class. + require_once plugin_dir_path( __FILE__ ) . 'classes/class-api-loader.php'; + // Load the API Performance Tracking Trait. + require_once plugin_dir_path( __FILE__ ) . 'classes/traits/Performance_Tracking_Trait.php'; + + // Autoload API endpoint files in /classes/api/. + $api_dir = plugin_dir_path( __FILE__ ) . 'classes/api/'; + foreach ( glob( $api_dir . '*.php' ) as $api_file ) { + require_once $api_file; } - } -} -add_action( 'init', 'pmprodev_gateway_debug_setup' ); - -/** - * If there is a redirect email set, redirect all PMPro emails to that email. - * - * @param string $recipient the email recipient - * @param object $email the email object - * - * @return string $recipient the email recipient - * @since 1.0 - */ -function pmprodev_redirect_emails( $recipient, $email ) { - - global $pmprodev_options; - - if ( ! empty( $pmprodev_options['redirect_email'] ) ) { - $recipient = $pmprodev_options['redirect_email']; + // Initialize the namespaced API Loader. + new PMPro_Toolkit\API_Loader(); } - return $recipient; -} -add_filter( 'pmpro_email_recipient', 'pmprodev_redirect_emails', 10, 2 ); - -/** - * Send debug email every time checkout page is hit. - * - * @param mixed $filter_contents to not break the wp_redirect filter. - * - * @return mixed $filter_contents to not break the wp_redirect filter. - * @since 1.0 - */ -function pmprodev_checkout_debug_email( $filter_contents = null ) { + /** + * Register WP-CLI commands for Toolkit scripts. + * + * @return void + * @since 1.1 + */ + if ( defined( 'WP_CLI' ) && WP_CLI && ! empty( $pmprodev_options['enable_cli_commands'] ) ) { + // Load CLI command class only if enabled via Toolkit setting. + require_once plugin_dir_path( __FILE__ ) . 'cli/Toolkit_Commands.php'; + \WP_CLI::add_command( 'pmpro-toolkit', 'PMPro_Toolkit\\Toolkit_Commands' ); + } - global $pmprodev_options, $current_user, $wpdb, $pmpro_msg, $pmpro_msgt; + /** + * Remove the cron jobs/action schedules for expiration warnings and expiring credit cards if the options are set. + * + * @return void + * @since 1.0 + */ + function pmprodev_gateway_debug_setup() { - // Ignore the dashboard, AJAX, and webhooks. - if ( is_admin() || defined( 'DOING_AJAX' ) || pmpro_doing_webhook() ) { - return $filter_contents; - } + global $pmprodev_options; - // Avoid issues if we're redirecting too early before pmpro_is_checkout will work. - if ( ! did_action( 'wp' ) ) { - return $filter_contents; - } + // define IPN/webhook debug emails + if ( ! empty( $pmprodev_options['ipn_debug'] ) && ! defined( 'PMPRO_IPN_DEBUG' ) ) { + define( 'PMPRO_IPN_DEBUG', $pmprodev_options['ipn_debug'] ); + } - // Make sure this is the checkout page. - if ( ! function_exists( 'pmpro_is_checkout' ) || ! pmpro_is_checkout() ) { - return $filter_contents; - } + if ( ! empty( $pmprodev_options['ipn_debug'] ) && ! defined( 'PMPRO_AUTHNET_SILENT_POST_DEBUG' ) ) { + define( 'PMPRO_AUTHNET_SILENT_POST_DEBUG', $pmprodev_options['ipn_debug'] ); + } - // Make sure they have turned this on. - if ( empty( $pmprodev_options['checkout_debug_when'] ) ) { - return $filter_contents; - } + if ( ! empty( $pmprodev_options['ipn_debug'] ) && ! defined( 'PMPRO_STRIPE_WEBHOOK_DEBUG' ) ) { + define( 'PMPRO_STRIPE_WEBHOOK_DEBUG', $pmprodev_options['ipn_debug'] ); + } - // Make sure we have an email to use. - if ( empty( $pmprodev_options['checkout_debug_email'] ) ) { - return $filter_contents; - } + if ( ! empty( $pmprodev_options['ipn_debug'] ) && ! defined( 'PMPRO_INS_DEBUG' ) ) { + define( 'PMPRO_INS_DEBUG', $pmprodev_options['ipn_debug'] ); + } - // Make sure the checkout form was submitted if using that option. - if ( $pmprodev_options['checkout_debug_when'] === 'on_submit' && empty( $_REQUEST['submit-checkout'] ) ) { - return $filter_contents; - } + // Unhook crons or Action Scheduler actions + if( !empty( $pmprodev_options['expire_memberships'] ) ) { + if ( class_exists( 'PMPro_Recurring_Actions' ) ) { + remove_action( 'pmpro_schedule_daily', array( PMPro_Recurring_Actions::instance(), 'check_for_expired_memberships' ) ); + } else { + remove_action( "pmpro_cron_expire_memberships", "pmpro_cron_expire_memberships" ); + } + } - // Make sure there is an error if using that option. - if ( $pmprodev_options['checkout_debug_when'] === 'on_error' && ( empty( $pmpro_msgt ) || $pmpro_msgt != 'pmpro_error' ) ) { - return $filter_contents; - } + if( !empty( $pmprodev_options['expiration_warnings'] ) ){ + if ( class_exists( 'PMPro_Recurring_Actions' ) ) { + remove_action( 'pmpro_schedule_daily', array( PMPro_Recurring_Actions::instance(), 'membership_expiration_reminders' ), 99 ); + } else { + remove_action( "pmpro_cron_expiration_warnings", "pmpro_cron_expiration_warnings" ); + } + } - // We're going to send an email. Make sure we don't send more than one. - $pmprodev_options['checkout_debug_when'] = false; + if( !empty( $pmprodev_options['payment_reminders'] ) ){ + if ( class_exists( 'PMPro_Recurring_Actions' ) ) { + remove_action( 'pmpro_schedule_daily', array( PMPro_Recurring_Actions::instance(), 'recurring_payment_reminders' ) ); + } else { + remove_action( "pmpro_cron_recurring_payment_reminders", "pmpro_cron_recurring_payment_reminders" ); + } + } - // Get some values. - $level = pmpro_getLevelAtCheckout(); - $email = new PMProEmail(); - if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) { - $http = 'https://'; - } else { - $http = 'http://'; } + add_action( 'init', 'pmprodev_gateway_debug_setup' ); + + /** + * If there is a redirect email set, redirect all PMPro emails to that email. + * + * @param string $recipient the email recipient + * @param object $email the email object + * + * @return string $recipient the email recipient + * @since 1.0 + */ + function pmprodev_redirect_emails( $recipient, $email ) { + + global $pmprodev_options; + + if ( ! empty( $pmprodev_options['redirect_email'] ) ) { + $recipient = $pmprodev_options['redirect_email']; + } - // Remove password data. - $user_pass_bu = $current_user->user_pass; - $current_user->user_pass = ''; - if ( isset( $_REQUEST['password'] ) ) { - $password_bu = $_REQUEST['password']; - $_REQUEST['password'] = ''; - } - if ( isset( $_REQUEST['password2'] ) ) { - $password2_bu = $_REQUEST['password2']; - $_REQUEST['password2'] = ''; + return $recipient; } + add_filter( 'pmpro_email_recipient', 'pmprodev_redirect_emails', 10, 2 ); + + /** + * Send debug email every time checkout page is hit. + * + * @param mixed $filter_contents to not break the wp_redirect filter. + * + * @return mixed $filter_contents to not break the wp_redirect filter. + * @since 1.0 + */ + function pmprodev_checkout_debug_email( $filter_contents = null ) { + + global $pmprodev_options, $current_user, $wpdb, $pmpro_msg, $pmpro_msgt; + + // Ignore the dashboard, AJAX, and webhooks. + if ( is_admin() || defined( 'DOING_AJAX' ) || pmpro_doing_webhook() ) { + return $filter_contents; + } - // Set up the email. - $email->subject = sprintf( '%s Checkout Page Debug Log', get_bloginfo( 'name' ) ); - $email->email = $pmprodev_options['checkout_debug_email']; - $email->template = 'checkout_debug'; - $email->body = file_get_contents( plugin_dir_path( __FILE__ ) . '/email/checkout_debug.html' ); - $email->data = array( - 'sitename' => get_bloginfo( 'sitename' ), - 'checkout_url' => $http . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], - 'submit' => ( empty( $_REQUEST['submit-checkout'] ) ? 'no' : 'yes' ), - 'level' => print_r( $level, true ), - 'user' => print_r( $current_user->data, true ), - 'request' => print_r( $_REQUEST, true ), - 'message_type' => ( empty( $pmpro_msgt ) ? 'N/A' : $pmpro_msgt . '|' ), - 'message' => $pmpro_msg, - ); - - // Add passwords back, just in case. - if ( isset( $user_pass_bu ) ) { - $current_user->user_pass = $user_pass_bu; - } - if ( isset( $password_bu ) ) { - $_REQUEST['password'] = $password_bu; - } - if ( isset( $user_pass_bu ) && isset( $password2_bu ) ) { - $_REQUEST['password2'] = $password2_bu; - } + // Avoid issues if we're redirecting too early before pmpro_is_checkout will work. + if ( ! did_action( 'wp' ) ) { + return $filter_contents; + } - $order = new MemberOrder(); - $order->getLastMemberOrder( $current_user->user_id ); + // Make sure this is the checkout page. + if ( ! function_exists( 'pmpro_is_checkout' ) || ! pmpro_is_checkout() ) { + return $filter_contents; + } - if ( ! empty( $order ) ) { - $email->data['order'] = print_r( $order, true ); - } + // Make sure they have turned this on. + if ( empty( $pmprodev_options['checkout_debug_when'] ) ) { + return $filter_contents; + } - $email->sendEmail(); + // Make sure we have an email to use. + if ( empty( $pmprodev_options['checkout_debug_email'] ) ) { + return $filter_contents; + } - return $filter_contents; -} -add_action( 'template_redirect', 'pmprodev_checkout_debug_email', 2 ); -add_filter( 'wp_redirect', 'pmprodev_checkout_debug_email', 100 ); -add_action( 'pmpro_membership_post_membership_expiry', 'pmprodev_checkout_debug_email' ); -add_action( 'shutdown', 'pmprodev_checkout_debug_email' ); + // Make sure the checkout form was submitted if using that option. + if ( $pmprodev_options['checkout_debug_when'] === 'on_submit' && empty( $_REQUEST['submit-checkout'] ) ) { + return $filter_contents; + } -/** - * Add settings page to the PMPro admin menu. - * - * @return void - * @since 1.0 - */ -function pmprodev_admin_menu() { - $pmprodev_menu_text = esc_html__( 'Toolkit', 'pmpro-toolkit' ); - add_submenu_page( - 'pmpro-dashboard', - $pmprodev_menu_text, - $pmprodev_menu_text, - 'manage_options', - 'pmpro-toolkit', - 'pmprodev_settings_page' - ); -} + // Make sure there is an error if using that option. + if ( $pmprodev_options['checkout_debug_when'] === 'on_error' && ( empty( $pmpro_msgt ) || $pmpro_msgt != 'pmpro_error' ) ) { + return $filter_contents; + } -add_action( 'admin_menu', 'pmprodev_admin_menu' ); -add_action( 'admin_bar_menu', 'pmprodev_admin_menu_bar', 2000 ); + // We're going to send an email. Make sure we don't send more than one. + $pmprodev_options['checkout_debug_when'] = false; -/** - * Add a menu item to the PMPro admin bar menu. - * - * @param WP_Admin_Bar $wp_admin_bar the WP_Admin_Bar object. - * - * @return void - * @since 1.0 - */ -function pmprodev_admin_menu_bar( $wp_admin_bar ) { - $wp_admin_bar->add_menu( - array( - 'id' => 'pmprodev', - 'title' => esc_html__( 'Toolkit', 'pmpro-toolkit' ), - 'href' => admin_url( 'admin.php?page=pmpro-toolkit' ), - 'parent' => 'paid-memberships-pro', - 'meta' => array( 'class' => 'pmpro-dev' ), - ) - ); -} + // Get some values. + $level = pmpro_getLevelAtCheckout(); + $email = new PMProEmail(); + if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) { + $http = 'https://'; + } else { + $http = 'http://'; + } -/** - * Catch request and call export function. - * - * @return void - * @since 1.0 - */ -function pmprodev_process_migration_export() { - if ( ! empty( $_REQUEST['page'] ) && 'pmpro-toolkit' === $_REQUEST['page'] && ! empty( $_REQUEST['section'] ) - && 'migration' === $_REQUEST['section'] && ! empty( $_REQUEST['pmprodev_export_options'] ) ) { - PMProDev_Migration_Assistant::export( $_REQUEST['pmprodev_export_options'] ); - } -} -add_action( 'admin_init', 'pmprodev_process_migration_export' ); + // Remove password data. + $user_pass_bu = $current_user->user_pass; + $current_user->user_pass = ''; + if ( isset( $_REQUEST['password'] ) ) { + $password_bu = $_REQUEST['password']; + $_REQUEST['password'] = ''; + } + if ( isset( $_REQUEST['password2'] ) ) { + $password2_bu = $_REQUEST['password2']; + $_REQUEST['password2'] = ''; + } -/** - * Load the settings page. - * - * @return void - * @since 1.0 - */ -function pmprodev_settings_page() { - require_once plugin_dir_path( __FILE__ ) . '/adminpages/toolkit.php'; -} + // Set up the email. + $email->subject = sprintf( '%s Checkout Page Debug Log', get_bloginfo( 'name' ) ); + $email->email = $pmprodev_options['checkout_debug_email']; + $email->template = 'checkout_debug'; + $email->body = file_get_contents( plugin_dir_path( __FILE__ ) . '/email/checkout_debug.html' ); + $email->data = array( + 'sitename' => get_bloginfo( 'sitename' ), + 'checkout_url' => $http . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], + 'submit' => ( empty( $_REQUEST['submit-checkout'] ) ? 'no' : 'yes' ), + 'level' => print_r( $level, true ), + 'user' => print_r( $current_user->data, true ), + 'request' => print_r( $_REQUEST, true ), + 'message_type' => ( empty( $pmpro_msgt ) ? 'N/A' : $pmpro_msgt . '|' ), + 'message' => $pmpro_msg, + ); -/** - * Load the text domain for translation. - * - * @return void - * @since 1.0 - */ -function pmpro_toolkit_load_textdomain() { - // get the locale - $locale = apply_filters( 'plugin_locale', get_locale(), 'pmpro-toolkit' ); - $mofile = 'pmpro-toolkit-' . $locale . '.mo'; + // Add passwords back, just in case. + if ( isset( $user_pass_bu ) ) { + $current_user->user_pass = $user_pass_bu; + } + if ( isset( $password_bu ) ) { + $_REQUEST['password'] = $password_bu; + } + if ( isset( $user_pass_bu ) && isset( $password2_bu ) ) { + $_REQUEST['password2'] = $password2_bu; + } - // paths to local (plugin) and global (WP) language files - $mofile_local = plugin_dir_path( __FILE__ ) . '/languages/' . $mofile; - $mofile_global = WP_LANG_DIR . '/pmpro/' . $mofile; + $order = new MemberOrder(); + $order->getLastMemberOrder( $current_user->user_id ); - // load global first - load_textdomain( 'pmpro-toolkit', $mofile_global ); + if ( ! empty( $order ) ) { + $email->data['order'] = print_r( $order, true ); + } - // load local second - load_textdomain( 'pmpro-toolkit', $mofile_local ); -} -add_action( 'init', 'pmpro_toolkit_load_textdomain', 1 ); + $email->sendEmail(); -/** - * Add links to the plugin row meta. - * - * @param array $links the links array - * @param string $file the file name - * @return array $links the links array - * @since 1.0 - */ -function pmprodev_plugin_row_meta( $links, $file ) { - if ( strpos( $file, 'pmpro-toolkit.php' ) !== false ) { - $new_links = array( - '' . esc_html__( 'Docs', 'pmpro-toolkit' ) . '', - '' . esc_html__( 'Support', 'pmpro-toolkit' ) . '', + return $filter_contents; + } + add_action( 'template_redirect', 'pmprodev_checkout_debug_email', 2 ); + add_filter( 'wp_redirect', 'pmprodev_checkout_debug_email', 100 ); + add_action( 'pmpro_membership_post_membership_expiry', 'pmprodev_checkout_debug_email' ); + add_action( 'shutdown', 'pmprodev_checkout_debug_email' ); + + /** + * Add settings page to the PMPro admin menu. + * + * @return void + * @since 1.0 + */ + function pmprodev_admin_menu() { + $pmprodev_menu_text = esc_html__( 'Toolkit', 'pmpro-toolkit' ); + add_submenu_page( + 'pmpro-dashboard', + $pmprodev_menu_text, + $pmprodev_menu_text, + 'manage_options', + 'pmpro-toolkit', + 'pmprodev_settings_page' ); - $links = array_merge( $links, $new_links ); } - return $links; -} -add_filter( 'plugin_row_meta', 'pmprodev_plugin_row_meta', 10, 2 ); -/** - * Enqueue scripts/styles on the frontend. - * - * @return void - * @since 1.0 - */ -function pmprodev_enqueue_scripts() { - wp_register_script( 'pmprodev-generate-checkout-info', plugins_url( 'js/pmprodev-generate-checkout-info.js', __FILE__ ), array( 'jquery' ) ); - wp_enqueue_script( 'pmprodev-generate-checkout-info' ); - // add css for the button - wp_register_style( 'pmprodev', plugins_url( 'css/pmprodev.css', __FILE__ ) ); - wp_enqueue_style( 'pmprodev' ); -} + add_action( 'admin_menu', 'pmprodev_admin_menu' ); + add_action( 'admin_bar_menu', 'pmprodev_admin_menu_bar', 2000 ); + + /** + * Add a menu item to the PMPro admin bar menu. + * + * @param WP_Admin_Bar $wp_admin_bar the WP_Admin_Bar object. + * + * @return void + * @since 1.0 + */ + function pmprodev_admin_menu_bar( $wp_admin_bar ) { + $wp_admin_bar->add_menu( + array( + 'id' => 'pmprodev', + 'title' => esc_html__( 'Toolkit', 'pmpro-toolkit' ), + 'href' => admin_url( 'admin.php?page=pmpro-toolkit' ), + 'parent' => 'paid-memberships-pro', + 'meta' => array( 'class' => 'pmpro-dev' ), + ) + ); + } -add_action( 'wp_enqueue_scripts', 'pmprodev_enqueue_scripts' ); + /** + * Catch request and call export function. + * + * @return void + * @since 1.0 + */ + function pmprodev_process_migration_export() { + if ( ! empty( $_REQUEST['page'] ) && 'pmpro-toolkit' === $_REQUEST['page'] && ! empty( $_REQUEST['section'] ) + && 'migration' === $_REQUEST['section'] && ! empty( $_REQUEST['pmprodev_export_options'] ) ) { + PMProDev_Migration_Assistant::export( $_REQUEST['pmprodev_export_options'] ); + } + } + add_action( 'admin_init', 'pmprodev_process_migration_export' ); + + /** + * Load the settings page. + * + * @return void + * @since 1.0 + */ + function pmprodev_settings_page() { + require_once plugin_dir_path( __FILE__ ) . '/adminpages/toolkit.php'; + } -/** - * Enqueue scripts/styles on the admin side. - * - * @return void - * @since 1.1 - */ -function pmprodev_enqueue_admin_scripts(){ - // if we're on a toolkit admin page - if ( isset( $_GET['page'] ) && 'pmpro-toolkit' === $_GET['page'] ) { - // Add css for the admin - wp_register_style( 'pmprodev-admin', plugins_url( 'css/pmpro-toolkit-admin.css', __FILE__ ) ); - wp_enqueue_style( 'pmprodev-admin' ); + /** + * Load the text domain for translation. + * + * @return void + * @since 1.0 + */ + function pmpro_toolkit_load_textdomain() { + // get the locale + $locale = apply_filters( 'plugin_locale', get_locale(), 'pmpro-toolkit' ); + $mofile = 'pmpro-toolkit-' . $locale . '.mo'; + + // paths to local (plugin) and global (WP) language files + $mofile_local = plugin_dir_path( __FILE__ ) . '/languages/' . $mofile; + $mofile_global = WP_LANG_DIR . '/pmpro/' . $mofile; + + // load global first + load_textdomain( 'pmpro-toolkit', $mofile_global ); + + // load local second + load_textdomain( 'pmpro-toolkit', $mofile_local ); + } + add_action( 'init', 'pmpro_toolkit_load_textdomain', 1 ); + + /** + * Add links to the plugin row meta. + * + * @param array $links the links array + * @param string $file the file name + * @return array $links the links array + * @since 1.0 + */ + function pmprodev_plugin_row_meta( $links, $file ) { + if ( strpos( $file, 'pmpro-toolkit.php' ) !== false ) { + $new_links = array( + '' . esc_html__( 'Docs', 'pmpro-toolkit' ) . '', + '' . esc_html__( 'Support', 'pmpro-toolkit' ) . '', + ); + $links = array_merge( $links, $new_links ); + } + return $links; + } + add_filter( 'plugin_row_meta', 'pmprodev_plugin_row_meta', 10, 2 ); + + /** + * Enqueue scripts/styles on the frontend. + * + * @return void + * @since 1.0 + */ + function pmprodev_enqueue_scripts() { + wp_register_script( 'pmprodev-generate-checkout-info', plugins_url( 'js/pmprodev-generate-checkout-info.js', __FILE__ ), array( 'jquery' ) ); + wp_enqueue_script( 'pmprodev-generate-checkout-info' ); + // add css for the button + wp_register_style( 'pmprodev', plugins_url( 'css/pmprodev.css', __FILE__ ) ); + wp_enqueue_style( 'pmprodev' ); } -} -add_action( 'admin_enqueue_scripts', 'pmprodev_enqueue_admin_scripts' ); + add_action( 'wp_enqueue_scripts', 'pmprodev_enqueue_scripts' ); + + /** + * Enqueue scripts/styles on the admin side. + * + * @return void + * @since 1.1 + */ + function pmprodev_enqueue_admin_scripts(){ + // if we're on a toolkit admin page + if ( isset( $_GET['page'] ) && 'pmpro-toolkit' === $_GET['page'] ) { + // Add css for the admin + wp_register_style( 'pmprodev-admin', plugins_url( 'css/pmpro-toolkit-admin.css', __FILE__ ) ); + wp_enqueue_style( 'pmprodev-admin' ); + } + } -/** - * Add a button to the checkout page to fill in the user data form. - * - * @return void - * @since 1.0 - */ -function pmprodev_create_button() { - ?> -