| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
/** |
| 6 |
* The admin posts metaboxes. |
| 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 CampaignSettings |
| 26 |
{ |
| 27 |
public function __construct() |
| 28 |
{ |
| 29 |
$this->intializeHooks(); |
| 30 |
} |
| 31 |
|
| 32 |
private function intializeHooks() |
| 33 |
{ |
| 34 |
|
| 35 |
// Save compaign settings. |
| 36 |
add_action('save_post_hurrytimer_countdown', [$this, 'save_settings'], 10, 3); |
| 37 |
|
| 38 |
// Custom publish metabox. |
| 39 |
add_action('post_submitbox_misc_actions', [$this, 'post_publish_metabox']); |
| 40 |
|
| 41 |
// Edit placeholder for headline. |
| 42 |
add_filter('enter_title_here', [$this, 'change_countdown_title'], 10); |
| 43 |
|
| 44 |
// Edit messages. |
| 45 |
add_filter('post_updated_messages', [$this, 'custom_updated_messages'], 10); |
| 46 |
|
| 47 |
// Campaign settings metabox. |
| 48 |
add_filter('add_meta_boxes', [$this, 'add_settings_metabox_template'], 10); |
| 49 |
|
| 50 |
add_filter('after_delete_post', [$this, 'maybe_delete_running_countdown'], 10); |
| 51 |
|
| 52 |
add_action("updated_post_meta", [$this, 'maybe_reset_running_countdown'], 10, 4); |
| 53 |
|
| 54 |
// Skip trash |
| 55 |
add_action("trashed_post", [$this, 'skip_trash']); |
| 56 |
|
| 57 |
// Edit delete link text. |
| 58 |
add_action('post_submitbox_start', [$this, 'delete_permanently_link']); |
| 59 |
} |
| 60 |
|
| 61 |
public function delete_permanently_link($post) |
| 62 |
{ |
| 63 |
if ($post->post_type !== HURRYT_POST_TYPE) { |
| 64 |
return; |
| 65 |
} |
| 66 |
?> |
| 67 |
|
| 68 |
<div id="delete-action" class="is-delete-permanently"> |
| 69 |
<?php if (current_user_can("delete_post", $post->ID)) { |
| 70 |
$delete_text = __('Delete', "hurrytimer"); ?> |
| 71 |
<a class="submitdelete deletion" href="<?php echo get_delete_post_link( |
| 72 |
$post->ID |
| 73 |
); ?>"> |
| 74 |
<?php echo $delete_text; ?></a> |
| 75 |
<?php |
| 76 |
} ?> |
| 77 |
</div> |
| 78 |
<?php |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Skip trash when deleting. |
| 83 |
* |
| 84 |
* @param int $post_id |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function skip_trash($post_id) |
| 89 |
{ |
| 90 |
wp_delete_post($post_id, true); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Edit notices messages. |
| 95 |
* |
| 96 |
* @param array $messages |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public function custom_updated_messages($messages) |
| 101 |
{ |
| 102 |
global $post; |
| 103 |
global $post_id; |
| 104 |
$messages[HURRYT_POST_TYPE] = array( |
| 105 |
0 => '', |
| 106 |
1 => __('Campaign updated', "hurrytimer"), |
| 107 |
2 => '', |
| 108 |
3 => '', |
| 109 |
4 => __('Campaign updated.', "hurrytimer"), |
| 110 |
5 => '', |
| 111 |
6 => __('Campaign published', "hurrytimer"), |
| 112 |
7 => __('Campaign saved.', "hurrytimer"), |
| 113 |
8 => __('Campaign submitted.', "hurrytimer"), |
| 114 |
9 => sprintf( |
| 115 |
__( |
| 116 |
'Campaign scheduled for: <strong>%1$s</strong>.', |
| 117 |
"hurrytimer" |
| 118 |
), |
| 119 |
date_i18n( |
| 120 |
__('M j, Y @ G:i', "hurrytimer"), |
| 121 |
strtotime($post->post_date) |
| 122 |
) |
| 123 |
), |
| 124 |
10 => __('Campaign draft updated.', "hurrytimer"), |
| 125 |
); |
| 126 |
|
| 127 |
return $messages; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* If time is edited, reset compaign. |
| 132 |
* |
| 133 |
* @param int $meta_id |
| 134 |
* @param int $object_id |
| 135 |
* @param string $meta_key |
| 136 |
* @param string $meta_value |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
public function maybe_reset_running_countdown( |
| 141 |
$meta_id, |
| 142 |
$object_id, |
| 143 |
$meta_key, |
| 144 |
$meta_value |
| 145 |
) { |
| 146 |
if ( ! in_array($meta_key, ['duration'])) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
// (new EvergreenCampaign($object_id))->reset(); |
| 151 |
} |
| 152 |
|
| 153 |
public function maybe_delete_running_countdown($post_id) |
| 154 |
{ |
| 155 |
//Evergreen_Countdown::reset($post_id); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Edit title placeholder. |
| 160 |
* |
| 161 |
* @param string $title |
| 162 |
* |
| 163 |
* @return string |
| 164 |
*/ |
| 165 |
public function change_countdown_title($title) |
| 166 |
{ |
| 167 |
global $post; |
| 168 |
if ($post->post_type === HURRYT_POST_TYPE) { |
| 169 |
return __( |
| 170 |
'Headline (optional)', |
| 171 |
"hurrytimer" |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
return $title; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Custom publish metabox. |
| 180 |
* |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
public function post_publish_metabox() |
| 184 |
{ |
| 185 |
global $post_id, $post; |
| 186 |
if ($post->post_type !== HURRYT_POST_TYPE) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
if (Utils\Helpers::isNewPost($post_id)) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
$isActive = Utils\Helpers::isPostActive($post_id); |
| 195 |
$publich_date = Utils\Helpers::format_date($post->post_date); |
| 196 |
$deactivateUrl = Utils\Helpers::deactivateUrl($post_id); |
| 197 |
$activateUrl = Utils\Helpers::activateUrl($post_id); |
| 198 |
|
| 199 |
include HURRYT_DIR . '/admin/templates/post-publish-metabox.php'; |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Save compaign settings |
| 205 |
* |
| 206 |
* @param int $post_id |
| 207 |
* |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
|
| 211 |
public function save_settings($post_id) |
| 212 |
{ |
| 213 |
if (wp_is_post_revision($post_id)) { |
| 214 |
return; |
| 215 |
} |
| 216 |
if ( ! current_user_can('edit_post', $post_id)) { |
| 217 |
return; |
| 218 |
} |
| 219 |
$countdown = new Campaign($post_id); |
| 220 |
$countdown->storeSettings($_POST); |
| 221 |
} |
| 222 |
|
| 223 |
public function add_settings_metabox_template($post_type) |
| 224 |
{ |
| 225 |
global $post_id; |
| 226 |
if ($post_type != HURRYT_POST_TYPE) { |
| 227 |
return; |
| 228 |
} |
| 229 |
remove_meta_box('wpseo_meta', HURRYT_POST_TYPE, 'normal'); |
| 230 |
remove_meta_box('slugdiv', HURRYT_POST_TYPE, 'normal'); |
| 231 |
remove_meta_box('eg-meta-box', HURRYT_POST_TYPE, 'normal'); |
| 232 |
|
| 233 |
add_meta_box( |
| 234 |
'hurrytimer-settings', |
| 235 |
__('Settings <a href="#" class="hurryt-fullscreen hidden"></a>', |
| 236 |
'hurrytimer'), |
| 237 |
array($this, 'settingsMetaboxTemplate'), |
| 238 |
$post_type, |
| 239 |
'normal', |
| 240 |
'high' |
| 241 |
); |
| 242 |
|
| 243 |
if ($post_id) { |
| 244 |
add_meta_box( |
| 245 |
'hrytmr-countdown__shortcode', |
| 246 |
__('Shortcode', 'hurrytimer'), |
| 247 |
array($this, 'shortcode_metabox_template'), |
| 248 |
$post_type, |
| 249 |
'side', |
| 250 |
'high' |
| 251 |
); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
public function shortcode_metabox_template() |
| 256 |
{ |
| 257 |
global $post_id; |
| 258 |
$shortcode = "[hurrytimer id='{$post_id}']"; |
| 259 |
include HURRYT_DIR . '/admin/templates/campaign-shortcode.php'; |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Campaign settings template. |
| 265 |
* |
| 266 |
* @return void |
| 267 |
*/ |
| 268 |
public function settingsMetaboxTemplate() |
| 269 |
{ |
| 270 |
global $post_id; |
| 271 |
$campaign = new Campaign($post_id); |
| 272 |
$campaign->loadSettings(); |
| 273 |
$products = $this->getWcProductsNames($campaign->wcProductsSelection, |
| 274 |
$campaign->wcProductsSelectionType); |
| 275 |
include HURRYT_DIR . '/admin/templates/campaign-settings.php'; |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* @param array $ids |
| 281 |
* @param $selection |
| 282 |
* |
| 283 |
* @return array |
| 284 |
*/ |
| 285 |
public function getWcProductsNames($ids, $selection) |
| 286 |
{ |
| 287 |
$result = []; |
| 288 |
if (empty($ids)) { |
| 289 |
return $result; |
| 290 |
} |
| 291 |
|
| 292 |
switch ($selection) { |
| 293 |
case C::WC_PS_TYPE_ALL: |
| 294 |
break; |
| 295 |
case C::WC_PS_TYPE_INCLUDE_PRODUCTS: |
| 296 |
case C::WC_PS_TYPE_EXCLUDE_PRODUCTS: |
| 297 |
$args = [ |
| 298 |
'post_type' => 'product', |
| 299 |
'post__in' => $ids, |
| 300 |
]; |
| 301 |
$products = get_posts($args); |
| 302 |
foreach ($products as $product) { |
| 303 |
$result[] = [ |
| 304 |
'id' => $product->ID, |
| 305 |
'text' => $product->post_title, |
| 306 |
]; |
| 307 |
} |
| 308 |
break; |
| 309 |
case C::WC_PS_TYPE_INCLUDE_CATEGORIES: |
| 310 |
case C::WC_PS_TYPE_EXCLUDE_CATEGORIES: |
| 311 |
$ids = array_map('intval', $ids); |
| 312 |
$args = [ |
| 313 |
'taxonomy' => 'product_cat', |
| 314 |
'term_ids' => $ids, |
| 315 |
'hide_empty' => false, |
| 316 |
]; |
| 317 |
|
| 318 |
$categories = get_terms($args); |
| 319 |
|
| 320 |
$categories = array_filter($categories, function ( |
| 321 |
$_category |
| 322 |
) use ($ids) { |
| 323 |
return in_array($_category->term_id, $ids); |
| 324 |
}); |
| 325 |
foreach ($categories as $category) { |
| 326 |
$result[] = [ |
| 327 |
'id' => $category->term_id, |
| 328 |
'text' => $category->name, |
| 329 |
]; |
| 330 |
} |
| 331 |
break; |
| 332 |
default: |
| 333 |
break; |
| 334 |
} |
| 335 |
|
| 336 |
return $result; |
| 337 |
} |
| 338 |
} |
| 339 |
|