PluginProbe
Leadpages / trunk
Leadpages vtrunk
1.3.0 1.2.1 1.2.2 1.2.3 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0
leadpages / includes / Assets.php

Assets.php in Leadpages trunk, at includes/Assets.php

260 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Leadpages;
4
5 defined('ABSPATH') || die('No script kiddies please!'); // Avoid direct file request
6
7 use Leadpages\providers\config\Config;
8 use Leadpages\providers\Utils;
9
10 // All of the names of the assets the plugin uses.
11 // Values must match those configured in webpack
12 const ASSET_LANDINGPAGES = 'landingpages';
13 const ASSET_SETTINGS = 'lp_settings';
14 // Names of the pages the plugin uses
15 const SLUG_LEADPAGES = 'leadpages';
16 const SLUG_SETTINGS = 'lp_settings';
17 const SLUG_OAUTH_COMPLETE = 'lp_login_complete';
18
19 /**
20 * Base asset management class for frontend scripts and styles.
21 */
22 class Assets {
23
24 use Utils;
25
26 /** @var Config */
27 public $config;
28
29 /**
30 * Path to the build and assets directories
31 */
32 public $build_path = 'build/';
33 public $assets_path = 'public/';
34
35 /**
36 * Array of arrays containing file paths to each type of asset (js, css, php)
37 * @var array
38 */
39 private $file_paths;
40
41 public function __construct() {
42 $this->config = Config::get_instance();
43
44 $assets = $this->get_asset_names();
45 foreach ($assets as $asset) {
46 $base_path = $this->build_path . $asset;
47 $this->file_paths[ $asset ] = [
48 'js' => $base_path . '.js',
49 'css' => $base_path . '.css',
50 'php' => $base_path . '.asset.php',
51 'name' => LEADPAGES_SLUG . '_' . $asset,
52 ];
53 }
54 }
55
56 /**
57 * Return all the names of all the assets that the plugin uses
58 * @return array
59 */
60 private function get_asset_names() {
61 return [
62 ASSET_LANDINGPAGES,
63 ASSET_SETTINGS,
64 // add more assets as needed...
65 ];
66 }
67
68 /**
69 * Wrapper around include construct for testing
70 *
71 * @param string $path
72 * @return mixed
73 */
74 public function get_asset_file( $path ) {
75 return include LEADPAGES_PATH . '/' . $path;
76 }
77
78 /**
79 * Enqueue scripts and styles for admin pages.
80 *
81 * @param string $hook_suffix The current admin page
82 */
83 public function enqueue_admin_scripts( $hook_suffix ) {
84 $this->enqueue_landing_pages_page($hook_suffix);
85 $this->enqueue_settings_page($hook_suffix);
86 $this->enqueue_leadpages_icons();
87 // add more scripts as needed...
88 }
89
90 /**
91 * Render the Landing Pages page (also the admin menu page)
92 *
93 * This method adds a menu page for the Leadpages plugin in the WordPress admin dashboard.
94 * The page is simply a div that a React component will attach to from the script enqueue'd
95 * in the `enqueue_admin_scripts` method.
96 */
97 public function render_admin_menu_page() {
98 add_menu_page(
99 'Leadpages', // Page title
100 'Leadpages', // Menu title
101 'manage_options', // Capability
102 SLUG_LEADPAGES,
103 function () {
104 echo '
105 <div id="leadpages-page-root"></div>
106 ';
107 },
108 null, // The icon will be added through CSS
109 80 // Positions menu under the "Settings" sidebar
110 );
111
112 // Top-level menu item (Leadpages) differ from the first sub-level item (Landing Pages)
113 // NOTE: This submenu is not visible in the UI navigation menu until there's been more
114 // than one submenu added (ex. Settings). This is a WordPress quirk. Adding this in now
115 // though for future development.
116 add_submenu_page(
117 SLUG_LEADPAGES, // Parent slug
118 'Landing Pages', // Page title
119 'Landing Pages', // Menu title
120 'manage_options', // Capability
121 SLUG_LEADPAGES,
122 function () {
123 echo '
124 <div id="leadpages-page-root"></div>
125 ';
126 }
127 );
128 }
129
130 public function render_settings_page() {
131 add_submenu_page(
132 SLUG_LEADPAGES, // Parent slug
133 'Settings', // Page title
134 'Settings', // Menu title
135 'manage_options', // Capability
136 SLUG_SETTINGS, // Menu slug
137 function () {
138 echo '
139 <div id="settings-page-root"></div>
140 ';
141 }
142 );
143 }
144
145 /**
146 * Render the OAuth2 completion page without a menu item. This page should only be seen within
147 * the popup window that the Leadpages OAuth login is displayed in.
148 *
149 * @see https://stackoverflow.com/questions/3902760/how-do-you-add-a-wordpress-admin-page-without-adding-it-to-the-menu/47577455#47577455
150 */
151 public function render_oauth_complete_page() {
152 add_submenu_page(
153 '', // By setting the parent slug to an empty string, it should not be visible in the menu
154 'Login Complete',
155 'Login Complete',
156 'manage_options',
157 SLUG_OAUTH_COMPLETE,
158 function () {
159 include_once LEADPAGES_PATH . '/includes/other/login_complete_page.php';
160 }
161 );
162 }
163
164 /**
165 * Enqueue scripts and styles for the Landing Pages page.
166 * This method loads the scripts and styles only on the Leadpages Landing Pages page.
167 *
168 * @param string $hook The current admin page
169 */
170 private function enqueue_landing_pages_page( $hook ) {
171 // Load only on ?page=leadpages (Leadpages page)
172 if ('toplevel_page_' . SLUG_LEADPAGES !== $hook) {
173 return;
174 }
175
176 // Automatically load imported dependencies and assets version.
177 $landing_pages_asset_file = $this->get_asset_file($this->file_paths[ ASSET_LANDINGPAGES ]['php']);
178
179 // Load JavaScript
180 wp_enqueue_script(
181 $this->file_paths[ ASSET_LANDINGPAGES ]['name'],
182 plugins_url($this->file_paths[ ASSET_LANDINGPAGES ]['js'], LEADPAGES_FILE),
183 $landing_pages_asset_file['dependencies'],
184 $landing_pages_asset_file['version'],
185 true
186 );
187
188 // leverage this to inject additional data into the script
189 // these are used to conditionally link out to analytics based on environment configuration
190 wp_localize_script(
191 $this->file_paths[ ASSET_LANDINGPAGES ]['name'],
192 LEADPAGES_NS . 'Data',
193 [
194 'homeUrl' => home_url(),
195 'leadpagesUrl' => $this->config->get('LEADPAGES_URL'),
196 'builderUrl' => $this->config->get('BUILDER_URL'),
197 'novaDashboardUrl' => $this->config->get('NOVA_DASHBOARD_URL'),
198 'novaAppUrl' => $this->config->get('NOVA_APP_URL'),
199 ]
200 );
201
202 // Load CSS
203 wp_enqueue_style(
204 $this->file_paths[ ASSET_LANDINGPAGES ]['name'],
205 plugins_url($this->file_paths[ ASSET_LANDINGPAGES ]['css'], LEADPAGES_FILE),
206 [ 'wp-components' ],
207 $landing_pages_asset_file['version']
208 );
209 }
210
211 /**
212 * Enqueue scripts and styles for the Settings page.
213 * This method loads the scripts and styles only on the Leadpages Settings page.
214 *
215 * @param string $hook The current admin page
216 */
217 private function enqueue_settings_page( $hook ) {
218 // Load only on ?page=settings (Settings page)
219 if ('leadpages_page_' . SLUG_SETTINGS !== $hook) {
220 return;
221 }
222
223 // Automatically load imported dependencies and assets version.
224 $settings_asset_file = $this->get_asset_file($this->file_paths[ ASSET_SETTINGS ]['php']);
225
226 // Load JavaScript
227 wp_enqueue_script(
228 $this->file_paths[ ASSET_SETTINGS ]['name'],
229 plugins_url($this->file_paths[ ASSET_SETTINGS ]['js'], LEADPAGES_FILE),
230 $settings_asset_file['dependencies'],
231 $settings_asset_file['version'],
232 true
233 );
234
235 // Load CSS
236 wp_enqueue_style(
237 $this->file_paths[ ASSET_SETTINGS ]['name'],
238 plugins_url($this->file_paths[ ASSET_SETTINGS ]['css'], LEADPAGES_FILE),
239 [ 'wp-components' ],
240 $settings_asset_file['version']
241 );
242 }
243
244 /*
245 * Enqueue the Leadpages icons. The LP icon in the top level menu item is dependent on this.
246 */
247 private function enqueue_leadpages_icons() {
248 // Version by file mtime so edits to this static stylesheet always bust the
249 // browser cache (the plugin version alone would not change between edits).
250 $icons_css = LEADPAGES_PATH . '/public/lp-icons.css';
251 $version = file_exists($icons_css) ? filemtime($icons_css) : LEADPAGES_VERSION;
252 wp_enqueue_style(
253 'leadpages-icons',
254 plugins_url('public/lp-icons.css', LEADPAGES_FILE),
255 [],
256 $version
257 );
258 }
259 }
260