1 | <?php |
---|
2 | /** |
---|
3 | * Plugin Name: Popper |
---|
4 | * Plugin URI: https://www.francescopepe.com/ |
---|
5 | * Description: Popup builder with exit-intent powered by Gutenberg. |
---|
6 | * Version: 0.7.5 |
---|
7 | * Author: Tropicalista |
---|
8 | * Author URI: https://www.francescopepe.com |
---|
9 | * License: GPL-2.0-or-later |
---|
10 | * License URI: https://www.gnu.org/licenses/gpl-2.0.html |
---|
11 | * Text Domain: popper |
---|
12 | * |
---|
13 | * @package popper |
---|
14 | */ |
---|
15 | |
---|
16 | defined( 'ABSPATH' ) || exit; |
---|
17 | |
---|
18 | require __DIR__ . '/vendor/autoload.php'; |
---|
19 | require_once __DIR__ . '/includes/class-frontend.php'; |
---|
20 | require_once __DIR__ . '/includes/class-submission.php'; |
---|
21 | |
---|
22 | load_plugin_textdomain( 'popper', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
---|
23 | |
---|
24 | /** |
---|
25 | * Init hook |
---|
26 | */ |
---|
27 | function popper_block_init() { |
---|
28 | register_block_type_from_metadata( __DIR__ . '/build/blocks/popper' ); |
---|
29 | register_block_type_from_metadata( __DIR__ . '/build/blocks/popper-body' ); |
---|
30 | register_block_type_from_metadata( __DIR__ . '/build/blocks/button' ); |
---|
31 | register_block_type_from_metadata( __DIR__ . '/build/blocks/form' ); |
---|
32 | register_block_type_from_metadata( __DIR__ . '/build/blocks/form-input' ); |
---|
33 | register_block_type_from_metadata( __DIR__ . '/build/blocks/form-select' ); |
---|
34 | register_block_type_from_metadata( __DIR__ . '/build/blocks/form-button' ); |
---|
35 | |
---|
36 | register_block_pattern_category( |
---|
37 | 'popup', |
---|
38 | array( 'label' => __( 'Popups', 'popper' ) ) |
---|
39 | ); |
---|
40 | |
---|
41 | $dir = plugin_dir_path( __FILE__ ); |
---|
42 | $dashboard_asset_path = "$dir/build/admin.asset.php"; |
---|
43 | |
---|
44 | $dashboard_asset = require $dashboard_asset_path; |
---|
45 | |
---|
46 | wp_register_script( |
---|
47 | 'popper-dashboard', |
---|
48 | plugin_dir_url( __FILE__ ) . '/build/admin.js', |
---|
49 | $dashboard_asset['dependencies'], |
---|
50 | $dashboard_asset['version'], |
---|
51 | true |
---|
52 | ); |
---|
53 | |
---|
54 | wp_register_style( |
---|
55 | 'popper-dashboard', |
---|
56 | plugin_dir_url( __FILE__ ) . '/build/style-admin.css', |
---|
57 | array( 'wp-components', 'wp-reset-editor-styles' ), |
---|
58 | $dashboard_asset['version'], |
---|
59 | ); |
---|
60 | |
---|
61 | global $wp_filesystem; |
---|
62 | // Initialize the WP filesystem, no more using 'file-put-contents' function. |
---|
63 | if ( empty( $wp_filesystem ) ) { |
---|
64 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
---|
65 | WP_Filesystem( true ); |
---|
66 | } |
---|
67 | |
---|
68 | $local_file = __DIR__ . '/assets/patterns.json'; |
---|
69 | |
---|
70 | if ( $wp_filesystem->exists( $local_file ) ) { |
---|
71 | $patterns = json_decode( $wp_filesystem->get_contents( $local_file ), true ); |
---|
72 | } |
---|
73 | |
---|
74 | if ( ! empty( $patterns ) ) { |
---|
75 | foreach ( $patterns as $pattern ) { |
---|
76 | if ( empty( $pattern['name'] ) ) { |
---|
77 | continue; |
---|
78 | } |
---|
79 | register_block_pattern( |
---|
80 | $pattern['name'], |
---|
81 | $pattern |
---|
82 | ); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | add_action( 'init', 'popper_block_init' ); |
---|
87 | |
---|
88 | /** |
---|
89 | * Register CPT. |
---|
90 | */ |
---|
91 | function popper_register() { |
---|
92 | $args = array( |
---|
93 | 'labels' => array( |
---|
94 | 'name' => _x( 'Popups', 'Post Type General Name', 'popper' ), |
---|
95 | 'singular_name' => _x( 'Popup', 'Post Type Singular Name', 'popper' ), |
---|
96 | 'menu_name' => __( 'Popups', 'popper' ), |
---|
97 | 'parent_item_colon' => __( 'Parent Popup', 'popper' ), |
---|
98 | 'all_items' => __( 'Popups', 'popper' ), |
---|
99 | 'view_item' => __( 'View Popup', 'popper' ), |
---|
100 | 'add_new_item' => __( 'Add New Popup', 'popper' ), |
---|
101 | 'add_new' => __( 'Add New', 'popper' ), |
---|
102 | 'edit_item' => __( 'Edit Popup', 'popper' ), |
---|
103 | 'update_item' => __( 'Update Popup', 'popper' ), |
---|
104 | 'search_items' => __( 'Search Popup', 'popper' ), |
---|
105 | 'not_found' => __( 'Not Found' ), |
---|
106 | 'not_found_in_trash' => __( 'Not found in Trash' ), |
---|
107 | ), |
---|
108 | 'public' => false, |
---|
109 | 'publicly_queryable' => false, |
---|
110 | 'has_archive' => false, |
---|
111 | 'show_ui' => true, |
---|
112 | 'menu_icon' => 'dashicons-external', |
---|
113 | 'exclude_from_search' => true, |
---|
114 | 'show_in_nav_menus' => true, |
---|
115 | 'rewrite' => false, |
---|
116 | 'hierarchical' => false, |
---|
117 | 'show_in_menu' => false, |
---|
118 | 'show_in_admin_bar' => true, |
---|
119 | 'show_in_rest' => true, |
---|
120 | 'capability_type' => 'post', |
---|
121 | 'template' => array( |
---|
122 | array( |
---|
123 | 'popper/popup', |
---|
124 | array( |
---|
125 | 'lock' => array( |
---|
126 | 'move' => false, |
---|
127 | 'remove' => true, |
---|
128 | ), |
---|
129 | ), |
---|
130 | ), |
---|
131 | ), |
---|
132 | // removed until this is solved: https://github.com/WordPress/gutenberg/issues/49005. |
---|
133 | // 'template_lock' => 'insert', |
---|
134 | 'supports' => array( |
---|
135 | 'title', |
---|
136 | 'editor', |
---|
137 | 'custom-fields', |
---|
138 | 'revisions', |
---|
139 | 'author', |
---|
140 | 'excerpt', |
---|
141 | ), |
---|
142 | ); |
---|
143 | register_post_type( 'popper', $args ); |
---|
144 | |
---|
145 | $defaults = array( |
---|
146 | 'running' => true, |
---|
147 | 'location' => array(), |
---|
148 | 'exclude' => array(), |
---|
149 | 'user' => array(), |
---|
150 | 'date' => array( |
---|
151 | array( |
---|
152 | 'type' => 'evergreen', |
---|
153 | 'startDate' => '', |
---|
154 | 'endDate' => '', |
---|
155 | 'customTime' => false, |
---|
156 | 'customDays' => array(), |
---|
157 | ), |
---|
158 | ), |
---|
159 | 'device' => array( |
---|
160 | 'desktop', |
---|
161 | 'tablet', |
---|
162 | 'mobile', |
---|
163 | ), |
---|
164 | ); |
---|
165 | |
---|
166 | register_meta( |
---|
167 | 'post', |
---|
168 | 'popper_rules', |
---|
169 | array( |
---|
170 | 'single' => true, |
---|
171 | 'type' => 'object', |
---|
172 | 'default' => $defaults, |
---|
173 | 'show_in_rest' => array( |
---|
174 | 'schema' => array( |
---|
175 | 'type' => 'object', |
---|
176 | 'additionalProperties' => true, |
---|
177 | 'properties' => array( |
---|
178 | 'running' => array( |
---|
179 | 'type' => 'boolean', |
---|
180 | ), |
---|
181 | 'location' => array( |
---|
182 | 'type' => 'array', |
---|
183 | ), |
---|
184 | 'exclude' => array( |
---|
185 | 'type' => 'array', |
---|
186 | ), |
---|
187 | 'user' => array( |
---|
188 | 'type' => 'array', |
---|
189 | ), |
---|
190 | 'date' => array( |
---|
191 | 'type' => 'object', |
---|
192 | 'additionalProperties' => true, |
---|
193 | 'properties' => array( |
---|
194 | 'type' => array( |
---|
195 | 'type' => 'string', |
---|
196 | ), |
---|
197 | 'startDate' => array( |
---|
198 | 'type' => 'string', |
---|
199 | ), |
---|
200 | 'endDate' => array( |
---|
201 | 'type' => 'string', |
---|
202 | ), |
---|
203 | 'customTime' => array( |
---|
204 | 'type' => 'boolean', |
---|
205 | ), |
---|
206 | 'customDays' => array( |
---|
207 | 'type' => 'array', |
---|
208 | ), |
---|
209 | ), |
---|
210 | ), |
---|
211 | 'device' => array( |
---|
212 | 'type' => 'array', |
---|
213 | ), |
---|
214 | ), |
---|
215 | ), |
---|
216 | ), |
---|
217 | 'object_subtype' => 'popper', |
---|
218 | ) |
---|
219 | ); |
---|
220 | |
---|
221 | register_post_meta( |
---|
222 | 'popper', |
---|
223 | '_popper_actions', |
---|
224 | array( |
---|
225 | 'show_in_rest' => array( |
---|
226 | 'schema' => array( |
---|
227 | 'items' => array( |
---|
228 | 'type' => 'object', |
---|
229 | 'properties' => array(), |
---|
230 | 'additionalProperties' => true, |
---|
231 | ), |
---|
232 | ), |
---|
233 | ), |
---|
234 | 'default' => array(), |
---|
235 | 'single' => true, |
---|
236 | 'type' => 'array', |
---|
237 | 'additionalProperties' => true, |
---|
238 | 'auth_callback' => function () { |
---|
239 | return current_user_can( 'edit_posts' ); |
---|
240 | }, |
---|
241 | ) |
---|
242 | ); |
---|
243 | |
---|
244 | register_post_meta( |
---|
245 | 'popper', |
---|
246 | '_popper_fields', |
---|
247 | array( |
---|
248 | 'show_in_rest' => array( |
---|
249 | 'schema' => array( |
---|
250 | 'type' => 'object', |
---|
251 | 'properties' => array(), |
---|
252 | 'additionalProperties' => true, |
---|
253 | ), |
---|
254 | ), |
---|
255 | 'default' => array(), |
---|
256 | 'single' => true, |
---|
257 | 'type' => 'object', |
---|
258 | 'auth_callback' => function () { |
---|
259 | return current_user_can( 'edit_posts' ); |
---|
260 | }, |
---|
261 | ) |
---|
262 | ); |
---|
263 | |
---|
264 | register_rest_field( |
---|
265 | 'popper', |
---|
266 | 'popper_locations', |
---|
267 | array( |
---|
268 | 'get_callback' => function ( $obj ) { |
---|
269 | $result = array( |
---|
270 | 'include' => array(), |
---|
271 | 'exclude' => array(), |
---|
272 | 'user' => array(), |
---|
273 | ); |
---|
274 | |
---|
275 | foreach ( $obj['meta']['popper_rules']['location'] as $value ) { |
---|
276 | array_push( $result['include'], \Popper\Conditions::get_saved_label( $value ) ); |
---|
277 | } |
---|
278 | foreach ( $obj['meta']['popper_rules']['exclude'] as $value ) { |
---|
279 | array_push( $result['exclude'], \Popper\Conditions::get_saved_label( $value ) ); |
---|
280 | } |
---|
281 | $result['user'] = \Popper\Conditions::get_user_label( $obj['meta']['popper_rules']['user'] ) ?? array(); |
---|
282 | return $result; |
---|
283 | }, |
---|
284 | ), |
---|
285 | ); |
---|
286 | } |
---|
287 | |
---|
288 | add_action( 'init', 'popper_register' ); |
---|
289 | |
---|
290 | /** |
---|
291 | * Print default positions options. |
---|
292 | */ |
---|
293 | function popper_positions() { |
---|
294 | $positions = \Popper\Conditions::get_conditions(); |
---|
295 | $users = \Popper\Conditions::get_user_conditions(); |
---|
296 | |
---|
297 | $screen = get_current_screen(); |
---|
298 | if ( 'edit-popper' === $screen->id || 'toplevel_page_popper' === $screen->id ) { |
---|
299 | wp_enqueue_script( 'popper-dashboard' ); |
---|
300 | } |
---|
301 | |
---|
302 | wp_localize_script( |
---|
303 | 'wp-block-directory', |
---|
304 | 'popper', |
---|
305 | array( |
---|
306 | 'positions' => $positions, |
---|
307 | 'users' => $users, |
---|
308 | 'installLink' => wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=formello' ), 'install-plugin_popper' ), |
---|
309 | ) |
---|
310 | ); |
---|
311 | |
---|
312 | wp_localize_script( |
---|
313 | 'popper-dashboard', |
---|
314 | 'popper', |
---|
315 | array( |
---|
316 | 'positions' => $positions, |
---|
317 | 'users' => $users, |
---|
318 | ) |
---|
319 | ); |
---|
320 | } |
---|
321 | add_action( 'admin_enqueue_scripts', 'popper_positions', 10, 2 ); |
---|
322 | |
---|
323 | /** |
---|
324 | * Fires after tracking permission allowed (optin) |
---|
325 | * |
---|
326 | * @param array $data The Appsero data. |
---|
327 | * |
---|
328 | * @return void |
---|
329 | */ |
---|
330 | function popper_tracker_optin( $data ) { |
---|
331 | $data['project'] = 'popper'; |
---|
332 | $response = wp_remote_post( |
---|
333 | 'https://hook.eu1.make.com/dplrdfggemll51whv3b21yjabuk8po0b', |
---|
334 | array( |
---|
335 | 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
---|
336 | 'body' => wp_json_encode( $data ), |
---|
337 | 'method' => 'POST', |
---|
338 | 'data_format' => 'body', |
---|
339 | ) |
---|
340 | ); |
---|
341 | } |
---|
342 | add_action( 'popper_tracker_optin', 'popper_tracker_optin', 10 ); |
---|
343 | |
---|
344 | /** |
---|
345 | * Initialize the plugin tracker |
---|
346 | * |
---|
347 | * @return void |
---|
348 | */ |
---|
349 | function popper_appsero_init_tracker() { |
---|
350 | |
---|
351 | if ( ! class_exists( 'Appsero\Client' ) ) { |
---|
352 | require_once __DIR__ . '/appsero/src/Client.php'; |
---|
353 | } |
---|
354 | |
---|
355 | $client = new Appsero\Client( '3cf9fb47-a835-47f8-be36-df7bdfceda26', 'Popup with exit intent, scroll triggered and anchor click for opt-ins, lead gen & more', __FILE__ ); |
---|
356 | |
---|
357 | // Active insights. |
---|
358 | $client->insights() |
---|
359 | ->add_plugin_data() |
---|
360 | ->init(); |
---|
361 | } |
---|
362 | |
---|
363 | popper_appsero_init_tracker(); |
---|
364 | |
---|
365 | /** |
---|
366 | * Update popper namespace |
---|
367 | * |
---|
368 | * @return void |
---|
369 | */ |
---|
370 | function popper_update_namespace() { |
---|
371 | $posts = get_posts( |
---|
372 | array( |
---|
373 | 'post_type' => 'popper', |
---|
374 | 'numberposts' => -1, |
---|
375 | ) |
---|
376 | ); |
---|
377 | |
---|
378 | foreach ( $posts as $post ) { |
---|
379 | $post->post_content = str_replace( 'wp:formello/popper', 'wp:popper/popup', $post->post_content ); |
---|
380 | $post->post_content = str_replace( 'wp-block-formello-popper', 'wp-block-popper-popup', $post->post_content ); |
---|
381 | |
---|
382 | global $wpdb; |
---|
383 | |
---|
384 | $wpdb->update( |
---|
385 | $wpdb->prefix . 'posts', |
---|
386 | array( |
---|
387 | 'post_content' => $post->post_content, |
---|
388 | ), |
---|
389 | array( 'id' => intval( $post->ID ) ) |
---|
390 | ); |
---|
391 | |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | register_activation_hook( __FILE__, 'popper_update_namespace' ); |
---|