-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopbot.php
217 lines (183 loc) · 6.85 KB
/
popbot.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
<?php
namespace popbot;
/**
* Plugin Name: PopBot
* Description: Convert your users with targeted pop-ups. Use triggers & rules to nudge your users towards conversions when they're ready using the perfect pop-up.
* Version: 1.0.3
* Requires at least: 5.7
* Requires PHP: 7.4.0
* Author: Web Results Direct
* Author URI: https://wrd.studio
* License: GPL v3 or later
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: popbot
* Domain Path: /languages
*/
class Popbot_Plugin {
const PLUGIN_FILE = __FILE__;
const VERSION = '1.0.3';
const PLUGIN_DIR = WP_PLUGIN_DIR . '/popbot';
const PLUGIN_URL = WP_PLUGIN_URL . '/popbot';
const CONTENT_DIR = WP_CONTENT_DIR . '/popbot';
const CONTENT_URL = WP_CONTENT_URL . '/popbot';
public static function includes() {
require_once static::PLUGIN_DIR . '/src/class-utils.php';
require_once static::PLUGIN_DIR . '/src/class-admin-page.php';
require_once static::PLUGIN_DIR . '/src/class-popbot.php';
require_once static::PLUGIN_DIR . '/src/class-popbot-assets.php';
require_once static::PLUGIN_DIR . '/src/class-popbot-analytics.php';
require_once static::PLUGIN_DIR . '/src/class-popbot-options.php';
require_once static::PLUGIN_DIR . '/src/class-popbot-metabox.php';
require_once static::PLUGIN_DIR . '/src/class-popbot-template.php';
require_once static::PLUGIN_DIR . '/src/class-custom-condition.php';
require_once static::PLUGIN_DIR . '/src/templating.php';
if ( is_admin() ) {
require_once static::PLUGIN_DIR . '/src/class-popbot-metabox.php';
} else {
require_once static::PLUGIN_DIR . '/src/class-user-journey-monitor.php';
}
}
public static function init() {
static::includes();
if ( Popbot_Options::get( 'popbot_require_rewrite_rules_flush' ) ) {
Popbot_Options::set( 'popbot_require_rewrite_rules_flush', false );
flush_rewrite_rules();
}
Popbot_Assets::init();
Popbot::init();
Custom_Condition::init();
Popbot_Metabox::init();
static::admin_pages();
add_action( 'rest_api_init', array( static::class, 'register_endpoints' ) );
add_action( 'parse_request', array( static::class, 'redirect_preview' ) );
add_action( 'admin_bar_menu', array( static::class, 'admin_bar_debugger' ), 999 );
}
public static function admin_pages() {
$icon_file = file_get_contents(static::PLUGIN_DIR . '/assets/icons/popbot.svg'); // phpcs:ignore -- This is a local file not remote.
$dashboard_page = new Admin_Page(
array(
'slug' => 'popbot-home',
'icon' => 'data:image/svg+xml;base64,' . base64_encode($icon_file), // phpcs:ignore -- base64_encode is being used for an admin page icon.
'title' => __( 'PopBot', 'popbot' ),
'redirect' => admin_url( 'admin.php?page=popbot-dashboard' ),
'hidden' => true,
'template' => static::PLUGIN_DIR . '/pages/home.php',
'scripts' => array( 'popbot-admin' ),
'styles' => array( 'popbot-admin' ),
)
);
new Admin_Page(
array(
'parent' => $dashboard_page,
'slug' => 'popbot-dashboard',
'title' => __( 'Dashboard', 'popbot' ),
'template' => static::PLUGIN_DIR . '/pages/home.php',
'scripts' => array( 'popbot-admin' ),
'styles' => array( 'popbot-admin' ),
)
);
new Admin_Page(
array(
'parent' => $dashboard_page,
'slug' => 'popbot-archive',
'title' => __( 'All PopBots', 'popbot' ),
'template' => static::PLUGIN_DIR . '/pages/archive.php',
'scripts' => array( 'popbot-admin' ),
'styles' => array( 'popbot-admin' ),
)
);
new Admin_Page(
array(
'parent' => $dashboard_page,
'slug' => 'popbot-edit',
'title' => __( 'Edit PopBot', 'popbot' ),
'template' => static::PLUGIN_DIR . '/pages/edit.php',
'scripts' => array( 'popbot-admin' ),
'styles' => array( 'popbot-admin' ),
'hidden' => true,
)
);
new Admin_Page(
array(
'parent' => $dashboard_page,
'slug' => 'popbot-templates',
'title' => __( 'Create a PopBot', 'popbot' ),
'template' => static::PLUGIN_DIR . '/pages/templates.php',
'scripts' => array( 'popbot-admin' ),
'styles' => array( 'popbot-admin' ),
)
);
}
public static function register_endpoints() {
require_once static::PLUGIN_DIR . '/src/routes/class-custom-route-custom-conditions.php';
require_once static::PLUGIN_DIR . '/src/routes/class-custom-route-popbot-analytics.php';
require_once static::PLUGIN_DIR . '/src/routes/class-custom-route-popbot-options.php';
require_once static::PLUGIN_DIR . '/src/routes/class-custom-route-popbot-templates.php';
require_once static::PLUGIN_DIR . '/src/routes/class-custom-route-popbot.php';
foreach ( Popbot::META_FIELDS as $field => $default ) {
register_post_meta(
Popbot::POST_TYPE,
$field,
array(
'type' => 'string',
'single' => true,
'show_in_rest' => true,
'default' => $default,
)
);
}
$analytics_route = new Custom_Route_Popbot_Analytics();
$analytics_route->register_routes();
$templates_route = new Custom_Route_Popbot_Templates();
$templates_route->register_routes();
$options_route = new Custom_Route_Popbot_Options();
$options_route->register_routes();
$popbots_route = new Custom_Route_Popbot();
$popbots_route->register_routes();
$custom_conditions_route = new Custom_Route_Custom_Conditions();
$custom_conditions_route->register_routes();
}
/**
* Redirects popBotPreview requests.
*
* We do this rather than a direct file call so we can keep all the WP & theme styles.
*/
public static function redirect_preview( $wp ) {
// Hijack requests for ...com/popBotPreview to show our template preview (only for admins).
if ( $wp->request === 'popBotPreview' && current_user_can( Popbot::CAPABILITY ) ) {
include static::PLUGIN_DIR . '/pages/templatePreview.php';
die();
}
}
public static function admin_bar_debugger( $admin_bar ) {
if ( is_admin() ) {
return;
}
$admin_bar->add_node(
array(
'id' => 'popbot-debugger-btn',
'title' => __( 'PopBot Debugger', 'popbot' ),
'href' => '',
'meta' => array(
'class' => 'popbot-debugger-opener',
),
)
);
}
static function activate(): void {
static::includes();
Popbot_Analytics::create_table();
Popbot_Options::set( 'popbot_require_rewrite_rules_flush', true );
Popbot_Options::set( 'popbot_version', self::VERSION );
}
static function uninstall(): void {
static::includes();
Popbot_Analytics::drop_table();
foreach ( array_keys( Popbot_Options::OPTIONS_WHITELIST ) as $key ) {
delete_option( $key );
}
}
}
add_action( 'init', array( 'popbot\\Popbot_Plugin', 'init' ), 0 );
register_activation_hook( Popbot_Plugin::PLUGIN_FILE, array( 'popbot\\Popbot_Plugin', 'activate' ) );
register_uninstall_hook( Popbot_Plugin::PLUGIN_FILE, array( 'popbot\\Popbot_Plugin', 'uninstall' ) );