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_txt_management'] ) ) {
$this->modules['txt'] = new Ads_Robots_Txt();
}
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_xml_sitemap'] ) ) {
$this->modules['sitemap'] = new XML_Sitemap();
}
if ( ! empty( $this->options['enable_post_republisher'] ) ) {
$this->modules['content_refresh'] = new \Yipresser\AdminOptimizer\Modules\Content_Refresh\Content_Refresh();
}
add_action( 'template_redirect', [ $this, 'disable_frontend_pages' ] );
}
/**
* 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-seo'] ) ) {
$nav_tab['content-seo'] = [
'title' => __( 'Content & SEO', 'admin-optimizer' ),
'slug' => 'adminoptim-content-seo-settings',
];
}
return $nav_tab;
}
/**
* List of Settings fields
*
* @param array $fields Settings fields.
*
* @return array
*/
public function settings_fields( $fields ) {
if ( empty( $fields['content-seo'] ) ) {
$fields['content-seo'] = [
'id' => 'adminoptimizer-content-seo-settings',
'title' => '',
'description' => '',
'menu_slug' => 'adminoptim-content-seo-settings',
'option_name' => MODULES_OPTION,
'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' => __( 'XML Sitemap on your site (Pro option)', 'admin-optimizer' ),
'id' => 'enable-xml-sitemap',
'name' => 'enable_xml_sitemap',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Enable and manage XML Sitemap for your site. (comes with Pro options)%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'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' => __( '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' => __( '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' => __( 'Robots.txt & Ads.txt manager', 'admin-optimizer' ),
'id' => 'enable-txt-management',
'name' => 'enable_txt_management',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Manage robots.txt, ads.txt, app-ads.txt directly in WordPress.%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' ), '
', '', '' ),
],
[
'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' ), '
', '', '' ),
],
[
'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 Category Archive Page (frontend)', 'admin-optimizer' ),
'id' => 'disable-category-archive',
'name' => 'disable_category_archive',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Disable all Category archive pages in the site frontend. Loading the Category archive URL will return a 404 (Not Found) error. This will not remove the Category feature in the Admin dashboard.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Disable Tag Archive Page (frontend)', 'admin-optimizer' ),
'id' => 'disable-tag-archive',
'name' => 'disable_tag_archive',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Disable all Tag archive pages in the site frontend. Loading the Tag archive URL will return a 404 (Not Found) error. This will not remove the Tag feature in the Admin dashboard.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Disable Author Archive Page (frontend)', 'admin-optimizer' ),
'id' => 'disable-author-archive',
'name' => 'disable_author_archive',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Disable all Author archive pages in the site frontend. Loading the Author archive URL will return a 404 (Not Found) error. This will not remove the Author feature in the Admin dashboard.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ),
],
[
'type' => 'slider-checkbox',
'title' => __( 'Disable Date Archive Page (frontend)', 'admin-optimizer' ),
'id' => 'disable-date-archive',
'name' => 'disable_date_archive',
// translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag.
'label' => sprintf( __( 'Disable all Date archive pages in the site frontend. Loading the Date archive URL will return a 404 (Not Found) error. This will not remove the Date feature in the Admin dashboard.%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;
}
/**
* Disable Frontend archive pages
*
* @return void
*/
public function disable_frontend_pages() {
if ( ! is_admin() ) {
global $wp_query;
$set_404 = false;
if ( ! empty( $this->options['disable_category_archive'] ) && is_category() ) {
$set_404 = true;
} elseif ( ! empty( $this->options['disable_tag_archive'] ) && is_tag() ) {
$set_404 = true;
} elseif ( ! empty( $this->options['disable_author_archive'] ) && is_author() ) {
$set_404 = true;
} elseif ( ! empty( $this->options['disable_date_archive'] ) && is_date() ) {
$set_404 = true;
}
if ( $set_404 ) {
$wp_query->set_404();
}
}
}
}