'_affiliate_links_target', 'title' => 'Link Target URL', 'description' => '* Enter your target URL', 'type' => 'url', 'required' => 'required' ), array( 'name' => '_affiliate_links_description', 'title' => 'Link Description', 'description' => 'Describe your link', 'type' => 'text' ), array( 'name' => '_affiliate_links_nofollow', 'global_name' => 'nofollow', 'title' => 'Nofollow Link', 'type' => 'checkbox', 'description' => 'Add "X-Robots-Tag: noindex, nofollow" to HTTP headers' ), array( 'name' => '_affiliate_links_redirect', 'global_name' => 'redirect', 'title' => 'Redirect Type', 'type' => 'radio', 'description' => 'Set redirection HTTP status code', 'values' => array( '301' => '301 Moved Permanently', '302' => '302 Found', '307' => '307 Temporary Redirect' ) ), ); public $admin_grid_fields = array( '_affiliate_links_target', '_affiliate_links_description', '_affiliate_links_redirect' ); /** * Hook into the appropriate actions when the class is constructed. */ public function __construct() { // Add metabox actions. add_action( 'load-post.php', array( $this, 'init_metabox' ) ); add_action( 'load-post-new.php', array( $this, 'init_metabox' ) ); // Add custom field values to admin grid columns. add_filter( 'manage_posts_columns', array( $this, 'columns_head' ) ); add_action( 'manage_posts_custom_column', array( $this, 'columns_content' ), 10, 2 ); add_action( 'restrict_manage_posts', array( $this, 'restrict_links_by_cat' ) ); // Add custom styling. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Remove the Yoast SEO columns add_action( 'manage_edit-' . Affiliate_Links::$post_type . '_columns', array( $this, 'hide_yoast_columns' ) ); } /** * Remove unused Yoast columns */ public function hide_yoast_columns( $columns ) { unset( $columns['wpseo-score'] ); unset( $columns['wpseo-title'] ); unset( $columns['wpseo-metadesc'] ); unset( $columns['wpseo-focuskw'] ); return $columns; } /** * Add admin css file. */ public function enqueue_scripts( $hook ) { if ( $hook != 'post.php' AND $hook != 'post-new.php' AND $hook != 'affiliate-links_page_affiliate_links' ) { return; } //css wp_register_style( 'affiliate-links-css', AFFILIATE_LINKS_PLUGIN_URL . 'admin/css/affiliate-links-admin.css', false, '1.6' ); wp_enqueue_style( 'affiliate-links-css' ); //js wp_enqueue_script( 'affiliate-links-js', AFFILIATE_LINKS_PLUGIN_URL . 'admin/js/affiliate-links-admin.js', array('jquery'), '1.6', true ); } /** * Modify admin grid column headers. */ public function columns_head( $defaults ) { global $typenow; if ( $typenow == Affiliate_Links::$post_type ) { $defaults['permalink'] = __( 'Link URL', Affiliate_Links::$text_domain ); foreach( $this->fields as $field ) { if( in_array( $field['name'], $this->admin_grid_fields ) ) $defaults[ $field['name'] ] = $field['title']; } $defaults['_affiliate_links_stat'] = __( 'Hits', Affiliate_Links::$text_domain ); } return $defaults; } /** * Modify admin grid columns. */ public function columns_content( $column_name, $post_id ) { switch ( $column_name ) { case 'permalink' : echo esc_html( get_the_permalink( $post_id ) ); break; case '_affiliate_links_target' : echo esc_html( get_post_meta( $post_id, '_affiliate_links_target', true ) ); break; case '_affiliate_links_stat' : echo esc_html( get_post_meta( $post_id, '_affiliate_links_stat', true ) ); break; case '_affiliate_links_description' : echo esc_html( get_post_meta( $post_id, '_affiliate_links_description', true ) ); break; case '_affiliate_links_redirect' : echo esc_html( get_post_meta( $post_id, '_affiliate_links_redirect', true ) ); break; case '_affiliate_links_nofollow' : echo esc_html( get_post_meta( $post_id, '_affiliate_links_nofollow', true ) ); break; } } /** * Add link category filter to admin grid. */ function restrict_links_by_cat() { global $typenow; global $wp_query; if ( $typenow == Affiliate_Links::$post_type ) { if( !empty( $wp_query->query[ Affiliate_Links::$taxonomy ] ) ) { $selected = $wp_query->query[ Affiliate_Links::$taxonomy ]; } else { $selected = 0; } wp_dropdown_categories( array( 'show_option_all' => __("All Categories", Affiliate_Links::$text_domain), 'taxonomy' => Affiliate_Links::$taxonomy, 'value_field' => 'slug', 'name' => Affiliate_Links::$taxonomy, 'orderby' => 'name', 'selected' => $selected, 'hierarchical' => true, 'depth' => 3, 'show_count' => true, 'hide_empty' => true, ) ); } } /** * Add appropriate actions. */ public function init_metabox() { add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ), 0 ); add_action( 'save_post', array( $this, 'save' ) ); } /** * Adds the meta box container. */ public function add_meta_box( $post_type ) { $post_types = array( Affiliate_Links::$post_type ); if ( in_array( $post_type, $post_types ) ) { add_meta_box( 'affiliate_links_settings', __( 'Link Settings', Affiliate_Links::$text_domain ), array( $this, 'render_metabox_content' ), $post_type, 'normal', 'high' ); add_meta_box( 'affiliate_links_embed', __( 'Link Embedding', Affiliate_Links::$text_domain ), array( $this, 'render_metabox_embed' ), $post_type, 'normal', 'high' ); add_meta_box( 'affiliate_links_sidebar', __( 'Information', Affiliate_Links::$text_domain ), array( $this, 'render_metabox_sidebar' ), $post_type, 'side', 'default' ); } } /** * Save metabox. */ public function save( $post_id ) { // Check if our nonce is set. if ( ! isset( $_POST['affiliate_links_custom_box_nonce'] ) ) return $post_id; $nonce = $_POST['affiliate_links_custom_box_nonce']; // Verify that the nonce is valid. if ( ! wp_verify_nonce( $nonce, 'affiliate_links_custom_box' ) ) return $post_id; // If this is an autosave, our form has not been submitted, so we don't want to do anything. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id; // Check the user's permissions. if ( ! current_user_can( 'edit_post', $post_id ) ) return $post_id; foreach( $this->fields as $field ) { // Sanitize the user input. $custom_field = isset( $_POST[ $field['name'] ] ) ? sanitize_text_field( $_POST[ $field['name'] ] ) : ''; // Update the meta field. update_post_meta( $post_id, $field['name'], $custom_field ); } // reset stat count if( isset( $_POST['_affiliate_links_stat'] ) ){ $count = (int) $_POST['_affiliate_links_stat']; // Update the meta field. update_post_meta( $post_id, '_affiliate_links_stat', $count ); } } /** * Render metabox content. */ public function render_metabox_content( $post ) { echo ''; // Add an nonce field so we can check for it later. wp_nonce_field( 'affiliate_links_custom_box', 'affiliate_links_custom_box_nonce' ); $this->link_field( $post->ID ); foreach( $this->fields as $field ){ // Retrieve an existing value from the database. $value = get_post_meta( $post->ID, $field['name'], true ); $this->render_field( $field, $value ); } $this->stats_field( $post->ID ); echo '
'; } /** * Render embed metabox content. */ public function render_metabox_embed( $post ) { load_template( dirname( __FILE__ ) . '/partials/metabox-embed.php' ); } /** * Render sidebar metabox content. */ public function render_metabox_sidebar( $post ) { load_template( dirname( __FILE__ ) . '/partials/metabox-sidebar.php' ); } /** * Generate settings field html. */ public function render_field( $field, $value ) { $func_name = 'render_' . $field['type'] . '_field'; if( method_exists( __CLASS__, $func_name ) ) { call_user_func_array( array( $this, $func_name ), array('field' => $field, 'value' => $value ) ); } else { call_user_func_array( array( $this, 'render_text_field' ), array('field' => $field, 'value' => $value ) ); } } /** * Generate text input field. */ public function render_text_field( $field, $value ) { $name = esc_attr( $field['name'] ); $title = esc_attr( $field['title'] ); $desc = esc_html( $field['description'] ); $type = esc_attr( $field['type'] ); ?> value="" >

> $value ) { ?> >

-