/**
  * Parse the status query.
  *
  * @since 1.0
  *
  * @return Where|null
  */
 protected function parse_type()
 {
     if ($this->args['type'] === 'any') {
         return null;
     } else {
         $white_list = Release::get_types();
         $types = (array) $this->args['type'];
         foreach ($types as $type) {
             if (!isset($white_list[$type])) {
                 throw new \InvalidArgumentException("Invalid type {$type}");
             }
         }
         return new Where('type', true, (array) $this->args['type']);
     }
 }
 public function test_types_exist()
 {
     $types = Release::get_types();
     $this->assertArrayHasKey('major', $types, 'Major type does not exist.');
     $this->assertArrayHasKey('minor', $types, 'Minor type does not exist.');
     $this->assertArrayHasKey('security', $types, 'Security type does not exist.');
     $this->assertArrayHasKey('pre-release', $types, 'Pre-release type does not exist.');
 }
 /**
  * Save post data to a new release.
  *
  * @since 1.0
  */
 public function save_new_release()
 {
     if (!isset($_POST['itelic-action']) || $_POST['itelic-action'] != 'add-new-release') {
         return;
     }
     if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'itelic-add-new-release')) {
         $this->errors[] = __("Request expired. Please try again.", Plugin::SLUG);
         return;
     }
     if (!isset($_POST['type-select']) || !array_key_exists($_POST['type-select'], Release::get_types())) {
         $this->errors[] = __("Invalid release type selected.", Plugin::SLUG);
         return;
     }
     $type = $_POST['type-select'];
     if (empty($_POST['product'])) {
         $this->errors[] = __("You must select a product.", Plugin::SLUG);
         return;
     }
     try {
         $product = itelic_get_product($_POST['product']);
     } catch (\Exception $e) {
         $this->errors[] = $e->getMessage();
         return;
     }
     if (!$product->has_feature('licensing')) {
         $this->errors[] = __("Product selected does not support licensing.", Plugin::SLUG);
         return;
     }
     if (empty($_POST['version'])) {
         $this->errors[] = __("Invalid version number entered.", Plugin::SLUG);
         return;
     }
     $version = sanitize_text_field($_POST['version']);
     if (empty($_POST['upload-file'])) {
         $this->errors[] = __("No software file selected.", Plugin::SLUG);
         return;
     }
     $attachment = get_post($_POST['upload-file']);
     $changelog = empty($_POST['whats-changed']) ? '' : stripslashes($_POST['whats-changed']);
     $security_message = empty($_POST['security-message']) ? '' : stripslashes($_POST['security-message']);
     if (!current_user_can('unfiltered_html')) {
         $changelog = wp_kses($changelog, wp_kses_allowed_html());
         $security_message = wp_kses($security_message, wp_kses_allowed_html());
     }
     $action = isset($_POST['release']) && $_POST['release'] ? 'release' : 'draft';
     $status = 'release' == $action ? Release::STATUS_ACTIVE : Release::STATUS_DRAFT;
     try {
         $args = array('product' => $product, 'file' => $attachment, 'version' => $version, 'type' => $type, 'status' => $status, 'changelog' => $changelog, 'security-message' => $security_message);
         /**
          * Filters the add new release args.
          *
          * @since 1.0
          *
          * @param array $args
          */
         $args = apply_filters('itelic_add_new_release_args', $args);
         $release = itelic_create_release($args);
         if (is_wp_error($release)) {
             $this->errors[] = $release->get_error_message();
             return;
         }
         if ($release) {
             /**
              * Fires when a new release is saved.
              *
              * @since 1.0
              *
              * @param Release $release
              */
             do_action('itelic_add_new_release_save', $release);
             $url = add_query_arg('new', true, itelic_get_admin_edit_release_link($release->get_pk()));
             wp_redirect($url);
             die;
         }
     } catch (\Exception $e) {
         $this->errors[] = $e->getMessage();
     }
 }
    /**
     * Render the types selector.
     *
     * @since 1.0
     *
     * @param string $selected
     */
    protected function render_types_tab($selected = '')
    {
        ?>

		<ul class="release-types">

			<?php 
        foreach (Release::get_types(true) as $type => $label) {
            ?>

				<li class="<?php 
            echo $type == $selected ? 'selected' : '';
            ?>
">
					<input type="radio" name="type-select" <?php 
            checked($selected, $type);
            ?>
 id="type-select-<?php 
            echo $type;
            ?>
" value="<?php 
            echo $type;
            ?>
">
					<label for="type-select-<?php 
            echo $type;
            ?>
">
						<span class="dashicons <?php 
            echo $this->get_icon_for_type($type);
            ?>
"></span>
						<span class="type-description"><?php 
            echo $label;
            ?>
</span>
					</label>
				</li>

			<?php 
        }
        ?>
		</ul>

		<?php 
    }
    /**
     * Extra controls to be displayed between bulk actions and pagination
     *
     * @since  1.0
     * @access protected
     *
     * @param string $which
     */
    protected function extra_tablenav($which)
    {
        if ($which !== 'top') {
            return;
        }
        $selected_product = isset($_GET['prod']) ? absint($_GET['prod']) : 0;
        $selected_type = isset($_GET['type']) ? $_GET['type'] : '';
        ?>

		<div class="product-filter-container">

			<label for="filter-by-product" class="screen-reader-text">
				<?php 
        _e("Filter by product", Plugin::SLUG);
        ?>
			</label>

			<select name="prod" id="filter-by-product" style="width:300px;margin-left:-8px;">

				<option value=""><?php 
        _e("All products", Plugin::SLUG);
        ?>
</option>

				<?php 
        foreach ($this->products as $product) {
            ?>

					<option value="<?php 
            echo esc_attr($product->ID);
            ?>
" <?php 
            selected($selected_product, $product->ID);
            ?>
>
						<?php 
            echo $product->post_title;
            ?>
					</option>

				<?php 
        }
        ?>

			</select>
		</div>

		<div class="type-filter-container">

			<label for="filter-by-type" class="screen-reader-text">
				<?php 
        _e("Filter by type", Plugin::SLUG);
        ?>
			</label>

			<select name="type" id="filter-by-type">

				<option value=""><?php 
        _e("All types", Plugin::SLUG);
        ?>
</option>

				<?php 
        foreach (Release::get_types(true) as $type => $label) {
            ?>

					<option value="<?php 
            echo esc_attr($type);
            ?>
" <?php 
            selected($selected_type, $type);
            ?>
>
						<?php 
            echo $label;
            ?>
					</option>

				<?php 
        }
        ?>

			</select>
		</div>

		<?php 
        submit_button(__('Filter'), 'button', 'filter_action', false);
    }
 /**
  * Enqueue scripts and styles.
  *
  * @since 1.0
  */
 private function enqueue()
 {
     $release = itelic_get_release($_GET['ID']);
     wp_enqueue_style('itelic-admin-releases-edit');
     wp_enqueue_script('itelic-admin-releases-edit');
     wp_localize_script('itelic-admin-releases-edit', 'ITELIC', array('prevVersion' => __("Previous version: %s", Plugin::SLUG), 'uploadTitle' => __("Choose Software File", Plugin::SLUG), 'uploadButton' => __("Replace File", Plugin::SLUG), 'uploadLabel' => __("Upload File", Plugin::SLUG), 'lessUpgrade' => __("Less", Plugin::SLUG), 'moreUpgrade' => __("More", Plugin::SLUG), 'saving' => __("Saving", Plugin::SLUG), 'ibdLoadOn' => 'loadCharts', 'statuses' => Release::get_statuses(), 'types' => Release::get_types(true), 'release' => $_GET['ID'], 'update_nonce' => wp_create_nonce('itelic-update-release-' . $_GET['ID']), 'ok' => __("Ok", Plugin::SLUG), 'cancel' => __("Cancel", Plugin::SLUG), 'currentVersion' => $release ? $release->get_product()->get_feature('licensing', array('field' => 'version')) : ''));
     wp_enqueue_media();
 }