# hurrytimer/1.1.1/admin/class-hurrytimer-admin.php

HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress &amp; WooCommerce, version 1.1.1. 317 lines.

- Page: https://pluginprobe.com/plugins/hurrytimer/1.1.1/code/admin/class-hurrytimer-admin.php
- Raw: https://pluginprobe.com/plugins/hurrytimer/1.1.1/raw/admin/class-hurrytimer-admin.php
- Modified: 2018-12-13T12:04:12+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/hurrytimer/1.1.1/code/admin/class-hurrytimer-admin.php#L10-L20`.

```php
<?php
namespace Hurrytimer;

require_once plugin_dir_path(dirname(__FILE__)) . 'admin/includes/class-hurrytimer-ct-settings.php';
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       http://nabillemsieh.com
 * @since      1.0.0
 *
 * @package    Hurrytimer
 * @subpackage Hurrytimer/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Hurrytimer
 * @subpackage Hurrytimer/admin
 * @author     Nabil Lemsieh <contact@nabillemsieh.com>
 */
class Hurrytimer_Admin
{

    /**
     * The ID of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string $plugin_name The ID of this plugin.
     */
    private $plugin_name;

    /**
     * The version of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string $version The current version of this plugin.
     */
    private $version;

    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     *
     * @param      string $plugin_name The name of this plugin.
     * @param      string $version The version of this plugin.
     */
    public function __construct($plugin_name, $version)
    {

        $this->plugin_name = $plugin_name;
        $this->version = $version;
        add_action('wp_ajax_search_products', [$this, 'search_products']);
        add_action('admin_init', [$this, 'maybe_activate_countdown']);
        add_action('admin_init', [$this, 'maybe_deactivate_countdown']);
        add_filter('post_row_actions', [$this, 'countdown_list_row_actions']);
        add_filter('bulk_actions-edit-' . HURRYTIMER_COUNTDOWN_PT, [$this, 'countdown_list_bulk_actions']);
        add_filter('manage_' . HURRYTIMER_COUNTDOWN_PT . '_posts_columns', [$this, 'countdown_list_columns']);
        add_action('manage_' . HURRYTIMER_COUNTDOWN_PT . '_posts_custom_column', [$this, 'countdown_list_columns_content'], 10, 2);
        add_action('views_edit-' . HURRYTIMER_COUNTDOWN_PT, [$this, 'countdown_list_header_links'], 10);
        add_filter('bulk_post_updated_messages', [$this, 'custom_bulk_post_updated_messages'], 10, 2);
        add_action('admin_menu', [$this, 'help_tabs']);
        add_action('admin_notices', [$this, 'countdown_activation_notice']);
        add_filter( 'admin_footer_text', [$this, 'admin_footer_text']); 

    }

    function admin_footer_text(){
        $screen = get_current_screen();
        if($screen->post_type === HURRYTIMER_COUNTDOWN_PT){
            echo '<strong>HurryTimer</strong> is made by <a href="http://nabillemsieh.com" target="_blank" style="font-weight: 500;">Nabil Lemsieh</a> | <a href="https://wordpress.org/support/plugin/hurrytimer/reviews/" target="_blank" style="font-weight: 500;">Leave a Review</a>';
        }
    }
    public function countdown_activation_notice()
    {
        $post_id = filter_input(INPUT_GET, 'post', \FILTER_VALIDATE_INT);
        $status = \filter_input(INPUT_GET, 'hrytmr_action');
        if (!$post_id || !$status || !($post = get_post($post_id))) {
            return;
        }
        if ($status === "hrytmr-ct-deactivate" && !Hurrytimer_CT_Settings::is_active($post_id)) {
            ?>
     <div class="notice notice-success is-dismissible">
         <p><?php _e('Countdown timer deactivated.', "hurrytimer");?></p>
     </div>
      <?php
} else {
            ?>
        <div class="notice notice-success is-dismissible">
        <p><?php _e('Countdown timer activated.', "hurrytimer");?></p>
    </div>
     <?php
}
    }
    public function help_tabs()
    {
        add_action('load-edit.php', [$this, 'edit_help_tabs']);
        add_action('load-post-new.php', [$this, 'edit_help_tabs']);
        add_action('load-post.php', [$this, 'edit_help_tabs']);
    }

    public function edit_help_tabs()
    {
        $screen = get_current_screen();
        if (strpos($screen->id, 'hurrytimer_countdown') === false) {
            return;
        }
        $screen->remove_help_tabs();
    }
    public function custom_bulk_post_updated_messages($bulk_messages, $bulk_counts)
    {
        $bulk_messages[HURRYTIMER_COUNTDOWN_PT] = [
            'updated' => _n('%s countdown timer updated.', '%s countdown timers updated.', $bulk_counts['updated']),
            'locked' => _n('%s countdown timer not updated, somebody is editing it.', '%s countdown timers not updated, somebody is editing them.', $bulk_counts['locked']),
            'deleted' => _n('%s countdown timer permanently deleted.', '%s countdown timers permanently deleted.', $bulk_counts['deleted']),
            'trashed' => _n('%s countdown timer deleted.', '%s countdown timers deleted.', $bulk_counts['trashed']),
            'untrashed' => _n('%s countdown timer restored from the Trash.', '%s countdown timers restored from the Trash.', $bulk_counts['untrashed']),
        ];

        return $bulk_messages;
    }
    public function countdown_list_header_links($views)
    {
        unset($views['trash']);
        return $views;
    }
    public function countdown_list_columns_content($column, $post_id)
    {
        global $post;
        $ct_instance = new Hurrytimer_Countdown($post_id);
        switch ($column) {
            case 'status':
                echo $post->post_status === "publish" ? __('Active', "hurrytimer") : __('Inactive', "hurrytimer");
                break;
            case 'mode':
                echo $ct_instance->get_mode() === CDT_MODE::EVERGREEN ? __('Evergreen', "hurrytimer") : __('Normal', "hurrytimer");
                break;
            case 'shortcode':
                echo '<pre class="htmr-list-shortcode">[hurrytimer id="' . $post_id . '"]</pre>';
                break;

        }
    }
    public function countdown_list_bulk_actions($actions)
    {
        unset($actions['edit']);
        return $actions;
    }
    public function countdown_list_columns($columns)
    {
        $columns = array_filter($columns, function ($column_name) {
            return in_array($column_name, ['cb', 'title', 'date']);
        }, ARRAY_FILTER_USE_KEY);
        $date = $columns['date'];
        unset($columns['date']);
        $columns = array_merge($columns, [
            'status' => __('Status', "hurrytimer"),
            'mode' => __('Mode', "hurrytimer"),
            'shortcode' => __('Shortcode', "hurrytimer"),
        ]);
        $columns['date'] = $date;
        return $columns;
    }
    /**
     * Register the stylesheets for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_styles()
    {
        wp_enqueue_style('wp-color-picker');
        wp_enqueue_style($this->plugin_name, HURRYTIMER_PLUGIN_URL . '/admin/css/admin.css', [], filemtime(HURRYTIMER_PLUGIN_DIR . '/admin/css/admin.css'), 'all');
    }

    public function countdown_list_row_actions($actions)
    {
        global $post;
        if ($post->post_type === HURRYTIMER_COUNTDOWN_PT) {
            unset($actions['inline hide-if-no-js']);
            unset($actions['trash']);
        }
        $actions['delete'] = '<a href="' . get_delete_post_link($post->ID) . '">' . __('Delete', "hurrytimer") . '</a>';
        return $actions;
    }
    /**
     * Register the JavaScript for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_scripts()
    {
        wp_enqueue_script('wp-color-picker');

        wp_enqueue_script(
            $this->plugin_name,
            HURRYTIMER_PLUGIN_URL . '/admin/js/admin.js',
            array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker'),
            time(),
            true);

        wp_localize_script($this->plugin_name, 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));

    }

    public function menu()
    {
        add_menu_page('HurryTimer', 'HurryTimer', 'manage_options', 'hurrytimer', [ & $this, 'welcome_page_tpl']);

        add_submenu_page('hurrytimer',
            __('Add New Timer', 'hurrytimer'),
            __('Add New Timer', 'hurrytimer'),
            'manage_options',
            'post-new.php?post_type=hurrytimer_countdown'
        );

        // add_submenu_page('hurrytimer',
        //     __('Settings',"hurrytimer"),
        //     __('Settings', "hurrytimer"),
        //     'manage_options',
        //     'hurrytimer_settings',
        //     array(&$this, 'settings_page_tpl')
        // );
    }

    public function maybe_activate_countdown()
    {
        if (
            isset($_GET['hrytmr_action'])
            && $_GET['hrytmr_action'] === "hrytmr-ct-activate"
            && isset($_GET['_wpnonce'])
            && wp_verify_nonce($_GET['_wpnonce'], 'hrytmr-ct-activate')
        ) {
            $post_id = filter_input(INPUT_GET, 'post');
            wp_update_post(['ID' => $post_id, 'post_status' => 'publish']);
        }
    }

    public function maybe_deactivate_countdown()
    {
        if (
            isset($_GET['hrytmr_action'])
            && $_GET['hrytmr_action'] === "hrytmr-ct-deactivate"
            && isset($_GET['_wpnonce'])
            && wp_verify_nonce($_GET['_wpnonce'], 'hrytmr-ct-deactivate')
        ) {
            $post_id = filter_input(INPUT_GET, 'post');
            wp_update_post(['ID' => $post_id, 'post_status' => 'draft']);
        }
    }

    public function welcome_page_tpl()
    {
        include plugin_dir_path(dirname(__FILE__)) . 'admin/templates/welcome.php';
    }

    public function new_countdown_page_tpl()
    {
        include plugin_dir_path(dirname(__FILE__)) . 'admin/templates/new-countdown.php';
    }

    public function meta_boxes()
    {
        require plugin_dir_path(dirname(__FILE__)) . 'admin/class-hurrytimer-metaboxes.php';

        new Hurrytimer_MetaBoxes();
    }

    /**
     * Search products
     *
     * @return void
     */
    public function search_products()
    {
        $search = sanitize_text_field($_GET['search']);
        $products_selection = sanitize_text_field($_GET['productsSelection']);
        $results = [];
        $exclude = array_map('intval', $_GET['exclude']);

        if (in_array($products_selection, [WC_Product_Selection::SPECIFIC_CATEGORIES, WC_Product_Selection::ALL_CATEGORIES_EXCEPT])) {
            $args = [
                "hide_empty" => false,
                "taxonomy" => "product_cat",
                "name__like" => $search,
                'exclude' => $exclude,
            ];

            $items = get_terms($args);
            $results = array_map(function ($item) {
                return [
                    "id" => $item->term_id,
                    "text" => $item->name,
                ];
            }, $items);
        } else {
            $args = ['s' => $search, 'post_type' => 'product',
                'post__not_in' => $exclude,
            ];
            $items = get_posts($args);
            $results = array_map(function ($item) {
                return [
                    "id" => $item->ID,
                    "text" => $item->post_title,
                ];
            }, $items);
        }
        exit(json_encode(["results" => $results, "pagination" => true]));

    }
}

```
