_x( self::$plugin_name, 'Post Type General Name', self::$text_domain ), 'singular_name' => _x( self::$singular_name, 'Post Type Singular Name', self::$text_domain ), 'menu_name' => __( self::$plugin_name, 'affiliate-links' ), 'name_admin_bar' => __( self::$plugin_name, 'affiliate-links' ), 'add_new_item' => __( 'Add New Link', self::$text_domain ), 'add_new' => __( 'Add New Link', self::$text_domain ), 'new_item' => __( 'New Link', self::$text_domain ), 'edit_item' => __( 'Edit Link', self::$text_domain ), 'update_item' => __( 'Update Link', self::$text_domain ), 'view_item' => __( 'View Link', self::$text_domain ), 'search_items' => __( 'Search Link', self::$text_domain ) ); if( !empty( self::$settings['slug'] ) ) { $slug = self::$settings['slug']; } else { $slug = self::$slug; } $rewrite = array( 'slug' => $slug, 'with_front' => false, 'pages' => false, 'feeds' => false, ); $args = array( 'label' => __( self::$singular_name, self::$text_domain ), 'description' => __( 'Affiliate Link Post Type', self::$text_domain ), 'labels' => $labels, 'supports' => array( 'title' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 100, 'menu_icon' => 'dashicons-admin-links', 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => false, 'exclude_from_search' => true, 'publicly_queryable' => true, 'rewrite' => $rewrite, 'capability_type' => 'page', ); register_post_type( self::$post_type, $args ); } /** * Register Custom Post Type. */ public function register_affiliate_links_cat() { $labels = array( 'name' => _x( 'Categories', 'Taxonomy General Name', self::$text_domain ), 'singular_name' => _x( 'Category', 'Taxonomy Singular Name', self::$text_domain ), 'menu_name' => __( 'Categories', self::$text_domain ) ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'public' => false, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => false, 'show_tagcloud' => false, 'rewrite' => false, ); register_taxonomy( self::$taxonomy, array( self::$post_type ), $args ); } /** * Redirect affiliate link. */ public function redirect() { if ( is_singular( Affiliate_Links::$post_type ) ) { $post_id = get_the_ID(); $target_url = get_post_meta( $post_id, '_affiliate_links_target', true ); $redirect_type = get_post_meta( $post_id, '_affiliate_links_redirect', true ); $nofollow = get_post_meta( $post_id, '_affiliate_links_nofollow', true ); if( empty( $target_url ) ){ if( !empty( self::$settings['default'] ) ) { $target_url = self::$settings['default']; } else { $target_url = home_url( '/' ); } } $this->count_stats( $post_id ); if( empty( $redirect_type ) ) { if( empty( self::$settings['redirect'] ) ) { $redirect_type = 301; } else { $redirect_type = (int) self::$settings['redirect']; } } if( $nofollow ) { header( "X-Robots-Tag: noindex, nofollow", true ); } wp_redirect( esc_url_raw( $target_url ), $redirect_type ); exit(); } } /** * Count link hits and update post_meta. */ public function count_stats( $post_id ) { if( !empty( self::$settings['stats'] ) ) { $current = (int) get_post_meta( $post_id, '_affiliate_links_stat', true ); update_post_meta( $post_id, '_affiliate_links_stat', $current + 1 ); } } }