| 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_action( "updated_post_meta", [ $this, 'maybe_reset_running_countdown' ], 10, 4 ); |
| 51 |
add_action( 'edit_form_before_permalink', [ $this, 'show_headline_moved_notice' ] ); |
| 52 |
} |
| 53 |
|
| 54 |
function show_headline_moved_notice( $post ) |
| 55 |
{ |
| 56 |
if ( !$post || $post->post_type !== HURRYT_POST_TYPE ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
if( get_option('hurryt_headline_moved_notice_dismissed') ){ |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
if(isset($_COOKIE['hurryt_headline_moved_notice_dismissed'])){ |
| 65 |
return; |
| 66 |
} |
| 67 |
if ( !filter_var( apply_filters( 'hurryt_show_headline_moved_notice', true ), FILTER_VALIDATE_BOOLEAN ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if ( !Installer::get_instance()->has_upgraded_from_2_2_28_or_prior() ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
?> |
| 76 |
<p class="description" style="margin-top:5px;font-style:normal; background:white; border: 1px solid #ccd0d4;;padding:10px;border-left: 1px solid #00a0d2;border-left-width: 4px;box-shadow: 0 1px 1px rgb(0 0 0 / 4%);"> |
| 77 |
The headline can now be edited under <a href="#" class="hurryt-open-hl-tab">Appearance → Elements → Headline</a>. Use the input box above to add the name of the campaign instead. |
| 78 |
<br> |
| 79 |
<button type="button" class="button-primary" style="margin-top:5px;" id="hurryt-dismiss-headline-moved-notice">OK, got it</button> |
| 80 |
</p> |
| 81 |
<?php |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Edit notices messages. |
| 87 |
* |
| 88 |
* @param array $messages |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public function custom_updated_messages( $messages ) |
| 93 |
{ |
| 94 |
global $post; |
| 95 |
$messages[ HURRYT_POST_TYPE ] = array( |
| 96 |
0 => '', |
| 97 |
1 => __( 'Campaign updated.', 'hurrytimer' ), |
| 98 |
2 => '', |
| 99 |
3 => '', |
| 100 |
4 => __( 'Campaign updated.', 'hurrytimer' ), |
| 101 |
5 => '', |
| 102 |
6 => __( 'Campaign published.', 'hurrytimer' ), |
| 103 |
7 => __( 'Campaign saved.', 'hurrytimer' ), |
| 104 |
8 => __( 'Campaign submitted.', 'hurrytimer' ), |
| 105 |
9 => sprintf( |
| 106 |
__( |
| 107 |
'Campaign scheduled for: <strong>%1$s</strong>.', |
| 108 |
'hurrytimer' |
| 109 |
), |
| 110 |
date_i18n(__( 'M j, Y @ G:i', 'hurrytimer' ),strtotime( $post->post_date )) |
| 111 |
), |
| 112 |
10 => __( 'Campaign draft updated.', 'hurrytimer' ), |
| 113 |
); |
| 114 |
|
| 115 |
return $messages; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* If time is edited, reset compaign. |
| 120 |
* |
| 121 |
* @param int $meta_id |
| 122 |
* @param int $object_id |
| 123 |
* @param string $meta_key |
| 124 |
* @param string $meta_value |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
public function maybe_reset_running_countdown( |
| 129 |
$meta_id, |
| 130 |
$object_id, |
| 131 |
$meta_key, |
| 132 |
$meta_value |
| 133 |
) { |
| 134 |
if ( !in_array( $meta_key, [ 'duration' ] ) ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
// (new EvergreenCampaign($object_id))->reset(); |
| 139 |
} |
| 140 |
|
| 141 |
public function maybe_delete_running_countdown( $post_id ) |
| 142 |
{ |
| 143 |
//Evergreen_Countdown::reset($post_id); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Edit title placeholder. |
| 148 |
* |
| 149 |
* @param string $title |
| 150 |
* |
| 151 |
* @return string |
| 152 |
*/ |
| 153 |
public function change_countdown_title( $title ) |
| 154 |
{ |
| 155 |
global $post; |
| 156 |
if ( $post && $post->post_type === HURRYT_POST_TYPE ) { |
| 157 |
return __( |
| 158 |
'Campaign name (optional)', |
| 159 |
"hurrytimer" |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
return $title; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Custom publish metabox. |
| 168 |
* |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public function post_publish_metabox() |
| 172 |
{ |
| 173 |
global $post_id, $post; |
| 174 |
if ( $post->post_type !== HURRYT_POST_TYPE ) { |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
if ( Utils\Helpers::isNewPost( $post_id ) ) { |
| 179 |
return; |
| 180 |
} |
| 181 |
$isActive = Utils\Helpers::isPostActive( $post_id ); |
| 182 |
$publich_date = Utils\Helpers::format_date( $post->post_date ); |
| 183 |
$deactivateUrl = Utils\Helpers::deactivateUrl( $post_id ); |
| 184 |
$activateUrl = Utils\Helpers::activateUrl( $post_id ); |
| 185 |
|
| 186 |
include HURRYT_DIR . '/admin/templates/post-publish-metabox.php'; |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Save compaign settings |
| 192 |
* |
| 193 |
* @param int $post_id |
| 194 |
* |
| 195 |
* @return void |
| 196 |
*/ |
| 197 |
|
| 198 |
public function save_settings( $post_id ) |
| 199 |
{ |
| 200 |
if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) |
| 201 |
|| (!isset($_POST['post_ID']) || $post_id != $_POST['post_ID']) |
| 202 |
|| (!current_user_can('edit_post', $post_id)) |
| 203 |
|
| 204 |
) { |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
$countdown = new Campaign( $post_id ); |
| 209 |
$countdown->storeSettings( $_POST ); |
| 210 |
} |
| 211 |
|
| 212 |
public function add_settings_metabox_template( $post_type ) |
| 213 |
{ |
| 214 |
global $post_id; |
| 215 |
if ( $post_type != HURRYT_POST_TYPE ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
remove_meta_box( 'wpseo_meta', HURRYT_POST_TYPE, 'normal' ); |
| 219 |
remove_meta_box( 'slugdiv', HURRYT_POST_TYPE, 'normal' ); |
| 220 |
remove_meta_box( 'eg-meta-box', HURRYT_POST_TYPE, 'normal' ); |
| 221 |
|
| 222 |
add_meta_box( |
| 223 |
'hurrytimer-settings', |
| 224 |
__( 'Settings <a href="#" class="hurryt-fullscreen hidden"></a>', |
| 225 |
'hurrytimer' ), |
| 226 |
array( $this, 'settingsMetaboxTemplate' ), |
| 227 |
$post_type, |
| 228 |
'normal', |
| 229 |
'high' |
| 230 |
); |
| 231 |
|
| 232 |
if ( $post_id ) { |
| 233 |
add_meta_box( |
| 234 |
'hrytmr-countdown__shortcode', |
| 235 |
__( 'Shortcode', 'hurrytimer' ), |
| 236 |
array( $this, 'shortcode_metabox_template' ), |
| 237 |
$post_type, |
| 238 |
'side', |
| 239 |
'high' |
| 240 |
); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public function shortcode_metabox_template() |
| 245 |
{ |
| 246 |
global $post_id; |
| 247 |
$shortcode = "[hurrytimer id='{$post_id}']"; |
| 248 |
include HURRYT_DIR . '/admin/templates/campaign-shortcode.php'; |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Campaign settings template. |
| 254 |
* |
| 255 |
* @return void |
| 256 |
*/ |
| 257 |
public function settingsMetaboxTemplate() |
| 258 |
{ |
| 259 |
global $post_id; |
| 260 |
$campaign = new Campaign( $post_id ); |
| 261 |
$campaign->loadSettings(); |
| 262 |
$resetCampaignAllVisitorsUrl = $this->createResetEvergreenCampaign( 'all' ); |
| 263 |
$resetCampaignCurrentAdminUrl = $this->createResetEvergreenCampaign( 'admin' ); |
| 264 |
$products = $this->getWcProductsNames( $campaign->wcProductsSelection, |
| 265 |
$campaign->wcProductsSelectionType ); |
| 266 |
include HURRYT_DIR . '/admin/templates/campaign-settings.php'; |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
public function createResetEvergreenCampaign( $scope = 'admin' ) |
| 271 |
{ |
| 272 |
global $post_id; |
| 273 |
|
| 274 |
$action = 'reset-evergreen-compaign'; |
| 275 |
|
| 276 |
return wp_nonce_url( |
| 277 |
add_query_arg( |
| 278 |
array( |
| 279 |
'hurryt-action' => $action, |
| 280 |
'hurryt-scope' => $scope, |
| 281 |
'postid' => $post_id, |
| 282 |
'post' => $post_id, |
| 283 |
'action' => 'edit', |
| 284 |
), |
| 285 |
admin_url( 'post.php' ) |
| 286 |
), |
| 287 |
$action |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* @param array $ids |
| 293 |
* @param $selection |
| 294 |
* |
| 295 |
* @return array |
| 296 |
*/ |
| 297 |
public function getWcProductsNames( $ids, $selection ) |
| 298 |
{ |
| 299 |
$result = []; |
| 300 |
if ( empty( $ids ) ) { |
| 301 |
return $result; |
| 302 |
} |
| 303 |
|
| 304 |
switch ( $selection ) { |
| 305 |
case C::WC_PS_TYPE_ALL: |
| 306 |
break; |
| 307 |
case C::WC_PS_TYPE_INCLUDE_PRODUCTS: |
| 308 |
case C::WC_PS_TYPE_EXCLUDE_PRODUCTS: |
| 309 |
$args = [ |
| 310 |
'post_type' => 'product', |
| 311 |
'post__in' => $ids, |
| 312 |
'post_status' => 'any', |
| 313 |
'posts_per_page' => -1 |
| 314 |
]; |
| 315 |
$products = get_posts( $args ); |
| 316 |
foreach ( $products as $product ) { |
| 317 |
$result[] = [ |
| 318 |
'id' => $product->ID, |
| 319 |
'text' => $product->post_title, |
| 320 |
]; |
| 321 |
} |
| 322 |
break; |
| 323 |
case C::WC_PS_TYPE_INCLUDE_CATEGORIES: |
| 324 |
case C::WC_PS_TYPE_EXCLUDE_CATEGORIES: |
| 325 |
$ids = array_map( 'intval', $ids ); |
| 326 |
$args = [ |
| 327 |
'taxonomy' => 'product_cat', |
| 328 |
'term_ids' => $ids, |
| 329 |
'hide_empty' => false, |
| 330 |
]; |
| 331 |
|
| 332 |
$categories = get_terms( $args ); |
| 333 |
|
| 334 |
$categories = array_filter( $categories, function ( |
| 335 |
$_category |
| 336 |
) use ( $ids ) { |
| 337 |
return in_array( $_category->term_id, $ids ); |
| 338 |
} ); |
| 339 |
foreach ( $categories as $category ) { |
| 340 |
$result[] = [ |
| 341 |
'id' => $category->term_id, |
| 342 |
'text' => $category->name, |
| 343 |
]; |
| 344 |
} |
| 345 |
break; |
| 346 |
default: |
| 347 |
break; |
| 348 |
} |
| 349 |
|
| 350 |
return $result; |
| 351 |
} |
| 352 |
|
| 353 |
} |
| 354 |
|