runtime_loader->boot_section_feature( 'svg_upload' );
if ( Module_Registry::is_enabled( 'image_alt_text' ) ) {
add_action( 'add_attachment', [ $this, 'add_image_alt_tag' ] );
}
if ( Module_Registry::is_enabled( 'image_underscore_to_hyphen' ) ) {
add_filter( 'sanitize_file_name', [ $this, 'convert_imagename_underscore_hyphen' ] );
}
add_filter(
'adminoptim_notification_message_' . self::SECTION_ID,
static function ( string $message, string $display ): string {
if ( 'banner' === $display ) {
return $message;
}
if ( ! Module_Registry::is_pro_active() ) {
return sprintf(
/* translators: 1: Opening strong tag, 2: Closing strong tag. */
__(
'%1$sUnlock advanced media tools.%2$s Get more powerful Media Library management, image handling, and media optimization features with Pro.',
'admin-optimizer'
),
'',
''
);
}
return $message;
},
10,
2
);
add_filter(
'adminoptim_notification_banner_content_' . self::SECTION_ID,
static function (): array {
$content = [
[
'message' => sprintf(
/* translators: 1: Opening strong tag, 2: Closing strong tag. */
__( '%1$sCleaner media handling, smarter image uploads, better control over filenames and metadata.%2$s Leave a ★★★★★ review on WordPress.org and help others manage their WordPress media more efficiently', 'admin-optimizer' ),
'',
''
),
'cta_type' => 'review',
],
[
'message' => sprintf(
/* translators: 1: Opening strong tag, 2: Closing strong tag. */
__( '%1$sHaving issues with Admin Optimizer media tools?%2$s', 'admin-optimizer' ),
'',
''
),
'cta_type' => 'support',
],
];
return $content[ wp_rand( 0, 1 ) ];
},
);
}
/**
* 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;
}
/**
* Ordinary module IDs this Section owns, mapped to their navigation labels.
*
* @return array
*/
protected function section_nav_modules(): array {
return [
'media_manager' => 'Media Manager',
'limit_image_size' => 'Limit Image Upload Size',
];
}
/**
* Section-owned "Media Controls" presentation group of lightweight
* SECTION_FEATURE capabilities.
*
* @return array{label:string,description:string,members:array}
*/
protected function section_feature_group(): ?array {
return [
'label' => __( 'Media Controls', 'admin-optimizer' ),
'description' => __( 'Manage image uploads, filenames, metadata, and other Media Library behavior.', 'admin-optimizer' ),
'members' => [
'svg_upload',
'image_alt_text',
'image_underscore_to_hyphen',
],
];
}
}