true, ); $post_types = get_post_types( $args ); /** * Filter post types on which the meta box is displayed * * @since 2.2.0 * * @param array $post_types Array of post types */ $post_types = apply_filters( 'tptn_meta_box_post_types', $post_types ); if ( in_array( $post_type, $post_types, true ) ) { add_meta_box( 'tptn_metabox', 'Top 10', 'tptn_call_meta_box', $post_type, 'advanced', 'default' ); } } add_action( 'add_meta_boxes', 'tptn_add_meta_box' ); /** * Function to call the meta box. * * @since 1.9.10 */ function tptn_call_meta_box() { global $wpdb, $post; $table_name = $wpdb->base_prefix . 'top_ten'; // Add an nonce field so we can check for it later. wp_nonce_field( 'tptn_meta_box', 'tptn_meta_box_nonce' ); // Get the number of visits for the post being editted. $resultscount = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( "SELECT postnumber, cntaccess FROM {$table_name} WHERE postnumber = %d AND blog_id = %d ", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $post->ID, get_current_blog_id() ) ); $total_count = $resultscount ? $resultscount->cntaccess : 0; // Get the post meta. $tptn_post_meta = get_post_meta( $post->ID, 'tptn_post_meta', true ); // Disable display option. if ( isset( $tptn_post_meta['disable_here'] ) ) { $disable_here = $tptn_post_meta['disable_here']; } else { $disable_here = 0; } if ( isset( $tptn_post_meta['exclude_this_post'] ) ) { $exclude_this_post = $tptn_post_meta['exclude_this_post']; } else { $exclude_this_post = 0; } ?>
ID, tptn_get_option( 'thumb_meta' ), true ); $value = ( $results ) ? $results : ''; ?>
/>
/>
'; } } /** * Function to save the meta box. * * @since 1.9.10 * * @param int $post_id Post ID. */ function tptn_save_meta_box( $post_id ) { global $wpdb; $tptn_post_meta = array(); $table_name = $wpdb->base_prefix . 'top_ten'; // Bail if we're doing an auto save. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // If our nonce isn't there, or we can't verify it, bail. if ( ! isset( $_POST['tptn_meta_box_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['tptn_meta_box_nonce'] ), 'tptn_meta_box' ) ) { return; } // If our current user can't edit this post, bail. if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // Update the posts view count. if ( isset( $_POST['total_count'] ) && isset( $_POST['total_count_original'] ) ) { $total_count = intval( $_POST['total_count'] ); $total_count_original = intval( $_POST['total_count_original'] ); $blog_id = get_current_blog_id(); if ( 0 === $total_count ) { $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( "DELETE FROM {$table_name} WHERE postnumber = %d AND blog_id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $post_id, $blog_id ) ); } elseif ( $total_count_original !== $total_count ) { $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( "INSERT INTO {$table_name} (postnumber, cntaccess, blog_id) VALUES( %d, %d, %d ) ON DUPLICATE KEY UPDATE cntaccess= %d ", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $post_id, $total_count, $blog_id, $total_count ) ); } } // Update the thumbnail URL. if ( isset( $_POST['thumb_meta'] ) ) { $thumb_meta = empty( $_POST['thumb_meta'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['thumb_meta'] ) ); } if ( ! empty( $thumb_meta ) ) { update_post_meta( $post_id, tptn_get_option( 'thumb_meta' ), $thumb_meta ); } else { delete_post_meta( $post_id, tptn_get_option( 'thumb_meta' ) ); } // Disable posts. if ( isset( $_POST['disable_here'] ) ) { $tptn_post_meta['disable_here'] = 1; } else { $tptn_post_meta['disable_here'] = 0; } if ( isset( $_POST['exclude_this_post'] ) ) { $tptn_post_meta['exclude_this_post'] = 1; } else { $tptn_post_meta['exclude_this_post'] = 0; } /** * Filter the Top 10 Post meta variable which contains post-specific settings * * @since 2.2.0 * * @param array $tptn_post_meta Top 10 post-specific settings * @param int $post_id Post ID */ $tptn_post_meta = apply_filters( 'tptn_post_meta', $tptn_post_meta, $post_id ); $tptn_post_meta_filtered = array_filter( $tptn_post_meta ); /**** Now we can start saving */ if ( empty( $tptn_post_meta_filtered ) ) { // Checks if all the array items are 0 or empty. delete_post_meta( $post_id, 'tptn_post_meta' ); // Delete the post meta if no options are set. } else { update_post_meta( $post_id, 'tptn_post_meta', $tptn_post_meta ); } /** * Action triggered when saving Contextual Related Posts meta box settings * * @since 2.2 * * @param int $post_id Post ID */ do_action( 'tptn_save_meta_box', $post_id ); } add_action( 'save_post', 'tptn_save_meta_box' ); add_action( 'edit_attachment', 'tptn_save_meta_box' );