暫無描述

Tools.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Admin_Tools
  10. */
  11. class PUM_Admin_Tools {
  12. /**
  13. * @var array
  14. */
  15. public static $notices = array();
  16. /**
  17. * Initializes the "Tools" page.
  18. */
  19. public static function init() {
  20. add_action( 'admin_notices', array( __CLASS__, 'notices' ) );
  21. add_action( 'admin_init', array( __CLASS__, 'emodal_process_import' ) );
  22. add_action( 'pum_tools_page_tab_betas', array( __CLASS__, 'betas_display' ) );
  23. add_action( 'pum_tools_page_tab_error_log', array( __CLASS__, 'errorlog_display' ) );
  24. add_action( 'pum_tools_page_tab_action_scheduler', array( __CLASS__, 'action_scheduler_display' ) );
  25. add_action( 'pum_tools_page_tab_import', array( __CLASS__, 'import_display' ) );
  26. add_action( 'pum_save_enabled_betas', array( __CLASS__, 'save_enabled_betas' ) );
  27. add_action( 'pum_empty_error_log', array( __CLASS__, 'error_log_empty' ) );
  28. }
  29. /**
  30. * Displays any saved admin notices.
  31. */
  32. public static function notices() {
  33. if ( isset( $_GET['imported'] ) ) {
  34. ?>
  35. <div class="updated">
  36. <p><?php esc_html_e( 'Successfully Imported your themes &amp; modals from Easy Modal.' ); ?></p>
  37. </div>
  38. <?php
  39. }
  40. if ( isset( $_GET['success'] ) && get_option( 'pum_settings_admin_notice' ) ) {
  41. self::$notices[] = array(
  42. 'type' => $_GET['success'] ? 'success' : 'error',
  43. 'message' => get_option( 'pum_settings_admin_notice' ),
  44. );
  45. delete_option( 'pum_settings_admin_notice' );
  46. }
  47. if ( ! empty( self::$notices ) ) {
  48. foreach ( self::$notices as $notice ) {
  49. ?>
  50. <div class="notice notice-<?php echo esc_attr( $notice['type'] ); ?> is-dismissible">
  51. <p><strong><?php esc_html( $notice['message'] ); ?></strong></p>
  52. <button type="button" class="notice-dismiss">
  53. <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'popup-maker' ); ?></span>
  54. </button>
  55. </div>
  56. <?php
  57. }
  58. }
  59. }
  60. /**
  61. * Render settings page with tabs.
  62. */
  63. public static function page() {
  64. $active_tab = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], self::tabs() ) ? $_GET['tab'] : 'system_info';
  65. wp_enqueue_style( 'pum-admin-general' );
  66. ?>
  67. <div class="wrap">
  68. <form id="pum-tools" method="post" action="">
  69. <?php wp_nonce_field( basename( __FILE__ ), 'pum_tools_nonce' ); ?>
  70. <button class="right top button-primary"><?php esc_html_e( 'Save', 'popup-maker' ); ?></button>
  71. <h1><?php esc_html_e( 'Popup Maker Tools', 'popup-maker' ); ?></h1>
  72. <h2 id="popmake-tabs" class="nav-tab-wrapper">
  73. <?php
  74. foreach ( self::tabs() as $tab_id => $tab_name ) {
  75. $tab_url = add_query_arg(
  76. array(
  77. 'tools-updated' => false,
  78. 'tab' => $tab_id,
  79. )
  80. );
  81. printf( '<a href="%s" title="%s" class="nav-tab %s">%s</a>', esc_url( $tab_url ), esc_attr( $tab_name ), $active_tab == $tab_id ? ' nav-tab-active' : '', esc_html( $tab_name ) );
  82. }
  83. ?>
  84. </h2>
  85. <div id="tab_container">
  86. <?php do_action( 'pum_tools_page_tab_' . $active_tab ); ?>
  87. <?php do_action( 'popmake_tools_page_tab_' . $active_tab ); ?>
  88. </div>
  89. </form>
  90. </div>
  91. <?php
  92. }
  93. /**
  94. * Tabs & labels
  95. *
  96. * @return array $tabs
  97. * @since 1.0
  98. */
  99. public static function tabs() {
  100. static $tabs;
  101. if ( ! isset( $tabs ) ) {
  102. $tabs = apply_filters(
  103. 'pum_tools_tabs',
  104. array(
  105. 'betas' => __( 'Beta Versions', 'popup-maker' ),
  106. 'error_log' => __( 'Error Log', 'popup-maker' ),
  107. 'action_scheduler' => __( 'Scheduled Actions', 'popup-maker' ),
  108. 'import' => __( 'Import / Export', 'popup-maker' ),
  109. )
  110. );
  111. /** @deprecated 1.7.0 */
  112. $tabs = apply_filters( 'popmake_tools_tabs', $tabs );
  113. }
  114. return $tabs;
  115. }
  116. /**
  117. * Return an array of all extensions with beta support
  118. *
  119. * Extensions should be added as 'extension-slug' => 'Extension Name'
  120. *
  121. * @return array $extensions The array of extensions
  122. * @since 1.5
  123. */
  124. public static function get_beta_enabled_extensions() {
  125. return apply_filters( 'pum_beta_enabled_extensions', array() );
  126. }
  127. /**
  128. * @return int|null|string
  129. */
  130. public static function get_active_tab() {
  131. $tabs = self::tabs();
  132. return isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $tabs ) ? sanitize_text_field( $_GET['tab'] ) : key( $tabs );
  133. }
  134. /**
  135. * Display beta opt-ins
  136. *
  137. * @since 1.3
  138. */
  139. public static function betas_display() {
  140. if ( ! current_user_can( 'manage_options' ) ) {
  141. return;
  142. }
  143. $has_beta = self::get_beta_enabled_extensions();
  144. do_action( 'pum_tools_betas_before' );
  145. ?>
  146. <div class="postbox pum-beta-support">
  147. <h3><span><?php esc_html_e( 'Enable Beta Versions', 'popup-maker' ); ?></span></h3>
  148. <div class="inside">
  149. <p><?php esc_html_e( 'Checking any of the below checkboxes will opt you in to receive pre-release update notifications. You can opt-out at any time. Pre-release updates do not install automatically, you will still have the opportunity to ignore update notifications.', 'popup-maker' ); ?></p>
  150. <table class="form-table pum-beta-support">
  151. <tbody>
  152. <?php foreach ( $has_beta as $slug => $product ) : ?>
  153. <tr>
  154. <?php $checked = self::extension_has_beta_support( $slug ); ?>
  155. <th scope="row"><?php echo esc_html( $product ); ?></th>
  156. <td>
  157. <input type="checkbox" name="enabled_betas[<?php echo esc_attr( $slug ); ?>]" id="enabled_betas[<?php echo esc_attr( $slug ); ?>]"<?php echo checked( $checked, true, false ); ?> value="1" />
  158. <label for="enabled_betas[<?php echo esc_attr( $slug ); ?>]"><?php printf( __( 'Get updates for pre-release versions of %s', 'popup-maker' ), $product ); ?></label>
  159. </td>
  160. </tr>
  161. <?php endforeach; ?>
  162. </tbody>
  163. </table>
  164. <input type="hidden" name="pum_action" value="save_enabled_betas" />
  165. <?php wp_nonce_field( 'pum_save_betas_nonce', 'pum_save_betas_nonce' ); ?>
  166. <?php submit_button( __( 'Save', 'popup-maker' ), 'secondary', 'submit', false ); ?>
  167. </div>
  168. </div>
  169. <?php
  170. do_action( 'pum_tools_betas_after' );
  171. }
  172. /**
  173. * Displays the contents of the Error Log tab
  174. *
  175. * @since 1.12.0
  176. */
  177. public static function errorlog_display() {
  178. ?>
  179. <h2>Error Log</h2>
  180. <a target="_blank" rel="noreferrer noopener" href="<?php echo esc_url( PUM_Utils_Logging::instance()->get_file_url() ); ?>" download="pum-debug.log" class="button button-primary button-with-icon"><i class="dashicons dashicons-download"></i>Download Error Log</a>
  181. <form action="" method="POST">
  182. <input type="hidden" name="pum_action" value="empty_error_log" />
  183. <?php wp_nonce_field( 'pum_popup_empty_log_nonce', 'pum_popup_empty_log_nonce' ); ?>
  184. <?php submit_button( 'Empty Error Log', '', 'popmake-empty-log', false ); ?>
  185. </form>
  186. <div id="log-viewer">
  187. <pre><?php echo esc_html( self::display_error_log() ); ?></pre>
  188. </div>
  189. <?php
  190. }
  191. /**
  192. * Displays the content for the Scheduled Actions tab.
  193. *
  194. * @uses ActionScheduler_AdminView::render_admin_ui()
  195. * @since 1.12.0
  196. */
  197. public static function action_scheduler_display() {
  198. if ( class_exists( 'ActionScheduler_AdminView' ) ) {
  199. $test = new ActionScheduler_AdminView();
  200. $test->render_admin_ui();
  201. }
  202. }
  203. /**
  204. * Displays the contents for the Import tab
  205. *
  206. * @since 1.12.0
  207. */
  208. public static function import_display() {
  209. ?>
  210. <h2>Using Easy Modal?</h2>
  211. <p>Click this button to import popups from the Easy Modal plugin.</p>
  212. <button id="popmake_emodal_v2_import" name="popmake_emodal_v2_import" class="button button-large">
  213. <?php esc_html_e( 'Import From Easy Modal v2', 'popup-maker' ); ?>
  214. </button>
  215. <?php
  216. }
  217. /**
  218. * Add a button to import easy modal data.
  219. *
  220. * @deprecated
  221. */
  222. public static function emodal_v2_import_button() {
  223. self::import_display();
  224. }
  225. /**
  226. * Empties error log when user clicks on button
  227. *
  228. * @since 1.12.0
  229. */
  230. public static function error_log_empty() {
  231. if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( $_POST['pum_popup_empty_log_nonce'], 'pum_popup_empty_log_nonce' ) ) {
  232. return;
  233. }
  234. PUM_Utils_Logging::instance()->clear_log();
  235. }
  236. /**
  237. * Process em import.
  238. */
  239. public static function emodal_process_import() {
  240. if ( ! isset( $_REQUEST['popmake_emodal_v2_import'] ) ) {
  241. return;
  242. }
  243. popmake_emodal_v2_import();
  244. wp_redirect( admin_url( 'edit.php?post_type=popup&page=pum-tools&imported=1' ), 302 );
  245. }
  246. /**
  247. * Save enabled betas
  248. *
  249. * @since 1.5
  250. */
  251. public static function save_enabled_betas() {
  252. if ( ! wp_verify_nonce( $_POST['pum_save_betas_nonce'], 'pum_save_betas_nonce' ) ) {
  253. return;
  254. }
  255. if ( ! current_user_can( 'manage_options' ) ) {
  256. return;
  257. }
  258. if ( ! empty( $_POST['enabled_betas'] ) ) {
  259. $enabled_betas = array_filter(
  260. array_map(
  261. array(
  262. __CLASS__,
  263. 'enabled_betas_sanitize_value',
  264. ),
  265. $_POST['enabled_betas']
  266. )
  267. );
  268. PUM_Utils_Options::update( 'enabled_betas', $enabled_betas );
  269. } else {
  270. PUM_Utils_Options::delete( 'enabled_betas' );
  271. }
  272. }
  273. /**
  274. * Sanitize the supported beta values by making them booleans
  275. *
  276. * @param mixed $value The value being sent in, determining if beta support is enabled.
  277. *
  278. * @return bool
  279. * @since 1.5
  280. */
  281. public static function enabled_betas_sanitize_value( $value ) {
  282. return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
  283. }
  284. /**
  285. * Check if a given extensions has beta support enabled
  286. *
  287. * @param string $slug The slug of the extension to check.
  288. *
  289. * @return bool True if enabled, false otherwise
  290. * @since 1.5
  291. */
  292. public static function extension_has_beta_support( $slug ) {
  293. $enabled_betas = PUM_Utils_Options::get( 'enabled_betas', array() );
  294. $return = false;
  295. if ( array_key_exists( $slug, $enabled_betas ) ) {
  296. $return = true;
  297. }
  298. return $return;
  299. }
  300. /**
  301. * Retrieves error log and prepares it for displaying
  302. *
  303. * @uses PUM_Utils_Logging::get_log()
  304. * @since 1.12.0
  305. */
  306. public static function display_error_log() {
  307. return PUM_Utils_Logging::instance()->get_log();
  308. }
  309. }