PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.11.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.11.1
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / blocks.php

blocks.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.11.1, at includes/blocks.php

417 lines 15.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit(); // Exit if accessed directly.
6 }
7
8 use ABlocks\Classes\AssetsGenerator;
9 use ABlocks\Helper;
10 use ABlocks\Classes\FileUpload;
11 use ABlocks\Classes\FontLoadLocally;
12 use ABlocks\Blocks\FormBuilder\BlockAttrSanitizer\Sanitizer as BlockSanitizer;
13 use ABlocks\Blocks\FormBuilder\Helper as FormBuilderHelper;
14
15 class Blocks {
16 public static function init() {
17 $self = new self();
18 // block initialization
19 add_action( 'init', [ $self, 'blocks_init' ] );
20 add_action( 'enqueue_block_assets', [ $self, 'load_academy_core_scripts' ] );
21 add_filter( 'block_categories_all', [ $self, 'register_block_category' ], 10, 2 );
22 // Assets Generator
23 add_action( 'save_post', [ $self, 'generate_block_assets' ], 10, 3 );
24 add_action( 'switch_theme', [ $self, 'clear_generated_block_assets' ] );
25 add_action( 'save_post', [ $self, 'download_google_fonts_locally' ], 20, 3 );
26 add_action( 'deleted_post', [ $self, 'flush_site_fonts_on_delete' ], 10, 2 );
27 // Self-hosting template fonts discovered on a front-end request happens
28 // here, off the visitor's request.
29 add_action( 'ablocks/download_site_fonts', [ '\ABlocks\Classes\FontCollector', 'update_site_fonts' ] );
30 // A theme swap changes which templates and template parts exist.
31 add_action( 'switch_theme', [ '\ABlocks\Classes\FontCollector', 'flush_site_fonts' ] );
32 // PERF-2: pre-warm the page's generated assets in the background after save,
33 // so the first visitor doesn't pay the parse/generate/write cost.
34 add_action( 'save_post', [ $self, 'prewarm_page_assets' ], 30, 3 );
35 // Recompute global typography fonts whenever the plugin's global settings change.
36 add_action( 'ablocks/after_save_settings', [ $self, 'refresh_global_fonts' ] );
37 // Bust the cached global CSS variables when global settings change.
38 add_action( 'ablocks/after_save_settings', [ $self, 'clear_global_css_cache' ] );
39
40 $self->register_block_sanitizer();
41 $self->form_builder_default_data();
42 }
43
44 public function blocks_init() {
45 // keep it common for Academy LMS and QuizPress
46 if ( ( helper::is_gutenberg_editor() && ( Helper::check_post_type_from_admin( 'academy_certificate' ) || Helper::check_post_type_from_admin( 'qp_certificate' ) ) ) || ( ! helper::is_gutenberg_editor() && ! is_admin() ) ) {
47 new \ABlocks\Blocks\AcademyCertificate\Block();
48 new \ABlocks\Blocks\AcademyCertificateText\Block();
49 new \ABlocks\Blocks\AcademyContainer\Block();
50 if ( Helper::is_plugin_active( 'academy-pro/academy-pro.php' ) ) {
51 new \ABlocks\Blocks\AcademyCertificateId\Block();
52 }
53 }
54 // Academy LMS Block Register Here
55 if ( Helper::is_plugin_active( 'academy/academy.php' ) ) {
56 add_filter( 'academy/is_load_common_scripts', '__return_true' );
57 if ( Helper::is_gutenberg_editor() ) {
58 add_filter( 'academy/is_load_common_js_scripts', '__return_false' );
59 }
60 new \ABlocks\Blocks\AcademyCourses\Block();
61 new \ABlocks\Blocks\AcademyEnrollForm\Block();
62 new \ABlocks\Blocks\AcademyStudentRegistrationForm\Block();
63 new \ABlocks\Blocks\AcademyCourseSearch\Block();
64 new \ABlocks\Blocks\AcademyInstructorRegistrationForm\Block();
65 new \ABlocks\Blocks\AcademyPdf\Block();
66 new \ABlocks\Blocks\AcademyPasswordResetForm\Block();
67 new \ABlocks\Blocks\AcademyLoginForm\Block();
68 new \ABlocks\Blocks\AcademyCourseMedia\Block();
69
70 new \ABlocks\Blocks\AcademyCourseCurriculums\Block();
71 new \ABlocks\Blocks\AcademyCourseReviews\Block();
72 new \ABlocks\Blocks\AcademyReviewForm\Block();
73 new \ABlocks\Blocks\AcademyReviewList\Block();
74 new \ABlocks\Blocks\AcademyAdditionInfo\Block();
75 new \ABlocks\Blocks\AcademyCourseDescription\Block();
76 new \ABlocks\Blocks\AcademyCourseInstructor\Block();
77 new \ABlocks\Blocks\AcademyEnrollContent\Block();
78 }//end if
79 // StoreEngine Register Block
80 if ( Helper::is_plugin_active( 'storeengine/storeengine.php' ) ) {
81 new \ABlocks\Blocks\StoreengineProducts\Block();
82 new \ABlocks\Blocks\StoreengineCouponForm\Block();
83 new \ABlocks\Blocks\StoreengineCartList\Block();
84 new \ABlocks\Blocks\StoreengineLoginForm\Block();
85 new \ABlocks\Blocks\StoreengineProductFilter\Block();
86 new \ABlocks\Blocks\StoreengineCheckoutForm\Block();
87 new \ABlocks\Blocks\StoreengineContinueButton\Block();
88 new \ABlocks\Blocks\StoreengineCheckoutButton\Block();
89 new \ABlocks\Blocks\StoreengineCartSubTable\Block();
90 new \ABlocks\Blocks\StoreengineCartButton\Block();
91 new \ABlocks\Blocks\StoreengineOrderInfo\Block();
92 new \ABlocks\Blocks\StoreengineBillingInfo\Block();
93 new \ABlocks\Blocks\StoreengineShippingInfo\Block();
94 new \ABlocks\Blocks\StoreengineCartNotice\Block();
95 new \ABlocks\Blocks\StoreengineOrderDetails\Block();
96 // Funnel Builder blocks (render the funnel shortcodes).
97 new \ABlocks\Blocks\StoreengineFunnelCheckout\Block();
98 new \ABlocks\Blocks\StoreengineFunnelOffer\Block();
99 new \ABlocks\Blocks\StoreengineFunnelAccept\Block();
100 new \ABlocks\Blocks\StoreengineFunnelDecline\Block();
101 new \ABlocks\Blocks\StoreengineFunnelContinue\Block();
102 new \ABlocks\Blocks\StoreengineMiniCart\Block();
103 new \ABlocks\Blocks\StoreengineProductGallery\Block();
104 new \ABlocks\Blocks\StoreengineProductSummary\Block();
105 new \ABlocks\Blocks\StoreengineProductDescription\Block();
106 new \ABlocks\Blocks\StoreengineProductReview\Block();
107 }//end if
108
109 if ( class_exists('EasyContentManager') ) {
110 // Easy Content Manager Register Block
111 new \ABlocks\Blocks\EcmBookmarkButton\Block();
112 new \ABlocks\Blocks\EcmBookmarkBadge\Block();
113 new \ABlocks\Blocks\EcmBookmarkList\Block();
114 new \ABlocks\Blocks\EcmBookmarkCount\Block();
115 new \ABlocks\Blocks\EcmReactionList\Block();
116 new \ABlocks\Blocks\EcmReaction\Block();
117 new \ABlocks\Blocks\EcmUpvote\Block();
118 new \ABlocks\Blocks\EcmUpvoteCount\Block();
119 new \ABlocks\Blocks\EcmUpvoteList\Block();
120 new \ABlocks\Blocks\EcmClaimButton\Block();
121 new \ABlocks\Blocks\EcmClaimBadge\Block();
122 new \ABlocks\Blocks\EcmClaimList\Block();
123 new \ABlocks\Blocks\EcmClaimCount\Block();
124 }
125
126 new \ABlocks\Blocks\Container\Block();
127 new \ABlocks\Blocks\Heading\Block();
128 new \ABlocks\Blocks\Paragraph\Block();
129 new \ABlocks\Blocks\Image\Block();
130 new \ABlocks\Blocks\Button\Block();
131 new \ABlocks\Blocks\DualButton\Block();
132 new \ABlocks\Blocks\Icon\Block();
133 new \ABlocks\Blocks\InfoBox\Block();
134 new \ABlocks\Blocks\Lists\Block();
135 new \ABlocks\Blocks\Counter\Block();
136 new \ABlocks\Blocks\StarRatings\Block();
137 new \ABlocks\Blocks\Divider\Block();
138 new \ABlocks\Blocks\Spacer\Block();
139 new \ABlocks\Blocks\Video\Block();
140 new \ABlocks\Blocks\LoopBuilder\Block();
141 new \ABlocks\Blocks\LoopTemplate\Block();
142 new \ABlocks\Blocks\LoopFilter\Block();
143 new \ABlocks\Blocks\LoopLoadMore\Block();
144 new \ABlocks\Blocks\Search\Block();
145 new \ABlocks\Blocks\Carousel\Block();
146 new \ABlocks\Blocks\CarouselChild\Block();
147 new \ABlocks\Blocks\Chart\Block();
148 new \ABlocks\Blocks\Toggle\Block();
149 new \ABlocks\Blocks\ToggleChild\Block();
150 new \ABlocks\Blocks\Accordion\Block();
151 new \ABlocks\Blocks\SingleAccordion\Block();
152 new \ABlocks\Blocks\Tabs\Block();
153 new \ABlocks\Blocks\TabsChild\Block();
154 new \ABlocks\Blocks\Countdown\Block();
155 new \ABlocks\Blocks\NewsTicker\Block();
156 new \ABlocks\Blocks\ImageHotspot\Block();
157 new \ABlocks\Blocks\ImageHotspotChild\Block();
158 new \ABlocks\Blocks\ProgressTracker\Block();
159 new \ABlocks\Blocks\ImageScroll\Block();
160 new \ABlocks\Blocks\Menu\Block();
161 new \ABlocks\Blocks\MenuItem\Block();
162 new \ABlocks\Blocks\MenuChildSub\Block();
163 new \ABlocks\Blocks\MenuChildMega\Block();
164 new \ABlocks\Blocks\StripeButton\Block();
165 new \ABlocks\Blocks\PaypalButton\Block();
166
167 new \ABlocks\Blocks\Table\Block();
168 new \ABlocks\Blocks\TableCell\Block();
169 new \ABlocks\Blocks\TableRow\Block();
170 new \ABlocks\Blocks\TableHeader\Block();
171 new \ABlocks\Blocks\TableFooter\Block();
172 new \ABlocks\Blocks\TableBody\Block();
173
174 new \ABlocks\Blocks\Coupon\Block();
175 new \ABlocks\Blocks\ContentTimeline\Block();
176 new \ABlocks\Blocks\ContentTimelineChild\Block();
177 new \ABlocks\Blocks\Map\Block();
178 new \ABlocks\Blocks\TableOfContent\Block();
179 new \ABlocks\Blocks\Modal\Block();
180 new \ABlocks\Blocks\ModalTrigger\Block();
181 new \ABlocks\Blocks\ModalPanel\Block();
182 new \ABlocks\Blocks\ImageComparison\Block();
183 new \ABlocks\Blocks\FlipBox\Block();
184 new \ABlocks\Blocks\FlipBoxChild\Block();
185 new \ABlocks\Blocks\PriceMenu\Block();
186 new \ABlocks\Blocks\PriceMenuItem\Block();
187 new \ABlocks\Blocks\SocialShares\Block();
188 new \ABlocks\Blocks\Notice\Block();
189 new \ABlocks\Blocks\SvgDraw\Block();
190 new \ABlocks\Blocks\FrontendDashboard\Block();
191 new \ABlocks\Blocks\LottieAnimation\Block();
192 new \ABlocks\Blocks\Marquee\Block();
193 new \ABlocks\Blocks\DynamicText\Block();
194 new \ABlocks\Blocks\MarqueeChild\Block();
195 new \ABlocks\Blocks\Logout\Block();
196 new \ABlocks\Blocks\FilterableCards\Block();
197 new \ABlocks\Blocks\FilterableCardsItem\Block();
198 new \ABlocks\Blocks\AdvanceLists\Block();
199 new \ABlocks\Blocks\AdvanceListItem\Block();
200 new \ABlocks\Blocks\QrCode\Block();
201 new \ABlocks\Blocks\FeaturedImage\Block();
202 new \ABlocks\Blocks\ScrollToTop\Block();
203 new \ABlocks\Blocks\Breadcrumb\Block();
204 new \ABlocks\Blocks\InteractiveCircleInfographic\Block();
205 // Form Builder
206 new \ABlocks\Blocks\FormBuilder\Block();
207 new \ABlocks\Blocks\FormInput\Block();
208 new \ABlocks\Blocks\FormPassword\Block();
209 new \ABlocks\Blocks\FormTextarea\Block();
210 new \ABlocks\Blocks\FormCheckbox\Block();
211 new \ABlocks\Blocks\FormSelect\Block();
212 new \ABlocks\Blocks\FormMultiStep\Block();
213 new \ABlocks\Blocks\FormMultiStepChild\Block();
214 new \ABlocks\Blocks\FormRadio\Block();
215 new \ABlocks\Blocks\TextPath\Block();
216 new \ABlocks\Blocks\taxonomyListing\Block();
217 new \ABlocks\Blocks\Player\Block();
218 }
219
220 public function register_block_category( $categories, $post ) {
221 return array_merge(
222 [
223 [
224 'slug' => 'ablocks',
225 'title' => __( 'ABlocks', 'ablocks' ),
226 ],
227 [
228 'slug' => 'academy',
229 'title' => __( 'Academy LMS', 'ablocks' ),
230 ],
231 [
232 'slug' => 'storeengine',
233 'title' => __( 'StoreEngine', 'ablocks' ),
234 ],
235 [
236 'slug' => 'easy-content-manager',
237 'title' => __( 'Easy Content Manager', 'ablocks' ),
238 ],
239 ],
240 $categories
241 );
242 }
243
244 public function load_academy_core_scripts() {
245 if ( ! Helper::is_plugin_active( 'academy/academy.php' ) || ! is_admin() ) {
246 return;
247 }
248 $ScriptsBase = new \Academy\Assets();
249 $ScriptsBase->frontend_common_assets();
250 }
251
252 public function generate_block_assets( $post_id, $post, $update ) {
253 if ( ! Helper::is_enabled_assets_generation() ) {
254 return;
255 }
256
257 if ( isset( $post->post_status ) && 'auto-draft' === $post->post_status ) {
258 return;
259 }
260
261 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
262 return;
263 }
264
265 if ( false !== wp_is_post_revision( $post_id ) ) {
266 return;
267 }
268
269 $uploader = new \ABlocks\Classes\FileUpload();
270
271 // Template-like content is shared across many pages, so a change there must
272 // invalidate every page's combined assets. Regular content only affects its
273 // own page — scope the invalidation to that one post so a single edit can't
274 // wipe the whole site's asset cache and cause a regeneration stampede.
275 $global_impact_post_types = apply_filters(
276 'ablocks/global_asset_invalidation_post_types',
277 [ 'ablocks_tb', 'wp_template', 'wp_template_part', 'wp_block' ]
278 );
279
280 if ( in_array( get_post_type( $post_id ), $global_impact_post_types, true ) ) {
281 $this->clear_generated_block_assets();
282 return;
283 }
284
285 // Drop just this post's cached files; they regenerate on next visit.
286 $uploader->delete_file( $post_id . '.min.css' );
287 $uploader->delete_file( $post_id . '.min.js' );
288 }
289
290 public function download_google_fonts_locally( $post_id, $post, $update ) {
291 if ( isset( $post->post_status ) && 'auto-draft' === $post->post_status ) {
292 return;
293 }
294
295 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
296 return;
297 }
298
299 if ( false !== wp_is_post_revision( $post_id ) ) {
300 return;
301 }
302
303 // Only process content that can contain aBlocks fonts (aBlocks blocks or
304 // reusable blocks that might wrap them).
305 if ( empty( $post->post_content )
306 || ( false === strpos( $post->post_content, 'wp:ablocks' ) && false === strpos( $post->post_content, 'wp:block' ) ) ) {
307 return;
308 }
309
310 // Collect the fonts this post actually uses from its saved attributes,
311 // cache them in post meta, and self-host them locally.
312 // See docs/FONT-MANAGEMENT-PLAN.md.
313 \ABlocks\Classes\FontCollector::save_post_fonts( $post_id );
314
315 // Templates, template parts and theme builder layouts render on pages that
316 // are not the queried post, so their fonts are tracked site-wide. Recompute
317 // (and download) here rather than on a visitor's request.
318 if ( in_array( get_post_type( $post_id ), \ABlocks\Classes\FontCollector::get_site_font_post_types(), true ) ) {
319 \ABlocks\Classes\FontCollector::update_site_fonts();
320 }
321 }
322
323 /**
324 * Deleting a template/part/layout can remove the last user of a font.
325 *
326 * @param int $post_id Post being deleted.
327 * @param \WP_Post $post The deleted post — by this point it is gone from the
328 * database, so the type has to come from here.
329 */
330 public function flush_site_fonts_on_delete( $post_id, $post = null ) {
331 $post_type = $post instanceof \WP_Post ? $post->post_type : get_post_type( $post_id );
332
333 if ( in_array( $post_type, \ABlocks\Classes\FontCollector::get_site_font_post_types(), true ) ) {
334 \ABlocks\Classes\FontCollector::flush_site_fonts();
335 }
336 }
337
338 /**
339 * Pre-warm a published page's combined CSS/JS in the background after save,
340 * via a non-blocking loopback request. This moves the parse/generate/write
341 * cost off the first visitor's request (PERF-2). If loopback is unavailable,
342 * the existing on-request generation still runs as a fallback.
343 */
344 public function prewarm_page_assets( $post_id, $post, $update ) {
345 if ( ! Helper::is_enabled_assets_generation() ) {
346 return;
347 }
348 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
349 return;
350 }
351 if ( false !== wp_is_post_revision( $post_id ) ) {
352 return;
353 }
354 if ( ! ( $post instanceof \WP_Post ) ) {
355 $post = get_post( $post_id );
356 }
357 if ( ! $post || 'publish' !== $post->post_status || ! is_post_type_viewable( $post->post_type ) ) {
358 return;
359 }
360 // Only for content that actually has aBlocks (or wrapping) blocks.
361 if ( empty( $post->post_content )
362 || ( false === strpos( $post->post_content, 'wp:ablocks' ) && false === strpos( $post->post_content, 'wp:block' ) ) ) {
363 return;
364 }
365 if ( ! (bool) apply_filters( 'ablocks/perf/prewarm_assets', true, $post_id ) ) {
366 return;
367 }
368
369 $url = get_permalink( $post_id );
370 if ( ! $url ) {
371 return;
372 }
373
374 wp_remote_get(
375 $url,
376 [
377 'blocking' => false,
378 'timeout' => 0.01,
379 'sslverify' => false,
380 'headers' => [ 'X-ABlocks-Prewarm' => '1' ],
381 ]
382 );
383 }
384
385 /**
386 * Recompute the global typography font set after settings are saved.
387 * Re-primes the in-memory settings so the collector reads fresh values.
388 */
389 public function refresh_global_fonts() {
390 $GLOBALS['ablocks_settings'] = json_decode( get_option( ABLOCKS_SETTINGS_NAME, '{}' ) );
391 \ABlocks\Classes\FontCollector::update_global_fonts();
392 }
393
394 /**
395 * Invalidate the cached global CSS variables (see Assets::global_css_variable).
396 */
397 public function clear_global_css_cache() {
398 delete_transient( 'ablocks_global_css_vars' );
399 }
400
401 public function form_builder_default_data(): void {
402 add_filter(
403 'ablocks/assets/editor_scripts_data',
404 [ FormBuilderHelper::class, 'form_builder_default_data' ]
405 );
406 }
407 public function register_block_sanitizer(): void {
408 BlockSanitizer::init();
409 }
410
411 public function clear_generated_block_assets() {
412 $uploader = new \ABlocks\Classes\FileUpload();
413 $uploader->delete_files();
414 Helper::clear_third_party_plugin_cache();
415 }
416 }
417