public static function set_vote($vote, $metric, $context_id, $user_id) { $metric_type = Evaluate_Metrics::get_metric_types()[$metric['type']]; $old_vote = $metric_type->get_vote($metric, $context_id, $user_id); $vote = $metric_type->validate_vote($vote, $old_vote, $metric['options']); $metric_type->set_vote($vote, $metric, $context_id, $user_id); $score = $metric_type->get_score($metric['metric_id'], $context_id); $score = $metric_type->modify_score($score, $vote, $old_vote, $metric, $context_id); return array('count' => $score['count'], 'average' => $score['average'], 'data' => $score['data'], 'vote' => $vote); }
public function prepare_items() { $this->_column_headers = array($this->get_columns(), array(), $this->get_sortable_columns()); $per_page = $this->get_items_per_page('metrics_per_page', 10); $current_page = $this->get_pagenum(); $metrics = Evaluate_Metrics::get_metrics(null, $per_page, $current_page); $total_items = count($metrics); $this->set_pagination_args(['total_items' => $total_items, 'per_page' => $per_page]); $this->items = $metrics; }
public static function render_page() { ?> <div class="wrap"> <h2>Evaluate Shortcodes</h2> This plugin provides a shortcode so that you can embed your metric in locations that are not naturally supported (such as widgets, other plugins, or inline in a post). <dl> <dt>ID</dt> <dd> <select> <option value=""> - Choose a Metric - </option> <?php $metrics = Evaluate_Metrics::get_metrics(); foreach ($metrics as $key => $metric) { ?> <option value="<?php echo $metric['metric_id']; ?> "><?php echo $metric['name']; ?> </option> <?php } ?> </select> <br> <small>Indicate which metric you want to embed using this shortcode</small> </dd> <dt>Key</dt> <dd> <input type="text"></input> <p><small>This parameter defines a unique key for the shortcode. A user will only be able two vote once for every unique key, even if it is embedded multiple times. <u>For example</u>: if you are embedding on two different posts, you would want to use two different keys, so that the user can vote differently for each post. On the other hand, if you are creating a feedback rating for the website, you would want to use the same key in all locations, so that the user only has 1 rating for the website.</small></p> <p><small> You can create your own key, or use a special option: <br><strong>%post_id%</strong> inserts the id of whichever post is currently being viewed. <br><strong>%date%</strong> inserts the current day. Meaning the user will be able to make a new vote on the next day, and will no longer be able to change their old vote. <br><strong>%url%</strong> inserts a unique id for the current url. <br>When creating your own key, use a meaningful word, so that you can recognize it when viewing the metric data. </small></p> </dd> </dl> <div>Your shortcode is</div> <code>[metric id="" key=""]</code> </div> <?php }
echo $text; ?> </option> <?php } ?> </select> </dd> <dt>Text</dt> <dd> <input type="text" name="<?php printf($name, 'text_up'); ?> " placeholder="Up" value="<?php echo $options['text_up']; ?> " autocomplete="off"></input> <br> <input type="text" name="<?php printf($name, 'text_down'); ?> " placeholder="Down" value="<?php echo $options['text_down']; ?> " autocomplete="off"></input> </dd> <?php } } Evaluate_Metrics::register_type(new Evaluate_Metric_Two_Way());
echo $slug; ?> " <?php selected($slug, $options['icon']); ?> > <?php echo $text; ?> </option> <?php } ?> </select> </dd> <dt>Maximum Rating</dt> <dd> <input type="number" min="2" name="<?php printf($name, 'max'); ?> " value="<?php echo $options['max']; ?> "></input> </dd> <?php // TODO: Add minimum rating } } Evaluate_Metrics::register_type(new Evaluate_Metric_Range());
private static function save_post_data() { if (!isset($_POST['action']) || $_POST['action'] !== 'save') { return; } global $wpdb; // TODO: Verify nonce. $metric_type = Evaluate_Metrics::get_metric_types()[$_POST['type']]; $options = $metric_type->filter_options($_POST['options']); $data = array('name' => sanitize_text_field($_POST['name']), 'type' => sanitize_text_field($_POST['type']), 'options' => serialize($options)); if (empty($_POST['metric_id'])) { $data['created'] = current_time('mysql', 1); $wpdb->insert(Evaluate::$metric_table, $data); $metric_id = $wpdb->insert_id; } else { $wpdb->update(Evaluate::$metric_table, $data, array('metric_id' => $_POST['metric_id'])); $metric_id = $_POST['metric_id']; } wp_redirect(add_query_arg('metric_id', $metric_id)); exit; }
} public static function get_metrics($ids = array(), $per_page = 5, $page_number = 1) { global $wpdb; $sql = "SELECT * FROM " . Evaluate::$metric_table; if (!empty($ids)) { $sql .= ' WHERE metric_id IN (' . implode(',', array_map('intval', $ids)) . ')'; } if (!empty($_REQUEST['orderby'])) { $sql .= ' ORDER BY ' . esc_sql($_REQUEST['orderby']); $sql .= !empty($_REQUEST['order']) ? ' ' . esc_sql($_REQUEST['order']) : ' ASC'; } if (!empty($per_page)) { $sql .= " LIMIT " . $per_page; $sql .= " OFFSET " . max($page_number - 1, 0) * $per_page; } $results = $wpdb->get_results($sql, 'ARRAY_A'); foreach ($results as $key => $result) { if (!empty($result['options'])) { $results[$key]['options'] = unserialize($result['options']); } } return apply_filters('evaluate_get_metrics', $results, $ids); } public static function get_metric_contexts() { // TODO: Run a filter that retrieves all contexts in which a metric can be posted. } } Evaluate_Metrics::init();
public function render_options($options, $name = 'options[%s]') { $options = shortcode_atts(array('rubric' => '', 'fields' => array()), $options); $rubrics = apply_filters('evaluate_get_rubrics', array()); $metric_types = Evaluate_Metrics::get_metric_types(); foreach ($metric_types as $slug => $title) { if ($slug === self::SLUG) { unset($metric_types[$slug]); } } ?> <dt>Rubric Definition</dt> <dd> <select class="nav" data-anchor="rubric-options" data-siblings="true" name="<?php printf($name, 'rubric'); ?> "> <option value="">- Choose a Rubric -</option> <option value="custom" <?php selected($options['rubric'], 'custom'); ?> >Custom</option> <?php foreach ($rubrics as $slug => $rubric) { ?> <option value="<?php echo $slug; ?> " <?php selected($slug, $options['rubric']); ?> > <?php echo $rubric['name']; ?> </option> <?php } ?> </select> <ul id="rubric-fields" class="rubric-options-custom rubric-options"<?php echo $options['rubric'] == 'custom' ? '' : ' style="display: none;"'; ?> > <?php $field_name = sprintf($name, 'fields') . '[#][%s]'; // ^ The '#' character will be replaced using JavaScript. If you change it, make sure you update evaluate-admin.js! // This is to get around the fact that "options['fields'][]['title']" is not a valid name attribute. if (!empty($options['fields'])) { foreach ($options['fields'] as $index => $field) { self::render_field($metric_types, $field_name, $field); } } self::render_field($metric_types, $field_name); ?> </ul> <?php foreach ($rubrics as $slug => $rubric) { ?> <div class="rubric-options-<?php echo $slug; ?> rubric-options"<?php echo $slug == $options['rubric'] ? '' : ' style="display: none;"'; ?> > <p><?php echo $rubric['description']; ?> </p> <strong>Fields:</strong> <ul> <?php foreach ($rubric['fields'] as $key => $field) { echo $field['title'] . " - a " . $metric_types[$field['type']]->name . " metric, with " . $field['weight'] . " weight."; } ?> </ul> </div> <?php } ?> </dd> <?php }
} // ===== ADMIN FACING CODE ==== // public function render_options($options, $name = 'options[%s]') { $options = shortcode_atts(array('question' => "", 'answers' => ""), $options); ?> <dt>Question</dt> <dd><input type="text" name="<?php printf($name, 'question'); ?> " value="<?php echo $options['question']; ?> " autocomplete="off"></input></dd> <dt>Answers</dt> <dd> <textarea name="<?php printf($name, 'answers'); ?> "><?php echo $options['answers']; ?> </textarea> <br> <small>One answer per line</small> </dd> <?php } } Evaluate_Metrics::register_type(new Evaluate_Metric_Poll());