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

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