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 do_ajax_vote()
 {
     error_log("AJAX VOTE, " . var_export($_POST, true));
     $args = shortcode_atts(array('metric_id' => null, 'context_id' => null, 'vote' => null), $_REQUEST);
     $nonce_key = self::get_nonce_key($args['metric_id'], $args['context_id']);
     check_ajax_referer($nonce_key, 'nonce');
     if ($args['metric_id'] != null && $args['context_id'] != null) {
         $metric = Evaluate_Metrics::get_metrics(array($args['metric_id']))[0];
         $score = self::set_vote($args['vote'], $metric, $args['context_id'], self::get_user_key());
         echo json_encode($score);
     }
     wp_die();
 }
    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 
    }
    public static function render_new_page()
    {
        wp_enqueue_script('evaluate-admin');
        $metric_types = Evaluate_Metrics::get_metric_types();
        if (isset($_GET['metric_id'])) {
            $metric_id = $_GET['metric_id'];
            $metric = Evaluate_Metrics::get_metrics(array($metric_id))[0];
        } else {
            $metric_id = "";
            $metric = array();
        }
        $metric = shortcode_atts(array('name' => "", 'type' => "", 'options' => array('title' => "")), $metric);
        var_dump($metric);
        ?>
		<div class="wrap">
			<h2><?php 
        echo empty($metric_id) ? "Create New Metric" : "Edit Metric";
        ?>
</h2>

			<form method="POST">
				<input type="hidden" name="action" value="save"></input>
				<input type="hidden" name="metric_id" value="<?php 
        echo $metric_id;
        ?>
"></input>
				<dl>
					<dt><label for="name">Name</label></dt>
					<dd><input name="name" value="<?php 
        echo $metric['name'];
        ?>
" autocomplete="off"></input></dd>
					<dt><label for="type">Type</label></dt>
					<dd>
						<select id="type" class="nav" data-anchor="options" name="type">
							<option value=""> - Choose a Type - </option>
							<?php 
        foreach ($metric_types as $slug => $metric_type) {
            ?>
								<option value="<?php 
            echo $slug;
            ?>
" <?php 
            selected($slug, $metric['type']);
            ?>
>
									<?php 
            echo $metric_type->name;
            ?>
								</option>
								<?php 
        }
        ?>
						</select>
					</dd>
					<dt><label for="options[title]">Display Title</label></dt>
					<dd><input name="options[title]" value="<?php 
        echo $metric['options']['title'];
        ?>
" autocomplete="off"></input></dd>
				</dl>
				<?php 
        foreach ($metric_types as $slug => $metric_type) {
            ?>
					<dl class="options-<?php 
            echo $slug;
            ?>
 options"<?php 
            echo $slug == $metric['type'] ? '' : ' style="display: none;"';
            ?>
>
						<?php 
            $metric_type->render_options($metric['options']);
            ?>
					</dl>
					<?php 
        }
        ?>
				<dl>
					<dt>Usage</dt>
					<dd>
						<?php 
        if (!isset($metric['options']['usage'])) {
            $usage = array('shortcodes');
        } else {
            $usage = $metric['options']['usage'];
        }
        $cases = array('shortcodes' => "Available as a shortcode", 'admins_only' => "Only visible to admins", 'comments_attached' => "Attached to Comments (i)", 'comments' => "Available on Comments");
        foreach (get_post_types(array('public' => true), 'objects') as $slug => $object) {
            $cases[$slug] = "Available on " . $object->labels->name;
        }
        foreach ($cases as $slug => $text) {
            ?>
							<label>
								<input type="checkbox" name="options[usage][]" value="<?php 
            echo $slug;
            ?>
" <?php 
            checked(in_array($slug, $usage));
            ?>
></input>
								<?php 
            echo $text;
            ?>
							</label>
							<br>
							<?php 
        }
        ?>
					</dd>
				</dl>
				<input class="button button-primary" type="submit" value="Save"></input>
			</form>
		</div>
		<?php 
    }