PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / trunk
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce vtrunk
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 trunk, at admin/CampaignSettings.php

397 lines 11.6 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 // Check for CSRF protection
212 if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'update-post_' . $post_id)) {
213 return;
214 }
215
216 if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
217 || (!isset($_POST['post_ID']) || $post_id != $_POST['post_ID'])
218 || (!current_user_can('publish_posts') || !current_user_can('edit_post', $post_id))
219 ) {
220 return;
221 }
222 // var_dump($_POST);exit;
223 $countdown = new Campaign($post_id);
224 $countdown->storeSettings(wp_unslash($_POST));
225 }
226
227 public function add_settings_metabox_template( $post_type )
228 {
229 global $post_id;
230 if ( $post_type != HURRYT_POST_TYPE ) {
231 return;
232 }
233 remove_meta_box( 'wpseo_meta', HURRYT_POST_TYPE, 'normal' );
234 remove_meta_box( 'slugdiv', HURRYT_POST_TYPE, 'normal' );
235 remove_meta_box( 'eg-meta-box', HURRYT_POST_TYPE, 'normal' );
236
237 add_meta_box(
238 'hurrytimer-settings',
239 __( 'Settings <a href="#" class="hurryt-fullscreen hidden"></a>',
240 'hurrytimer' ),
241 array( $this, 'settingsMetaboxTemplate' ),
242 $post_type,
243 'normal',
244 'high'
245 );
246
247 if ( $post_id ) {
248 add_meta_box(
249 'hrytmr-countdown__shortcode',
250 __( 'Shortcode', 'hurrytimer' ),
251 array( $this, 'shortcode_metabox_template' ),
252 $post_type,
253 'side',
254 'high'
255 );
256 }
257 }
258
259 public function shortcode_metabox_template()
260 {
261 global $post_id;
262 $shortcode = "[hurrytimer id='{$post_id}']";
263 include HURRYT_DIR . '/admin/templates/campaign-shortcode.php';
264
265 }
266
267 /**
268 * Campaign settings template.
269 *
270 * @return void
271 */
272 public function settingsMetaboxTemplate()
273 {
274 global $post_id;
275 $campaign = new Campaign( $post_id );
276 $campaign->loadSettings();
277 $resetCampaignAllVisitorsUrl = $this->createResetEvergreenCampaign( 'all' );
278 $resetCampaignCurrentAdminUrl = $this->createResetEvergreenCampaign( 'admin' );
279 $products = $this->getWcProductsNames( $campaign->wcProductsSelection,
280 $campaign->wcProductsSelectionType );
281 include HURRYT_DIR . '/admin/templates/campaign-settings.php';
282
283 }
284
285 public function createResetEvergreenCampaign( $scope = 'admin' )
286 {
287 global $post_id;
288
289 $action = 'reset-evergreen-compaign';
290
291 return wp_nonce_url(
292 add_query_arg(
293 array(
294 'hurryt-action' => $action,
295 'hurryt-scope' => $scope,
296 'postid' => $post_id,
297 'post' => $post_id,
298 'action' => 'edit',
299 ),
300 admin_url('post.php')
301 ),
302 $action
303 );
304 }
305
306 /**
307 * @param array $ids
308 * @param $selection
309 *
310 * @return array
311 */
312 public function getWcProductsNames( $ids, $selection )
313 {
314 $result = [];
315 if ( empty( $ids ) ) {
316 return $result;
317 }
318
319 switch ( $selection ) {
320 case C::WC_PS_TYPE_ALL:
321 break;
322 case C::WC_PS_TYPE_INCLUDE_PRODUCTS:
323 case C::WC_PS_TYPE_EXCLUDE_PRODUCTS:
324 $args = [
325 'post_type' => 'product',
326 'post__in' => $ids,
327 'post_status' => 'any',
328 'posts_per_page' => -1
329 ];
330 $products = get_posts( $args );
331 foreach ( $products as $product ) {
332 $result[] = [
333 'id' => $product->ID,
334 'text' => $product->post_title,
335 ];
336 }
337 break;
338 case C::WC_PS_TYPE_INCLUDE_CATEGORIES:
339 case C::WC_PS_TYPE_EXCLUDE_CATEGORIES:
340 $ids = array_map( 'intval', $ids );
341 $args = [
342 'taxonomy' => 'product_cat',
343 'term_ids' => $ids,
344 'hide_empty' => false,
345 ];
346
347 $categories = get_terms( $args );
348
349 $categories = array_filter( $categories, function (
350 $_category
351 ) use ( $ids ) {
352 return in_array( $_category->term_id, $ids );
353 } );
354 foreach ( $categories as $category ) {
355 $result[] = [
356 'id' => $category->term_id,
357 'text' => $category->name,
358 ];
359 }
360 break;
361 default:
362 break;
363 }
364
365 return $result;
366 }
367
368 public function handle_duplicate_campaign()
369 {
370 if (isset($_POST['duplicate_campaign']) && $_POST['duplicate_campaign'] == '1') {
371 $post_id = get_the_ID();
372 $post = get_post($post_id);
373 if ($post && $post->post_type === HURRYT_POST_TYPE) {
374 $new_post_id = wp_insert_post([
375 'post_title' => $post->post_title . ' (Copy)',
376 'post_type' => $post->post_type,
377 'post_status' => 'draft',
378 ]);
379
380 if ($new_post_id) {
381 // Copy post meta
382 $meta = get_post_meta($post_id);
383 foreach ($meta as $key => $values) {
384 foreach ($values as $value) {
385 add_post_meta($new_post_id, $key, maybe_unserialize($value));
386 }
387 }
388 }
389 }
390 // Redirect to the new post edit page
391 wp_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
392 exit;
393 }
394 }
395
396 }
397