/**
  * Create the key.
  */
 public function create_key()
 {
     if (!isset($_POST['itelic-add-new-key']) || !isset($_POST['_wpnonce'])) {
         return;
     }
     if (!wp_verify_nonce($_POST['_wpnonce'], 'itelic-add-new-key')) {
         $this->message[View::NOTICE_ERROR] = __("Request expired. Please try again.", Plugin::SLUG);
         return;
     }
     if (!current_user_can('manage_options')) {
         $this->message[View::NOTICE_ERROR] = __("You don't have permission to do this.", Plugin::SLUG);
         return;
     }
     if (empty($_POST['product'])) {
         $this->message[View::NOTICE_ERROR] = __("You must select a product.", Plugin::SLUG);
         return;
     }
     $product = absint($_POST['product']);
     if (empty($_POST['username'])) {
         $customer = absint($_POST['customer']);
     } else {
         $customer = wp_insert_user(array('user_login' => $_POST['username'], 'user_email' => $_POST['email'], 'first_name' => $_POST['first'], 'last_name' => $_POST['last'], 'user_pass' => wp_generate_password(24, true, true)));
         if (is_wp_error($customer)) {
             $this->message[View::NOTICE_ERROR] = $customer->get_error_message();
             return;
         }
     }
     $activations = intval($_POST['activations']);
     $expiration = \ITELIC\make_local_time($_POST['expiration']);
     $key = $_POST['license'];
     $paid = $_POST['paid'];
     $args = array('key' => $key, 'product' => $product, 'customer' => $customer, 'paid' => $paid, 'limit' => $activations, 'expires' => \ITELIC\convert_local_to_gmt($expiration));
     /**
      * Filters the args used to create a new license key.
      *
      * @since 1.0
      *
      * @param array $args
      */
     $args = apply_filters('itelic_add_new_license_args', $args);
     $key = itelic_create_key($args);
     /**
      * Fires when a new license key is created from the add new form.
      *
      * @since 1.0
      *
      * @param Key $key
      */
     do_action('itelic_add_new_license_save', $key);
     if (is_wp_error($key)) {
         $this->message[View::NOTICE_ERROR] = $key->get_error_message();
     } else {
         /**
          * Determine whether or not to send WP's new user notification
          * when a key is manually created from the add new license key page.
          *
          * @since 1.0
          *
          * @param bool $send
          * @param Key  $key
          */
         $send_notification = apply_filters('itelic_add_new_license_send_new_user_notification', true, $key);
         if ($send_notification) {
             wp_new_user_notification($customer, null, 'both');
         }
         wp_redirect(itelic_get_admin_edit_key_link($key->get_key()));
         exit;
     }
 }
 /**
  * Setup the table.
  */
 public function setup_table()
 {
     if (!Dispatch::is_current_view('list')) {
         return;
     }
     try {
         $msg = $this->process_bulk_actions();
         if ($msg) {
             $this->message[View::NOTICE_SUCCESS] = $msg;
         }
     } catch (\Exception $e) {
         $this->message[View::NOTICE_ERROR] = $e->getMessage();
     }
     $query = new Keys($this->generate_get_key_args());
     $keys = $query->get_results();
     $total = $query->get_total_items();
     $products = itelic_get_products_with_licensing_enabled();
     $this->table = new Table($this->prepare_data($keys), $total, $products);
     if (isset($_GET['s'])) {
         $key = itelic_get_key($_GET['s']);
         if ($key) {
             wp_redirect(itelic_get_admin_edit_key_link($key->get_key()));
             die;
         }
     }
 }
    /**
     * Render the page.
     *
     * @return void
     */
    protected function render_page()
    {
        wp_enqueue_style('itelic-admin-licenses-profile');
        ?>

		<div class="user-edit-block <?php 
        echo esc_attr($this->get_tab_slug());
        ?>
-user-edit-block">
			<div class="inner-wrap">

				<h3><?php 
        _e("Licenses", Plugin::SLUG);
        ?>
</h3>

				<table>

					<thead>
					<tr>
						<th class="key"><?php 
        _e("Key", Plugin::SLUG);
        ?>
</th>
						<th class="status"><?php 
        _e("Status", Plugin::SLUG);
        ?>
</th>
						<th class="product"><?php 
        _e("Product", Plugin::SLUG);
        ?>
</th>
					</tr>
					</thead>

					<tfoot>
					<tr>
						<th class="key"><?php 
        _e("Key", Plugin::SLUG);
        ?>
</th>
						<th class="status"><?php 
        _e("Status", Plugin::SLUG);
        ?>
</th>
						<th class="product"><?php 
        _e("Product", Plugin::SLUG);
        ?>
</th>
					</tr>
					</tfoot>

					<tbody>

					<?php 
        foreach ($this->get_keys() as $key) {
            ?>
						<tr>
							<td class="key">
								<a href="<?php 
            echo itelic_get_admin_edit_key_link($key->get_key());
            ?>
">
									<?php 
            echo substr($key->get_key(), 0, 30);
            if (strlen($key->get_key()) > 30) {
                echo '&hellip;';
            }
            ?>
								</a>
							</td>
							<td class="status"><?php 
            echo $key->get_status(true);
            ?>
</td>
							<td class="product">
								<a href="<?php 
            echo get_edit_post_link($key->get_product()->ID);
            ?>
">
									<?php 
            echo $key->get_product()->post_title;
            ?>
								</a>
							</td>
						</tr>
					<?php 
        }
        ?>

					</tbody>

				</table>

			</div>
		</div>

		<?php 
    }
/**
 * Display the license key for a transaction product on the payments detail
 * page.
 *
 * @internal
 *
 * @since 1.0
 *
 * @param \WP_Post $post
 * @param array    $transaction_product
 */
function display_keys_on_transaction_detail($post, $transaction_product)
{
    $key = get_key_for_transaction_product($post->ID, $transaction_product['product_id']);
    if ($key === null) {
        return;
    }
    $link = '<a href="' . itelic_get_admin_edit_key_link($key->get_key()) . '">' . $key->get_key() . '</a>';
    echo "<h4 class='product-license-key'>";
    printf(__("License Key: %s", Plugin::SLUG), $link);
    echo "</h4>";
}
 /**
  * Add a "view" link to the key column.
  *
  * @param $item array
  *
  * @return string
  */
 public function column_key($item)
 {
     $del_link = add_query_arg(array('itelic_action' => 'delete', 'key' => $item['key'], 'nonce' => wp_create_nonce('itelic-delete-license-' . $item['key'])), Dispatch::get_tab_link('licenses'));
     //Build row actions
     $actions = array('view' => sprintf('<a href="%1$s">%2$s</a>', itelic_get_admin_edit_key_link($item['key']), __("View", Plugin::SLUG)), 'delete' => sprintf('<a href="%1$s" class="submitdelete">%2$s</a>', $del_link, __("Delete", Plugin::SLUG)));
     //Return the title contents
     return sprintf('%1$s %2$s', $item['key'], $this->row_actions($actions));
 }