PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.11.1
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.11.1
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / admin / CampaignSettings.php

CampaignSettings.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.11.1, at admin/CampaignSettings.php

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