Common_Ratings
Rate items on a scale from 0 to 5
class Common_Ratings { public function __construct() { if ( is_admin() ) { add_action( 'load-post.php', array( $this, 'init_metabox' ) ); add_action( 'load-post-new.php', array( $this, 'init_metabox' ) ); } } public function init_metabox() { add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) ); add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 ); } public function add_metabox() { add_meta_box( 'ratings-meta', __( 'Ratings', '10sm' ), array( $this, 'render_metabox' ), array( 'bean-audio', 'bean-video', 'bean-book', 'bean-image', 'bean-gallery', 'bean-person', 'bean-company', 'you-do-review' ), 'advanced', 'default' ); } public function render_metabox( $post ) { // Add nonce for security and authentication. wp_nonce_field( 'tensm_nonce_action', 'tensm_nonce' ); // Retrieve an existing value from the database. $tensm_ratings_radio = get_post_meta( $post->ID, 'tensm_ratings-radio', true ); // Set default values. if( empty( $tensm_ratings_radio ) ) $tensm_ratings_radio = ''; // Form fields. echo '<table class="form-table">'; echo ' <tr>'; echo ' <th><label for="tensm_ratings-radio" class="tensm_ratings-radio_label">' . __( 'Rating', '10sm' ) . '</label></th>'; echo ' <td>'; echo ' <label><input type="radio" name="tensm_ratings-radio" class="tensm_ratings_radio_field" value="tensm_0" ' . checked( $tensm_ratings_radio, 'tensm_0', false ) . '> ' . __( '0', '10sm' ) . '</label><br>'; echo ' <label><input type="radio" name="tensm_ratings-radio" class="tensm_ratings_radio_field" value="tensm_1" ' . checked( $tensm_ratings_radio, 'tensm_1', false ) . '> ' . __( '1', '10sm' ) . '</label><br>'; echo ' <label><input type="radio" name="tensm_ratings-radio" class="tensm_ratings_radio_field" value="tensm_2" ' . checked( $tensm_ratings_radio, 'tensm_2', false ) . '> ' . __( '2', '10sm' ) . '</label><br>'; echo ' <label><input type="radio" name="tensm_ratings-radio" class="tensm_ratings_radio_field" value="tensm_3" ' . checked( $tensm_ratings_radio, 'tensm_3', false ) . '> ' . __( '3', '10sm' ) . '</label><br>'; echo ' <label><input type="radio" name="tensm_ratings-radio" class="tensm_ratings_radio_field" value="tensm_4" ' . checked( $tensm_ratings_radio, 'tensm_4', false ) . '> ' . __( '4', '10sm' ) . '</label><br>'; echo ' <label><input type="radio" name="tensm_ratings-radio" class="tensm_ratings_radio_field" value="tensm_5" ' . checked( $tensm_ratings_radio, 'tensm_5', false ) . '> ' . __( '5', '10sm' ) . '</label><br>'; echo ' <p class="description">' . __( '0 = terrible, 5 = great', '10sm' ) . '</p>'; echo ' </td>'; echo ' </tr>'; echo '</table>'; } public function save_metabox( $post_id, $post ) { // Add nonce for security and authentication. $nonce_name = isset( $_POST['tensm_nonce'] ) ? $_POST['tensm_nonce'] : ''; $nonce_action = 'tensm_nonce_action'; // Check if a nonce is set. if ( ! isset( $nonce_name ) ) return; // Check if a nonce is valid. if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) ) return; // Check if the user has permissions to save data. if ( ! current_user_can( 'edit_post', $post_id ) ) return; // Check if it's not an autosave. if ( wp_is_post_autosave( $post_id ) ) return; // Check if it's not a revision. if ( wp_is_post_revision( $post_id ) ) return; // Sanitize user input. $tensm_new_ratings_radio = isset( $_POST[ 'tensm_ratings-radio' ] ) ? $_POST[ 'tensm_ratings-radio' ] : ''; // Update the meta field in the database. update_post_meta( $post_id, 'tensm_ratings-radio', $tensm_new_ratings_radio ); } } new Common_Ratings;