-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.christmas-tree-touchpoints.php
550 lines (460 loc) · 14.9 KB
/
class.christmas-tree-touchpoints.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
/**
* Custom Campaigns Class file
*
* @package CustomCampaign
* @author Adam Onishi <[email protected]>
* @license GPL2
* @copyright 2014 Adam Onishi
*/
class WaaChristmasTouchpoints
{
protected static $version = '0.0.1';
protected static $plugin_slug = 'christmas-tree-touchpoints';
protected static $instance = null;
private function __construct()
{
// Setup post type & custom fields
add_action( 'init', array( $this, 'setupPostType') );
add_action( 'init', array( $this, 'setupCustomFields') );
// Include Stylesheet/scripts
add_action( 'wp_enqueue_scripts', array( $this, 'frontendScriptsStyles' ), 12 );
// Add actions for ajax content
// All content for tree
add_action('wp_ajax_waa_get_tp_content', array( $this, 'getAllContent' ) );
add_action('wp_ajax_nopriv_waa_get_tp_content', array( $this, 'getAllContent' ) );
// Paginated content
add_action('wp_ajax_waa_pagination_content', array( $this, 'getPagedContent' ) );
add_action('wp_ajax_nopriv_waa_pagination_content', array( $this, 'getPagedContent' ) );
}
public static function get_instance()
{
if( null === self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
public static function __callStatic($method, $args)
{
return call_user_func_array(array( static::get_instance(), $method ), $args);
}
/**
* Copy template into current theme
* @author Adam Onishi ([email protected])
*/
public function copyTemplate()
{
$template = plugin_dir_path( __FILE__ ) . '/templates/christmas-touchpoints.php';
$dest = get_stylesheet_directory() . '/templates/christmas-touchpoints.php';
if( ! file_exists( $dest ) ) {
copy( $template, $dest );
}
}
/**
* Remove template from current theme
* @author Adam Onishi ([email protected])
*/
public function removeTemplate()
{
$dest = get_stylesheet_directory() . '/templates/christmas-touchpoints.php';
$template_dir = get_stylesheet_directory() . '/templates';
if( file_exists( $dest ) ) {
unlink( $dest );
}
if ( $files = @scandir($template_dir) && (count($files) > 2) ) {
rmdir($template_dir);
}
}
/**
* Find or test the current template
* @author Adam Onishi ([email protected])
* @param string $name [template name to test against or false to return the name]
* @return boolean/string [true/false if is or not the template being used, or the name of the template if no name passed]
*/
private function isTemplate( $name = false )
{
global $post;
$template_file = get_post_meta($post->ID,'_wp_page_template',TRUE);
// check for a template type
if( ! $name ) {
return $template_file;
}
if ( $template_file === $name ) {
return true;
}
return false;
}
/**
* Enqueue scripts and styles for the front end campaign page
* @author Adam Onishi ([email protected])
*/
public function frontendScriptsStyles()
{
$file = dirname(__FILE__) . '/christmas-tree-touchpoints.php';
$plugin_url = plugin_dir_url($file);
// CSS for the front end
if( ! is_admin() && $this->isTemplate('templates/christmas-touchpoints.php') ) {
wp_enqueue_style( 'waa-xmas', $plugin_url . 'css/xmas.css', false, $this->version, 'screen' );
wp_enqueue_script( 'waa-tree-modernizr', $plugin_url . 'js/modernizr.js', false, $this->version, false );
wp_enqueue_script( 'waa-tree-script', $plugin_url . 'js/christmas-tree-touchpoints.js', array('jquery'), $this->version, true );
$ajaxData = array(
'url' => admin_url('admin-ajax.php' ),
'waaAjaxNonce' => wp_create_nonce( 'waa-ajax-nonce' ),
);
wp_localize_script( 'waa-tree-script', 'waaAjaxData', $ajaxData );
}
}
public function getAllContent()
{
// Get nonce
$nonce = $_POST['nonce'];
// Test nonce
if ( ! wp_verify_nonce( $nonce, 'waa-ajax-nonce' ) ) {
$response = json_encode( array(
'error' => true,
'message' => 'Nonce error!'
));
die( $response );
}
$filters = $_POST['filters'];
$remove = array();
$post_id = $_POST['post_id'];
foreach( $filters as $name => $value ) {
if( 'false' === $value ) {
$remove[] = $name;
}
}
if( ! is_user_logged_in() ) {
$remove[] = 'offer';
}
$content = array();
$touchpoints = new WP_Query('post_type=waa_xmas_touchpoints&posts_per_page=-1&order=ASC');
$posts = $touchpoints->posts;
$day = $touchpoints->found_posts;
foreach( $posts as $post ) {
$content[] = $this->getDayContent($post, $remove, $post_id, $day);
$day--;
}
// Set up and send the response
$response = json_encode(array(
'results' => true,
'data' => $content,
));
die( $response );
}
private function getDayContent($post, $remove, $post_id, $day)
{
$rows = get_field('waa_touchpoints', $post->ID);
$types = array();
$content = array();
$counter = 0;
if( is_array($rows) ) {
foreach( $rows as $row ) {
if( ! in_array( $row['waa_ctp_icon'], $remove ) ) {
$contentID = '-' . $day . '-' . $counter;
$types[] = $row['waa_ctp_icon'];
$content[] = array(
'image' => $row['waa_ctp_image'],
'content' => $row['waa_ctp_content'],
'title' => $row['waa_ctp_title'],
'link' => $row['waa_ctp_link'],
'type' => $row['waa_ctp_icon'],
'share' => $this->shareTools($post_id, $contentID, false),
);
$counter++;
}
}
}
return array(
'types' => $types,
'content' => $content,
'rows' => $rows,
);
}
public function getPagedContent()
{
// Get nonce
$nonce = $_POST['nonce'];
// Test nonce
if ( ! wp_verify_nonce( $nonce, 'waa-ajax-nonce' ) ) {
$response = json_encode( array(
'error' => true,
'message' => 'Nonce error!'
));
die( $response );
}
$post_id = $_POST['page_id'];
$paged = $_POST['paged'];
$per_page = $_POST['posts_per_page'];
$prev_pages = $paged - 1;
$moar = true;
$remove = array();
if( ! is_user_logged_in() ) {
$remove[] = 'offer';
}
$content = '';
$count = 0;
$touchpoints = new WP_Query('post_type=waa_xmas_touchpoints&posts_per_page=' . $per_page . '&paged=' . $paged);
$posts = $touchpoints->posts;
$found = $touchpoints->found_posts;
if( $paged > 0 ) {
$count = $found - ($per_page * $prev_pages);
} else {
$count = $found;
}
foreach( $posts as $post ) {
$content .= $this->buildPagedContent($post, $count, $remove, $post_id);
$count--;
}
if( $count < 1 ) {
$moar = false;
}
// Set up and send the response
$response = json_encode(array(
'results' => true,
'content' => $content,
'paged' => $paged,
'moar' => $moar,
'count' => $count,
));
die( $response );
}
private function buildPagedContent($post, $count, $remove, $post_id)
{
$rows = get_field('waa_touchpoints', $post->ID);
ob_start();
?>
<article class="advent-day">
<h2 class="advent-day__title" data-day-number="<?php echo $count; ?>"><?php echo $count; ?></h2>
<div class="advent-day__container">
<?php
if( is_array($rows) ) {
foreach( $rows as $row ) {
if( ! in_array( $row['waa_ctp_icon'], $remove ) ) {
?>
<section class="advent-day__item advent-day__item--<?php echo $row['waa_ctp_icon']; ?>">
<?php if( $row['waa_ctp_image'] ) { ?>
<img src="<?php echo $row['waa_ctp_image']; ?>" alt=" " class="advent-day__image">
<?php } ?>
<div class="advent-day__content">
<h3><i class="advent-day__icon advent-day__icon--<?php echo $row['waa_ctp_icon']; ?>"></i><?php echo $row['waa_ctp_title']; ?></h3>
<p><?php echo $row['waa_ctp_content']; ?></p>
<p><a href="<?php echo $row['waa_ctp_link']; ?>">Read more ›</a></p>
<?php $this->shareTools($post_id); ?>
</div>
</section>
<?php
}
}
}
?>
</div>
</article>
<?php
$content = ob_get_contents();
ob_end_clean();
return $content;
}
public function toggleSwitch($option)
{
?>
<div class="toggle-switch">
<input id="switch-<?php echo $option; ?>" type="checkbox" name="filter" value="<?php echo $option; ?>" class="toggle-switch__input" checked>
<label for="switch-<?php echo $option; ?>" class="toggle-switch__option">
<span class="toggle-switch__state--active">ON</span>
<i class="toggle-switch__icon"></i>
<span class="toggle-switch__state--inactive">OFF</span>
</label>
</div>
<?php
}
public function shareTools($post_id, $contentID = false, $echo = true)
{
$content_hash = '';
$url = get_the_permalink($post_id);
if( $contentID ) {
$url .= '#content' . $contentID;
}
// FACEBOOK & TWITTER
$share_tools = '<div class="advent-day__share">';
$share_tools .= '<a href="https://www.facebook.com/sharer/sharer.php?u=' . urlencode( $url ) . '" target="_blank" class="post-facebook-share"><i class="fa fa-facebook-square"></i><span> Share</span></a>';
$share_tools .= '<a href="https://twitter.com/share?url=' . urlencode( $url ) . '&text=' . urlencode( 'Every Little Helps #makechristmas' ) . '" target="_blank" class="post-twitter-share"><i class="fa fa fa-twitter"></i><span> Tweet</span></a>';
// YAM IT
if( is_user_logged_in() ) {
$share_tools .= "<a href=\"javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f= 'https://www.yammer.com/home/bookmarklet',l=d.location,e=encodeURIComponent,p='?bookmarklet_pop=1&v=1&u='+e(l.href)%20+'&t='+e(d.title.replace(/^ *| *$/g,''))%20+'&s='+e(s),u=f+p;a=function()%20{if%20(!window.open(u,'sharer','toolbar=0,status=0,resizeable=1,width=650,height=550'))l.href=f+p};if%20(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}void(0);\" class=\"yammer-send\"><img src=\"/wp-content/themes/ourtesco/images/common/yammer-icon.png\" alt=\"Yammer icon\"><span>Yam it!</span></a>";
}
$share_tools .= '</div>';
if( $echo ) {
echo $share_tools;
} else {
return $share_tools;
}
}
public function pagination($query)
{
$big = 999999999; // need an unlikely integer
$pagination = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages,
'prev_next' => True,
'prev_text' => __('‹'),
'next_text' => __('›'),
) );
if( !empty($pagination) ) {
?>
<div class="touchpoint-articles-pagination">
<div class="pagination btn-container waa-pagination">
<?php echo str_replace( 'page-numbers', 'btn', $pagination ); ?>
</div>
</div>
<?php
}
}
public function setupPostType()
{
// Help kids learn post type
$labels = array(
'name' => __('Day', 'dirtisgood'),
'singular_name' => __('Days', 'dirtisgood'),
'add_new' => __('Add New', 'dirtisgood'),
'add_new_item' => __('Add New Day', 'dirtisgood'),
'edit_item' => __('Edit Day', 'dirtisgood'),
'new_item' => __('New Day', 'dirtisgood'),
'all_items' => __('All Days', 'dirtisgood'),
'view_item' => __('View Days', 'dirtisgood'),
'search_items' => __('Search Days', 'dirtisgood'),
'not_found' => __('No days found', 'dirtisgood'),
'not_found_in_trash' => __('No days found in Trash', 'dirtisgood'),
'parent_item_colon' => '',
'menu_name' => __('Christmas Touchpoints', 'dirtisgood'),
);
$args = array(
'labels' => $labels,
'public' => true,
'rewrite' => array( 'slug' => 'touchpoints' ),
'capability_type' => 'post',
'has_archive' => false,
'exclude_from_search' => true,
'show_in_nav_menus' => false,
'hierarchical' => false,
'can_export' => true,
'publicly_queryable' => true,
'menu_position' => 2,
'menu_icon' => 'dashicons-star-filled',
'supports' => array( 'title', 'author', 'revisions', 'page-attributes' ),
);
register_post_type( 'waa_xmas_touchpoints', $args );
}
public function setupCustomFields()
{
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_xmas-touchpoints',
'title' => 'Xmas Touchpoints',
'fields' => array (
array (
'key' => 'field_5469d34db2fc6',
'label' => 'Touchpoints',
'name' => 'waa_touchpoints',
'type' => 'repeater',
'instructions' => 'Add examples of good customer feedback on a variety of social platforms (max 6 per day)',
'sub_fields' => array (
array (
'key' => 'field_5469d384b2fc7',
'label' => 'Title',
'name' => 'waa_ctp_title',
'type' => 'text',
'column_width' => '',
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'formatting' => 'html',
'maxlength' => '',
),
array (
'key' => 'field_5469d74a324ae',
'label' => 'Image',
'name' => 'waa_ctp_image',
'type' => 'image',
'instructions' => 'Should be 400x240px in size, optimised to about 70% jpg when exported from Photoshop.',
'column_width' => '',
'save_format' => 'url',
'preview_size' => 'thumbnail',
'library' => 'all',
),
array (
'key' => 'field_5469d407b2fc8',
'label' => 'Content',
'name' => 'waa_ctp_content',
'type' => 'textarea',
'column_width' => '',
'default_value' => '',
'placeholder' => '',
'maxlength' => '',
'rows' => '',
'formatting' => 'br',
),
array (
'key' => 'field_5469d421b2fc9',
'label' => 'Link',
'name' => 'waa_ctp_link',
'type' => 'text',
'column_width' => '',
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'formatting' => 'html',
'maxlength' => '',
),
array (
'key' => 'field_5469d459b2fca',
'label' => 'Icon',
'name' => 'waa_ctp_icon',
'type' => 'select',
'column_width' => '',
'choices' => array (
'offer' => 'Colleague offer',
'facebook' => 'Facebook',
'news' => 'News',
'twitter' => 'Twitter',
'yammer' => 'Yammer',
'youtube' => 'YouTube',
),
'default_value' => '',
'allow_null' => 0,
'multiple' => 0,
),
),
'row_min' => '',
'row_limit' => 6,
'layout' => 'row',
'button_label' => 'Add Touchpoint',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'waa_xmas_touchpoints',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'acf_after_title',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
}
}