*/ class Admin extends WordPressHooks { private $plugin_name; private $options_name; private $version; private $indexer; private $pluginPath; /** @var bool Sometimes sanitize() gets called twice. Avoid repeating operations. */ private $didAnyOperations = false; /** * Initialize the class and set its properties. * */ public function __construct() { $this->plugin_name = INDEX_WP_USERS_FOR_SPEED_NAME; $this->version = INDEX_WP_USERS_FOR_SPEED_VERSION; $this->pluginPath = plugin_dir_path( dirname( __FILE__ ) ); $this->options_name = INDEX_WP_USERS_FOR_SPEED_PREFIX . 'options'; $this->indexer = Indexer::getInstance(); /* action link for plugins page */ add_filter( 'plugin_action_links_' . INDEX_WP_USERS_FOR_SPEED_FILENAME, [ $this, 'action_link' ] ); parent::__construct(); } /** @noinspection PhpUnused */ public function action__admin_menu() { add_users_page( esc_html__( 'Index WP Users For Speed', 'index-wp-users-for-speed' ), esc_html__( 'Index For Speed', 'index-wp-users-for-speed' ), 'manage_options', $this->plugin_name, [ $this, 'render_admin_page' ], 12 ); $this->addTimingSection(); } private function addTimingSection() { $page = $this->plugin_name; add_settings_section( 'indexing', esc_html__( 'Rebuilding user indexes', 'index-wp-users-for-speed' ), [ $this, 'render_indexing_section' ], $page ); add_settings_field( 'auto_rebuild', esc_html__( 'Rebuild indexes', 'index-wp-users-for-speed' ), [ $this, 'render_auto_rebuild_field' ], $page, 'indexing' ); add_settings_field( 'rebuild_time', esc_html__( '...at this time', 'index-wp-users-for-speed' ), [ $this, 'render_rebuild_time_field' ], $page, 'indexing' ); add_settings_section( 'quickedit', esc_html__( 'Choosing authors when editing posts and pages', 'index-wp-users-for-speed' ), [ $this, 'render_quickedit_section' ], $page ); add_settings_field( 'quickedit_threshold', esc_html__( 'Use selection boxes', 'index-wp-users-for-speed' ), [ $this, 'render_quickedit_threshold_field' ], $page, 'quickedit' ); $option = get_option( $this->options_name ); /* make sure default option is in place, to avoid double sanitize call */ if ( $option === false ) { add_option( $this->options_name, [ 'auto_rebuild' => 'on', 'rebuild_time' => '00:25', 'quickedit_threshold_limit' => 50, ] ); } register_setting( $this->options_name, $this->options_name, [ 'sanitize_callback' => [ $this, 'sanitize_settings' ] ] ); } /** @noinspection PhpRedundantOptionalArgumentInspection */ public function sanitize_settings( $input ) { require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/indexer.php'; $this->indexer = Indexer::getInstance(); $didAnOperation = false; try { $autoRebuild = isset( $input['auto_rebuild'] ) && ( $input['auto_rebuild'] === 'on' || $input['auto_rebuild'] === 'nowon' ); $nowRebuild = isset( $input['auto_rebuild'] ) && ( $input['auto_rebuild'] === 'nowoff' || $input['auto_rebuild'] === 'nowon' ); $time = isset( $input['rebuild_time'] ) ? $input['rebuild_time'] : ''; $timeString = $this->formatTime( $time ); if ( $timeString === false ) { add_settings_error( $this->options_name, 'rebuild', esc_html__( 'Incorrect time.', 'index-wp-users-for-speed' ), 'error' ); return $input; } if ( $nowRebuild ) { add_settings_error( $this->options_name, 'rebuild', esc_html__( 'User index rebuilding starting', 'index-wp-users-for-speed' ), 'info' ); if ( ! $this->didAnyOperations ) { $didAnOperation = true; $this->indexer->rebuildNow(); } } if ( $autoRebuild ) { /* translators: 1: localized time like 1:22 PM or 13:22 */ $format = __( 'Automatic index rebuilding scheduled for %1$s each day', 'index-wp-users-for-speed' ); $display = esc_html( sprintf( $format, $timeString ) ); add_settings_error( $this->options_name, 'rebuild', $display, 'success' ); if ( ! $this->didAnyOperations ) { $didAnOperation = true; $this->indexer->enableAutoRebuild( $this->timeToSeconds( $time ) ); } } else { $display = esc_html__( 'Automatic index rebuilding disabled', 'index-wp-users-for-speed' ); add_settings_error( $this->options_name, 'rebuild', $display, 'success' ); if ( ! $this->didAnyOperations ) { $didAnOperation = true; $this->indexer->disableAutoRebuild(); } } } catch ( Exception $ex ) { add_settings_error( $this->options_name, 'rebuild', esc_html( $ex->getMessage() ), 'error' ); } if ( $didAnOperation ) { $this->didAnyOperations = true; } /* persist on and off */ if ( isset( $input['auto_rebuild'] ) ) { $i = $input['auto_rebuild']; $i = $i === 'nowon' ? 'on' : $i; $i = $i === 'nowoff' ? 'off' : $i; $input['auto_rebuild'] = $i; } return $input; } /** * @param string $time like '16:42' * * @return string|false time string or false if input was bogus. */ private function formatTime( $time ) { $ts = $this->timeToSeconds( $time ); $utc = new DateTimeZone ( 'UTC' ); return $ts === false ? $time : wp_date( get_option( 'time_format' ), $ts, $utc ); } /** * @param string $time like '16:42' * * @return false|int */ private function timeToSeconds( $time ) { try { if ( preg_match( '/^\d\d:\d\d$/', $time ) ) { $ts = intval( substr( $time, 0, 2 ) ) * HOUR_IN_SECONDS; $ts += intval( substr( $time, 3, 2 ) ) * MINUTE_IN_SECONDS; if ( $ts >= 0 && $ts < DAY_IN_SECONDS ) { return intval( $ts ); } } } catch ( Exception $ex ) { return false; } return false; } public function render_admin_page() { /* avoid this overhead unless we actually USE the admin page */ wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/admin.css', [], $this->version, 'all' ); require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/indexer.php'; $this->indexer = Indexer::getInstance(); include_once $this->pluginPath . 'admin/views/page.php'; } public function render_indexing_section() { ?>
options_name ); $autoRebuild = isset( $options['auto_rebuild'] ) ? $options['auto_rebuild'] : 'on'; ?>
options_name ); $limit = isset( $options['quickedit_threshold_limit'] ) ? $options['quickedit_threshold_limit'] : 50; ?>