'_affiliate_links_target', 'title' => 'Link Target URL', 'description' => '* Enter your target URL', 'type' => 'url', 'required' => 'required', 'sanitize_callback' => 'esc_url_raw' ), 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 ) { global $post; 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_register_script( 'affiliate-links-js', AFFILIATE_LINKS_PLUGIN_URL . 'admin/js/affiliate-links-admin.js', array( 'jquery' ), '1.6', true ); if ( $post ) { wp_localize_script( 'affiliate-links-js', 'afLinksAdmin', array( 'linkId' => $post->ID, 'permalink' => get_the_permalink( $post->ID ), 'shortcode' => 'af_link' ) ); } wp_enqueue_script( 'affiliate-links-js', false, 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' ); foreach ( $this->get_fields() as $field ) { if ( in_array( $field['name'], $this->admin_grid_fields ) ) { $defaults[ $field['name'] ] = $field['title']; } } $defaults['_affiliate_links_stat'] = __( 'Hits', 'affiliate-links' ); } 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' ), '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' ), array( $this, 'render_metabox_content' ), $post_type, 'normal', 'high' ); add_meta_box( 'affiliate_links_embed', __( 'Link Embedding', 'affiliate-links' ), array( $this, 'render_metabox_embed' ), $post_type, 'normal', 'high' ); add_meta_box( 'affiliate_links_sidebar', __( 'Information', 'affiliate-links' ), array( $this, 'render_metabox_sidebar' ), $post_type, 'side', 'default' ); } } public function is_form_skip_save( $post_id ) { return ( ! isset( $_POST['affiliate_links_custom_box_nonce'] ) ) || ( ! wp_verify_nonce( $_POST['affiliate_links_custom_box_nonce'], 'affiliate_links_custom_box' ) ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ( ! current_user_can( 'edit_post', $post_id ) ); } /** * Save metabox. */ public function save( $post_id ) { if ( $this->is_form_skip_save( $post_id ) ) { return $post_id; } foreach ( $this->get_fields() as $field ) { // Update the meta field. update_post_meta( $post_id, $field['name'], $this->get_sanitized_value( $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 ); } } public function get_sanitized_value( $field ) { if ( ! isset( $_POST[ $field['name'] ] ) ) { return ''; } $sanitize_callback = ( isset( $field['sanitize_callback'] ) ) ? $field['sanitize_callback'] : 'sanitize_text_field'; return call_user_func( $sanitize_callback, $_POST[ $field['name'] ] ); } public function get_fields() { return apply_filters( 'af_links_get_fields', $this->fields ); } /** * Render metabox content. */ public function render_metabox_content( $post ) { global $post_type_object; echo '
' . __( 'Before you can use this link you need to publish it.' ) . '
'; } } } /** * 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'] ); ?>