Skip to content

Commit 72e4365

Browse files
Release/1.0.0 (#5)
* Add support for ACF * Setup basic trigger settings * Setup appearance settings * Add appearance fields * Remove var_dump * Setup actions fields * Add Trigger fields * Setup display fields * Fix admin load bug * Remove cookie param * Setup state functionality * Only display popups the user hasn't responded to * Update storage on accept * Update storage on decline * Fix bad autofill var * Remove legacy cookie functionality * build assets * Remove popup on action * Enqueue Minified Assets (#7) * Add env helper * Setup env specific enqueues * Build assets (#8) * Remove popup on action (#9) * Remove popup on action * Build assets * Add composer.json (#12) * Feature/composer (#13) * Add composer.json * Update composer
1 parent 164e0f4 commit 72e4365

File tree

23 files changed

+971
-20073
lines changed

23 files changed

+971
-20073
lines changed

classes/Controllers/Client/Enqueue.php

Lines changed: 82 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Poppy\Controllers\Client;
44

5+
use Poppy\Utils\Helpers;
6+
57
class Enqueue
68
{
79
private static $class = null;
@@ -19,7 +21,7 @@ public static function enqueue_scripts()
1921

2022
wp_register_script(
2123
'poppy',
22-
\Poppy\URI . $path,
24+
Helpers::env_check($path),
2325
null,
2426
false,
2527
true
@@ -38,77 +40,118 @@ public static function enqueue_scripts()
3840

3941
public static function enqueue_styles()
4042
{
43+
if (is_admin()) {
44+
return false;
45+
}
46+
4147
$path = 'dist/styles/poppy.css';
4248

4349
wp_enqueue_style(
4450
'poppy',
45-
\Poppy\URI . $path
51+
Helpers::env_check($path)
4652
);
4753
}
4854

49-
private function get_popup_data($popup) {
50-
$meta = get_post_meta($popup->ID);
51-
// var_dump($meta);
55+
private function get_popup_data($popup)
56+
{
57+
$fields = get_fields($popup->ID);
5258

5359
return [
5460
'title' => $popup->post_title,
5561
'slug' => $popup->post_name,
5662

5763
// Appearance
58-
'alignment' => array_key_exists('alignment', $meta) ? $meta['alignment'] : 'center',
59-
'position' => array_key_exists('position', $meta) ? $meta['position'] : 'center',
60-
'size' => array_key_exists('size', $meta) ? $meta['size'] : 'narrow',
61-
'docked' => array_key_exists('alignment', $meta) ? $meta['docked'] : false,
62-
'peek' => array_key_exists('peek', $meta) ? $meta['peek'] : true,
63-
'peek_message' => array_key_exists('peek_message', $meta) ? $meta['peek_message'] : 'Hola',
64+
'alignment' => array_key_exists('alignment', $fields) ? $fields['alignment'] : 'center',
65+
'position' => array_key_exists('position', $fields) ? $fields['position'] : 'center',
66+
'size' => array_key_exists('size', $fields) ? $fields['size'] : 'narrow',
67+
'docked' => array_key_exists('alignment', $fields) ? $fields['docked'] : false,
68+
'peek' => array_key_exists('peek', $fields) ? $fields['peek'] : true,
69+
'peek_message' => array_key_exists('peek_message', $fields) ? $fields['peek_message'] : '',
6470

6571
// Actions
66-
'actions' => [
67-
[
68-
'label' => 'More',
69-
'action' => 'more',
70-
'url' => 'https://www.google.com',
71-
'target' => '_blank',
72-
],
73-
[
74-
'label' => 'Decline',
75-
'action' => 'decline'
76-
],
77-
[
78-
'label' => 'Accept',
79-
'action' => 'accept'
80-
]
81-
],
72+
'actions' => self::get_actions($fields),
8273

8374
// Trigger
84-
'trigger' => [
85-
'type' => 'load',
86-
],
87-
88-
// Rewrite using slug
89-
'cookie' => [
90-
'name' => 'testing-cookie',
91-
'expires' => '',
92-
],
75+
'trigger' => self::get_trigger($fields),
9376

9477
// content
9578
'content' => apply_filters('the_content', $popup->post_content)
9679
];
9780
}
9881

99-
private function get_popups() {
82+
private function get_popups()
83+
{
84+
global $post;
85+
10086
$popups_query_args = [
10187
'post_type' => 'poppy',
10288
'post_status' => 'publish',
89+
'meta_query' => [
90+
'relation' => 'OR',
91+
[
92+
'key' => 'pages',
93+
'value' => '"' . $post->ID . '"',
94+
'compare' => 'LIKE'
95+
],
96+
[
97+
'key' => 'all_pages',
98+
'value' => '1',
99+
]
100+
]
103101
];
104102
$popups_query = new \WP_Query($popups_query_args);
105-
$_popups = array_map(
103+
$popups = array_map(
106104
[self::$class, 'get_popup_data'],
107105
$popups_query->posts
108106
);
109107

110108
return [
111-
'popups' => $_popups,
109+
'popups' => $popups,
110+
];
111+
}
112+
113+
private function get_actions($fields)
114+
{
115+
$actions = [
116+
[
117+
'label' => array_key_exists('more_link', $fields) && $fields['more_link'] !== '' ? $fields['more_link']['title'] : 'More',
118+
'action' => 'more',
119+
'url' => array_key_exists('url', $fields) && $fields['more_link'] !== '' ? $fields['more_link']['url'] : '#',
120+
'target' => array_key_exists('target', $fields) && $fields['more_link'] !== '' ? $fields['more_link']['target'] : '_blank',
121+
],
122+
[
123+
'label' => array_key_exists('decline_label', $fields) && $fields['decline_label'] !== '' ? $fields['decline_label'] : 'Decline',
124+
'action' => 'decline'
125+
],
126+
[
127+
'label' => array_key_exists('accept_label', $fields) && $fields['accept_label']!== '' ? $fields['accept_label'] : 'Accept',
128+
'action' => 'accept'
129+
]
112130
];
131+
132+
return array_values(array_filter(
133+
$actions,
134+
function ($action) use ($fields) {
135+
return array_key_exists($action['action'], $fields) && $fields[$action['action']];
136+
}
137+
));
138+
}
139+
140+
private function get_trigger($fields) {
141+
$trigger = [
142+
'type' => array_key_exists('trigger_type', $fields) ? $fields['trigger_type'] : 'load',
143+
];
144+
145+
if (array_key_exists("trigger_scroll", $fields)) {
146+
$trigger = array_merge(
147+
$trigger,
148+
[
149+
'measurement' => $fields['trigger_scroll']['measurement'],
150+
'value' => $fields['trigger_scroll']['value_' . $fields['trigger_scroll']['measurement']],
151+
]
152+
);
153+
}
154+
155+
return $trigger;
113156
}
114157
}

classes/Models/Settings/Actions.php

Lines changed: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,93 @@
22

33
namespace Poppy\Models\Settings;
44

5-
use Poppy\Utils\Views;
5+
use Poppy\Utils\Fields;
6+
use Poppy\Models\PostTypes;
67

7-
class Actions
8+
class Actions extends RegisterSettings
89
{
9-
public static function init()
10-
{
11-
$class = new self;
12-
add_action('add_meta_boxes', [$class, 'register'], 10);
13-
add_action('save_post', [$class, 'save']);
14-
}
10+
const SLUG = 'actions';
11+
const LABEL = 'Actions';
1512

16-
public static function register()
17-
{
18-
$class = new self;
19-
\add_meta_box(
20-
'poppy_actions',
21-
'Actions',
22-
[$class, 'render'],
23-
'poppy',
24-
'side',
25-
'high'
26-
);
27-
}
13+
public static function init()
14+
{
15+
$acf = new Fields();
16+
$fields = [
17+
$acf->add('boolean', [
18+
'label' => 'More',
19+
'slug' => 'more',
20+
'instructions' => 'Display a link to another page with more information',
21+
]),
22+
$acf->add('link', [
23+
'label' => 'More Link',
24+
'slug' => 'more_link',
25+
'conditional_logic' => [
26+
[
27+
[
28+
'field' => 'more',
29+
'operator' => '==',
30+
'value' => '1'
31+
]
32+
]
33+
]
34+
]),
35+
$acf->add('boolean', [
36+
'label' => 'Decline',
37+
'slug' => 'decline',
38+
'instructions' => 'Allow user to decline your popup',
39+
]),
40+
$acf->add('text', [
41+
'label' => 'Decline Label',
42+
'slug' => 'decline_label',
43+
'placeholder' => 'Decline',
44+
'conditional_logic' => [
45+
[
46+
[
47+
'field' => 'decline',
48+
'operator' => '==',
49+
'value' => '1'
50+
]
51+
]
52+
]
53+
]),
54+
$acf->add('boolean', [
55+
'label' => 'Accept',
56+
'slug' => 'accept',
57+
'instructions' => 'Allow user to accept your popup',
58+
]),
59+
$acf->add('text', [
60+
'label' => 'Accept Label',
61+
'slug' => 'accept_label',
62+
'placeholder' => 'Accept',
63+
'conditional_logic' => [
64+
[
65+
[
66+
'field' => 'accept',
67+
'operator' => '==',
68+
'value' => '1'
69+
]
70+
]
71+
]
72+
]),
73+
];
2874

29-
public static function render() {
30-
global $post;
31-
$meta = get_post_meta($post->ID);
32-
$fields = [
33-
'accept_display' => true,
34-
'decline_display' => true,
35-
'more_display' => false,
36-
];
37-
38-
echo Views::get_view('Settings/Actions', $fields);
39-
}
40-
41-
public static function save($post_id) {
42-
// var_dump($_POST['poppy']);
43-
// die;
44-
}
75+
$location = [
76+
[
77+
[
78+
'param' => 'post_type',
79+
'operator' => '==',
80+
'value' => PostTypes\Poppy::SLUG,
81+
],
82+
],
83+
];
84+
85+
parent::register([
86+
'slug' => PostTypes\Poppy::SLUG . '__' . self::SLUG,
87+
'label_placement' => 'top',
88+
'name' => __(self::LABEL, 'core'),
89+
'fields' => $fields,
90+
'position' => 'side',
91+
'location' => $location,
92+
]);
93+
}
4594
}

0 commit comments

Comments
 (0)