'Sitewide',
'frontpage' => 'Front page',
'homepage' => 'Home page',
'single' => 'Single Post',
'page' => 'Page',
'category' => 'Category archive',
'tag' => 'Tag archive',
'author' => 'Author archive',
'date' => 'Date archive',
'search' => 'Search',
'404' => '404',
];
const LOCATION_FUNC = [
'frontpage' => 'is_front_page',
'homepage' => 'is_home',
'single' => 'is_single',
'page' => 'is_page',
'category' => 'is_category',
'tag' => 'is_tag',
'author' => 'is_author',
'date' => 'is_date',
'search' => 'is_search',
'404' => 'is_404',
];
protected $output;
protected $presets;
/**
* Instantiate sub-classes and start each one.
*
* @return void
*/
protected function init(): void {
add_action( 'init', [ $this, 'register' ] );
add_action( 'init', [ $this, 'register_builtin_shortcodes' ], 10 );
$this->settings = new Custom_Code_Settings();
$this->output = new Custom_Code_Output();
$this->presets = new Custom_Code_Presets();
add_filter(
'adminoptim_notification_message_' . self::MODULE_SLUG,
static function ( string $message, string $display ): string {
if ( 'banner' === $display ) {
return sprintf(
/* translators: 1: Opening strong tag, 2: Closing strong tag. */
__(
'%1$sManaging custom code without editing theme files?%2$s Leave a ★★★★★ review on WordPress.org and help others discover Admin Optimizer.',
'admin-optimizer'
),
'',
''
);
} else {
return sprintf(
/* translators: 1: Opening strong tag, 2: Closing strong tag. */
__(
'%1$sManaging custom code without editing theme files?%2$s Manage advanced code snippets with the full power of Admin Optimizer Pro.',
'admin-optimizer'
),
'',
''
);
}
},
10,
2
);
}
/**
* Register the ao_custom_code post type.
*
* @return void
*/
public function register(): void {
$labels = [
'name' => _x( 'Custom Code', 'Post Type General Name', 'admin-optimizer' ),
'singular_name' => _x( 'Custom Code', 'Post Type Singular Name', 'admin-optimizer' ),
'menu_name' => __( 'Custom Code', 'admin-optimizer' ),
'all_items' => __( 'All Custom Code', 'admin-optimizer' ),
'add_new' => __( 'Add New', 'admin-optimizer' ),
'add_new_item' => __( 'Add New Custom Code', 'admin-optimizer' ),
'new_item' => __( 'New Custom Code', 'admin-optimizer' ),
'edit_item' => __( 'Edit Custom Code', 'admin-optimizer' ),
'update_item' => __( 'Update Custom Code', 'admin-optimizer' ),
'search_items' => __( 'Search Custom Code', 'admin-optimizer' ),
'item_published' => __( 'Custom Code published.', 'admin-optimizer' ),
'item_updated' => __( 'Custom Code updated.', 'admin-optimizer' ),
'item_scheduled' => __( 'Custom Code scheduled.', 'admin-optimizer' ),
'item_reverted_to_draft' => __( 'Custom Code reverted to draft.', 'admin-optimizer' ),
];
register_post_type(
self::POST_TYPE,
[
'labels' => $labels,
'supports' => [ 'title', 'revisions' ],
'hierarchical' => false,
'public' => false,
'show_ui' => true,
'show_in_menu' => false,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'show_in_rest' => false,
'capability_type' => 'post',
]
);
}
/**
* Register the built-in library shortcodes
*
* @return void
*/
public function register_builtin_shortcodes(): void {
add_shortcode( 'ao_copyright', static fn() => '©' );
add_shortcode( 'ao_year', static fn() => date_i18n( 'Y' ) );
add_shortcode( 'ao_sitename', static fn() => esc_html( get_bloginfo( 'name' ) ) );
add_shortcode( 'ao_tagline', static fn() => esc_html( get_bloginfo( 'description' ) ) );
add_shortcode( 'ao_home_url', static fn() => esc_url( home_url( '/' ) ) );
add_shortcode(
'ao_post_title',
static function () {
return is_singular() ? esc_html( get_the_title() ) : '';
}
);
add_shortcode(
'ao_post_date',
static function () {
return is_singular() ? esc_html( get_the_date() ) : '';
}
);
add_shortcode(
'ao_post_modified',
static function () {
return is_singular() ? esc_html( get_the_modified_date() ) : '';
}
);
add_shortcode(
'ao_user_name',
static function () {
return is_user_logged_in() ? esc_html( wp_get_current_user()->display_name ) : '';
}
);
add_shortcode(
'ao_user_email',
static function () {
return is_user_logged_in() ? esc_html( wp_get_current_user()->user_email ) : '';
}
);
add_shortcode( 'ao_login_url', static fn() => esc_url( wp_login_url() ) );
add_shortcode( 'ao_logout_url', static fn() => esc_url( wp_logout_url() ) );
add_shortcode(
'ao_privacy_page',
static function () {
$url = get_privacy_policy_url();
return $url ? esc_url( $url ) : '';
}
);
}
/**
* Return the module's submenu definition.
*
* @return array
*/
protected function get_submenu_config(): array {
return [
'page_title' => __( 'Custom Code', 'admin-optimizer' ),
'menu_title' => __( 'Custom Code', 'admin-optimizer' ),
'capability' => self::CAPABILITY,
'menu_slug' => 'ao-custom-code',
'callback' => [ $this->settings, 'render_admin_page' ],
];
}
/**
* Include both the custom list page and native snippet edit screens.
*
* @param string $hook_suffix Current admin page hook suffix.
*/
protected function is_settings_screen( string $hook_suffix ): bool {
if ( parent::is_settings_screen( $hook_suffix ) ) {
return true;
}
$screen = get_current_screen();
return in_array( $hook_suffix, [ 'post.php', 'post-new.php' ], true )
&& $screen
&& self::POST_TYPE === $screen->post_type;
}
}