self::translate( 'Banner' ),
'singular_name' => self::translate( 'Banner' ),
'add_new' => self::translate( 'Add New' ),
'add_new_item' => self::translate( 'Add New Banner' ),
'edit_item' => self::translate( 'Edit Banner' ),
'new_item' => self::translate( 'New Banner' ),
'view_item' => self::translate( 'View Banner' ),
'view_items' => self::translate( 'View Banners' ),
'search_items' => self::translate( 'Search Banners' ),
'not_found' => self::translate( 'No Banners found.' ),
'not_found_in_trash' => self::translate( 'No Banners found in Trash.' ),
'all_items' => self::translate( 'All Banners' ),
'archives' => self::translate( 'Banner Archives' ),
'attributes' => self::translate( 'Banner Attributes' ),
'insert_into_item' => self::translate( /** @lang text */ 'Insert into Banner' ),
'uploaded_to_this_item' => self::translate( 'Uploaded to this Banner' ),
'featured_image' => self::translate( 'Featured Image' ),
'set_featured_image' => self::translate( 'Set featured image' ),
'remove_featured_image' => self::translate( 'Remove featured image' ),
'use_featured_image' => self::translate( 'Use as featured image' ),
'filter_items_list' => self::translate( 'Filter Banners list' ),
'items_list_navigation' => self::translate( 'Banners list navigation' ),
'items_list' => self::translate( 'Banners list' ),
];
register_post_type( self::POST_TYPE, [
'labels' => $labels,
'public' => false,
'hierarchical' => false,
'show_ui' => true,
'menu_icon' => 'dashicons-paperclip',
'capability_type' => 'post',
'supports' => [ 'title', 'editor' ],
] );
}
public static function load_textdomain() {
$plugin_rel_path = basename( dirname( __FILE__ ) ) . '/languages/';
load_plugin_textdomain( self::TEXT_DOMAIN, false, $plugin_rel_path );
}
public static function admin_enqueue_scripts() {
global $pagenow, $typenow;
if ( in_array( $pagenow, [ 'post-new.php', 'post.php' ] ) && self::POST_TYPE == $typenow ) {
$suffix = self::get_script_suffix();
wp_enqueue_style( 'anhb_admin', plugins_url( "css/admin{$suffix}.css", __FILE__ ) );
wp_enqueue_script( 'anhb_edit_form', plugins_url( "js/edit-form{$suffix}.js", __FILE__ ), [ 'jquery' ] );
}
}
public static function enqueue_scripts() {
$suffix = self::get_script_suffix();
wp_enqueue_style( 'anhb_user', plugins_url( "css/user{$suffix}.css", __FILE__ ) );
wp_enqueue_script( 'anhb_user', plugins_url( "js/user{$suffix}.js", __FILE__ ), [ 'jquery-effects-core', 'underscore' ] );
}
public static function edit_form_after_title( $post ) {
if ( self::POST_TYPE != $post->post_type ) {
return;
}
$subtitle = get_post_meta( $post->ID, self::get_meta_key( 'subtitle' ), true );
$subtitle_label_class = ($subtitle) ? 'screen-reader-text' : '';
?>
ID}" );
wp_nonce_field($action, $name);
}
public static function edit_form_after_editor( $post ) {
if ( self::POST_TYPE != $post->post_type ) {
return;
}
$name = 'pages';
$value = get_post_meta( $post->ID, self::get_meta_key( $name ), true );
if ( empty( $value ) ) {
$value = [];
}
require_once 'class/anhb-walker-page.php';
echo '';
wp_list_pages( [
'walker' => new ANHB_Walker_Page(),
'title_li' => '' . self::translate( 'Pages' ) . '
',
'el_name' => $name,
'el_value' => $value,
] );
echo '
';
}
public static function save_post( $post_ID ) {
$nonce_name = self::get_meta_key( 'wpnonce' );
$nonce_action = self::get_meta_key( "save_{$post_ID}" );
if ( ! isset( $_POST[$nonce_name] )
|| ! wp_verify_nonce( $_POST[$nonce_name], $nonce_action )
) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_ID ) ) {
return;
}
if ( isset( $_POST['post_type'] ) && self::POST_TYPE === $_POST['post_type'] ) {
foreach ( self::$meta_fields as $field ) {
$value = @$_POST[$field];
if ( is_callable( [ self::class, "sanitize_{$field}" ] ) ) {
$value = call_user_func( [ self::class, "sanitize_{$field}" ], $value );
}
update_post_meta( $post_ID, self::get_meta_key( $field ), $value );
}
}
if ( ! empty( self::$errors ) ) {
$error = new WP_Error();
foreach ( self::$errors as $code => $message ) {
$error->add( $code, $message );
}
set_transient( self::get_transient_key( 'save_post_error', $post_ID ), $error, 45 );
remove_action( 'save_post_' . self::POST_TYPE, [ self::class, __FUNCTION__ ] ); // unhook this function to prevent indefinite loop
wp_update_post( [ 'ID' => $post_ID, 'post_status' => 'draft' ] ); // update the post to change post status
remove_action( 'save_post_' . self::POST_TYPE, [ self::class, __FUNCTION__ ] ); // re-hook this function again
}
}
public static function sanitize_badge_new( $val ) {
return ( $val == 'y' ) ? 'y' : 'n';
}
public static function sanitize_pages( $val ) {
if ( ! is_array( $val ) ) {
$val = [];
}
return $val;
}
public static function admin_notices() {
$transient = self::get_transient_key( 'save_post_error', @$_GET['post'] );
/** @var WP_Error $error */
$error = get_transient( $transient );
if ( $error ) {
foreach ( $error->get_error_codes() as $code ) {
?>
get_error_message( $code ); ?>
ID, self::get_meta_key( $new_name ), true );
?>
ID, self::get_meta_key( 'pages' ), true );
if ( empty( $pages ) ) {
$output .= '-';
} else {
if ( null === $wp_pages ) {
$_wp_pages = get_pages();
foreach ( $_wp_pages as $page ) {
$wp_pages[$page->ID] = $page;
}
unset( $_wp_pages );
}
$li = '';
foreach ( $pages as $page_id ) {
if ( array_key_exists( $page_id, $wp_pages ) ) {
$url = esc_attr( get_permalink( $page_id ) );
$title = esc_html( $wp_pages[$page_id]->post_title );
$li .= "{$title}";
}
}
if ( ! empty( $li ) ) {
$output .= "";
}
}
echo $output;
}
public static function wp_footer() {
global $post;
$banners = self::get_banners_ids_by_post_id( $post->ID );
if ( ! empty( $banners ) ) {
if ( 1 < count( $banners ) ) {
$keys = array_keys( $banners );
$key_index = rand( 0, count( $keys ) );
$key = $keys[$key_index];
$banner_id = $banners[$key];
} else {
$banner_id = array_shift( $banners );
}
$query = new WP_Query( [
'post_type' => self::POST_TYPE,
'p' => $banner_id
] );
if ( $query->have_posts() ) {
$query->the_post();
$subtitle = get_post_meta( get_the_ID(), self::get_meta_key( 'subtitle' ), true );
$badge_new = get_post_meta( get_the_ID(), self::get_meta_key( 'badge_new' ), true );
?>
postmeta} where meta_key = '{$meta_key}'";
$result = $wpdb->get_results( $query );
foreach ( $result as $row ) {
$row->meta_value = unserialize( $row->meta_value );
if ( in_array( $post_id, $row->meta_value ) ) {
$banners[] = $row->post_id;
}
}
return $banners;
}
/* Functions */
public static function translate( $text, $text_domain = self::TEXT_DOMAIN ) {
return __( $text, $text_domain );
}
public static function get_script_suffix() {
//return SCRIPT_DEBUG ? '' : '.min';
return '';
}
public static function get_curr_lang() {
$lang = apply_filters( 'wpml_current_language', null );
if ( ! $lang ) {
return self::DEFAULT_LANG;
}
return $lang;
}
public static function get_meta_key( $name ) {
return '_'. self::POST_TYPE . '_' . $name;
}
public static function get_transient_key( $key, $post_id ) {
$user_id = get_current_user_id();
return self::get_meta_key( "{$key}_{$post_id}_{$user_id}" );
}
}
AbaninjaHighlightBanner::init();