options = $options;
$this->init();
}
/**
* Start the engine running
*
* @return void
*/
private function init() {
add_filter( 'adminoptimizer_settings_navtab', [ $this, 'add_settings_navtab' ] );
add_filter( 'adminoptimizer_settings_sections', [ $this, 'settings_fields' ] );
if ( ! empty( $this->options['enable_image_alt_text'] ) ) {
add_action( 'add_attachment', [ $this, 'add_image_alt_tag' ] );
}
if ( ! empty( $this->options['enable_image-_underscore_to_hyphen'] ) ) {
add_filter( 'sanitize_file_name', [ $this, 'convert_imagename_underscore_hyphen' ] );
}
if ( ! empty( $this->options['enable_svg_upload'] ) ) {
$this->modules['svg_upload'] = new SVG_Upload();
}
if ( ! empty( $this->options['enable_limit_image_upload_size'] ) ) {
$this->modules['limit_image_upload_size'] = new Limit_Image_Size();
}
if ( ! empty( $this->options['enable_publish_missed_posts'] ) ) {
$this->modules['auto_publish'] = new Publish_Missed_Schedule();
} else {
add_action( 'init', [ $this, 'maybe_remove_publication_check_schedule' ], 20 );
}
if ( ! empty( $this->options['enable_auto_add_anchor'] ) ) {
add_filter( 'block_editor_settings_all', [ $this, 'enable_anchor_target' ] );
}
if ( ! empty( $this->options['manage_post_modified_date'] ) ) {
$this->modules['modified_date'] = new Modified_Date();
}
if ( ! empty( $this->options['enable_post_cloner'] ) ) {
$this->modules['post_cloner'] = new \Yipresser\AdminOptimizer\Modules\Post_Cloner\Post_Cloner();
}
if ( ! empty( $this->options['enable_post_republisher'] ) ) {
$this->modules['content_refresh'] = new \Yipresser\AdminOptimizer\Modules\Content_Refresh\Content_Refresh();
}
if ( ! empty( $this->options['disable_gutenberg_editor'] ) ) {
add_filter( 'gutenberg_can_edit_post', '__return_false', 5 );
add_filter( 'use_block_editor_for_post', '__return_false', 5 );
}
if ( ! empty( $this->options['enable_auto_open_advanced'] ) ) {
$this->modules['auto_open_advanced'] = new Auto_Open_Advanced();
}
if ( ! empty( $this->options['enable_page_ordering'] ) ) {
$this->modules['page_ordering'] = new Page_Ordering();
}
if ( ! empty( $this->options['enable_disable_comments'] ) ) {
add_action( 'init', [ $this, 'disable_comments_post_type_support' ], 100 );
add_action( 'admin_menu', [ $this, 'remove_comments_admin_menu' ], 100 );
add_action( 'wp_dashboard_setup', [ $this, 'remove_comments_dashboard_widget' ], 100 );
add_action( 'admin_bar_menu', [ $this, 'remove_comments_admin_bar_item' ], 100 );
add_filter( 'comments_open', '__return_false', 20 );
add_filter( 'pings_open', '__return_false', 20 );
add_filter( 'comments_array', '__return_empty_array', 20 );
add_filter( 'wp_insert_post_data', [ $this, 'close_comments_and_pings' ], 20 );
}
}
/**
* Add settings navtab
*
* @param array $nav_tab List of nav tabs.
*
* @return array
*/
public function add_settings_navtab( $nav_tab ) {
if ( empty( $nav_tab['content-media'] ) ) {
$nav_tab['content-media'] = [
'title' => __( 'Content & Media', 'admin-optimizer' ),
'slug' => 'adminoptim-content-media-settings',
];
}
return $nav_tab;
}
/**
* List of Settings fields
*
* @param array $fields Settings fields.
*
* @return array
*/
public function settings_fields( $fields ) {
if ( empty( $fields['content-media-module'] ) ) {
$fields['content-media-module'] = [
'id' => 'adminoptimizer-content-media-module-settings',
'title' => __( 'Modules', 'admin-optimizer' ),
'description' => '',
'menu_slug' => 'adminoptim-content-media-settings',
'option_name' => MODULES_OPTION,
'args' => [
'before_section' => '
',
'after_section' => '
',
'section_class' => 'module',
],
'fields' => [
[
'type' => 'slider-checkbox',
'title' => __( 'Post Cloning (Pro option)', 'admin-optimizer' ),
'id' => 'enable-post-cloner',
'name' => 'enable_post_cloner',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Duplicate posts, pages and custom posts with a single click. Update and republish content easily with this module. (comes with Pro options)%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Content Refresh (Pro option)', 'admin-optimizer' ),
'id' => 'enable-post-republisher',
'name' => 'enable_post_republisher',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Refresh stale-but-ranking content with a new updated content and publish date while preserving SEO value. Perfect for evergreen guides, tutorials, and best-ofs (comes with Pro options)%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Lock Post\'s Modification Date (Pro option)', 'admin-optimizer' ),
'id' => 'manage-post-modified-date',
'name' => 'manage_post_modified_date',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Change or prevent updating of the last modified date for each post. (comes with Pro options)%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Limit image upload file size', 'admin-optimizer' ),
'id' => 'enable-limit-image-upload-size',
'name' => 'enable_limit_image_upload_size',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Limit the image upload file size.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
],
];
}
if ( empty( $fields['content-media-tweaks'] ) ) {
$fields['content-media-tweaks'] = [
'id' => 'adminoptimizer-content-media-tweaks-settings',
'title' => __( 'Tweaks', 'admin-optimizer' ),
'description' => __( 'Activate with a single click. No additional configuration required.', 'admin-optimizer' ),
'menu_slug' => 'adminoptim-content-media-settings',
'option_name' => MODULES_OPTION,
'args' => [
'before_section' => '',
'after_section' => '
',
'section_class' => 'tweaks',
],
'fields' => [
[
'type' => 'slider-checkbox',
'title' => __( 'Auto publish posts with missed schedule', 'admin-optimizer' ),
'id' => 'enable-publish-missed-posts',
'name' => 'enable_publish_missed_posts',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Check posts every 15 minutes and auto publish posts with missed schedule.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Disable Comments', 'admin-optimizer' ),
'id' => 'enable-disable-comments',
'name' => 'enable_disable_comments',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Disables comments and pingbacks across the entire site and removes the comment-related WordPress interface.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Auto Add HTML Anchor to Heading Block', 'admin-optimizer' ),
'id' => 'enable-auto-add-anchor',
'name' => 'enable_auto_add_anchor',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Transform your WordPress headings into clickable link targets, making it easier for readers to navigate through your posts and share specific sections.
(only works in Gutenberg editor)%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Disable Gutenberg Editor', 'admin-optimizer' ),
'id' => 'disable-gutenberg-editor',
'name' => 'disable_gutenberg_editor',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'This will disable Gutenberg Block Editor and restore the Classic editor as the default editor.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Page Ordering', 'admin-optimizer' ),
'id' => 'enable-page-ordering',
'name' => 'enable_page_ordering',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Order your pages or hierarchical custom post types with drag and drop, right from the Pages list.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Auto open Advanced field in Gutenberg on mouse hover', 'admin-optimizer' ),
'id' => 'enable-auto-open-advanced',
'name' => 'enable_auto_open_advanced',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Reduce mouse clicks and open the Advanced field in Gutenberg when you move your mouse over it.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'SVG image upload', 'admin-optimizer' ),
'id' => 'enable-svg-upload',
'name' => 'enable_svg_upload',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Enable upload of SVG file. %1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Set image filename as alt text', 'admin-optimizer' ),
'id' => 'enable-image-alt-text',
'name' => 'enable_image_alt_text',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Auto set image filename as alt text.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Convert underscore (_) in image filename to hyphen (-)', 'admin-optimizer' ),
'id' => 'enable-image-underscore-to-hyphen',
'name' => 'enable_image_underscore_to_hyphen',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Auto sanitize the image filename and convert underscore (_) to hyphen (-) during image upload. %1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
],
];
}
return $fields;
}
/**
* Remove check missed post schedule
*
* @return void
*/
public function maybe_remove_publication_check_schedule() {
if ( function_exists( 'as_has_scheduled_action' ) && as_has_scheduled_action( 'adminoptim_publish_missed_post' ) ) {
as_unschedule_action( 'adminoptim_publish_missed_post' );
}
}
/**
* Auto add anchor target to Headings block
*
* @param array $editor_settings Editor settings.
*
* @return array
*/
public function enable_anchor_target( $editor_settings ) {
$editor_settings['generateAnchors'] = true;
return $editor_settings;
}
/**
* Add image alt tag
*
* @param int $post_id Post ID.
*
* @return void
*/
public function add_image_alt_tag( $post_id ) {
// Check if uploaded file is an image, else do nothing.
if ( wp_attachment_is_image( $post_id ) ) {
$my_image_title = get_post( $post_id )->post_title;
// Sanitize the title: remove hyphens, underscores & extra spaces.
$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ', $my_image_title );
// Sanitize the title: capitalize first letter of every word (other letters lower case).
$my_image_title = ucwords( strtolower( $my_image_title ) );
// Set the image Alt-Text.
update_post_meta( $post_id, '_wp_attachment_image_alt', $my_image_title );
}
}
/**
* Convert image name underscore to hyphen
*
* @param string $filename Filename.
*
* @return string
*/
public function convert_imagename_underscore_hyphen( $filename ): string {
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );
$name = str_replace( '_', '-', $name );
return $name . $ext;
}
/**
* Remove comment and trackback support from every registered post type.
*
* @return void
*/
public function disable_comments_post_type_support() {
foreach ( get_post_types() as $post_type ) {
if ( post_type_supports( $post_type, 'comments' ) ) {
remove_post_type_support( $post_type, 'comments' );
}
if ( post_type_supports( $post_type, 'trackbacks' ) ) {
remove_post_type_support( $post_type, 'trackbacks' );
}
}
}
/**
* Remove comment-related items from the admin menu.
*
* @return void
*/
public function remove_comments_admin_menu() {
remove_menu_page( 'edit-comments.php' );
remove_submenu_page( 'options-general.php', 'options-discussion.php' );
}
/**
* Remove the Recent Comments dashboard widget.
*
* @return void
*/
public function remove_comments_dashboard_widget() {
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
/**
* Remove the Comments item from the admin toolbar.
*
* @param \WP_Admin_Bar $wp_admin_bar Admin toolbar instance.
*
* @return void
*/
public function remove_comments_admin_bar_item( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'comments' );
}
/**
* Keep comments and pings closed when content is created or updated.
*
* @param array $data Slashed post data to be inserted.
*
* @return array
*/
public function close_comments_and_pings( $data ) {
$data['comment_status'] = 'closed';
$data['ping_status'] = 'closed';
return $data;
}
}