options['post_types'] ) ) { $this->options['post_types'] = [ 'post' ]; } $this->settings = new Modified_Date_Settings( $this->options ); add_action( 'post_submitbox_misc_actions', [ $this, 'add_classic_modified_date_field' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_scripts' ] ); add_filter( 'wp_insert_post_data', [ $this, 'maybe_lock_modified_date' ], 999, 2 ); foreach ( $this->options['post_types'] as $post_type ) { add_filter( "rest_pre_insert_{$post_type}", [ $this, 'add_modified_params' ], 10, 2 ); } add_action( 'init', [ $this, 'register_post_meta' ] ); if ( ! empty( $this->options['add_modified_date_column'] ) ) { add_action( 'init', [ $this, 'add_custom_columns' ], 100 ); } add_filter( 'adminoptim_notification_message_' . self::MODULE_SLUG, static function ( string $message, string $display ): string { if ( 'banner' === $display ) { return sprintf( /* translators: 1: Opening strong tag, 2: Closing strong tag. */ __( '%1$sManaging post modified dates with ease?%2$s Leave a ★★★★★ review on WordPress.org and help others discover Admin Optimizer.', 'admin-optimizer' ), '', '' ); } else { return sprintf( /* translators: 1: Opening strong tag, 2: Closing strong tag. */ __( '%1$sManaging post modified dates with ease?%2$s Take full control over modified dates with advanced publishing options in Admin Optimizer Pro.', 'admin-optimizer' ), '', '' ); } }, 10, 2 ); } /** * Return the module's submenu definition. * * @return array */ protected function get_submenu_config(): array { return [ 'page_title' => __( 'Lock Modified Date', 'admin-optimizer' ), 'menu_title' => __( 'Lock Modified Date', 'admin-optimizer' ), 'menu_slug' => self::MENU_SLUG, 'callback' => [ $this->settings, 'render_settings_page' ], ]; } /** * * Add modified date fields to Clasic editor * * @param \WP_Post $post post object. * * @return void */ public function add_classic_modified_date_field( $post ) { if ( ! empty( $this->options['publish_only'] ) && 'publish' !== $post->post_status ) { return; } $current_user_can_modify = apply_filters( 'adminoptim_modified_date_user_check', true ); if ( ! $current_user_can_modify ) { return; } $post_type = ! empty( $post->post_type ) ? get_post_type_object( $post->post_type ) : ''; if ( empty( $post_type ) || ! $post_type->public ) { return; } $is_allowed_post_type = apply_filters( 'adminoptim_modified_date_post_type_check', true, $post->post_type ); if ( ! $is_allowed_post_type ) { return; } if ( ! empty( $this->options['disable_modified_date_update'] ) ) { $modified_message = esc_html__( 'The Modified Date field is currently locked.', 'admin-optimizer' ); $input_name = 'adminoptim_update_modified_date'; $input_label = esc_html__( 'Update modified date', 'admin-optimizer' ); } else { $modified_message = ''; $input_name = 'adminoptim_lock_modified_date'; $input_label = esc_html__( 'Lock modified date', 'admin-optimizer' ); } ?>
ID ) . ' at ' . get_the_modified_time( 'H:i', $post->ID ) ); ?>
' . esc_html( $modified_message ) . '

'; } ?>
options['publish_only'] ) && ( isset( $post->post_status ) && 'publish' !== $post->post_status ) ) { return; } if ( in_array( $pagenow, [ 'post.php', 'edit.php', 'post-new.php', 'page.php', 'edit-pages.php', 'page-new.php' ], true ) ) { $enqueue = true; if ( ! empty( $this->options['disable_publish_modified_date'] ) && 'publish' !== $post->post_status ) { $enqueue = false; } $current_user_can_modify = apply_filters( 'adminoptim_modified_date_user_check', true ); if ( ! $current_user_can_modify ) { $enqueue = false; } $post_type = ! empty( $post->post_type ) ? get_post_type_object( $post->post_type ) : ''; if ( empty( $post_type ) || ! $post_type->public ) { $enqueue = false; } $is_allowed_post_type = true; if ( isset( $post->post_type ) ) { $is_allowed_post_type = apply_filters( 'adminoptim_modified_date_post_type_check', true, $post->post_type ); } if ( ! $is_allowed_post_type ) { $enqueue = false; } if ( $enqueue ) { if ( ! $screen->is_block_editor() ) { wp_enqueue_script( 'adminoptimizer-modified-date-classic', self::MODULE_URL . 'assets/js/modified-classic.min.js', [ 'jquery' ], ADMINOPTIMIZER_VERSION, true ); wp_enqueue_style( 'adminoptimizer-modified-date-classic', self::MODULE_URL . 'assets/css/modified-date.min.css', [], ADMINOPTIMIZER_VERSION ); } $modified = []; if ( ! empty( $this->options['disable_modified_date_update'] ) ) { $modified['locked'] = true; } else { $modified['locked'] = false; } if ( isset( $post->post_modified ) ) { $modified['modified_date'] = $post->post_modified; } else { $modified['modified_date'] = ''; } wp_localize_script( 'adminoptimizer-modified-date-classic', 'adminoptimizerModifiedDate', $modified ); wp_localize_script( 'adminoptimizer-modified-date-block', 'adminoptimizerModifiedDate', $modified ); } } } /** * Enqueue scripts for block editor * * @return void */ public function enqueue_block_scripts() { global $pagenow, $post; $screen = get_current_screen(); if ( ! empty( $this->options['publish_only'] ) && ( isset( $post->post_status ) && 'publish' !== $post->post_status ) ) { return; } if ( in_array( $pagenow, [ 'post.php', 'edit.php', 'page.php', 'edit-pages.php' ], true ) ) { if ( $screen->is_block_editor() ) { $enqueue = true; if ( ! empty( $this->options['disable_publish_modified_date'] ) && 'publish' !== $post->post_status ) { $enqueue = false; } $current_user_can_modify = apply_filters( 'adminoptim_modified_date_user_check', true ); if ( ! $current_user_can_modify ) { $enqueue = false; } $post_type = ! empty( $post->post_type ) ? get_post_type_object( $post->post_type ) : ''; if ( empty( $post_type ) || ! $post_type->public ) { $enqueue = false; } $is_allowed_post_type = apply_filters( 'adminoptim_modified_date_post_type_check', true, $post->post_type ); if ( ! $is_allowed_post_type ) { $enqueue = false; } if ( $enqueue ) { wp_enqueue_script( 'adminoptimizer-modified-date-block', self::MODULE_URL . 'block/dist/index.js', [ 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ], ADMINOPTIMIZER_VERSION, true ); wp_set_script_translations( 'adminoptimizer-modified-date-block', 'admin-optimizer', ADMINOPTIMIZER_PATH . 'languages' ); } } } } /** * Filter pre_insert result and add modified param * * @param object $prepared_post An object representing a single post prepared for inserting or updating the database. * @param \WP_REST_REQUEST $request Request object. * * @return object */ public function add_modified_params( $prepared_post, $request ) { $params = $request->get_params(); if ( isset( $params['modified'] ) ) { $prepared_post->adminoptim_modified = $params['modified']; } if ( isset( $params['meta']['_adminoptim_lock_modified_date'] ) ) { $prepared_post->adminoptim_lockmodifieddate = $params['meta']['_adminoptim_lock_modified_date']; } return $prepared_post; } /** * Filters slashed post data just before it is inserted into the database. * * @param array $data An array of slashed, sanitized, and processed post data. * @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data. * * @return array */ public function maybe_lock_modified_date( $data, $postarr ) { if ( ! isset( $postarr['ID'] ) ) { return $data; } if ( ! empty( $this->options['publish_only'] ) && 'publish' !== $data['post_status'] ) { return $data; } if ( isset( $data['post_type'] ) ) { $is_allowed_post_type = apply_filters( 'adminoptim_modified_date_post_type_check', true, $data['post_type'] ); if ( ! $is_allowed_post_type ) { return $data; } } $current_user_can_modify = apply_filters( 'adminoptim_modified_date_user_check', true ); /** * Handle classic save. */ if ( isset( $postarr['adminoptim_is_classic'] ) ) { // for classic editor. if ( isset( $postarr['adminoptim_lock_modified_date'] ) ) { if ( '1' === $postarr['adminoptim_lock_modified_date'] ) { if ( isset( $postarr['post_modified'] ) ) { $data['post_modified'] = $postarr['post_modified']; } if ( isset( $postarr['post_modified_gmt'] ) ) { $data['post_modified_gmt'] = $postarr['post_modified_gmt']; } } elseif ( '-1' === $postarr['adminoptim_lock_modified_date'] ) { if ( ! empty( $postarr['adminoptim_modified_date'] ) && $current_user_can_modify ) { $last_modified_time = sanitize_text_field( wp_unslash( $postarr['adminoptim_modified_date'] ) ); $modified_timestamp = strtotime( $last_modified_time ); $modified_date = date( 'Y-m-d H:i:s', $modified_timestamp ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date $data['post_modified'] = $modified_date; $data['post_modified_gmt'] = gmdate( 'Y-m-d H:i:s', $modified_timestamp ); } } } } else { /** * Handle block editor save. */ $lock_modified_date = false; if ( ! isset( $postarr['adminoptim_lockmodifieddate'] ) ) { $meta_exist = metadata_exists( $postarr['post_type'], $postarr['ID'], '_adminoptim_lock_modified_date' ); if ( $meta_exist ) { $lock_meta = get_post_meta( $postarr['ID'], '_adminoptim_lock_modified_date', true ); if ( $lock_meta ) { $lock_modified_date = true; } } elseif ( ! empty( $this->options['disable_modified_date_update'] ) ) { $lock_modified_date = true; } } elseif ( ! empty( $postarr['adminoptim_lockmodifieddate'] ) ) { $lock_modified_date = true; } if ( $lock_modified_date ) { if ( isset( $postarr['post_modified'] ) ) { $data['post_modified'] = $postarr['post_modified']; } if ( isset( $postarr['post_modified_gmt'] ) ) { $data['post_modified_gmt'] = $postarr['post_modified_gmt']; } } else { $data['post_modified'] = current_time( 'mysql' ); $data['post_modified_gmt'] = current_time( 'mysql', true ); // Check the duplicate request. $temp_date = get_post_meta( $postarr['ID'], '_adminoptim_temp_modified_date', true ); $published_timestamp = strtotime( $postarr['post_date'] ); if ( $current_user_can_modify ) { if ( ! empty( $postarr['adminoptim_modified'] ) ) { $modified_timestamp = strtotime( $postarr['adminoptim_modified'] ); if ( $modified_timestamp >= $published_timestamp ) { $modified_date = date( 'Y-m-d H:i:s', $modified_timestamp ); //phpcs:ignore } else { $modified_date = date( 'Y-m-d H:i:s', $published_timestamp ); //phpcs:ignore } $data['post_modified'] = $modified_date; $data['post_modified_gmt'] = get_gmt_from_date( $modified_date ); update_post_meta( $postarr['ID'], '_adminoptim_temp_modified_date', $postarr['adminoptim_modified'] ); } elseif ( ! empty( $temp_date ) ) { $temp_modified_timestamp = strtotime( $temp_date ); if ( $temp_modified_timestamp >= $published_timestamp ) { $temp_modified_date = date( 'Y-m-d H:i:s', $temp_modified_timestamp ); //phpcs:ignore } else { $temp_modified_date = date( 'Y-m-d H:i:s', $temp_modified_timestamp ); //phpcs:ignore } $data['post_modified'] = $temp_modified_date; $data['post_modified_gmt'] = get_gmt_from_date( $temp_modified_date ); delete_post_meta( $postarr['ID'], '_adminoptim_temp_modified_date' ); } } } } return $data; } /** * Register post meta * * @return void */ public function register_post_meta() { foreach ( $this->options['post_types'] as $post_type ) { register_post_meta( $post_type, '_adminoptim_lock_modified_date', [ 'show_in_rest' => true, 'type' => 'boolean', 'single' => true, 'default' => ! empty( $this->options['disable_modified_date_update'] ), 'auth_callback' => '__return_true', ] ); } } /** * Actions to add custom columns to Posts * * @return void */ public function add_custom_columns() { add_action( 'manage_post_posts_custom_column', [ $this, 'render_custom_columns' ], 10, 2 ); add_filter( 'manage_post_posts_columns', [ $this, 'custom_columns' ] ); add_filter( 'manage_edit-post_sortable_columns', [ $this, 'add_sortable_column' ] ); } /** * Filters the columns displayed in the Posts list table. * * @param array $columns An associative array of column headings. * * @return array */ public function custom_columns( $columns ) { if ( ! isset( $columns['modified'] ) ) { $columns['modified'] = __( 'Modified Date', 'admin-optimizer' ); } return $columns; } /** * Fires in each custom column in the Posts list table. * * @param string $column_name The name of the column to display. * * @param int $post_id The current post ID. * * @return void */ public function render_custom_columns( $column_name, $post_id ) { if ( 'modified' === $column_name ) { $content = __( 'Modified', 'admin-optimizer' ) . '
'; $content .= get_the_modified_date( 'Y/m/d', $post_id ) . ' at ' . get_the_modified_time( '', $post_id ); echo wp_kses( $content, [ 'br' => [] ] ); } } /** * Filters the Post list table sortable columns. * * @param array $columns An array of sortable columns. * * @return array */ public function add_sortable_column( $columns ) { if ( ! isset( $columns['modified'] ) ) { $columns['modified'] = 'modified'; } return $columns; } /** * Print out HTML form date elements for editing modified date. * * @param int $tab_index The tabindex attribute to add. Default 0. * @param bool $multi Whether the additional fields and buttons should be added. Default false. * * @return void */ protected function touch_modified_time( int $tab_index, bool $multi ) { global $wp_locale; $post = get_post(); $edit = ! ( in_array( $post->post_status, [ 'draft', 'pending' ], true ) && ( ! $post->post_date_gmt || '0000-00-00 00:00:00' === $post->post_date_gmt ) ); $tab_index_attribute = ''; if ( $tab_index > 0 ) { $tab_index_attribute = " tabindex=\"$tab_index\""; } $post_date = $post->post_modified; $jj = ( $edit ) ? mysql2date( 'd', $post_date, false ) : current_time( 'd' ); $mm = ( $edit ) ? mysql2date( 'm', $post_date, false ) : current_time( 'm' ); $aa = ( $edit ) ? mysql2date( 'Y', $post_date, false ) : current_time( 'Y' ); $hh = ( $edit ) ? mysql2date( 'H', $post_date, false ) : current_time( 'H' ); $mn = ( $edit ) ? mysql2date( 'i', $post_date, false ) : current_time( 'i' ); $ss = ( $edit ) ? mysql2date( 's', $post_date, false ) : current_time( 's' ); $cur_jj = current_time( 'd' ); $cur_mm = current_time( 'm' ); $cur_aa = current_time( 'Y' ); $cur_hh = current_time( 'H' ); $cur_mn = current_time( 'i' ); $month = ''; $day = ''; $year = ''; $hour = ''; $minute = ''; echo '
'; /* translators: 1: Month, 2: Day, 3: Year, 4: Hour, 5: Minute. */ printf( __( '%1$s %2$s, %3$s at %4$s:%5$s', 'admin-optimizer' ), $month, $day, $year, $hour, $minute ); // phpcs:ignore echo '
'; if ( $multi ) { return; } echo "\n\n"; $map = array( 'mmm' => array( $mm, $cur_mm ), 'mjj' => array( $jj, $cur_jj ), 'maa' => array( $aa, $cur_aa ), 'mhh' => array( $hh, $cur_hh ), 'mmn' => array( $mn, $cur_mn ), ); foreach ( $map as $timeunit => $value ) { [ $unit, $curr ] = $value; echo '' . "\n"; $cur_timeunit = 'cur_' . $timeunit; echo '' . "\n"; //phpcs:ignore } ?>