PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/shop quần áo starloveshop.com/wp-content/plugins/mailchimp-for-wp/includes/class-admin.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 467 lines | 258 code | 88 blank | 121 comment | 42 complexity | ca6ce204ea2e0fe07ca2a6f346ee95e4 MD5 | raw file
  1. <?php
  2. if( ! defined( "MC4WP_LITE_VERSION" ) ) {
  3. header( 'Status: 403 Forbidden' );
  4. header( 'HTTP/1.1 403 Forbidden' );
  5. exit;
  6. }
  7. class MC4WP_Lite_Admin
  8. {
  9. /**
  10. * @var bool True if the BWS Captcha plugin is activated.
  11. */
  12. private $has_captcha_plugin = false;
  13. /**
  14. * @var string The relative path to the main plugin file from the plugins dir
  15. */
  16. private $plugin_file = 'mailchimp-for-wp/mailchimp-for-wp.php';
  17. /**
  18. * Constructor
  19. */
  20. public function __construct()
  21. {
  22. $this->setup_hooks();
  23. // did the user click on upgrade to pro link?
  24. if( isset( $_GET['page'] ) && $_GET['page'] === 'mc4wp-lite-upgrade' && false === headers_sent() ) {
  25. wp_redirect( 'https://mc4wp.com/#utm_source=lite-plugin&utm_medium=link&utm_campaign=menu-upgrade-link' );
  26. exit;
  27. }
  28. }
  29. /**
  30. * Upgrade routine
  31. */
  32. private function upgrade() {
  33. // Only run if db option is at older version than code constant
  34. $db_version = get_option( 'mc4wp_lite_version', 0 );
  35. if( version_compare( MC4WP_LITE_VERSION, $db_version, '<=' ) ) {
  36. return false;
  37. }
  38. // define a constant that we're running an upgrade
  39. define( 'MC4WP_DOING_UPGRADE', true );
  40. // update code version
  41. update_option( 'mc4wp_lite_version', MC4WP_LITE_VERSION );
  42. }
  43. /**
  44. * Registers all hooks
  45. */
  46. private function setup_hooks() {
  47. global $pagenow;
  48. // Actions used throughout WP Admin
  49. add_action( 'admin_init', array( $this, 'initialize' ) );
  50. add_action( 'admin_menu', array( $this, 'build_menu' ) );
  51. add_action( 'admin_enqueue_scripts', array( $this, 'load_css_and_js' ) );
  52. // Hooks for Plugins overview
  53. if( isset( $pagenow ) && $pagenow === 'plugins.php' ) {
  54. $this->plugin_file = plugin_basename( MC4WP_LITE_PLUGIN_FILE );
  55. add_filter( 'plugin_action_links_' . $this->plugin_file, array( $this, 'add_plugin_settings_link' ), 10, 2 );
  56. add_filter( 'plugin_row_meta', array( $this, 'add_plugin_meta_links'), 10, 2 );
  57. }
  58. // Hooks for Form settings page
  59. if( isset( $_GET['page'] ) && $_GET['page'] === 'mc4wp-lite-form-settings' ) {
  60. add_filter( 'quicktags_settings', array( $this, 'set_quicktags_buttons' ), 10, 2 );
  61. }
  62. }
  63. /**
  64. * Initializes various stuff used in WP Admin
  65. *
  66. * - Registers settings
  67. * - Checks if the Captcha plugin is activated
  68. * - Loads the plugin text domain
  69. */
  70. public function initialize() {
  71. // register settings
  72. register_setting( 'mc4wp_lite_settings', 'mc4wp_lite', array( $this, 'validate_settings' ) );
  73. register_setting( 'mc4wp_lite_checkbox_settings', 'mc4wp_lite_checkbox', array( $this, 'validate_checkbox_settings' ) );
  74. register_setting( 'mc4wp_lite_form_settings', 'mc4wp_lite_form', array( $this, 'validate_form_settings' ) );
  75. // load the plugin text domain
  76. load_plugin_textdomain( 'mailchimp-for-wp', false, dirname( $this->plugin_file ) . '/languages/' );
  77. // store whether this plugin has the BWS captcha plugin running (http://wordpress.org/plugins/captcha/)
  78. $this->has_captcha_plugin = function_exists( 'cptch_display_captcha_custom' );
  79. $this->upgrade();
  80. }
  81. /**
  82. * Set which Quicktag buttons should appear in the form mark-up editor
  83. *
  84. * @param array $settings
  85. * @param string $editor_id
  86. * @return array
  87. */
  88. public function set_quicktags_buttons( $settings, $editor_id = '' )
  89. {
  90. if( $editor_id !== 'mc4wpformmarkup' ) {
  91. return $settings;
  92. }
  93. $settings['buttons'] = 'strong,em,link,img,ul,li,close';
  94. return $settings;
  95. }
  96. /**
  97. * Add the settings link to the Plugins overview
  98. * @param array $links
  99. * @return array
  100. */
  101. public function add_plugin_settings_link( $links, $file )
  102. {
  103. if( $file !== $this->plugin_file ) {
  104. return $links;
  105. }
  106. $settings_link = '<a href="admin.php?page=mc4wp-lite">'. __( 'Settings', 'mailchimp-for-wp' ) . '</a>';
  107. array_unshift( $links, $settings_link );
  108. return $links;
  109. }
  110. /**
  111. * Adds meta links to the plugin in the WP Admin > Plugins screen
  112. *
  113. * @param array $links
  114. * @param string $file
  115. *
  116. * @return array
  117. */
  118. public function add_plugin_meta_links( $links, $file ) {
  119. if( $file !== $this->plugin_file ) {
  120. return $links;
  121. }
  122. $links[] = '<a href="http://wordpress.org/plugins/mailchimp-for-wp/faq/">FAQ</a>';
  123. $links[] = '<a href="https://mc4wp.com/#utm_source=lite-plugin&utm_medium=link&utm_campaign=plugins-upgrade-link">' . __( 'Upgrade to Pro', 'mailchimp-for-wp' ) . '</a>';
  124. return $links;
  125. }
  126. /**
  127. * Register the setting pages and their menu items
  128. */
  129. public function build_menu() {
  130. /**
  131. * @filter mc4wp_settings_cap
  132. * @expects string A valid WP capability like 'manage_options' (default)
  133. *
  134. * Use to customize the required user capability to access the MC4WP settings pages
  135. */
  136. $required_cap = apply_filters( 'mc4wp_settings_cap', 'manage_options' );
  137. add_menu_page( 'MailChimp for WP Lite', 'MailChimp for WP', $required_cap, 'mc4wp-lite', array($this, 'show_api_settings'), MC4WP_LITE_PLUGIN_URL . 'assets/img/menu-icon.png' );
  138. add_submenu_page( 'mc4wp-lite', 'API Settings - MailChimp for WP Lite', __( 'MailChimp Settings', 'mailchimp-for-wp' ), $required_cap, 'mc4wp-lite', array( $this, 'show_api_settings' ) );
  139. add_submenu_page( 'mc4wp-lite', 'Checkbox Settings - MailChimp for WP Lite', __( 'Checkboxes', 'mailchimp-for-wp' ), $required_cap, 'mc4wp-lite-checkbox-settings', array($this, 'show_checkbox_settings' ) );
  140. add_submenu_page( 'mc4wp-lite', 'Form Settings - MailChimp for WP Lite', __( 'Forms', 'mailchimp-for-wp' ), $required_cap, 'mc4wp-lite-form-settings', array( $this, 'show_form_settings' ) );
  141. add_submenu_page( 'mc4wp-lite', 'Upgrade to Pro - MailChimp for WP Lite', __( 'Upgrade to Pro', 'mailchimp-for-wp' ), $required_cap, 'mc4wp-lite-upgrade', array( $this, 'redirect_to_pro' ) );
  142. }
  143. /**
  144. * Validates the General settings
  145. *
  146. * @param array $settings
  147. * @return array
  148. */
  149. public function validate_settings( $settings ) {
  150. if( isset( $settings['api_key'] ) ) {
  151. $settings['api_key'] = sanitize_text_field( $settings['api_key'] );
  152. }
  153. return $settings;
  154. }
  155. /**
  156. * Validates the Form settings
  157. *
  158. * @param array $settings
  159. * @return array
  160. */
  161. public function validate_form_settings( $settings ) {
  162. // If settings is malformed, just store an empty array.
  163. if( ! is_array( $settings ) ) {
  164. return array();
  165. }
  166. // Loop through new settings
  167. foreach( $settings as $key => $value ) {
  168. // sanitize text fields
  169. if( substr( $key, 0, 5 ) === 'text_' ) {
  170. $settings[ $key ] = strip_tags( trim( $value ), '<a><b><strong><em><br><i><u><pre><script><abbr><strike>' );
  171. continue;
  172. }
  173. switch( $key ) {
  174. // sanitize markup textarea
  175. case 'markup' :
  176. $settings[ $key ] = preg_replace( '/<\/?form(.|\s)*?>/i', '', $value );
  177. break;
  178. // sanitize select
  179. case 'css':
  180. $settings[ $key ] = sanitize_text_field( $value );
  181. break;
  182. // sanitize radio & checkbox inputs
  183. case 'double_optin':
  184. case 'hide_after_success':
  185. $settings[ $key ] = ( $value == 1 ) ? 1 : 0;
  186. break;
  187. }
  188. }
  189. return $settings;
  190. }
  191. /**
  192. * Validates the Checkbox settings
  193. *
  194. * @param array $settings
  195. * @return array
  196. */
  197. public function validate_checkbox_settings( $settings ) {
  198. // If settings is malformed, just store an empty array.
  199. if( ! is_array( $settings ) ) {
  200. return array();
  201. }
  202. // Loop through new settings
  203. foreach( $settings as $key => $value ) {
  204. switch( $key ) {
  205. case 'lists':
  206. if( ! is_array( $value ) ) {
  207. $settings[ $key ] = array();
  208. } else {
  209. foreach( $settings[ $key ] as $list_key => $list_value ) {
  210. $settings[ $key ][$list_key] = sanitize_text_field( $list_value );
  211. }
  212. }
  213. break;
  214. // sanitize text inputs
  215. case 'label' :
  216. $settings[ $key ] = strip_tags( trim( $value ), '<a><b><strong><em><br><i><u><pre><script><abbr><strike>' );
  217. break;
  218. // sanitize radio & checkbox inputs
  219. case 'double_optin':
  220. case 'show_at_comment_form':
  221. case 'show_at_registration_form':
  222. case 'precheck':
  223. case 'css':
  224. $settings[ $key ] = ( $value == 1 ) ? 1 : 0;
  225. break;
  226. }
  227. }
  228. return $settings;
  229. }
  230. /**
  231. * @param string $hook
  232. */
  233. public function load_css_and_js( $hook )
  234. {
  235. // only load asset files on the MailChimp for WordPress settings pages
  236. if( false === isset( $_GET['page'] ) || false === stristr( $_GET['page'], 'mc4wp-lite' ) ) {
  237. return;
  238. }
  239. $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
  240. $mailchimp = new MC4WP_MailChimp();
  241. // css
  242. wp_enqueue_style( 'mc4wp-admin-css', MC4WP_LITE_PLUGIN_URL . 'assets/css/admin' . $suffix . '.css' );
  243. // js
  244. wp_register_script( 'mc4wp-beautifyhtml', MC4WP_LITE_PLUGIN_URL . 'assets/js/beautify-html'. $suffix .'.js', array( 'jquery' ), MC4WP_LITE_VERSION, true );
  245. wp_register_script( 'mc4wp-admin', MC4WP_LITE_PLUGIN_URL . 'assets/js/admin' . $suffix . '.js', array( 'jquery', 'quicktags' ), MC4WP_LITE_VERSION, true );
  246. wp_enqueue_script( array( 'jquery', 'mc4wp-beautifyhtml', 'mc4wp-admin' ) );
  247. wp_localize_script( 'mc4wp-admin', 'mc4wp',
  248. array(
  249. 'hasCaptchaPlugin' => $this->has_captcha_plugin,
  250. 'strings' => array(
  251. 'proOnlyNotice' => __( 'This option is only available in MailChimp for WordPress Pro.', 'mailchimp-for-wp' ),
  252. 'fieldWizard' => array(
  253. 'proOnly' => __( '(PRO ONLY)', 'mailchimp-for-wp' ),
  254. 'buttonText' => __( 'Button text', 'mailchimp-for-wp' ),
  255. 'initialValue' => __( 'Initial value', 'mailchimp-for-wp' ),
  256. 'optional' => __( '(optional)', 'mailchimp-for-wp' ),
  257. 'labelFor' => __( 'Label for', 'mailchimp-for-wp' ),
  258. 'orLeaveEmpty' => __( '(or leave empty)', 'mailchimp-for-wp' ),
  259. 'subscribe' => __( 'Subscribe', 'mailchimp-for-wp' )
  260. )
  261. ),
  262. 'mailchimpLists' => $mailchimp->get_lists()
  263. )
  264. );
  265. }
  266. /**
  267. * Returns available checkbox integrations
  268. *
  269. * @return array
  270. */
  271. public function get_checkbox_compatible_plugins()
  272. {
  273. static $checkbox_plugins;
  274. if( is_array( $checkbox_plugins ) ) {
  275. return $checkbox_plugins;
  276. }
  277. $checkbox_plugins = array(
  278. 'comment_form' => __( "Comment form", 'mailchimp-for-wp' ),
  279. "registration_form" => __( "Registration form", 'mailchimp-for-wp' )
  280. );
  281. if( is_multisite() ) {
  282. $checkbox_plugins['multisite_form'] = __( "MultiSite forms", 'mailchimp-for-wp' );
  283. }
  284. if( class_exists("BuddyPress") ) {
  285. $checkbox_plugins['buddypress_form'] = __( "BuddyPress registration", 'mailchimp-for-wp' );
  286. }
  287. if( class_exists('bbPress') ) {
  288. $checkbox_plugins['bbpress_forms'] = "bbPress";
  289. }
  290. if ( class_exists( 'WooCommerce' ) ) {
  291. $checkbox_plugins['woocommerce_checkout'] = sprintf( __( '%s checkout', 'mailchimp-for-wp' ), 'WooCommerce' );
  292. }
  293. if ( class_exists( 'Easy_Digital_Downloads' ) ) {
  294. $checkbox_plugins['edd_checkout'] = sprintf( __( '%s checkout', 'mailchimp-for-wp' ), 'Easy Digital Downloads' );
  295. }
  296. return $checkbox_plugins;
  297. }
  298. /**
  299. * Redirects to the premium version of MailChimp for WordPress (uses JS)
  300. */
  301. public function redirect_to_pro()
  302. {
  303. ?><script type="text/javascript">window.location.replace('https://mc4wp.com/#utm_source=lite-plugin&utm_medium=link&utm_campaign=menu-upgrade-link'); </script><?php
  304. }
  305. /**
  306. * Show the API settings page
  307. */
  308. public function show_api_settings()
  309. {
  310. $opts = mc4wp_get_options( 'general' );
  311. $connected = ( mc4wp_get_api()->is_connected() );
  312. // cache renewal triggered manually?
  313. $force_cache_refresh = isset( $_POST['mc4wp-renew-cache'] ) && $_POST['mc4wp-renew-cache'] == 1;
  314. $mailchimp = new MC4WP_MailChimp();
  315. $lists = $mailchimp->get_lists( $force_cache_refresh );
  316. if ( $force_cache_refresh ) {
  317. if ( false === empty ( $lists ) ) {
  318. add_settings_error( "mc4wp", "mc4wp-cache-success", __( 'Renewed MailChimp cache.', 'mailchimp-for-wp' ), 'updated' );
  319. } else {
  320. add_settings_error( "mc4wp", "mc4wp-cache-error", __( 'Failed to renew MailChimp cache - please try again later.', 'mailchimp-for-wp' ) );
  321. }
  322. }
  323. require MC4WP_LITE_PLUGIN_DIR . 'includes/views/api-settings.php';
  324. }
  325. /**
  326. * Show the Checkbox settings page
  327. */
  328. public function show_checkbox_settings()
  329. {
  330. $mailchimp = new MC4WP_MailChimp();
  331. $opts = mc4wp_get_options( 'checkbox' );
  332. $lists = $mailchimp->get_lists();
  333. require MC4WP_LITE_PLUGIN_DIR . 'includes/views/checkbox-settings.php';
  334. }
  335. /**
  336. * Show the forms settings page
  337. */
  338. public function show_form_settings()
  339. {
  340. $opts = mc4wp_get_options( 'form' );
  341. $mailchimp = new MC4WP_MailChimp();
  342. $lists = $mailchimp->get_lists();
  343. // create array of missing form fields
  344. $missing_form_fields = array();
  345. // check if form contains EMAIL field
  346. $search = preg_match( '/<(input|textarea)(?=[^>]*name="EMAIL")[^>]*>/i', $opts['markup'] );
  347. if( ! $search) {
  348. $missing_form_fields[] = sprintf( __( 'An EMAIL field. Example: <code>%s</code>', 'mailchimp-for-wp' ), '&lt;input type="email" name="EMAIL" /&gt;' );
  349. }
  350. // check if form contains submit button
  351. $search = preg_match( '/<(input|button)(?=[^>]*type="submit")[^>]*>/i', $opts['markup'] );
  352. if( ! $search ) {
  353. $missing_form_fields[] = sprintf( __( 'A submit button. Example: <code>%s</code>', 'mailchimp-for-wp' ), '&lt;input type="submit" value="'. __( 'Sign Up', 'mailchimp-for-wp' ) .'" /&gt;' );
  354. }
  355. // loop through selected list ids
  356. if( isset( $opts['lists'] ) && is_array( $opts['lists'] ) ) {
  357. foreach( $opts['lists'] as $list_id ) {
  358. // get list object
  359. $list = $mailchimp->get_list( $list_id );
  360. if( ! is_object( $list ) ) {
  361. continue;
  362. }
  363. // loop through merge vars of this list
  364. foreach( $list->merge_vars as $merge_var ) {
  365. // if field is required, make sure it's in the form mark-up
  366. if( ! $merge_var->req || $merge_var->tag === 'EMAIL' ) {
  367. continue;
  368. }
  369. // search for field tag in form mark-up using 'name="FIELD_NAME' without closing " because of array fields
  370. $search = stristr( $opts['markup'], 'name="'. $merge_var->tag );
  371. if( false === $search ) {
  372. $missing_form_fields[] = sprintf( __( 'A \'%s\' field', 'mailchimp-for-wp' ), $merge_var->tag );
  373. }
  374. }
  375. }
  376. }
  377. require MC4WP_LITE_PLUGIN_DIR . 'includes/views/form-settings.php';
  378. }
  379. }