'_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' ) ); /** * 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 custom styling. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); } /** * Add admin css file. */ public function enqueue_scripts( $hook ) { if ( $hook != 'post.php' AND $hook != 'post-new.php' ) { return; } wp_register_style( 'affiliate-links', AFFILIATE_LINKS_PLUGIN_URL . 'admin/css/affiliate-links-admin.css', false, '1.0' ); wp_enqueue_style( 'affiliate-links' ); } /** * Modify admin grid column headers. */ public function columns_head( $defaults ) { $defaults['permalink'] = __( 'Link URL', Affiliate_Links::$text_domain ); foreach( $this->fields as $field ) { $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 ) { if( $column_name == 'permalink' ) { echo get_the_permalink( $post_id ); } else { echo get_post_meta( $post_id, $column_name, true ); } } /** * Add appropriate actions. */ public function init_metabox() { add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); 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_meta_box_content' ), $post_type, 'advanced', 'high' ); } } /** * 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 ); } } /** * Render metabox content. */ public function render_meta_box_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->generate_field( $field, $value ); } $this->stats_field( $post->ID ); echo '
'; } /** * Generate settings field html. */ public function generate_field( $field, $value ) { $name = esc_attr( $field['name'] ); $title = esc_attr( $field['title'] ); $desc = esc_html( $field['description'] ); $type = esc_html( $field['type'] ); ?> value="" >