PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/mailchimp-for-wp/includes/admin/class-admin.php

https://gitlab.com/bhargavi_dcw/dflocal
PHP | 513 lines | 221 code | 94 blank | 198 comment | 21 complexity | 9dee53f7a64959a316a9f6dc530116c5 MD5 | raw file
  1. <?php
  2. /**
  3. * Class MC4WP_Admin
  4. *
  5. * @ignore
  6. * @access private
  7. */
  8. class MC4WP_Admin {
  9. /**
  10. * @var string The relative path to the main plugin file from the plugins dir
  11. */
  12. protected $plugin_file;
  13. /**
  14. * @var MC4WP_MailChimp
  15. */
  16. protected $mailchimp;
  17. /**
  18. * @var MC4WP_Admin_Messages
  19. */
  20. protected $messages;
  21. /**
  22. * @var MC4WP_Admin_Ads
  23. */
  24. protected $ads;
  25. /**
  26. * @var MC4WP_Update_Optin
  27. */
  28. protected $update_optin;
  29. /**
  30. * Constructor
  31. *
  32. * @param MC4WP_Admin_Messages $messages
  33. * @param MC4WP_MailChimp $mailchimp
  34. */
  35. public function __construct( MC4WP_Admin_Messages $messages, MC4WP_MailChimp $mailchimp ) {
  36. $this->mailchimp = $mailchimp;
  37. $this->messages = $messages;
  38. $this->plugin_file = plugin_basename( MC4WP_PLUGIN_FILE );
  39. $this->ads = new MC4WP_Admin_Ads();
  40. $this->load_translations();
  41. // update opt-in
  42. $this->update_optin = new MC4WP_Update_Optin( '4.0.0', $this->plugin_file, MC4WP_PLUGIN_DIR . 'includes/views/parts/update-4.x-notice.php' );
  43. }
  44. /**
  45. * Registers all hooks
  46. */
  47. public function add_hooks() {
  48. // Actions used globally throughout WP Admin
  49. add_action( 'admin_menu', array( $this, 'build_menu' ) );
  50. add_action( 'admin_init', array( $this, 'initialize' ) );
  51. add_action( 'current_screen', array( $this, 'customize_admin_texts' ) );
  52. add_action( 'wp_dashboard_setup', array( $this, 'register_dashboard_widgets' ) );
  53. add_action( 'mc4wp_admin_empty_lists_cache', array( $this, 'renew_lists_cache' ) );
  54. add_action( 'mc4wp_admin_empty_debug_log', array( $this, 'empty_debug_log' ) );
  55. add_action( 'admin_notices', array( $this, 'show_api_key_notice' ) );
  56. add_action( 'mc4wp_admin_dismiss_api_key_notice', array( $this, 'dismiss_api_key_notice' ) );
  57. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
  58. $this->ads->add_hooks();
  59. $this->messages->add_hooks();
  60. $this->update_optin->add_hooks();
  61. }
  62. /**
  63. * Initializes various stuff used in WP Admin
  64. *
  65. * - Registers settings
  66. */
  67. public function initialize() {
  68. // register settings
  69. register_setting( 'mc4wp_settings', 'mc4wp', array( $this, 'save_general_settings' ) );
  70. // Load upgrader
  71. $this->init_upgrade_routines();
  72. // listen for custom actions
  73. $this->listen_for_actions();
  74. }
  75. /**
  76. * Listen for `_mc4wp_action` requests
  77. */
  78. public function listen_for_actions() {
  79. // listen for any action (if user is authorised)
  80. if( ! $this->is_user_authorized() || ! isset( $_REQUEST['_mc4wp_action'] ) ) {
  81. return false;
  82. }
  83. $action = (string) $_REQUEST['_mc4wp_action'];
  84. /**
  85. * Allows you to hook into requests containing `_mc4wp_action` => action name.
  86. *
  87. * The dynamic portion of the hook name, `$action`, refers to the action name.
  88. *
  89. * By the time this hook is fired, the user is already authorized. After processing all the registered hooks,
  90. * the request is redirected back to the referring URL.
  91. *
  92. * @since 3.0
  93. */
  94. do_action( 'mc4wp_admin_' . $action );
  95. // redirect back to where we came from
  96. $redirect_url = remove_query_arg( '_mc4wp_action' );
  97. wp_redirect( $redirect_url );
  98. exit;
  99. }
  100. /**
  101. * Register dashboard widgets
  102. */
  103. public function register_dashboard_widgets() {
  104. if( ! $this->is_user_authorized() ) {
  105. return false;
  106. }
  107. /**
  108. * Setup dashboard widget, users are authorized by now.
  109. *
  110. * Use this hook to register your own dashboard widgets for users with the required capability.
  111. *
  112. * @since 3.0
  113. */
  114. do_action( 'mc4wp_dashboard_setup' );
  115. return true;
  116. }
  117. /**
  118. * Upgrade routine
  119. */
  120. private function init_upgrade_routines() {
  121. // upgrade routine for upgrade routine....
  122. $previous_version = get_option( 'mc4wp_lite_version', 0 );
  123. if( $previous_version ) {
  124. delete_option( 'mc4wp_lite_version' );
  125. update_option( 'mc4wp_version', $previous_version );
  126. }
  127. // Only run if db option is at older version than code constant
  128. $previous_version = get_option( 'mc4wp_version', 0 );
  129. // Installing or rollback?
  130. if( empty( $previous_version ) || version_compare( $previous_version, MC4WP_VERSION, '>' ) ) {
  131. update_option( 'mc4wp_version', MC4WP_VERSION );
  132. return false;
  133. }
  134. // This means we're good!
  135. if( version_compare( $previous_version, MC4WP_VERSION ) > -1 ) {
  136. return false;
  137. }
  138. define( 'MC4WP_DOING_UPGRADE', true );
  139. $upgrade_routines = new MC4WP_Upgrade_Routines( $previous_version, MC4WP_VERSION, dirname( __FILE__ ) . '/migrations' );
  140. $upgrade_routines->run();
  141. update_option( 'mc4wp_version', MC4WP_VERSION );
  142. }
  143. /**
  144. * Renew MailChimp lists cache
  145. */
  146. public function renew_lists_cache() {
  147. $this->mailchimp->empty_cache();
  148. // try getting new lists to fill cache again
  149. $lists = $this->mailchimp->get_lists();
  150. if( ! empty( $lists ) ) {
  151. $this->messages->flash( __( 'Success! The cached configuration for your MailChimp lists has been renewed.', 'mailchimp-for-wp' ) );
  152. }
  153. }
  154. /**
  155. * Load the plugin translations
  156. */
  157. private function load_translations() {
  158. // load the plugin text domain
  159. load_plugin_textdomain( 'mailchimp-for-wp', false, dirname( $this->plugin_file ) . '/languages' );
  160. }
  161. /**
  162. * Customize texts throughout WP Admin
  163. */
  164. public function customize_admin_texts() {
  165. $texts = new MC4WP_Admin_Texts( $this->plugin_file );
  166. $texts->add_hooks();
  167. }
  168. /**
  169. * Validates the General settings
  170. * @param array $settings
  171. * @return array
  172. */
  173. public function save_general_settings( array $settings ) {
  174. $current = mc4wp_get_options();
  175. // merge with current settings to allow passing partial arrays to this method
  176. $settings = array_merge( $current, $settings );
  177. // toggle usage tracking
  178. if( $settings['allow_usage_tracking'] !== $current['allow_usage_tracking'] ) {
  179. MC4WP_Usage_Tracking::instance()->toggle( $settings['allow_usage_tracking'] );
  180. }
  181. // Make sure not to use obfuscated key
  182. if( strpos( $settings['api_key'], '*' ) !== false ) {
  183. $settings['api_key'] = $current['api_key'];
  184. }
  185. // Sanitize API key
  186. $settings['api_key'] = sanitize_text_field( $settings['api_key'] );
  187. // if API key changed, empty MailChimp cache
  188. if ( $settings['api_key'] !== $current['api_key'] ) {
  189. $this->mailchimp->empty_cache();
  190. }
  191. /**
  192. * Runs right before general settings are saved.
  193. *
  194. * @param array $settings The updated settings array
  195. * @param array $current The old settings array
  196. */
  197. do_action( 'mc4wp_save_settings', $settings, $current );
  198. return $settings;
  199. }
  200. /**
  201. * Load scripts and stylesheet on MailChimp for WP Admin pages
  202. *
  203. * @return bool
  204. */
  205. public function enqueue_assets() {
  206. global $wp_scripts;
  207. $prefix = 'mailchimp-for-wp';
  208. // only load asset files on the MailChimp for WordPress settings pages
  209. if( empty( $_GET['page'] ) || strpos( $_GET['page'], $prefix ) !== 0 ) {
  210. return false;
  211. }
  212. $page = ltrim( substr( $_GET['page'], strlen( $prefix ) ), '-' );
  213. $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
  214. // css
  215. wp_register_style( 'mc4wp-admin', MC4WP_PLUGIN_URL . 'assets/css/admin-styles' . $suffix . '.css', array(), MC4WP_VERSION );
  216. wp_enqueue_style( 'mc4wp-admin' );
  217. // js
  218. wp_register_script( 'es5-shim', MC4WP_PLUGIN_URL . 'assets/js/third-party/es5-shim.min.js', array(), MC4WP_VERSION );
  219. $wp_scripts->add_data( 'es5-shim', 'conditional', 'lt IE 9' );
  220. // @todo: eventually get rid of jQuery here
  221. wp_register_script( 'mc4wp-admin', MC4WP_PLUGIN_URL . 'assets/js/admin' . $suffix . '.js', array( 'jquery', 'es5-shim' ), MC4WP_VERSION, true );
  222. wp_enqueue_script( array( 'jquery', 'es5-shim', 'mc4wp-admin' ) );
  223. wp_localize_script( 'mc4wp-admin', 'mc4wp_vars',
  224. array(
  225. 'mailchimp' => array(
  226. 'lists' => $this->mailchimp->get_lists()
  227. ),
  228. 'countries' => MC4WP_Tools::get_countries(),
  229. 'l10n' => array(
  230. 'pro_only' => __( 'This is a pro-only feature. Please upgrade to the premium version to be able to use it.', 'mailchimp-for-wp' )
  231. )
  232. )
  233. );
  234. /**
  235. * Hook to enqueue your own custom assets on the MailChimp for WordPress setting pages.
  236. *
  237. * @since 3.0
  238. *
  239. * @param string $suffix
  240. * @param string $page
  241. */
  242. do_action( 'mc4wp_admin_enqueue_assets', $suffix, $page );
  243. return true;
  244. }
  245. /**
  246. * Does the logged-in user have the required capability?
  247. *
  248. * @return bool
  249. */
  250. public function is_user_authorized() {
  251. return current_user_can( $this->get_required_capability() );
  252. }
  253. /**
  254. * Get required capability to access settings page and view dashboard widgets.
  255. *
  256. * @return string
  257. */
  258. public function get_required_capability() {
  259. $capability = 'manage_options';
  260. /**
  261. * Filters the required user capability to access the settings pages & dashboard widgets.
  262. *
  263. * @ignore
  264. * @deprecated 3.0
  265. */
  266. $capability = apply_filters( 'mc4wp_settings_cap', $capability );
  267. /**
  268. * Filters the required user capability to access the MailChimp for WordPress' settings pages, view the dashboard widgets.
  269. *
  270. * Defaults to `manage_options`
  271. *
  272. * @since 3.0
  273. * @param string $capability
  274. * @see https://codex.wordpress.org/Roles_and_Capabilities
  275. */
  276. $capability = (string) apply_filters( 'mc4wp_admin_required_capability', $capability );
  277. return $capability;
  278. }
  279. /**
  280. * Register the setting pages and their menu items
  281. */
  282. public function build_menu() {
  283. $required_cap = $this->get_required_capability();
  284. $menu_items = array(
  285. 'general' => array(
  286. 'title' => __( 'MailChimp API Settings', 'mailchimp-for-wp' ),
  287. 'text' => __( 'MailChimp', 'mailchimp-for-wp' ),
  288. 'slug' => '',
  289. 'callback' => array( $this, 'show_generals_setting_page' ),
  290. 'position' => 0
  291. ),
  292. 'other' => array(
  293. 'title' => __( 'Other Settings', 'mailchimp-for-wp' ),
  294. 'text' => __( 'Other', 'mailchimp-for-wp' ),
  295. 'slug' => 'other',
  296. 'callback' => array( $this, 'show_other_setting_page' ),
  297. 'position' => 90
  298. )
  299. );
  300. /**
  301. * Filters the menu items to appear under the main menu item.
  302. *
  303. * To add your own item, add an associative array in the following format.
  304. *
  305. * $menu_items[] = array(
  306. * 'title' => 'Page title',
  307. * 'text' => 'Menu text',
  308. * 'slug' => 'Page slug',
  309. * 'callback' => 'my_page_function',
  310. * 'position' => 50
  311. * );
  312. *
  313. * @param array $menu_items
  314. * @since 3.0
  315. */
  316. $menu_items = (array) apply_filters( 'mc4wp_admin_menu_items', $menu_items );
  317. // add top menu item
  318. add_menu_page( 'MailChimp for WP', 'MailChimp for WP', $required_cap, 'mailchimp-for-wp', array( $this, 'show_generals_setting_page' ), MC4WP_PLUGIN_URL . 'assets/img/icon.png', '99.68491' );
  319. // sort submenu items by 'position'
  320. uasort( $menu_items, array( $this, 'sort_menu_items_by_position' ) );
  321. // add sub-menu items
  322. array_walk( $menu_items, array( $this, 'add_menu_item' ) );
  323. }
  324. /**
  325. * @param array $item
  326. */
  327. public function add_menu_item( array $item ) {
  328. // generate menu slug
  329. $slug = 'mailchimp-for-wp';
  330. if( ! empty( $item['slug'] ) ) {
  331. $slug .= '-' . $item['slug'];
  332. }
  333. // provide some defaults
  334. $parent_slug = ! empty( $item['parent_slug']) ? $item['parent_slug'] : 'mailchimp-for-wp';
  335. $capability = ! empty( $item['capability'] ) ? $item['capability'] : $this->get_required_capability();
  336. // register page
  337. $hook = add_submenu_page( $parent_slug, $item['title'] . ' - MailChimp for WordPress', $item['text'], $capability, $slug, $item['callback'] );
  338. // register callback for loading this page, if given
  339. if( array_key_exists( 'load_callback', $item ) ) {
  340. add_action( 'load-' . $hook, $item['load_callback'] );
  341. }
  342. }
  343. /**
  344. * Show the API Settings page
  345. */
  346. public function show_generals_setting_page() {
  347. $opts = mc4wp_get_options();
  348. $connected = ( mc4wp('api')->is_connected() );
  349. $lists = $this->mailchimp->get_lists();
  350. $obfuscated_api_key = mc4wp_obfuscate_string( $opts['api_key'] );
  351. require MC4WP_PLUGIN_DIR . 'includes/views/general-settings.php';
  352. }
  353. /**
  354. * Show the Other Settings page
  355. */
  356. public function show_other_setting_page() {
  357. $opts = mc4wp_get_options();
  358. $log = $this->get_log();
  359. $log_reader = new MC4WP_Debug_Log_Reader( $log->file );
  360. require MC4WP_PLUGIN_DIR . 'includes/views/other-settings.php';
  361. }
  362. /**
  363. * @param $a
  364. * @param $b
  365. *
  366. * @return int
  367. */
  368. public function sort_menu_items_by_position( $a, $b ) {
  369. $pos_a = isset( $a['position'] ) ? $a['position'] : 80;
  370. $pos_b = isset( $b['position'] ) ? $b['position'] : 90;
  371. return $pos_a < $pos_b ? -1 : 1;
  372. }
  373. /**
  374. * Empties the log file
  375. */
  376. public function empty_debug_log() {
  377. $log = $this->get_log();
  378. file_put_contents( $log->file, '' );
  379. $this->messages->flash( __( 'Log successfully emptied.', 'mailchimp-for-wp' ) );
  380. }
  381. /**
  382. * Shows a notice when API key is not set.
  383. */
  384. public function show_api_key_notice() {
  385. // don't show if on settings page already
  386. if( isset( $_GET['page'] ) && $_GET['page'] === 'mailchimp-for-wp' ) {
  387. return;
  388. }
  389. // only show to user with proper permissions
  390. if( ! $this->is_user_authorized() ) {
  391. return;
  392. }
  393. // don't show if dismissed
  394. if( get_transient( 'mc4wp_api_key_notice_dismissed' ) ) {
  395. return;
  396. }
  397. // don't show if api key is set already
  398. $options = mc4wp_get_options();
  399. if( ! empty( $options['api_key'] ) ) {
  400. return;
  401. }
  402. echo '<div class="notice notice-warning" style="position: relative; padding-right: 36px;">';
  403. echo '<p>' . sprintf( __( 'To get started with MailChimp for WordPress, please <a href="%s">enter your MailChimp API key on the settings page of the plugin</a>.', 'mailchimp-for-wp' ), admin_url( 'admin.php?page=mailchimp-for-wp' ) ) . '</p>';
  404. echo '<form method="post"><input type="hidden" name="_mc4wp_action" value="dismiss_api_key_notice" /><button type="submit" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></form>';
  405. echo '</div>';
  406. }
  407. /**
  408. * Dismisses the API key notice for 1 week
  409. */
  410. public function dismiss_api_key_notice() {
  411. set_transient( 'mc4wp_api_key_notice_dismissed', 1, 3600 * 24 * 7 );
  412. }
  413. /**
  414. * @return MC4WP_Debug_Log
  415. */
  416. protected function get_log() {
  417. return mc4wp('log');
  418. }
  419. }