|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Services: AMP Services. |
|
4
|
|
|
* |
|
5
|
|
|
* The file defines a class for AMP related manipulations. |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://wordlift.io |
|
8
|
|
|
* |
|
9
|
|
|
* @since 3.12.0 |
|
10
|
|
|
* |
|
11
|
|
|
* @package Wordlift |
|
12
|
|
|
* @subpackage Wordlift/public |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Handles AMP related manipulation in the generated HTML of entity pages |
|
17
|
|
|
* |
|
18
|
|
|
* @since 3.12.0 |
|
19
|
|
|
* @package Wordlift |
|
20
|
|
|
* @subpackage Wordlift/public |
|
21
|
|
|
*/ |
|
22
|
|
|
class Wordlift_AMP_Service { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The {@link \Wordlift_Jsonld_Service} instance. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 3.19.1 |
|
28
|
|
|
* @access private |
|
29
|
|
|
* @var \Wordlift_Jsonld_Service $jsonld_service The {@link \Wordlift_Jsonld_Service} instance. |
|
30
|
|
|
*/ |
|
31
|
|
|
private $jsonld_service; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create a {@link Wordlift_AMP_Service} instance. |
|
35
|
|
|
* @since 3.19.1 |
|
36
|
|
|
* |
|
37
|
|
|
* @param \Wordlift_Jsonld_Service $jsonld_service |
|
38
|
|
|
*/ |
|
39
|
|
|
function __construct( $jsonld_service ) { |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$this->jsonld_service = $jsonld_service; |
|
42
|
|
|
|
|
43
|
|
|
add_action( 'amp_init', array( $this, 'register_entity_cpt_with_amp_plugin', ) ); |
|
44
|
|
|
add_filter( 'amp_post_template_metadata', array( $this, 'amp_post_template_metadata', ), 99, 2 ); |
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Register the `entity` post type with the AMP plugin. |
|
50
|
|
|
* |
|
51
|
|
|
* @since 3.12.0 |
|
52
|
|
|
*/ |
|
53
|
|
|
function register_entity_cpt_with_amp_plugin() { |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
if ( ! defined( 'AMP_QUERY_VAR' ) ) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $post_type ) { |
|
60
|
|
|
// Do not change anything for posts and pages. |
|
61
|
|
|
if ( 'post' === $post_type || 'page' === $post_type ) { |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
add_post_type_support( $post_type, AMP_QUERY_VAR ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Filters Schema.org metadata for a post. |
|
71
|
|
|
* |
|
72
|
|
|
* @since 3.19.1 |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $metadata Metadata. |
|
75
|
|
|
* @param WP_Post $post Post. |
|
76
|
|
|
* |
|
77
|
|
|
* @return array Return WordLift's generated JSON-LD. |
|
78
|
|
|
*/ |
|
79
|
|
|
function amp_post_template_metadata( $metadata, $post ) { |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
return $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Check if current page is amp endpoint. |
|
86
|
|
|
* |
|
87
|
|
|
* @since 3.20.0 |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function is_amp_endpoint() { |
|
92
|
|
|
return function_exists('is_amp_endpoint') && is_amp_endpoint(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.