options = $options; add_action( 'admin_init', [ $this, 'init' ] ); } /** * Option keys that only the Pro settings UI writes. * * @return string[] */ protected function get_pro_only_option_keys(): array { return [ 'post_types', 'edit_roles' ]; } /** * Start the engine running * * @return void */ public function init() { $this->settings_options = [ [ 'option_group' => Modified_Date::MENU_SLUG, 'option_name' => Modified_Date::OPTION_NAME, 'args' => [ 'sanitize_callback' => [ $this, 'sanitize_settings' ] ], ], ]; $this->settings_sections = [ [ 'id' => 'adminoptimizer-modified-date-default', 'title' => '', 'description' => '', 'menu_slug' => Modified_Date::MENU_SLUG, 'option_name' => Modified_Date::OPTION_NAME, 'fields' => [ [ 'type' => 'checkbox', 'title' => __( 'Lock modification date by default', 'admin-optimizer' ), 'id' => 'post-modified-date', 'name' => 'disable_modified_date_update', 'label' => __( 'Prevent any user from updating the modified post. Can be overridden for each post.', 'admin-optimizer' ), ], [ 'type' => 'checkbox', 'title' => __( 'Only lock modified date for Published post', 'admin-optimizer' ), 'id' => 'publish-post-modified-date', 'name' => 'publish_only', 'label' => __( 'The modification date of post with different post status will not be locked.', 'admin-optimizer' ), ], [ 'type' => 'checkbox', 'title' => __( 'Add a Modified Date column to Posts section', 'admin-optimizer' ), 'id' => 'post-modified-date-column', 'name' => 'add_modified_date_column', 'label' => __( 'Sort the post oder by Modified Date', 'admin-optimizer' ), ], ], ], ]; if ( $this->is_pro_active() ) { $this->settings_sections = $this->inject_pro_fields( $this->settings_sections, 'adminoptimizer-modified-date-default', [ [ 'type' => 'callback', 'title' => __( 'Lock modified date for the following Post Types', 'admin-optimizer' ), 'id' => 'post-types-support', 'name' => 'post_types', 'desc' => '', 'callback' => [ $this, 'render_post_types_fields' ], ], [ 'type' => 'callback', 'id' => 'modified-date-edit-roles', 'name' => 'edit_roles', 'title' => __( 'Only allow these user roles to make changes to the Modified Date', 'admin-optimizer' ), 'callback' => [ $this, 'render_edit_roles_checkboxes' ], 'desc' => __( 'Only these roles can lock or update the modified date.', 'admin-optimizer' ), ], ] ); } else { $this->settings_sections[] = [ 'id' => 'adminoptimizer-modified-date-pro', 'title' => __( 'Pro Options', 'admin-optimizer' ), /* translators: %1$s is the anchor link to the Pro version. %2$s is the closing anchor tag */ 'description' => sprintf( __( 'Upgrade to the %1$sPro version%2$s to access these features', 'admin-optimizer' ), '', '' ), 'menu_slug' => Modified_Date::MENU_SLUG . '_pro', 'option_name' => Modified_Date::OPTION_NAME, 'args' => [ 'before_section' => '
', 'after_section' => '
', ], 'fields' => [ [ 'type' => 'callback', 'title' => __( 'Lock modified date for the following Post Types', 'admin-optimizer' ), 'id' => 'post-types-support', 'name' => 'post_types', 'desc' => '', 'callback' => [ $this, 'render_disabled_post_types_fields' ], ], [ 'type' => 'callback', 'id' => 'modified-date-edit-roles', 'name' => 'edit_roles', 'title' => __( 'Only allow these user roles to make changes to the Modified Date', 'admin-optimizer' ), 'callback' => [ $this, 'render_disabled_edit_roles_checkboxes' ], 'desc' => __( 'Only these roles can lock or update the modified date.', 'admin-optimizer' ), ], ], ]; } $this->setup(); } /** * Callback function for rendering the Pro-enabled custom post types field. * * @return void */ public function render_post_types_fields() { $option = []; if ( ! isset( $this->options['post_types'] ) ) { $option['post_types'] = [ 'post' ]; } else { $option['post_types'] = $this->options['post_types']; } $custom_post_types = get_post_types( [ 'show_ui' => true, '_builtin' => false, 'public' => true, ], 'objects' ); $post_types = []; foreach ( $custom_post_types as $custom_post_type ) { $post_types[ $custom_post_type->name ] = $custom_post_type->label; } $post_types = array_merge( [ 'all' => __( 'All Post Types', 'admin-optimizer' ), 'post' => 'Post', 'page' => 'Page', ], $post_types ); foreach ( $post_types as $post_type_name => $post_type_label ) : ?>
true, '_builtin' => false, 'public' => true, ], 'objects' ); $post_types = []; foreach ( $custom_post_types as $custom_post_type ) { $post_types[ $custom_post_type->name ] = $custom_post_type->label; } $post_types = array_merge( [ 'post' => 'Post', 'page' => 'Page', ], $post_types ); foreach ( $post_types as $post_type_name => $post_type_label ) : ?>
options['edit_roles'] ) && is_array( $this->options['edit_roles'] ) ) ? $this->options['edit_roles'] : []; $roles = get_editable_roles(); if ( ! empty( $field['desc'] ) ) { echo '

' . esc_html( $field['desc'] ) . '

'; } foreach ( $roles as $role => $role_details ) { $name = translate_user_role( $role_details['name'] ); $checked = ''; $disabled = ''; if ( 'administrator' === $role ) { $checked = ' checked="checked"'; $disabled = ' disabled="disabled"'; } if ( ! empty( $enabled_user_roles ) && in_array( $role, $enabled_user_roles, true ) ) { $checked = ' checked="checked"'; } ?>
' . esc_html( $field['desc'] ) . '

'; } foreach ( $roles as $role => $role_details ) { $name = translate_user_role( $role_details['name'] ); $disabled = ' disabled="disabled"'; ?>
is_pro_active(); if ( is_array( $options ) ) { if ( isset( $options['disable_modified_date_update'] ) ) { $sanitized_options['disable_modified_date_update'] = '1'; } if ( isset( $options['publish_only'] ) ) { $sanitized_options['publish_only'] = '1'; } if ( isset( $options['add_modified_date_column'] ) ) { $sanitized_options['add_modified_date_column'] = '1'; } if ( $is_pro ) { if ( isset( $options['post_types'] ) ) { $custom_post_types = array_unique( array_merge( [ 'post', 'page' ], get_post_types( [ 'show_ui' => true, '_builtin' => false, 'public' => true, ] ) ) ); if ( is_array( $options['post_types'] ) ) { if ( in_array( 'all', $options['post_types'], true ) ) { $sanitized_options['post_types'] = [ 'all' ]; } else { $subset = array_diff( $options['post_types'], $custom_post_types ); if ( $subset ) { $sanitized_options['post_types'] = array_diff( $options['post_types'], $subset ); } else { $sanitized_options['post_types'] = $options['post_types']; } } } else { $sanitized_options['post_types'] = [ 'post' ]; } } else { $sanitized_options['post_types'] = [ 'post' ]; } if ( isset( $options['edit_roles'] ) ) { $roles = get_editable_roles(); $all_roles = []; foreach ( $roles as $role_name => $role_details ) { $all_roles[] = $role_name; } if ( is_array( $options['edit_roles'] ) ) { array_map( 'sanitize_text_field', $options['edit_roles'] ); if ( ! in_array( 'administrator', $options['edit_roles'], true ) ) { $options['edit_roles'][] = 'administrator'; } $has_invalid_roles = array_diff( $options['edit_roles'], $all_roles ); if ( ! $has_invalid_roles ) { $sanitized_options['edit_roles'] = $options['edit_roles']; } else { $sanitized_options['edit_roles'] = array_diff( $options['edit_roles'], $has_invalid_roles ); } } else { $sanitized_options['edit_roles'] = [ 'administrator' ]; } } else { $sanitized_options['edit_roles'] = [ 'administrator' ]; } } } return $this->carry_forward_inactive_pro_keys( $sanitized_options ); } /** * Render Settings page * * @return void */ public function render_settings_page() { settings_errors(); ?>
render_settings_on_page( Modified_Date::MENU_SLUG ); if ( ! $this->is_pro_active() ) : ?> render_settings_on_page( Modified_Date::MENU_SLUG . '_pro', [ 'remove_submit_button' => true ] ); ?>