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

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