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', 'gutenberg_roles', 'enable_switch_link', 'switch_link_post_types', 'switch_link_roles' ];
}
/**
* Start the engine running
*
* @return void
*/
public function init() {
$this->settings_options = [
[
'option_group' => Disable_Gutenberg::MENU_SLUG,
'option_name' => Disable_Gutenberg::OPTION_NAME,
'args' => [ 'sanitize_callback' => [ $this, 'sanitize_settings' ] ],
],
];
$this->settings_sections = [
'free' => [
'id' => 'adminoptimizer-disable-gutenberg-section',
'title' => '',
'menu_slug' => Disable_Gutenberg::MENU_SLUG,
'option_name' => Disable_Gutenberg::OPTION_NAME,
'fields' => [
[
'type' => 'checkbox',
'title' => __( 'Disable Gutenberg Editor Sitewide', 'admin-optimizer' ),
'id' => 'disable-gutenberg-editor',
'name' => 'disable_sitewide',
'label' => __( 'Disable Gutenberg (and switch to Classic editor)', 'admin-optimizer' ),
],
],
],
];
if ( $this->is_pro_active() ) {
$this->settings_sections = $this->inject_pro_fields(
$this->settings_sections,
'adminoptimizer-disable-gutenberg-section',
[
[
'type' => 'callback',
'title' => __( 'Disable Gutenberg Editor only on the following Post Types', 'admin-optimizer' ),
'id' => 'disable-post-types',
'name' => 'post_types',
'callback' => [ $this, 'render_post_types' ],
'param' => 'gutenberg',
],
[
'type' => 'callback',
'title' => __( 'Disable Gutenberg editor only for these user roles', 'admin-optimizer' ),
'id' => 'disable-gutenberg-roles',
'name' => 'gutenberg_roles',
'callback' => [ $this, 'render_roles' ],
'param' => 'gutenberg',
],
]
);
$this->settings_sections['switch'] = [
'id' => 'adminoptimizer-gutenberg-switch',
'title' => __( 'Quick Gutenberg/Classic Switch', 'admin-optimizer' ),
'description' => __( 'Allow quickly switch between Gutenberg and Classic editor in the Post Editor', 'admin-optimizer' ),
'menu_slug' => Disable_Gutenberg::MENU_SLUG,
'option_name' => Disable_Gutenberg::OPTION_NAME,
'fields' => [
[
'type' => 'checkbox',
'title' => __( 'Add Switch link', 'admin-optimizer' ),
'id' => 'gutenberg-switch-link',
'name' => 'enable_switch_link',
'label' => __( 'Add a link in post editor to quickly switch between Gutenberg and Classic editor.', 'admin-optimizer' ),
],
[
'type' => 'callback',
'title' => __( 'Add switch link to the following Post Types', 'admin-optimizer' ),
'id' => 'switch-link-post-types',
'name' => 'switch_link_post_types',
'callback' => [ $this, 'render_post_types' ],
'param' => 'switch',
],
[
'type' => 'callback',
'title' => __( 'Show switch link only for these user roles', 'admin-optimizer' ),
'id' => 'switch-link-roles',
'name' => 'switch_link_roles',
'callback' => [ $this, 'render_roles' ],
'param' => 'switch',
],
],
];
} else {
$this->settings_sections['pro'] = [
'id' => 'adminoptimizer-disable-gutenberg-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' => Disable_Gutenberg::MENU_SLUG . '_pro',
'option_name' => Disable_Gutenberg::OPTION_NAME,
'args' => [
'before_section' => '
',
'after_section' => '
',
],
'fields' => [
[
'type' => 'callback',
'title' => __( 'Disable Gutenberg Editor only on the following Post Types', 'admin-optimizer' ),
'id' => 'disable-post-types',
'name' => 'post_types-disabled',
'disabled' => 'disabled',
'callback' => [ $this, 'render_post_types' ],
'param' => 'gutenberg',
],
[
'type' => 'callback',
'title' => __( 'Disable Gutenberg editor only for these user roles', 'admin-optimizer' ),
'id' => 'disable-gutenberg-roles',
'name' => 'gutenberg_roles-disabled',
'disabled' => 'disabled',
'callback' => [ $this, 'render_roles' ],
'param' => 'gutenberg',
],
[
'type' => 'checkbox',
'title' => __( 'Add Switch link', 'admin-optimizer' ),
'id' => 'gutenberg-switch-link',
'name' => 'enable_switch_link',
'label' => __( 'Add a link in post editor to quickly switch between Gutenberg and Classic editor.', 'admin-optimizer' ),
'disabled' => 'disabled',
],
[
'type' => 'callback',
'title' => __( 'Add switch link to the following Post Types', 'admin-optimizer' ),
'id' => 'switch-link-post-types',
'name' => 'switch_link_post_types',
'callback' => [ $this, 'render_post_types' ],
'disabled' => 'disabled',
'param' => 'switch',
],
[
'type' => 'callback',
'title' => __( 'Show switch link only for these user roles', 'admin-optimizer' ),
'id' => 'switch-link-roles',
'name' => 'switch_link_roles',
'callback' => [ $this, 'render_roles' ],
'disabled' => 'disabled',
'param' => 'switch',
],
],
];
}
$this->setup();
}
/**
* Render post type checkboxes for either the "disable Gutenberg" or the
* "switch link" settings section, depending on `$args['param']`.
*
* @param array $args Field args; expects `param` ('gutenberg'|'switch') and `name`.
*
* @return void
*/
public function render_post_types( $args ) {
if ( 'switch' === $args['param'] ) {
$option = [ 'switch_link_post_types' => [ 'all' ] ];
if ( isset( $this->options['switch_link_post_types'] ) ) {
$option['switch_link_post_types'] = $this->options['switch_link_post_types'];
}
} else {
$option = [ 'post_types' => [ 'all' ] ];
if ( isset( $this->options['post_types'] ) ) {
$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
);
$key = 'switch' === $args['param'] ? 'switch_link_post_types' : 'post_types';
foreach ( $post_types as $post_type_name => $post_type_label ) : ?>
options['switch_link_roles'] ) ) {
$options = (array) $this->options['switch_link_roles'];
}
} elseif ( isset( $this->options['gutenberg_roles'] ) ) {
$options = (array) $this->options['gutenberg_roles'];
}
$roles = get_editable_roles();
$roles = array_merge(
[
'all' => [ 'name' => __( 'All User Roles', 'admin-optimizer' ) ],
],
$roles
);
foreach ( $roles as $role => $details ) {
$checked = '';
if ( in_array( $role, $options, true ) ) {
$checked = ' checked="checked"';
}
$classname = '';
if ( 'all' !== $role ) {
$classname = 'adminoptim-other-user-role';
}
$disabled = isset( $args['disabled'] ) ? 'disabled' : '';
if ( 'all' !== $role && in_array( 'all', $options, true ) ) {
$disabled = 'disabled="disabled"';
}
if ( ( 'administrator' === $role || 'all' === $role ) && ! $this->is_administrator( get_current_user_id() ) ) {
// Permanently locked, independent of the "All" toggle above —
// data-locked tells the "All" checkbox's JS handler to leave
// this one disabled rather than re-enabling it along with
// the other roles.
$disabled = 'disabled="disabled" data-locked="1"';
}
?>
render_settings_on_page( Disable_Gutenberg::MENU_SLUG ); ?>
is_pro_active() ) : ?>
render_settings_on_page( Disable_Gutenberg::MENU_SLUG . '_pro', [ 'remove_submit_button' => true ] ); ?>
is_pro_active() ) {
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'] = [ 'all' ];
}
} else {
$sanitized_options['post_types'] = [ 'all' ];
}
$is_administrator = $this->is_administrator( get_current_user_id() );
if ( isset( $options['gutenberg_roles'] ) ) {
$custom_roles = array_unique(
array_merge(
[ 'all' ],
array_keys( get_editable_roles() )
)
);
if ( ! $is_administrator ) {
// "all" and "administrator" both reach into administrators'
// own editing experience — only an administrator may save
// either, regardless of what was actually submitted (the
// UI locks these two, but the submission itself is the
// real boundary; a forged request must be rejected here too).
$custom_roles = array_diff( $custom_roles, [ 'all', 'administrator' ] );
}
if ( is_array( $options['gutenberg_roles'] ) ) {
$submitted = $is_administrator ? $options['gutenberg_roles'] : array_diff( $options['gutenberg_roles'], [ 'all', 'administrator' ] );
if ( $is_administrator && in_array( 'all', $submitted, true ) ) {
$sanitized_options['gutenberg_roles'] = [ 'all' ];
} else {
$subset = array_diff( $submitted, $custom_roles );
if ( $subset ) {
$sanitized_options['gutenberg_roles'] = array_diff( $submitted, $subset );
} else {
$sanitized_options['gutenberg_roles'] = $submitted;
}
}
} else {
$sanitized_options['gutenberg_roles'] = $is_administrator ? [ 'all' ] : [];
}
} else {
$sanitized_options['gutenberg_roles'] = $is_administrator ? [ 'all' ] : [];
}
if ( isset( $options['enable_switch_link'] ) ) {
$sanitized_options['enable_switch_link'] = '1';
}
if ( isset( $options['switch_link_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['switch_link_post_types'] ) ) {
if ( in_array( 'all', $options['switch_link_post_types'], true ) ) {
$sanitized_options['switch_link_post_types'] = [ 'all' ];
} else {
$subset = array_diff( $options['switch_link_post_types'], $custom_post_types );
if ( $subset ) {
$sanitized_options['switch_link_post_types'] = array_diff( $options['switch_link_post_types'], $subset );
} else {
$sanitized_options['switch_link_post_types'] = $options['switch_link_post_types'];
}
}
} else {
$sanitized_options['switch_link_post_types'] = [ 'all' ];
}
} else {
$sanitized_options['switch_link_post_types'] = [ 'all' ];
}
if ( isset( $options['switch_link_roles'] ) ) {
$custom_roles = array_unique(
array_merge(
[ 'all' ],
array_keys( get_editable_roles() )
)
);
if ( ! $is_administrator ) {
// "all" and "administrator" both reach into administrators'
// own editing experience — only an administrator may save
// either, regardless of what was actually submitted (the
// UI locks these two, but the submission itself is the
// real boundary; a forged request must be rejected here too).
$custom_roles = array_diff( $custom_roles, [ 'all', 'administrator' ] );
}
if ( is_array( $options['switch_link_roles'] ) ) {
$submitted = $is_administrator ? $options['switch_link_roles'] : array_diff( $options['switch_link_roles'], [ 'all', 'administrator' ] );
if ( $is_administrator && in_array( 'all', $submitted, true ) ) {
$sanitized_options['switch_link_roles'] = [ 'all' ];
} else {
$subset = array_diff( $submitted, $custom_roles );
if ( $subset ) {
$sanitized_options['switch_link_roles'] = array_diff( $submitted, $subset );
} else {
$sanitized_options['switch_link_roles'] = $submitted;
}
}
} else {
$sanitized_options['switch_link_roles'] = $is_administrator ? [ 'all' ] : [];
}
} else {
$sanitized_options['switch_link_roles'] = $is_administrator ? [ 'all' ] : [];
}
}
return $this->carry_forward_inactive_pro_keys( $sanitized_options );
}
/**
* Enqueue module settings assets.
*
* @param string $hook_suffix Current admin page hook suffix.
*/
public function enqueue_settings_assets( string $hook_suffix ): void {
if ( $this->is_pro_active() ) {
wp_enqueue_script( 'adminoptim_disable_gutenberg', Disable_Gutenberg::MODULE_URL . 'assets/js/disable-gutenberg.min.js', [ 'jquery' ], ADMINOPTIMIZER_VERSION, true );
} else {
wp_enqueue_style( 'adminoptim-modules-pro-settings' );
}
}
}