MC4WP_Lite_Admin

The MailChimp for WordPress Lite MC4WP Lite Admin class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-admin.php  
  1. class MC4WP_Lite_Admin 
  2.  
  3. /** 
  4. * @var bool True if the BWS Captcha plugin is activated. 
  5. */ 
  6. private $has_captcha_plugin = false; 
  7.  
  8. /** 
  9. * @var string The relative path to the main plugin file from the plugins dir 
  10. */ 
  11. private $plugin_file; 
  12.  
  13. /** 
  14. * Constructor 
  15. */ 
  16. public function __construct() { 
  17.  
  18. $this->plugin_file = plugin_basename( MC4WP_LITE_PLUGIN_FILE ); 
  19.  
  20. $this->load_translations(); 
  21. $this->setup_hooks(); 
  22. $this->listen(); 
  23.  
  24. /** 
  25. * Upgrade routine 
  26. */ 
  27. private function load_upgrader() { 
  28.  
  29. // Only run if db option is at older version than code constant 
  30. $db_version = get_option( 'mc4wp_lite_version', 0 ); 
  31. if( version_compare( MC4WP_LITE_VERSION, $db_version, '<=' ) ) { 
  32. return false; 
  33.  
  34. $upgrader = new MC4WP_DB_Upgrader( MC4WP_LITE_VERSION, $db_version ); 
  35. $upgrader->run(); 
  36.  
  37. /** 
  38. * Registers all hooks 
  39. */ 
  40. private function setup_hooks() { 
  41.  
  42. global $pagenow; 
  43. $current_page = isset( $pagenow ) ? $pagenow : ''; 
  44.  
  45. // Actions used globally throughout WP Admin 
  46. add_action( 'admin_init', array( $this, 'initialize' ) ); 
  47. add_action( 'admin_menu', array( $this, 'build_menu' ) ); 
  48. add_action( 'admin_enqueue_scripts', array( $this, 'load_css_and_js' ) ); 
  49.  
  50. // Hooks for Plugins overview page 
  51. if( $current_page === 'plugins.php' ) { 
  52. $this->plugin_file = plugin_basename( MC4WP_LITE_PLUGIN_FILE ); 
  53.  
  54. add_filter( 'plugin_action_links_' . $this->plugin_file, array( $this, 'add_plugin_settings_link' ), 10, 2 ); 
  55. add_filter( 'plugin_row_meta', array( $this, 'add_plugin_meta_links'), 10, 2 ); 
  56.  
  57. // Hooks for Form settings page 
  58. if( $this->get_current_page() === 'mailchimp-for-wp-form-settings' ) { 
  59. add_filter( 'quicktags_settings', array( $this, 'set_quicktags_buttons' ), 10, 2 ); 
  60.  
  61.  
  62. /** 
  63. * Load the plugin translations 
  64. */ 
  65. private function load_translations() { 
  66. // load the plugin text domain 
  67. load_plugin_textdomain( 'mailchimp-for-wp', false, dirname( $this->plugin_file ) . '/languages' ); 
  68.  
  69. /** 
  70. * Initializes various stuff used in WP Admin 
  71. * - Registers settings 
  72. * - Checks if the Captcha plugin is activated 
  73. * - Loads the plugin text domain 
  74. */ 
  75. public function initialize() { 
  76.  
  77. // register settings 
  78. register_setting( 'mc4wp_lite_settings', 'mc4wp_lite', array( $this, 'validate_settings' ) ); 
  79. register_setting( 'mc4wp_lite_checkbox_settings', 'mc4wp_lite_checkbox', array( $this, 'validate_settings' ) ); 
  80. register_setting( 'mc4wp_lite_form_settings', 'mc4wp_lite_form', array( $this, 'validate_settings' ) ); 
  81.  
  82. // store whether this plugin has the BWS captcha plugin running (https://wordpress.org/plugins/captcha/) 
  83. $this->has_captcha_plugin = function_exists( 'cptch_display_captcha_custom' ); 
  84.  
  85. $this->load_upgrader(); 
  86.  
  87. /** 
  88. * Listen to various mc4wp actions 
  89. */ 
  90. private function listen() { 
  91. // did the user click on upgrade to pro link? 
  92. if( $this->get_current_page() === 'mailchimp-for-wp-upgrade' && false === headers_sent() ) { 
  93. wp_redirect( 'https://mc4wp.com/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=menu-upgrade-link' ); 
  94. exit; 
  95.  
  96. /** 
  97. * Set which Quicktag buttons should appear in the form mark-up editor 
  98. * @param array $settings 
  99. * @param string $editor_id 
  100. * @return array 
  101. */ 
  102. public function set_quicktags_buttons( $settings, $editor_id = '' ) 
  103. if( $editor_id !== 'mc4wpformmarkup' ) { 
  104. return $settings; 
  105.  
  106. $settings['buttons'] = 'strong, em, link, img, ul, li, close'; 
  107.  
  108. return $settings; 
  109.  
  110. /** 
  111. * Add the settings link to the Plugins overview 
  112. * @param array $links 
  113. * @param $file 
  114. * @return array 
  115. */ 
  116. public function add_plugin_settings_link( $links, $file ) { 
  117. if( $file !== $this->plugin_file ) { 
  118. return $links; 
  119.  
  120. $settings_link = '<a href="' . admin_url( 'admin.php?page=mailchimp-for-wp' ) . '">'. __( 'Settings', 'mailchimp-for-wp' ) . '</a>'; 
  121. array_unshift( $links, $settings_link ); 
  122. return $links; 
  123.  
  124. /** 
  125. * Adds meta links to the plugin in the WP Admin > Plugins screen 
  126. * @param array $links 
  127. * @param string $file 
  128. * @return array 
  129. */ 
  130. public function add_plugin_meta_links( $links, $file ) { 
  131. if( $file !== $this->plugin_file ) { 
  132. return $links; 
  133.  
  134. $links[] = '<a href="https://wordpress.org/plugins/mailchimp-for-wp/faq/">FAQ</a>'; 
  135. $links[] = '<a href="https://mc4wp.com/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=plugins-upgrade-link">' . __( 'Upgrade to MailChimp for WordPress Pro', 'mailchimp-for-wp' ) . '</a>'; 
  136. return $links; 
  137.  
  138. /** 
  139. * Register the setting pages and their menu items 
  140. */ 
  141. public function build_menu() { 
  142.  
  143. /** 
  144. * @filter mc4wp_settings_cap 
  145. * @expects string A valid WP capability like 'manage_options' (default) 
  146. * Use to customize the required user capability to access the MC4WP settings pages 
  147. */ 
  148. $required_cap = apply_filters( 'mc4wp_settings_cap', 'manage_options' ); 
  149.  
  150. $menu_items = array( 
  151. array( 
  152. 'title' => __( 'MailChimp API Settings', 'mailchimp-for-wp' ),  
  153. 'text' => __( 'MailChimp', 'mailchimp-for-wp' ),  
  154. 'slug' => '',  
  155. 'callback' => array( $this, 'show_api_settings' ),  
  156. ),  
  157. array( 
  158. 'title' => __( 'Checkbox Settings', 'mailchimp-for-wp' ),  
  159. 'text' => __( 'Checkboxes', 'mailchimp-for-wp' ),  
  160. 'slug' => 'checkbox-settings',  
  161. 'callback' => array( $this, 'show_checkbox_settings' ),  
  162. ),  
  163. array( 
  164. 'title' => __( 'Form Settings', 'mailchimp-for-wp' ),  
  165. 'text' => __( 'Forms', 'mailchimp-for-wp' ),  
  166. 'slug' => 'form-settings',  
  167. 'callback' => array( $this, 'show_form_settings' ) ),  
  168. array( 
  169. 'title' => __( 'Upgrade to Pro', 'mailchimp-for-wp' ),  
  170. 'text' => '<span style="line-height: 20px;"><span class="dashicons dashicons-external"></span> ' .__( 'Upgrade to Pro', 'mailchimp-for-wp' ),  
  171. 'slug' => 'upgrade',  
  172. 'callback' => array( $this, 'redirect_to_pro' ),  
  173. ),  
  174. ); 
  175.  
  176. $menu_items = apply_filters( 'mc4wp_menu_items', $menu_items ); 
  177.  
  178. // add top menu item 
  179. add_menu_page( 'MailChimp for WP Lite', 'MailChimp for WP', $required_cap, 'mailchimp-for-wp', array( $this, 'show_api_settings' ), MC4WP_LITE_PLUGIN_URL . 'assets/img/menu-icon.png', '99.68491' ); 
  180.  
  181. // add submenu pages 
  182. foreach( $menu_items as $item ) { 
  183. $slug = ( '' !== $item['slug'] ) ? "mailchimp-for-wp-{$item['slug']}" : 'mailchimp-for-wp'; 
  184. add_submenu_page( 'mailchimp-for-wp', $item['title'] . ' - MailChimp for WordPress Lite', $item['text'], $required_cap, $slug, $item['callback'] ); 
  185.  
  186.  
  187.  
  188. /** 
  189. * Validates the General settings 
  190. * @param array $settings 
  191. * @return array 
  192. */ 
  193. public function validate_settings( array $settings ) { 
  194.  
  195. // sanitize simple text fields (no HTML, just chars & numbers) 
  196. $simple_text_fields = array( 'api_key', 'redirect', 'css' ); 
  197. foreach( $simple_text_fields as $field ) { 
  198. if( isset( $settings[ $field ] ) ) { 
  199. $settings[ $field ] = sanitize_text_field( $settings[ $field ] ); 
  200.  
  201. // validate woocommerce checkbox position 
  202. if( isset( $settings['woocommerce_position'] ) ) { 
  203. // make sure position is either 'order' or 'billing' 
  204. if( ! in_array( $settings['woocommerce_position'], array( 'order', 'billing' ) ) ) { 
  205. $settings['woocommerce_position'] = 'billing'; 
  206.  
  207. // dynamic sanitization 
  208. foreach( $settings as $setting => $value ) { 
  209. // strip special tags from text settings 
  210. if( substr( $setting, 0, 5 ) === 'text_' || $setting === 'label' ) { 
  211. $value = trim( $value ); 
  212. $value = strip_tags( $value, '<a><b><strong><em><i><br><u><script><span><abbr><strike>' ); 
  213. $settings[ $setting ] = $value; 
  214.  
  215. // strip <form> from form mark-up 
  216. if( isset( $settings[ 'markup'] ) ) { 
  217. $settings[ 'markup' ] = preg_replace( '/<\/?form(.|\s)*?>/i', '', $settings[ 'markup'] ); 
  218.  
  219. return $settings; 
  220.  
  221. /** 
  222. * Load scripts and stylesheet on MailChimp for WP Admin pages 
  223. * @return bool 
  224. */ 
  225. public function load_css_and_js() { 
  226. // only load asset files on the MailChimp for WordPress settings pages 
  227. if( strpos( $this->get_current_page(), 'mailchimp-for-wp' ) !== 0 ) { 
  228. return false; 
  229.  
  230. $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; 
  231. $mailchimp = new MC4WP_MailChimp(); 
  232.  
  233. // css 
  234. wp_enqueue_style( 'mc4wp-admin-css', MC4WP_LITE_PLUGIN_URL . 'assets/css/admin' . $suffix . '.css' ); 
  235.  
  236. // js 
  237. wp_register_script( 'mc4wp-beautifyhtml', MC4WP_LITE_PLUGIN_URL . 'assets/js/third-party/beautify-html'. $suffix .'.js', array( 'jquery' ), MC4WP_LITE_VERSION, true ); 
  238. wp_register_script( 'mc4wp-admin', MC4WP_LITE_PLUGIN_URL . 'assets/js/admin' . $suffix . '.js', array( 'jquery', 'quicktags' ), MC4WP_LITE_VERSION, true ); 
  239. wp_enqueue_script( array( 'jquery', 'mc4wp-beautifyhtml', 'mc4wp-admin' ) ); 
  240. wp_localize_script( 'mc4wp-admin', 'mc4wp',  
  241. array( 
  242. 'hasCaptchaPlugin' => $this->has_captcha_plugin,  
  243. 'strings' => array( 
  244. 'proOnlyNotice' => __( 'This option is only available in MailChimp for WordPress Pro.', 'mailchimp-for-wp' ),  
  245. 'fieldWizard' => array( 
  246. 'proOnly' => __( '(PRO ONLY)', 'mailchimp-for-wp' ),  
  247. 'buttonText' => __( 'Button text', 'mailchimp-for-wp' ),  
  248. 'initialValue' => __( 'Initial value', 'mailchimp-for-wp' ),  
  249. 'optional' => __( '(optional)', 'mailchimp-for-wp' ),  
  250. 'labelFor' => __( 'Label for', 'mailchimp-for-wp' ),  
  251. 'orLeaveEmpty' => __( '(or leave empty)', 'mailchimp-for-wp' ),  
  252. 'subscribe' => __( 'Subscribe', 'mailchimp-for-wp' ),  
  253. 'unsubscribe' => __( 'Unsubscribe', 'mailchimp-for-wp' ),  
  254. ),  
  255. 'mailchimpLists' => $mailchimp->get_lists() 
  256. ); 
  257.  
  258. return true; 
  259.  
  260. /** 
  261. * Returns available checkbox integrations 
  262. * @return array 
  263. */ 
  264. public function get_checkbox_compatible_plugins() 
  265. static $checkbox_plugins; 
  266.  
  267. if( is_array( $checkbox_plugins ) ) { 
  268. return $checkbox_plugins; 
  269.  
  270. $checkbox_plugins = array( 
  271. 'comment_form' => __( 'Comment form', 'mailchimp-for-wp' ),  
  272. 'registration_form' => __( 'Registration form', 'mailchimp-for-wp' ) 
  273. ); 
  274.  
  275. if( is_multisite() ) { 
  276. $checkbox_plugins['multisite_form'] = __( 'MultiSite forms', 'mailchimp-for-wp' ); 
  277.  
  278. if( class_exists( 'BuddyPress' ) ) { 
  279. $checkbox_plugins['buddypress_form'] = __( 'BuddyPress registration', 'mailchimp-for-wp' ); 
  280.  
  281. if( class_exists( 'bbPress' ) ) { 
  282. $checkbox_plugins['bbpress_forms'] = 'bbPress'; 
  283.  
  284. if ( class_exists( 'WooCommerce' ) ) { 
  285. $checkbox_plugins['woocommerce_checkout'] = sprintf( __( '%s checkout', 'mailchimp-for-wp' ), 'WooCommerce' ); 
  286.  
  287. if ( class_exists( 'Easy_Digital_Downloads' ) ) { 
  288. $checkbox_plugins['edd_checkout'] = sprintf( __( '%s checkout', 'mailchimp-for-wp' ), 'Easy Digital Downloads' ); 
  289.  
  290. return $checkbox_plugins; 
  291.  
  292. /** 
  293. * Redirects to the premium version of MailChimp for WordPress (uses JS) 
  294. */ 
  295. public function redirect_to_pro() 
  296. ?><script type="text/javascript">window.location.replace('https://mc4wp.com/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=menu-upgrade-link'); </script><?php 
  297.  
  298. /** 
  299. * Show the API settings page 
  300. */ 
  301. public function show_api_settings() 
  302. $opts = mc4wp_get_options( 'general' ); 
  303. $connected = ( mc4wp_get_api()->is_connected() ); 
  304.  
  305. // cache renewal triggered manually? 
  306. $force_cache_refresh = isset( $_POST['mc4wp-renew-cache'] ) && $_POST['mc4wp-renew-cache'] == 1; 
  307. $mailchimp = new MC4WP_MailChimp(); 
  308. $lists = $mailchimp->get_lists( $force_cache_refresh ); 
  309.  
  310. if( $lists && count( $lists ) === 100 ) { 
  311. add_settings_error( 'mc4wp', 'mc4wp-lists-at-limit', __( 'The plugin can only fetch a maximum of 100 lists from MailChimp, only your first 100 lists are shown.', 'mailchimp-for-wp' ) ); 
  312.  
  313. if ( $force_cache_refresh ) { 
  314. if ( false === empty ( $lists ) ) { 
  315. add_settings_error( 'mc4wp', 'mc4wp-cache-success', __( 'Renewed MailChimp cache.', 'mailchimp-for-wp' ), 'updated' ); 
  316. } else { 
  317. add_settings_error( 'mc4wp', 'mc4wp-cache-error', __( 'Failed to renew MailChimp cache - please try again later.', 'mailchimp-for-wp' ) ); 
  318.  
  319. require MC4WP_LITE_PLUGIN_DIR . 'includes/views/api-settings.php'; 
  320.  
  321. /** 
  322. * Show the Checkbox settings page 
  323. */ 
  324. public function show_checkbox_settings() 
  325. $mailchimp = new MC4WP_MailChimp(); 
  326. $opts = mc4wp_get_options( 'checkbox' ); 
  327. $lists = $mailchimp->get_lists(); 
  328. require MC4WP_LITE_PLUGIN_DIR . 'includes/views/checkbox-settings.php'; 
  329.  
  330. /** 
  331. * Show the forms settings page 
  332. */ 
  333. public function show_form_settings() 
  334. $opts = mc4wp_get_options( 'form' ); 
  335. $mailchimp = new MC4WP_MailChimp(); 
  336. $lists = $mailchimp->get_lists(); 
  337.  
  338. require MC4WP_LITE_PLUGIN_DIR . 'includes/views/form-settings.php'; 
  339.  
  340. /** 
  341. * @return string 
  342. */ 
  343. protected function get_current_page() { 
  344. return isset( $_GET['page'] ) ? $_GET['page'] : ''; 
  345.