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

355 lines 9.9 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
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(
111 __( 'M j, Y @ G:i', "hurrytimer" ),
112 strtotime( $post->post_date )
113 )
114 ),
115 10 => __( 'Campaign draft updated.', "hurrytimer" ),
116 );
117
118 return $messages;
119 }
120
121 /**
122 * If time is edited, reset compaign.
123 *
124 * @param int $meta_id
125 * @param int $object_id
126 * @param string $meta_key
127 * @param string $meta_value
128 *
129 * @return void
130 */
131 public function maybe_reset_running_countdown(
132 $meta_id,
133 $object_id,
134 $meta_key,
135 $meta_value
136 ) {
137 if ( !in_array( $meta_key, [ 'duration' ] ) ) {
138 return;
139 }
140
141 // (new EvergreenCampaign($object_id))->reset();
142 }
143
144 public function maybe_delete_running_countdown( $post_id )
145 {
146 //Evergreen_Countdown::reset($post_id);
147 }
148
149 /**
150 * Edit title placeholder.
151 *
152 * @param string $title
153 *
154 * @return string
155 */
156 public function change_countdown_title( $title )
157 {
158 global $post;
159 if ( $post && $post->post_type === HURRYT_POST_TYPE ) {
160 return __(
161 'Campaign name (optional)',
162 "hurrytimer"
163 );
164 }
165
166 return $title;
167 }
168
169 /**
170 * Custom publish metabox.
171 *
172 * @return void
173 */
174 public function post_publish_metabox()
175 {
176 global $post_id, $post;
177 if ( $post->post_type !== HURRYT_POST_TYPE ) {
178 return;
179 }
180
181 if ( Utils\Helpers::isNewPost( $post_id ) ) {
182 return;
183 }
184 $isActive = Utils\Helpers::isPostActive( $post_id );
185 $publich_date = Utils\Helpers::format_date( $post->post_date );
186 $deactivateUrl = Utils\Helpers::deactivateUrl( $post_id );
187 $activateUrl = Utils\Helpers::activateUrl( $post_id );
188
189 include HURRYT_DIR . '/admin/templates/post-publish-metabox.php';
190
191 }
192
193 /**
194 * Save compaign settings
195 *
196 * @param int $post_id
197 *
198 * @return void
199 */
200
201 public function save_settings( $post_id )
202 {
203 if ( wp_is_post_revision( $post_id ) ) {
204 return;
205 }
206 if ( !current_user_can( 'edit_post', $post_id ) ) {
207 return;
208 }
209 $countdown = new Campaign( $post_id );
210 $countdown->storeSettings( $_POST );
211 }
212
213 public function add_settings_metabox_template( $post_type )
214 {
215 global $post_id;
216 if ( $post_type != HURRYT_POST_TYPE ) {
217 return;
218 }
219 remove_meta_box( 'wpseo_meta', HURRYT_POST_TYPE, 'normal' );
220 remove_meta_box( 'slugdiv', HURRYT_POST_TYPE, 'normal' );
221 remove_meta_box( 'eg-meta-box', HURRYT_POST_TYPE, 'normal' );
222
223 add_meta_box(
224 'hurrytimer-settings',
225 __( 'Settings <a href="#" class="hurryt-fullscreen hidden"></a>',
226 'hurrytimer' ),
227 array( $this, 'settingsMetaboxTemplate' ),
228 $post_type,
229 'normal',
230 'high'
231 );
232
233 if ( $post_id ) {
234 add_meta_box(
235 'hrytmr-countdown__shortcode',
236 __( 'Shortcode', 'hurrytimer' ),
237 array( $this, 'shortcode_metabox_template' ),
238 $post_type,
239 'side',
240 'high'
241 );
242 }
243 }
244
245 public function shortcode_metabox_template()
246 {
247 global $post_id;
248 $shortcode = "[hurrytimer id='{$post_id}']";
249 include HURRYT_DIR . '/admin/templates/campaign-shortcode.php';
250
251 }
252
253 /**
254 * Campaign settings template.
255 *
256 * @return void
257 */
258 public function settingsMetaboxTemplate()
259 {
260 global $post_id;
261 $campaign = new Campaign( $post_id );
262 $campaign->loadSettings();
263 $resetCampaignAllVisitorsUrl = $this->createResetEvergreenCampaign( 'all' );
264 $resetCampaignCurrentAdminUrl = $this->createResetEvergreenCampaign( 'admin' );
265 $products = $this->getWcProductsNames( $campaign->wcProductsSelection,
266 $campaign->wcProductsSelectionType );
267 include HURRYT_DIR . '/admin/templates/campaign-settings.php';
268
269 }
270
271 public function createResetEvergreenCampaign( $scope = 'admin' )
272 {
273 global $post_id;
274
275 $action = 'reset-evergreen-compaign';
276
277 return wp_nonce_url(
278 add_query_arg(
279 array(
280 'hurryt-action' => $action,
281 'hurryt-scope' => $scope,
282 'postid' => $post_id,
283 'post' => $post_id,
284 'action' => 'edit',
285 ),
286 admin_url( 'post.php' )
287 ),
288 $action
289 );
290 }
291
292 /**
293 * @param array $ids
294 * @param $selection
295 *
296 * @return array
297 */
298 public function getWcProductsNames( $ids, $selection )
299 {
300 $result = [];
301 if ( empty( $ids ) ) {
302 return $result;
303 }
304
305 switch ( $selection ) {
306 case C::WC_PS_TYPE_ALL:
307 break;
308 case C::WC_PS_TYPE_INCLUDE_PRODUCTS:
309 case C::WC_PS_TYPE_EXCLUDE_PRODUCTS:
310 $args = [
311 'post_type' => 'product',
312 'post__in' => $ids,
313 'post_status' => 'any',
314 'posts_per_page' => -1
315 ];
316 $products = get_posts( $args );
317 foreach ( $products as $product ) {
318 $result[] = [
319 'id' => $product->ID,
320 'text' => $product->post_title,
321 ];
322 }
323 break;
324 case C::WC_PS_TYPE_INCLUDE_CATEGORIES:
325 case C::WC_PS_TYPE_EXCLUDE_CATEGORIES:
326 $ids = array_map( 'intval', $ids );
327 $args = [
328 'taxonomy' => 'product_cat',
329 'term_ids' => $ids,
330 'hide_empty' => false,
331 ];
332
333 $categories = get_terms( $args );
334
335 $categories = array_filter( $categories, function (
336 $_category
337 ) use ( $ids ) {
338 return in_array( $_category->term_id, $ids );
339 } );
340 foreach ( $categories as $category ) {
341 $result[] = [
342 'id' => $category->term_id,
343 'text' => $category->name,
344 ];
345 }
346 break;
347 default:
348 break;
349 }
350
351 return $result;
352 }
353
354 }
355