| 1 |
<?php |
| 2 |
namespace Hurrytimer; |
| 3 |
|
| 4 |
require_once plugin_dir_path(dirname(__FILE__)) . 'admin/includes/class-hurrytimer-ct-settings.php'; |
| 5 |
/** |
| 6 |
* The admin-specific functionality of the plugin. |
| 7 |
* |
| 8 |
* @link http://nabillemsieh.com |
| 9 |
* @since 1.0.0 |
| 10 |
* |
| 11 |
* @package Hurrytimer |
| 12 |
* @subpackage Hurrytimer/admin |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* The admin-specific functionality of the plugin. |
| 17 |
* |
| 18 |
* Defines the plugin name, version, and two examples hooks for how to |
| 19 |
* enqueue the admin-specific stylesheet and JavaScript. |
| 20 |
* |
| 21 |
* @package Hurrytimer |
| 22 |
* @subpackage Hurrytimer/admin |
| 23 |
* @author Nabil Lemsieh <contact@nabillemsieh.com> |
| 24 |
*/ |
| 25 |
class Hurrytimer_Admin |
| 26 |
{ |
| 27 |
|
| 28 |
/** |
| 29 |
* The ID of this plugin. |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
* @access private |
| 33 |
* @var string $plugin_name The ID of this plugin. |
| 34 |
*/ |
| 35 |
private $plugin_name; |
| 36 |
|
| 37 |
/** |
| 38 |
* The version of this plugin. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
* @access private |
| 42 |
* @var string $version The current version of this plugin. |
| 43 |
*/ |
| 44 |
private $version; |
| 45 |
|
| 46 |
/** |
| 47 |
* Initialize the class and set its properties. |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* |
| 51 |
* @param string $plugin_name The name of this plugin. |
| 52 |
* @param string $version The version of this plugin. |
| 53 |
*/ |
| 54 |
public function __construct($plugin_name, $version) |
| 55 |
{ |
| 56 |
|
| 57 |
$this->plugin_name = $plugin_name; |
| 58 |
$this->version = $version; |
| 59 |
add_action('wp_ajax_search_products', [$this, 'search_products']); |
| 60 |
add_action('admin_init', [$this, 'maybe_activate_countdown']); |
| 61 |
add_action('admin_init', [$this, 'maybe_deactivate_countdown']); |
| 62 |
add_filter('post_row_actions', [$this, 'countdown_list_row_actions']); |
| 63 |
add_filter('bulk_actions-edit-' . HURRYTIMER_COUNTDOWN_PT, [$this, 'countdown_list_bulk_actions']); |
| 64 |
add_filter('manage_' . HURRYTIMER_COUNTDOWN_PT . '_posts_columns', [$this, 'countdown_list_columns']); |
| 65 |
add_action('manage_' . HURRYTIMER_COUNTDOWN_PT . '_posts_custom_column', [$this, 'countdown_list_columns_content'], 10, 2); |
| 66 |
add_action('views_edit-' . HURRYTIMER_COUNTDOWN_PT, [$this, 'countdown_list_header_links'], 10); |
| 67 |
add_filter('bulk_post_updated_messages', [$this, 'custom_bulk_post_updated_messages'], 10, 2); |
| 68 |
add_action('admin_menu', [$this, 'help_tabs']); |
| 69 |
add_action('admin_notices', [$this, 'countdown_activation_notice']); |
| 70 |
add_filter( 'admin_footer_text', [$this, 'admin_footer_text']); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
function admin_footer_text(){ |
| 75 |
$screen = get_current_screen(); |
| 76 |
if($screen->post_type === HURRYTIMER_COUNTDOWN_PT){ |
| 77 |
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>'; |
| 78 |
} |
| 79 |
} |
| 80 |
public function countdown_activation_notice() |
| 81 |
{ |
| 82 |
$post_id = filter_input(INPUT_GET, 'post', \FILTER_VALIDATE_INT); |
| 83 |
$status = \filter_input(INPUT_GET, 'hrytmr_action'); |
| 84 |
if (!$post_id || !$status || !($post = get_post($post_id))) { |
| 85 |
return; |
| 86 |
} |
| 87 |
if ($status === "hrytmr-ct-deactivate" && !Hurrytimer_CT_Settings::is_active($post_id)) { |
| 88 |
?> |
| 89 |
<div class="notice notice-success is-dismissible"> |
| 90 |
<p><?php _e('Countdown timer deactivated.', "hurrytimer");?></p> |
| 91 |
</div> |
| 92 |
<?php |
| 93 |
} else { |
| 94 |
?> |
| 95 |
<div class="notice notice-success is-dismissible"> |
| 96 |
<p><?php _e('Countdown timer activated.', "hurrytimer");?></p> |
| 97 |
</div> |
| 98 |
<?php |
| 99 |
} |
| 100 |
} |
| 101 |
public function help_tabs() |
| 102 |
{ |
| 103 |
add_action('load-edit.php', [$this, 'edit_help_tabs']); |
| 104 |
add_action('load-post-new.php', [$this, 'edit_help_tabs']); |
| 105 |
add_action('load-post.php', [$this, 'edit_help_tabs']); |
| 106 |
} |
| 107 |
|
| 108 |
public function edit_help_tabs() |
| 109 |
{ |
| 110 |
$screen = get_current_screen(); |
| 111 |
if (strpos($screen->id, 'hurrytimer_countdown') === false) { |
| 112 |
return; |
| 113 |
} |
| 114 |
$screen->remove_help_tabs(); |
| 115 |
} |
| 116 |
public function custom_bulk_post_updated_messages($bulk_messages, $bulk_counts) |
| 117 |
{ |
| 118 |
$bulk_messages[HURRYTIMER_COUNTDOWN_PT] = [ |
| 119 |
'updated' => _n('%s countdown timer updated.', '%s countdown timers updated.', $bulk_counts['updated']), |
| 120 |
'locked' => _n('%s countdown timer not updated, somebody is editing it.', '%s countdown timers not updated, somebody is editing them.', $bulk_counts['locked']), |
| 121 |
'deleted' => _n('%s countdown timer permanently deleted.', '%s countdown timers permanently deleted.', $bulk_counts['deleted']), |
| 122 |
'trashed' => _n('%s countdown timer deleted.', '%s countdown timers deleted.', $bulk_counts['trashed']), |
| 123 |
'untrashed' => _n('%s countdown timer restored from the Trash.', '%s countdown timers restored from the Trash.', $bulk_counts['untrashed']), |
| 124 |
]; |
| 125 |
|
| 126 |
return $bulk_messages; |
| 127 |
} |
| 128 |
public function countdown_list_header_links($views) |
| 129 |
{ |
| 130 |
unset($views['trash']); |
| 131 |
return $views; |
| 132 |
} |
| 133 |
public function countdown_list_columns_content($column, $post_id) |
| 134 |
{ |
| 135 |
global $post; |
| 136 |
$ct_instance = new Hurrytimer_Countdown($post_id); |
| 137 |
switch ($column) { |
| 138 |
case 'status': |
| 139 |
echo $post->post_status === "publish" ? __('Active', "hurrytimer") : __('Inactive', "hurrytimer"); |
| 140 |
break; |
| 141 |
case 'mode': |
| 142 |
echo $ct_instance->get_mode() === CDT_MODE::EVERGREEN ? __('Evergreen', "hurrytimer") : __('Normal', "hurrytimer"); |
| 143 |
break; |
| 144 |
case 'shortcode': |
| 145 |
echo '<pre class="htmr-list-shortcode">[hurrytimer id="' . $post_id . '"]</pre>'; |
| 146 |
break; |
| 147 |
|
| 148 |
} |
| 149 |
} |
| 150 |
public function countdown_list_bulk_actions($actions) |
| 151 |
{ |
| 152 |
unset($actions['edit']); |
| 153 |
return $actions; |
| 154 |
} |
| 155 |
public function countdown_list_columns($columns) |
| 156 |
{ |
| 157 |
$columns = array_filter($columns, function ($column_name) { |
| 158 |
return in_array($column_name, ['cb', 'title', 'date']); |
| 159 |
}, ARRAY_FILTER_USE_KEY); |
| 160 |
$date = $columns['date']; |
| 161 |
unset($columns['date']); |
| 162 |
$columns = array_merge($columns, [ |
| 163 |
'status' => __('Status', "hurrytimer"), |
| 164 |
'mode' => __('Mode', "hurrytimer"), |
| 165 |
'shortcode' => __('Shortcode', "hurrytimer"), |
| 166 |
]); |
| 167 |
$columns['date'] = $date; |
| 168 |
return $columns; |
| 169 |
} |
| 170 |
/** |
| 171 |
* Register the stylesheets for the admin area. |
| 172 |
* |
| 173 |
* @since 1.0.0 |
| 174 |
*/ |
| 175 |
public function enqueue_styles() |
| 176 |
{ |
| 177 |
wp_enqueue_style('wp-color-picker'); |
| 178 |
wp_enqueue_style($this->plugin_name, HURRYTIMER_PLUGIN_URL . '/admin/css/admin.css', [], filemtime(HURRYTIMER_PLUGIN_DIR . '/admin/css/admin.css'), 'all'); |
| 179 |
} |
| 180 |
|
| 181 |
public function countdown_list_row_actions($actions) |
| 182 |
{ |
| 183 |
global $post; |
| 184 |
if ($post->post_type === HURRYTIMER_COUNTDOWN_PT) { |
| 185 |
unset($actions['inline hide-if-no-js']); |
| 186 |
unset($actions['trash']); |
| 187 |
} |
| 188 |
$actions['delete'] = '<a href="' . get_delete_post_link($post->ID) . '">' . __('Delete', "hurrytimer") . '</a>'; |
| 189 |
return $actions; |
| 190 |
} |
| 191 |
/** |
| 192 |
* Register the JavaScript for the admin area. |
| 193 |
* |
| 194 |
* @since 1.0.0 |
| 195 |
*/ |
| 196 |
public function enqueue_scripts() |
| 197 |
{ |
| 198 |
wp_enqueue_script('wp-color-picker'); |
| 199 |
|
| 200 |
wp_enqueue_script( |
| 201 |
$this->plugin_name, |
| 202 |
HURRYTIMER_PLUGIN_URL . '/admin/js/admin.js', |
| 203 |
array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker'), |
| 204 |
time(), |
| 205 |
true); |
| 206 |
|
| 207 |
wp_localize_script($this->plugin_name, 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php'))); |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
public function menu() |
| 212 |
{ |
| 213 |
add_menu_page('HurryTimer', 'HurryTimer', 'manage_options', 'hurrytimer', [ & $this, 'welcome_page_tpl']); |
| 214 |
|
| 215 |
add_submenu_page('hurrytimer', |
| 216 |
__('Add New Timer', 'hurrytimer'), |
| 217 |
__('Add New Timer', 'hurrytimer'), |
| 218 |
'manage_options', |
| 219 |
'post-new.php?post_type=hurrytimer_countdown' |
| 220 |
); |
| 221 |
|
| 222 |
// add_submenu_page('hurrytimer', |
| 223 |
// __('Settings',"hurrytimer"), |
| 224 |
// __('Settings', "hurrytimer"), |
| 225 |
// 'manage_options', |
| 226 |
// 'hurrytimer_settings', |
| 227 |
// array(&$this, 'settings_page_tpl') |
| 228 |
// ); |
| 229 |
} |
| 230 |
|
| 231 |
public function maybe_activate_countdown() |
| 232 |
{ |
| 233 |
if ( |
| 234 |
isset($_GET['hrytmr_action']) |
| 235 |
&& $_GET['hrytmr_action'] === "hrytmr-ct-activate" |
| 236 |
&& isset($_GET['_wpnonce']) |
| 237 |
&& wp_verify_nonce($_GET['_wpnonce'], 'hrytmr-ct-activate') |
| 238 |
) { |
| 239 |
$post_id = filter_input(INPUT_GET, 'post'); |
| 240 |
wp_update_post(['ID' => $post_id, 'post_status' => 'publish']); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public function maybe_deactivate_countdown() |
| 245 |
{ |
| 246 |
if ( |
| 247 |
isset($_GET['hrytmr_action']) |
| 248 |
&& $_GET['hrytmr_action'] === "hrytmr-ct-deactivate" |
| 249 |
&& isset($_GET['_wpnonce']) |
| 250 |
&& wp_verify_nonce($_GET['_wpnonce'], 'hrytmr-ct-deactivate') |
| 251 |
) { |
| 252 |
$post_id = filter_input(INPUT_GET, 'post'); |
| 253 |
wp_update_post(['ID' => $post_id, 'post_status' => 'draft']); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
public function welcome_page_tpl() |
| 258 |
{ |
| 259 |
include plugin_dir_path(dirname(__FILE__)) . 'admin/templates/welcome.php'; |
| 260 |
} |
| 261 |
|
| 262 |
public function new_countdown_page_tpl() |
| 263 |
{ |
| 264 |
include plugin_dir_path(dirname(__FILE__)) . 'admin/templates/new-countdown.php'; |
| 265 |
} |
| 266 |
|
| 267 |
public function meta_boxes() |
| 268 |
{ |
| 269 |
require plugin_dir_path(dirname(__FILE__)) . 'admin/class-hurrytimer-metaboxes.php'; |
| 270 |
|
| 271 |
new Hurrytimer_MetaBoxes(); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Search products |
| 276 |
* |
| 277 |
* @return void |
| 278 |
*/ |
| 279 |
public function search_products() |
| 280 |
{ |
| 281 |
$search = sanitize_text_field($_GET['search']); |
| 282 |
$products_selection = sanitize_text_field($_GET['productsSelection']); |
| 283 |
$results = []; |
| 284 |
$exclude = array_map('intval', $_GET['exclude']); |
| 285 |
|
| 286 |
if (in_array($products_selection, [WC_Product_Selection::SPECIFIC_CATEGORIES, WC_Product_Selection::ALL_CATEGORIES_EXCEPT])) { |
| 287 |
$args = [ |
| 288 |
"hide_empty" => false, |
| 289 |
"taxonomy" => "product_cat", |
| 290 |
"name__like" => $search, |
| 291 |
'exclude' => $exclude, |
| 292 |
]; |
| 293 |
|
| 294 |
$items = get_terms($args); |
| 295 |
$results = array_map(function ($item) { |
| 296 |
return [ |
| 297 |
"id" => $item->term_id, |
| 298 |
"text" => $item->name, |
| 299 |
]; |
| 300 |
}, $items); |
| 301 |
} else { |
| 302 |
$args = ['s' => $search, 'post_type' => 'product', |
| 303 |
'post__not_in' => $exclude, |
| 304 |
]; |
| 305 |
$items = get_posts($args); |
| 306 |
$results = array_map(function ($item) { |
| 307 |
return [ |
| 308 |
"id" => $item->ID, |
| 309 |
"text" => $item->post_title, |
| 310 |
]; |
| 311 |
}, $items); |
| 312 |
} |
| 313 |
exit(json_encode(["results" => $results, "pagination" => true])); |
| 314 |
|
| 315 |
} |
| 316 |
} |
| 317 |
|