-
Notifications
You must be signed in to change notification settings - Fork 19
/
better-amp.php
1650 lines (1239 loc) · 37.6 KB
/
better-amp.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Better AMP - WordPress Complete AMP
Plugin URI: https://betterstudio.com/wp-plugins/better-amp/
Description: Add FULL AMP support to your WordPress site.
Author: Better Studio
Version: 1.13.0
Author URI: http://betterstudio.com
*/
/***
* BetterAMP is BetterStudio solution for implementing Google AMP completely in WordPress.
*
* ______ _ _ ___ ___ ________
* | ___ \ | | | | / _ \| \/ | ___ \
* | |_/ / ___| |_| |_ ___ _ __/ /_\ \ . . | |_/ /
* | ___ \/ _ \ __| __/ _ \ '__| _ | |\/| | __/
* | |_/ / __/ |_| || __/ | | | | | | | | |
* \____/ \___|\__|\__\___|_| \_| |_|_| |_|_|
*
* Copyright © 2017 Better Studio
*
*
* Our portfolio is here: http://themeforest.net/user/Better-Studio/portfolio
*
* \--> BetterStudio, 2017 <--/
*/
// Fire up!
Better_AMP::get_instance();
/**
* Main class for BetterAMP
*
* @since 1.0.0
*/
class Better_AMP {
/**
* Main Better AMP instance
*
* @since 1.0.0
*
* @var self
*/
private static $instance;
/**
* Better AMP version number
*
* @since 1.0.0
*/
const VERSION = '1.13.0';
/**
* Default endpoint for AMP URL of site.
*
* @since 1.9.0
*/
const SLUG = 'amp';
/**
* @since 1.0.0
*/
const STARTPOINT = self::SLUG;
/**
* Default template directory
* This can be overridden by filter
*
* @since 1.0.0
*/
const TEMPLATE_DIR = 'better-amp';
/**
* pre_get_posts hook priority
*
* @since 1.1
*/
const ISOLATE_QUERY_HOOK_PRIORITY = 100;
/**
* Store array of posts id to exlucde transform permalinks to amp
*
* Array structure: array {
* 'post id' => dont care,
* ...
* }
*
* @see transform_post_link_to_amp
*
* @var array
*/
public $excluded_posts_id = array();
/**
* Get live instance of Better AMP
*
* @since 1.1
* @return self
*/
public static function get_instance() {
if ( ! self::$instance instanceof self ) {
self::$instance = new self();
self::$instance->init();
}
return self::$instance;
} // get_instance
/**
* Cloning is forbidden.
*
* @since 1.0.0
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cloning Better_AMP is forbidden', 'better-amp' ), '' );
}
/**
* Unserializing is forbidden.
*
* @since 1.0.0
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Unserializing Better_AMP is forbidden', 'better-amp' ), '' );
}
/**
* Initialize
*
* @since 1.0.0
*/
public function init() {
$this->initial_constants();
$this->load_text_domain();
$this->register_autoload();
$this->include_files();
$this->apply_hooks();
$this->admin_hooks();
$this->metaboxes();
}
/**
* Define core constants
*
* @since 1.0.0
*/
protected function initial_constants() {
define( 'BETTER_AMP_PATH', dirname( __FILE__ ) . '/' );
define( 'BETTER_AMP_INC', dirname( __FILE__ ) . '/includes/' );
define( '__BETTER_AMP_FILE__', __FILE__ );
define( 'BETTER_AMP_OVERRIDE_TPL_DIR', apply_filters( 'better-amp/template/dir-name', Better_AMP::TEMPLATE_DIR ) );
define( 'BETTER_AMP_TPL_COMPAT_ABSPATH', BETTER_AMP_PATH . 'theme-compat/' );
}
/**
* Load plugin textdomain file
*
* @since 1.0.0
*/
protected function load_text_domain() {
load_plugin_textdomain( 'better-amp', false, plugin_basename( BETTER_AMP_PATH ) . '/languages' );
}
/**
* Include Dependencies
*
* @since 1.0.0
*/
protected function include_files() {
require_once BETTER_AMP_PATH . 'bootstrap.php';
}
/**
* Register WP filters and actions
*
* @since 1.0.0
*/
protected function apply_hooks() {
// Registers the AMP rewrite rules
add_action( 'init', array( $this, 'add_rewrite' ) );
add_action( 'init', array( $this, 'append_index_rewrite_rule' ) );
add_action( 'template_redirect', array( $this, 'plugins_compatibility' ) );
// Initialize AMP components
add_action( 'init', array( $this, 'include_components' ) );
// Changes page template file with AMP template file
add_filter( 'template_include', array( $this, 'include_template_file' ), 9999 );
// override template file
add_filter( 'comments_template', array( $this, 'override_comments_template' ), 9999 );
// Initialize AMP theme and it's functionality
add_action( 'after_setup_theme', array( $this, 'include_template_functions_php' ), 1 );
// Register the AMP special shortcode ports
add_filter( 'the_content', array( $this, 'register_components_shortcodes' ), 1 );
// Replace all links inside contents to AMP version.
// Stops user to go outside of AMP version.
add_action( 'wp', array( $this, 'replace_internal_links_with_amp_version' ) );
// Registers all components scripts into the header style and scripts
add_action( 'better-amp/template/enqueue-scripts', array( $this, 'enqueue_components_scripts' ) );
// Let the components to do their functionality in head
add_action( 'better-amp/template/head', array( $this, 'trigger_component_head' ), 0 );
// Collect all output to can enqueue only needed scripts and styles in pages.
add_filter( 'template_include', array( $this, 'buffer_better_amp_start' ), 1 );
add_action( 'better-amp/template/footer', array( $this, 'buffer_better_amp_end' ), 999 );
// Collect and rollback all main query posts to disable thirdparty codes to change main query!
// action after 1000 priority can work
add_action( 'pre_get_posts', array( $this, 'isolate_pre_get_posts_start' ), 1 );
add_action( 'pre_get_posts', array( $this, 'isolate_pre_get_posts_end' ), self::ISOLATE_QUERY_HOOK_PRIORITY );
add_action( 'pre_get_posts', array( $this, 'compatible_plugins_themes' ), 0 );
register_activation_hook( __FILE__, array( $this, 'install' ) );
add_filter( 'request', array( $this, 'fix_search_page_queries' ) );
add_filter( 'redirect_canonical', array( $this, '_fix_prevent_extra_redirect_single_pagination' ) );
// Auto Redirect Mobile Users
add_action( 'template_redirect', array( $this, 'auto_redirect_to_amp' ), 1 );
// Init BF JSON-LD
add_action( 'template_redirect', 'Better_AMP::init_json_ld', 1 );
$this->fix_front_page_display_options();
// Fire the modules
Better_Amp_Redirect_Router::Run();
Better_AMP_Custom_Script::Run();
Better_AMP_Panel::Run();
} // apply_hooks
/**
* Get WordPress option
*
* @param string $option
* @param mixed $default
*
* @since 1.3.0
* @return mixed
*/
public static function get_option( $option, $default = false ) {
$tmp = isset( $GLOBALS['_amp_bypass_option'] ) ? $GLOBALS['_amp_bypass_option'] : false;
$GLOBALS['_amp_bypass_option'] = true;
$results = get_option( $option, $default );
$GLOBALS['_amp_bypass_option'] = $tmp;
return $results;
}
/**
* Fix front page display option to detect homepage
*/
public function fix_front_page_display_options() {
add_filter( 'pre_option_page_on_front', array( $this, '_return_zero_in_amp' ) );
add_filter( 'pre_option_show_on_front', array( $this, '_fix_show_on_front' ) );
}
/**
* Prevent redirect pages within single post
*
* @param $redirect
*
* @since 1.1
* @return bool
*/
public function _fix_prevent_extra_redirect_single_pagination( $redirect ) {
if ( $redirect && is_better_amp() && get_query_var( 'page' ) > 1 ) {
return false;
}
return $redirect;
}
public function admin_hooks() {
if ( ! is_admin() ) {
return;
}
if ( get_transient( 'better-amp-flush-rules' ) ) {
add_action( 'admin_init', 'flush_rewrite_rules' );
delete_transient( 'better-amp-flush-rules' );
}
add_action( 'admin_head', array( $this, 'admin_styles' ) );
add_action( 'admin_menu', array( $this, 'fix_admin_sub_menu' ), 999 );
}
/**
* Register rewrite rules and flush permalink in installation
*
* @since 1.0.0
*/
public function install() {
$this->add_rewrite();
set_transient( 'better-amp-flush-rules', true );
$this->include_template_functions_php();
if ( Better_AMP_Panel::Run()->is_fresh_install() ) {
Better_AMP_Panel::Run()->panel_reset();
}
}
/**
* Check AMP version of the posts exists
*
* @since 1.1
* @return bool of exists
*/
public function amp_version_exists() {
static $filters;
if ( $this->is_amp_excluded_by_url() ) {
return apply_filters( 'better-amp/amp-version-exists', false );
}
if ( ! isset( $filters ) ) {
$filters = wp_parse_args(
apply_filters( 'better-amp/filter/config', array() ),
array(
'disabled_post_types' => array(),
'disabled_taxonomies' => array(),
'disabled_homepage' => false,
'disabled_search' => false,
)
);
}
if ( is_singular() ) {
$post_id = get_queried_object_id();
} elseif ( is_home() && better_amp_is_static_home_page() ) {
$post_id = intval( apply_filters( 'better-amp/template/page-on-front', 0 ) );
} else {
$post_id = 0;
}
if ( $post_id ) {
if ( get_post_meta( $post_id, 'disable-better-amp', true ) || isset( $this->excluded_posts_id[ $post_id ] ) ) {
return apply_filters( 'better-amp/amp-version-exists', false );
}
}
if ( empty( $filters ) ) {
return apply_filters( 'better-amp/amp-version-exists', true );
}
if ( is_home() || is_front_page() ) {
return apply_filters( 'better-amp/amp-version-exists', ! $filters['disabled_homepage'] );
}
if ( is_search() ) {
return apply_filters( 'better-amp/amp-version-exists', ! $filters['disabled_search'] );
}
if ( is_singular() ) {
return apply_filters( 'better-amp/amp-version-exists', ! in_array( get_queried_object()->post_type, $filters['disabled_post_types'] ) );
}
if ( is_post_type_archive() ) {
$queried_object = get_queried_object();
if ( $queried_object instanceof WP_Post_Type ) { # WP >= 4.6.0
$post_type = $queried_object->name;
} elseif ( $queried_object instanceof WP_Post ) { # WP < 4.6.0
$post_type = $queried_object->post_type;
} else {
return apply_filters( 'better-amp/amp-version-exists', false );
}
return apply_filters( 'better-amp/amp-version-exists', ! in_array( $post_type, $filters['disabled_post_types'] ) );
}
if ( is_tax() || is_category() || is_tag() ) {
return apply_filters( 'better-amp/amp-version-exists', ! in_array( get_queried_object()->taxonomy, $filters['disabled_taxonomies'] ) );
}
return apply_filters( 'better-amp/amp-version-exists', true );
}
/**
* Whether to check if current page has been marked as none-AMP version?
*
* @since 1.9.8
*
* @return bool
*/
protected function is_amp_excluded_by_url() {
if ( ! $excluded_patterns = better_amp_excluded_urls_format() ) {
return false;
}
// Get current page
$current_path = trim( str_replace( home_url(), '', better_amp_guess_none_amp_url() ), '/' );
foreach ( $excluded_patterns as $url_format ) {
if ( empty( $url_format ) ) {
continue;
}
$url_format = trim( $url_format, '/' ); // throw surrounded slash away
// Format given url to valid PCRE regex
$pattern = better_amp_transpile_text_to_pattern( $url_format, '#' );
$pattern = '#^/?' . $pattern . '/*$#i';
// Check if the given url is match with current page url path
if ( preg_match( $pattern, $current_path ) ) {
return true;
}
}
return false;
}
/**
* Callback: Prevent third party codes to change the main query on AMP version
*
* You can add action to 'pre_get_posts' with priority grater than 1000 to change it.
*
* Action: pre_get_posts
*
* @see isolate_pre_get_posts_end
*
* @since 1.0.0
*
* @param WP_Query $wp_query
*/
public function isolate_pre_get_posts_start( $wp_query ) {
global $better_amp_isolate_pre_get_posts;
if ( is_better_amp( $wp_query ) && ! is_admin() && $wp_query->is_main_query() ) {
$better_amp_isolate_pre_get_posts = $wp_query->query_vars;
}
}
/**
* Rollback the main query vars.
*
* @param WP_Query $wp_query
*
* @since 1.0.0
*
* @see isolate_pre_get_posts_end for more documentation
*
* Action: pre_get_posts
*/
public function isolate_pre_get_posts_end( &$wp_query ) {
global $better_amp_isolate_pre_get_posts;
if ( is_better_amp( $wp_query ) && ! is_admin() && $wp_query->is_main_query() ) {
if ( $better_amp_isolate_pre_get_posts ) {
$wp_query->query_vars = $better_amp_isolate_pre_get_posts;
unset( $better_amp_isolate_pre_get_posts );
}
}
}
/**
* Register missed hooks for supported plugins/themes to bypass isolation
*
* @see isolate_pre_get_posts_start
* @see isolate_pre_get_posts_end
*
* @since 1.1
*/
public function compatible_plugins_themes() {
if ( ! is_better_amp() ) {
return;
}
$priority = self::ISOLATE_QUERY_HOOK_PRIORITY + 1;
// WooCommerce compatibility
if ( class_exists( 'WooCommerce' ) ) {
$callback = array( WooCommerce::instance()->query, 'pre_get_posts' );
if ( is_callable( $callback ) ) {
add_action( 'pre_get_posts', $callback, $priority );
}
// WooCommerce didn't change main query in "shop" page
add_action( 'pre_get_posts', array( $this, '_fix_woocommerce_shop_page_query' ), $priority + 1 );
$this->excluded_posts_id[ wc_get_page_id( 'checkout' ) ] = true;
}
// BetterStudio themes compatibility
add_action( 'better-framework/menu/walker/init', array( $this, 'disable_bf_mega_menu' ) );
}
/**
* Fixes global WP_Query for shop pages in AMP!
*
* @param $q
*/
function _fix_woocommerce_shop_page_query( $q ) {
// We only want to affect the main query
if ( ! $q->is_main_query() ) {
return;
}
if ( isset( $q->queried_object->ID ) && $q->queried_object->ID === wc_get_page_id( 'shop' ) ) {
$q->set( 'post_type', 'product' );
$q->set( 'posts_per_page', 8 );
$q->set( 'page', '' );
$q->set( 'pagename', '' );
// Fix conditional Functions
$q->is_archive = true;
$q->is_post_type_archive = true;
$q->is_singular = false;
$q->is_page = false;
}
}
/**
* Disable BetterStudio themes mega menu in AMP pages
*
* @param BF_Menu_Walker $walker
*
* @since 1.1
*/
public function disable_bf_mega_menu( &$walker ) {
$fields = $walker->get_mega_menu_fields_id();
unset( $fields['mega_menu'] );
$walker->set_mega_menu_fields_id( $fields );
}
/**
* Callback: Add rewrite rules
* Action: init
*
* @since 1.0.0
*/
public function add_rewrite() {
better_amp_add_rewrite_startpoint( self::STARTPOINT, EP_ALL );
/**
* Automattic amp compatibility
*/
$amp_qv = defined( 'AMP_QUERY_VAR' ) ? AMP_QUERY_VAR : self::STARTPOINT;
better_amp_add_rewrite_endpoint( $amp_qv, EP_ALL );
}
/**
* Add a rewrite rule to detect site.com/amp/ requests
*
* @since 1.0.0
*
* @return array
*/
public function append_index_rewrite_rule() {
add_rewrite_rule( self::STARTPOINT . '/?$', "index.php?amp=index", 'top' );
}
/**
* Callback: Include AMP template file in AMP pages
* Action : template_include
*
* @param string $template_file_path original template file path
*
* @since 1.0.0
*
* @return string
*/
public function include_template_file( $template_file_path ) {
if ( ! is_better_amp() ) {
return $template_file_path;
}
$include = $this->template_loader();
if ( $include = apply_filters( 'better-amp/template/include', $include ) ) {
return $include;
} elseif ( current_user_can( 'switch_themes' ) ) {
wp_die( __( 'Better-AMP Theme Was Not Found!', 'better-amp' ) );
} else {
return BETTER_AMP_TPL_COMPAT_ABSPATH . '/no-template.php';
}
}
/**
* Include WooCommerce AMP templates in AMP pages
*
* @param string $located
* @param string $template_name
*
* @since 1.1
* @return string
*/
public function include_wc_template_file( $located, $template_name ) {
$template_name = 'woocommerce/' . ltrim( $template_name, '/' );
if ( $new_path = better_amp_locate_template( $template_name, false, false ) ) {
return $new_path;
}
return $located;
}
/**
* Replace amp comment file with theme file
*
* @param string $file
*
* @since 1.1
* @return string
*/
public function override_comments_template( $file ) {
if ( is_better_amp() ) {
if ( $path = better_amp_locate_template( basename( $file ) ) ) {
return $path;
}
}
return $file;
}
/**
* Get AMP template file base on page of WordPress
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
* @copyright credit goes to WordPress team @see wp-includes/template-loader.php
* @access private
*
* @since 1.0.0
*
* @return string
*/
protected function template_loader() {
if ( function_exists( 'is_embed' ) && is_embed() && $template = better_amp_embed_template() ) :
elseif ( function_exists( 'is_woocommerce' ) && is_woocommerce() && is_page( wc_get_page_id( 'shop' ) ) && $template = better_amp_locate_template( 'woocommerce.php' ) ) :
elseif ( is_404() && $template = better_amp_404_template() ) :
elseif ( is_search() && $template = better_amp_search_template() ) :
elseif ( better_amp_is_static_home_page() && $template = better_amp_static_home_page_template() ) :
$this->set_page_query( apply_filters( 'better-amp/template/page-on-front', 0 ) );
elseif ( is_front_page() && $template = better_amp_front_page_template() ) :
elseif ( is_home() && $template = better_amp_home_template() ) :
elseif ( is_post_type_archive() && $template = better_amp_post_type_archive_template() ) :
elseif ( is_tax() && $template = better_amp_taxonomy_template() ) :
elseif ( is_attachment() && $template = better_amp_attachment_template() ) :
remove_filter( 'the_content', 'prepend_attachment' );
elseif ( is_single() && $template = better_amp_single_template() ) :
elseif ( is_page() && $template = better_amp_page_template() ) :
elseif ( is_singular() && $template = better_amp_singular_template() ) :
elseif ( is_category() && $template = better_amp_category_template() ) :
elseif ( is_tag() && $template = better_amp_tag_template() ) :
elseif ( is_author() && $template = better_amp_author_template() ) :
elseif ( is_date() && $template = better_amp_date_template() ) :
elseif ( is_archive() && $template = better_amp_archive_template() ) :
elseif ( is_paged() && $template = better_amp_paged_template() ) :
else :
$template = better_amp_index_template();
endif;
return $template;
}
/**
* Include active template functions.php file if exits
*
* Callback: include
* action : after_setup_theme
*
* @since 1.0.0
*/
public function include_template_functions_php() {
if ( $theme_root = better_amp_get_template_directory() ) {
if ( file_exists( $theme_root . '/functions.php' ) ) {
include $theme_root . '/functions.php';
}
apply_filters( 'better-amp/template/init', $theme_root );
}
}
/**
* Callback: Include registered AMP components
* Action: init
*
* @since 1.0.0
*/
public function include_components() {
include BETTER_AMP_INC . 'components/class-better-amp-img-component.php';
include BETTER_AMP_INC . 'components/class-better-amp-iframe-component.php';
include BETTER_AMP_INC . 'components/class-better-amp-instagram-component.php';
include BETTER_AMP_INC . 'components/class-better-amp-carousel-component.php';
include BETTER_AMP_INC . 'components/class-better-amp-playbuzz-component.php';
}
/**
* Transforms HTML content to AMP content
*
* todo: Add file caching
*
* @param Better_AMP_HTML_Util $instance
* @param boolean $sanitize
*
* @since 1.0.0
*/
public function render_content( Better_AMP_HTML_Util $instance, $sanitize = false ) {
$this->call_components_method( 'render', $instance );
if ( $sanitize ) {
$sanitizer = new Better_AMP_Content_Sanitizer( $instance );
$sanitizer->sanitize();
}
} // render_content
/**
* Register an autoloader for Better AMP classes
*
* @since 1.0.0
*/
public function register_autoload() {
spl_autoload_register( array( __CLASS__, 'autoload_amp_classes' ) );
}
/**
* Autoload handler for better AMP classes only
*
* @param string $class_name class to include
*
* @since 1.0.0
*/
public static function autoload_amp_classes( $class_name ) {
if ( substr( $class_name, 0, 11 ) !== 'Better_AMP_' ) {
return;
}
$is_interface = substr( $class_name, - 10 ) === '_Interface';
$class_name_prefix = $is_interface ? 'interface-' : 'class-';
$sanitized_class_name = strtolower( $class_name );
$sanitized_class_name = str_replace( '_', '-', $sanitized_class_name );
// Remove interface suffix
if ( $is_interface ) {
$sanitized_class_name = substr( $sanitized_class_name, 0, - 10 );
}
$class_file = BETTER_AMP_INC . 'classes/' . $class_name_prefix . $sanitized_class_name . '.php';
if ( file_exists( $class_file ) ) {
require_once $class_file;
}
}
/**
* Transform HTML content to AMP version when AMP version requested
*
* @param string $content html
*
* @since 1.0.0
*
* @return string
*/
public function convert_content_to_amp( $content ) {
if ( is_better_amp() ) {
$content = $this->render_content( $content );
}
return $content;
} // convert_content_to_amp
/**
* Determines that method exists and is callable on object instance
*
* @param Better_AMP_Component $instance Live object of Better_AMP_Component
* @param string $method_name Method of object
* @param array $args
*
* @since 1.0.0
*
* @return bool
*/
private function _can_call_component_method( &$instance, &$method_name, &$args ) {
$return = is_callable( array( $instance, $method_name ) );
switch ( $method_name ) {
case 'enqueue_amp_scripts':
$return = $return && $instance->can_enqueue_scripts();
break;
}
return $return;
}
/**
* Fire specific method of all components
*
* @param string $method_name component method
*
* @param mixed $param
*
* @return mixed
* @since 1.0.0
*
*/
public function call_components_method( $method_name, $param = null ) {
global $better_amp_registered_components;
if ( ! $better_amp_registered_components ) {
return $param;
}
// collect and prepare method arguments
$args = func_get_args();
$args = array_slice( $args, 1 );
if ( ! isset( $args[0] ) ) {
$args[0] = null;
}
// iterate registered components and call method on them
foreach ( $better_amp_registered_components as $component ) {
$instance = Better_AMP_Component::instance( $component['component_class'] );
if ( $this->_can_call_component_method( $instance, $method_name, $args ) ) {
$args[0] = call_user_func_array( array( $instance, $method_name ), $args );
}
}
return $args[0];
}
/**
* Add components shortcode before do_shortcodes
*
* @param string $content
*
* @since 1.0.0
*
* @return string
*/
public function register_components_shortcodes( $content = '' ) {
if ( ! is_better_amp() ) {
return $content;
}
$this->call_components_method( 'register_shortcodes' );
return $content;
}
/**
* Callback: Fire head method of component for following purpose:
* 1) Component able to add_filter or add_action if needed
* 2) Create fresh instance of each component and cache
*
* Action : better-amp/template/head
*
* @since 1.0.0
*/
public function trigger_component_head() {
$this->call_components_method( 'head' );
}
/**
* Clean components instances and free up the memory
*
* @param string $content
*
* @since 1.0.0
*