D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
vblioqus
/
safa777.info
/
wp-content
/
themes
/
infopediya
/
inc
/
metaboxes
/
Filename :
star-ratting.php
back
Copy
<?php class infopediyaRattingMetabox { private $screens = array('post'); private $fields = array( // array( // 'label' => 'Star ratting', // 'id' => 'infopediya_meta_file_ratting', // 'type' => 'number', // 'default' => '4.7', // ), array( 'label' => 'Number of 5 stars', 'id' => 'infopediya_meta_file_5_rattings', 'type' => 'number', 'default' => 200, ), array( 'label' => 'Number of 4 stars', 'id' => 'infopediya_meta_file_4_rattings', 'type' => 'number', 'default' => 40, ), array( 'label' => 'Number of 3 stars', 'id' => 'infopediya_meta_file_3_rattings', 'type' => 'number', 'default' => 20, ), array( 'label' => 'Number of 2 stars', 'id' => 'infopediya_meta_file_2_rattings', 'type' => 'number', 'default' => 10, ), array( 'label' => 'Number of 1 stars', 'id' => 'infopediya_meta_file_1_rattings', 'type' => 'number', 'default' => 20, ), ); public function __construct() { add_action('add_meta_boxes', array($this, 'add_meta_boxes')); add_action('save_post', array($this, 'save_fields')); } public function add_meta_boxes() { foreach ($this->screens as $s) { add_meta_box( 'infopediya_stars', __('Star ratting', 'infopediya'), array($this, 'meta_box_callback_star'), $s, ); } } public function meta_box_callback_star($post) { wp_nonce_field('infopediya_star_data', 'infopediya_star_nonce'); echo "Set ratting for your article"; $this->field_generator_star($post); } public function field_generator_star($post) { $output = ''; foreach ($this->fields as $field) { $label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>'; $meta_value = get_post_meta($post->ID, $field['id'], true); if (empty($meta_value)) { if (isset($field['default'])) { $meta_value = $field['default']; } } switch ($field['type']) { case 'select': $input = sprintf( '<select id="%s" name="%s">', $field['id'], $field['id'] ); foreach ($field['options'] as $key => $value) { $field_value = !is_numeric($key) ? $key : $value; $input .= sprintf( '<option %s value="%s">%s</option>', $meta_value === $field_value ? 'selected' : '', $field_value, $value ); } $input .= '</select>'; break; default: $input = sprintf( '<input %s id="%s" name="%s" type="%s" value="%s">', $field['type'] !== 'color' ? 'style="width: 50%"' : '', $field['id'], $field['id'], $field['type'], $meta_value ); } $output .= $this->format_rows($label, $input); } echo '<table class="form-table"><tbody>' . $output . '</tbody></table>'; } public function format_rows($label, $input) { return '<div style="margin-top: 10px;"><strong>' . $label . '</strong></div><div>' . $input . '</div>'; } public function save_fields($post_id) { if (!isset($_POST['infopediya_star_nonce'])) { return $post_id; } $nonce = $_POST['infopediya_star_nonce']; if (!wp_verify_nonce($nonce, 'infopediya_star_data')) { return $post_id; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } foreach ($this->fields as $field) { if (isset($_POST[$field['id']])) { switch ($field['type']) { case 'email': $_POST[$field['id']] = sanitize_email($_POST[$field['id']]); break; case 'text': $_POST[$field['id']] = sanitize_text_field($_POST[$field['id']]); break; } update_post_meta($post_id, $field['id'], $_POST[$field['id']]); } else if ($field['type'] === 'checkbox') { update_post_meta($post_id, $field['id'], '0'); } } } } if (class_exists('infopediyaRattingMetabox')) { new infopediyaRattingMetabox; }