@@ -18,118 +18,118 @@ |
||
18 | 18 | */ |
19 | 19 | class Wordlift_Schemaorg_Class_Service { |
20 | 20 | |
21 | - /** |
|
22 | - * The term meta key holding the CamelCase name for the term. The term has also a WP_Term->name |
|
23 | - * property which however is to be considered a customizable label (especially for languages other |
|
24 | - * than English). |
|
25 | - * |
|
26 | - * @since 3.20.0 |
|
27 | - */ |
|
28 | - const NAME_META_KEY = '_wl_name'; |
|
29 | - |
|
30 | - /** |
|
31 | - * The term meta key holding the term URI. |
|
32 | - * |
|
33 | - * @since 3.20.0 |
|
34 | - */ |
|
35 | - const URI_META_KEY = '_wl_uri'; |
|
36 | - |
|
37 | - /** |
|
38 | - * The term meta key holding the list of children terms ids. |
|
39 | - * |
|
40 | - * @since 3.20.0 |
|
41 | - */ |
|
42 | - const PARENT_OF_META_KEY = '_wl_parent_of'; |
|
43 | - |
|
44 | - /** |
|
45 | - * The singleton instance. |
|
46 | - * |
|
47 | - * @since 3.20.0 |
|
48 | - * @access private |
|
49 | - * @var \Wordlift_Schemaorg_Class_Service $instance The singleton instance. |
|
50 | - */ |
|
51 | - private static $instance; |
|
52 | - |
|
53 | - /** |
|
54 | - * Create a {@link Wordlift_Schema_Class} instance. |
|
55 | - * |
|
56 | - * @since 3.20.0 |
|
57 | - */ |
|
58 | - public function __construct() { |
|
59 | - |
|
60 | - add_action( 'wp_ajax_wl_schemaorg_class', array( $this, 'schemaorg_class' ) ); |
|
61 | - |
|
62 | - self::$instance = $this; |
|
63 | - |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Get the singleton instance. |
|
68 | - * |
|
69 | - * @since 3.20.0 |
|
70 | - * @return \Wordlift_Schemaorg_Class_Service The singleton instance. |
|
71 | - */ |
|
72 | - public static function get_instance() { |
|
73 | - |
|
74 | - return self::$instance; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * The end-point to output the list of terms from the `wl_entity_taxonomy`. |
|
79 | - * |
|
80 | - * Example output: |
|
81 | - * ``` |
|
82 | - * { |
|
83 | - * "name": "AMRadioChannel", |
|
84 | - * "dashname": "am-radio-channel", |
|
85 | - * "description": "A radio channel that uses AM.", |
|
86 | - * "children": [] |
|
87 | - * } |
|
88 | - * ``` |
|
89 | - * |
|
90 | - * @since 3.20.0 |
|
91 | - */ |
|
92 | - public function schemaorg_class() { |
|
93 | - |
|
94 | - // Since we want to be compatible with WP 4.4, we use the pre-4.5.0 style when |
|
95 | - // calling `get_terms`. |
|
96 | - $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) ); |
|
97 | - |
|
98 | - // PHP 5.3 compat. |
|
99 | - $name_meta_key = self::NAME_META_KEY; |
|
100 | - $parent_of_meta_key = self::PARENT_OF_META_KEY; |
|
101 | - |
|
102 | - $json = array_map( |
|
103 | - function ( $term ) use ( $name_meta_key, $parent_of_meta_key ) { |
|
104 | - // Do not change the following, the `name` is used to reference the correct |
|
105 | - // Schema.org class (CamelCase name). Do not use WP_Term->name. |
|
106 | - $camel_case_name = get_term_meta( $term->term_id, $name_meta_key, true ); |
|
107 | - |
|
108 | - return array( |
|
109 | - 'id' => $term->term_id, |
|
110 | - // Do not change the following, the `name` is used to reference the correct |
|
111 | - // Schema.org class (CamelCase name). Do not use WP_Term->name. |
|
112 | - 'name' => $camel_case_name, |
|
113 | - 'dashname' => $term->slug, |
|
114 | - 'description' => $term->description, |
|
115 | - 'children' => array_map( |
|
116 | - function ( $child ) { |
|
117 | - // Map the slug to the term id. |
|
118 | - $child_term = get_term_by( 'slug', $child, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
119 | - |
|
120 | - return array( 'id' => $child_term->term_id ); |
|
121 | - }, |
|
122 | - get_term_meta( $term->term_id, $parent_of_meta_key ) |
|
123 | - ), |
|
124 | - ); |
|
125 | - |
|
126 | - }, |
|
127 | - $terms |
|
128 | - ); |
|
129 | - |
|
130 | - // Finally send the data. |
|
131 | - wp_send_json_success( array( 'schemaClasses' => $json ) ); |
|
132 | - |
|
133 | - } |
|
21 | + /** |
|
22 | + * The term meta key holding the CamelCase name for the term. The term has also a WP_Term->name |
|
23 | + * property which however is to be considered a customizable label (especially for languages other |
|
24 | + * than English). |
|
25 | + * |
|
26 | + * @since 3.20.0 |
|
27 | + */ |
|
28 | + const NAME_META_KEY = '_wl_name'; |
|
29 | + |
|
30 | + /** |
|
31 | + * The term meta key holding the term URI. |
|
32 | + * |
|
33 | + * @since 3.20.0 |
|
34 | + */ |
|
35 | + const URI_META_KEY = '_wl_uri'; |
|
36 | + |
|
37 | + /** |
|
38 | + * The term meta key holding the list of children terms ids. |
|
39 | + * |
|
40 | + * @since 3.20.0 |
|
41 | + */ |
|
42 | + const PARENT_OF_META_KEY = '_wl_parent_of'; |
|
43 | + |
|
44 | + /** |
|
45 | + * The singleton instance. |
|
46 | + * |
|
47 | + * @since 3.20.0 |
|
48 | + * @access private |
|
49 | + * @var \Wordlift_Schemaorg_Class_Service $instance The singleton instance. |
|
50 | + */ |
|
51 | + private static $instance; |
|
52 | + |
|
53 | + /** |
|
54 | + * Create a {@link Wordlift_Schema_Class} instance. |
|
55 | + * |
|
56 | + * @since 3.20.0 |
|
57 | + */ |
|
58 | + public function __construct() { |
|
59 | + |
|
60 | + add_action( 'wp_ajax_wl_schemaorg_class', array( $this, 'schemaorg_class' ) ); |
|
61 | + |
|
62 | + self::$instance = $this; |
|
63 | + |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Get the singleton instance. |
|
68 | + * |
|
69 | + * @since 3.20.0 |
|
70 | + * @return \Wordlift_Schemaorg_Class_Service The singleton instance. |
|
71 | + */ |
|
72 | + public static function get_instance() { |
|
73 | + |
|
74 | + return self::$instance; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * The end-point to output the list of terms from the `wl_entity_taxonomy`. |
|
79 | + * |
|
80 | + * Example output: |
|
81 | + * ``` |
|
82 | + * { |
|
83 | + * "name": "AMRadioChannel", |
|
84 | + * "dashname": "am-radio-channel", |
|
85 | + * "description": "A radio channel that uses AM.", |
|
86 | + * "children": [] |
|
87 | + * } |
|
88 | + * ``` |
|
89 | + * |
|
90 | + * @since 3.20.0 |
|
91 | + */ |
|
92 | + public function schemaorg_class() { |
|
93 | + |
|
94 | + // Since we want to be compatible with WP 4.4, we use the pre-4.5.0 style when |
|
95 | + // calling `get_terms`. |
|
96 | + $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) ); |
|
97 | + |
|
98 | + // PHP 5.3 compat. |
|
99 | + $name_meta_key = self::NAME_META_KEY; |
|
100 | + $parent_of_meta_key = self::PARENT_OF_META_KEY; |
|
101 | + |
|
102 | + $json = array_map( |
|
103 | + function ( $term ) use ( $name_meta_key, $parent_of_meta_key ) { |
|
104 | + // Do not change the following, the `name` is used to reference the correct |
|
105 | + // Schema.org class (CamelCase name). Do not use WP_Term->name. |
|
106 | + $camel_case_name = get_term_meta( $term->term_id, $name_meta_key, true ); |
|
107 | + |
|
108 | + return array( |
|
109 | + 'id' => $term->term_id, |
|
110 | + // Do not change the following, the `name` is used to reference the correct |
|
111 | + // Schema.org class (CamelCase name). Do not use WP_Term->name. |
|
112 | + 'name' => $camel_case_name, |
|
113 | + 'dashname' => $term->slug, |
|
114 | + 'description' => $term->description, |
|
115 | + 'children' => array_map( |
|
116 | + function ( $child ) { |
|
117 | + // Map the slug to the term id. |
|
118 | + $child_term = get_term_by( 'slug', $child, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
119 | + |
|
120 | + return array( 'id' => $child_term->term_id ); |
|
121 | + }, |
|
122 | + get_term_meta( $term->term_id, $parent_of_meta_key ) |
|
123 | + ), |
|
124 | + ); |
|
125 | + |
|
126 | + }, |
|
127 | + $terms |
|
128 | + ); |
|
129 | + |
|
130 | + // Finally send the data. |
|
131 | + wp_send_json_success( array( 'schemaClasses' => $json ) ); |
|
132 | + |
|
133 | + } |
|
134 | 134 | |
135 | 135 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | */ |
58 | 58 | public function __construct() { |
59 | 59 | |
60 | - add_action( 'wp_ajax_wl_schemaorg_class', array( $this, 'schemaorg_class' ) ); |
|
60 | + add_action('wp_ajax_wl_schemaorg_class', array($this, 'schemaorg_class')); |
|
61 | 61 | |
62 | 62 | self::$instance = $this; |
63 | 63 | |
@@ -93,17 +93,17 @@ discard block |
||
93 | 93 | |
94 | 94 | // Since we want to be compatible with WP 4.4, we use the pre-4.5.0 style when |
95 | 95 | // calling `get_terms`. |
96 | - $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) ); |
|
96 | + $terms = get_terms(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array('get' => 'all')); |
|
97 | 97 | |
98 | 98 | // PHP 5.3 compat. |
99 | 99 | $name_meta_key = self::NAME_META_KEY; |
100 | 100 | $parent_of_meta_key = self::PARENT_OF_META_KEY; |
101 | 101 | |
102 | 102 | $json = array_map( |
103 | - function ( $term ) use ( $name_meta_key, $parent_of_meta_key ) { |
|
103 | + function($term) use ($name_meta_key, $parent_of_meta_key) { |
|
104 | 104 | // Do not change the following, the `name` is used to reference the correct |
105 | 105 | // Schema.org class (CamelCase name). Do not use WP_Term->name. |
106 | - $camel_case_name = get_term_meta( $term->term_id, $name_meta_key, true ); |
|
106 | + $camel_case_name = get_term_meta($term->term_id, $name_meta_key, true); |
|
107 | 107 | |
108 | 108 | return array( |
109 | 109 | 'id' => $term->term_id, |
@@ -113,13 +113,13 @@ discard block |
||
113 | 113 | 'dashname' => $term->slug, |
114 | 114 | 'description' => $term->description, |
115 | 115 | 'children' => array_map( |
116 | - function ( $child ) { |
|
116 | + function($child) { |
|
117 | 117 | // Map the slug to the term id. |
118 | - $child_term = get_term_by( 'slug', $child, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
118 | + $child_term = get_term_by('slug', $child, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
119 | 119 | |
120 | - return array( 'id' => $child_term->term_id ); |
|
120 | + return array('id' => $child_term->term_id); |
|
121 | 121 | }, |
122 | - get_term_meta( $term->term_id, $parent_of_meta_key ) |
|
122 | + get_term_meta($term->term_id, $parent_of_meta_key) |
|
123 | 123 | ), |
124 | 124 | ); |
125 | 125 | |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | ); |
129 | 129 | |
130 | 130 | // Finally send the data. |
131 | - wp_send_json_success( array( 'schemaClasses' => $json ) ); |
|
131 | + wp_send_json_success(array('schemaClasses' => $json)); |
|
132 | 132 | |
133 | 133 | } |
134 | 134 |
@@ -16,119 +16,119 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Schemaorg_Sync_Batch_Operation implements Wordlift_Batch_Operation_Interface { |
18 | 18 | |
19 | - private static $instance = null; |
|
20 | - |
|
21 | - public static function get_instance() { |
|
22 | - |
|
23 | - if ( ! isset( self::$instance ) ) { |
|
24 | - self::$instance = new self(); |
|
25 | - } |
|
26 | - |
|
27 | - return self::$instance; |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * Process the batch operation starting from the specified offset. |
|
32 | - * |
|
33 | - * @param int $offset Start from the specified offset (or 0 if not specified). |
|
34 | - * @param int $limit Process the specified amount of items per call (or 10 if not specified). |
|
35 | - * |
|
36 | - * @return array { |
|
37 | - * The operation result. |
|
38 | - * |
|
39 | - * @type int $next The next offset. |
|
40 | - * @type int $limit The amount of items to process per call. |
|
41 | - * @type int $remaining The remaining number of elements to process. |
|
42 | - * } |
|
43 | - * @since 3.20.0 |
|
44 | - */ |
|
45 | - public function process( $offset = 0, $limit = 10 ) { |
|
46 | - |
|
47 | - // Get the schema classes. |
|
48 | - $all_schema_classes = $this->get_schema_classes(); |
|
49 | - |
|
50 | - // Get only the part that we need to process. |
|
51 | - $schema_classes = array_slice( $all_schema_classes, $offset, $limit ); |
|
52 | - |
|
53 | - // Load the Schema.org classes. |
|
54 | - foreach ( $schema_classes as $schema_class ) { |
|
55 | - $slug = $schema_class['dashname']; |
|
56 | - $term = term_exists( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
57 | - |
|
58 | - $args = array( |
|
59 | - 'parent' => 0, |
|
60 | - 'description' => $schema_class['description'], |
|
61 | - 'slug' => $schema_class['dashname'], |
|
62 | - ); |
|
63 | - if ( null !== $term ) { |
|
64 | - wp_update_term( $term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
65 | - } else { |
|
66 | - $term = wp_insert_term( $schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
67 | - } |
|
68 | - |
|
69 | - // Update the parents/children relationship. |
|
70 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY ); |
|
71 | - foreach ( $schema_class['children'] as $child ) { |
|
72 | - add_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname'] ); |
|
73 | - } |
|
74 | - |
|
75 | - // Update the term name. |
|
76 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY ); |
|
77 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name'] ); |
|
78 | - |
|
79 | - // Update the term URI. |
|
80 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY ); |
|
81 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}" ); |
|
82 | - |
|
83 | - } |
|
84 | - |
|
85 | - // Calculate the return values. |
|
86 | - $next = $offset + $limit; |
|
87 | - $remaining = $this->count(); |
|
88 | - |
|
89 | - return array( |
|
90 | - 'next' => $next, |
|
91 | - 'limit' => $limit, |
|
92 | - 'complete' => ( 0 === $remaining ), |
|
93 | - 'remaining' => $remaining, |
|
94 | - ); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Count the number of elements that would be affected by the operation. |
|
99 | - * |
|
100 | - * @return int The number of elements that would be affected. |
|
101 | - * @since 3.20.0 |
|
102 | - */ |
|
103 | - public function count() { |
|
104 | - |
|
105 | - // Schema Classes count. |
|
106 | - $schema_classes_count = count( $this->get_schema_classes() ); |
|
107 | - |
|
108 | - // Terms count. |
|
109 | - $terms_count = wp_count_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
110 | - |
|
111 | - // Return the difference. |
|
112 | - return $schema_classes_count - $terms_count; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Get the schema.org classes from the JSON file. |
|
117 | - * |
|
118 | - * @return array An array of schema classes. |
|
119 | - * @since 3.20.0 |
|
120 | - */ |
|
121 | - private function get_schema_classes() { |
|
122 | - |
|
123 | - // Load the file contents. |
|
124 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
125 | - $contents = file_get_contents( __DIR__ . '/schema-classes.json' ); |
|
126 | - |
|
127 | - // Decode the JSON contents. |
|
128 | - $json = json_decode( $contents, true ); |
|
129 | - |
|
130 | - // Return the schema classes or an empty array. |
|
131 | - return isset( $json['schemaClasses'] ) ? $json['schemaClasses'] : array(); |
|
132 | - } |
|
19 | + private static $instance = null; |
|
20 | + |
|
21 | + public static function get_instance() { |
|
22 | + |
|
23 | + if ( ! isset( self::$instance ) ) { |
|
24 | + self::$instance = new self(); |
|
25 | + } |
|
26 | + |
|
27 | + return self::$instance; |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * Process the batch operation starting from the specified offset. |
|
32 | + * |
|
33 | + * @param int $offset Start from the specified offset (or 0 if not specified). |
|
34 | + * @param int $limit Process the specified amount of items per call (or 10 if not specified). |
|
35 | + * |
|
36 | + * @return array { |
|
37 | + * The operation result. |
|
38 | + * |
|
39 | + * @type int $next The next offset. |
|
40 | + * @type int $limit The amount of items to process per call. |
|
41 | + * @type int $remaining The remaining number of elements to process. |
|
42 | + * } |
|
43 | + * @since 3.20.0 |
|
44 | + */ |
|
45 | + public function process( $offset = 0, $limit = 10 ) { |
|
46 | + |
|
47 | + // Get the schema classes. |
|
48 | + $all_schema_classes = $this->get_schema_classes(); |
|
49 | + |
|
50 | + // Get only the part that we need to process. |
|
51 | + $schema_classes = array_slice( $all_schema_classes, $offset, $limit ); |
|
52 | + |
|
53 | + // Load the Schema.org classes. |
|
54 | + foreach ( $schema_classes as $schema_class ) { |
|
55 | + $slug = $schema_class['dashname']; |
|
56 | + $term = term_exists( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
57 | + |
|
58 | + $args = array( |
|
59 | + 'parent' => 0, |
|
60 | + 'description' => $schema_class['description'], |
|
61 | + 'slug' => $schema_class['dashname'], |
|
62 | + ); |
|
63 | + if ( null !== $term ) { |
|
64 | + wp_update_term( $term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
65 | + } else { |
|
66 | + $term = wp_insert_term( $schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
67 | + } |
|
68 | + |
|
69 | + // Update the parents/children relationship. |
|
70 | + delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY ); |
|
71 | + foreach ( $schema_class['children'] as $child ) { |
|
72 | + add_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname'] ); |
|
73 | + } |
|
74 | + |
|
75 | + // Update the term name. |
|
76 | + delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY ); |
|
77 | + update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name'] ); |
|
78 | + |
|
79 | + // Update the term URI. |
|
80 | + delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY ); |
|
81 | + update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}" ); |
|
82 | + |
|
83 | + } |
|
84 | + |
|
85 | + // Calculate the return values. |
|
86 | + $next = $offset + $limit; |
|
87 | + $remaining = $this->count(); |
|
88 | + |
|
89 | + return array( |
|
90 | + 'next' => $next, |
|
91 | + 'limit' => $limit, |
|
92 | + 'complete' => ( 0 === $remaining ), |
|
93 | + 'remaining' => $remaining, |
|
94 | + ); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Count the number of elements that would be affected by the operation. |
|
99 | + * |
|
100 | + * @return int The number of elements that would be affected. |
|
101 | + * @since 3.20.0 |
|
102 | + */ |
|
103 | + public function count() { |
|
104 | + |
|
105 | + // Schema Classes count. |
|
106 | + $schema_classes_count = count( $this->get_schema_classes() ); |
|
107 | + |
|
108 | + // Terms count. |
|
109 | + $terms_count = wp_count_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
110 | + |
|
111 | + // Return the difference. |
|
112 | + return $schema_classes_count - $terms_count; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Get the schema.org classes from the JSON file. |
|
117 | + * |
|
118 | + * @return array An array of schema classes. |
|
119 | + * @since 3.20.0 |
|
120 | + */ |
|
121 | + private function get_schema_classes() { |
|
122 | + |
|
123 | + // Load the file contents. |
|
124 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
125 | + $contents = file_get_contents( __DIR__ . '/schema-classes.json' ); |
|
126 | + |
|
127 | + // Decode the JSON contents. |
|
128 | + $json = json_decode( $contents, true ); |
|
129 | + |
|
130 | + // Return the schema classes or an empty array. |
|
131 | + return isset( $json['schemaClasses'] ) ? $json['schemaClasses'] : array(); |
|
132 | + } |
|
133 | 133 | |
134 | 134 | } |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | |
21 | 21 | public static function get_instance() { |
22 | 22 | |
23 | - if ( ! isset( self::$instance ) ) { |
|
23 | + if ( ! isset(self::$instance)) { |
|
24 | 24 | self::$instance = new self(); |
25 | 25 | } |
26 | 26 | |
@@ -42,43 +42,43 @@ discard block |
||
42 | 42 | * } |
43 | 43 | * @since 3.20.0 |
44 | 44 | */ |
45 | - public function process( $offset = 0, $limit = 10 ) { |
|
45 | + public function process($offset = 0, $limit = 10) { |
|
46 | 46 | |
47 | 47 | // Get the schema classes. |
48 | 48 | $all_schema_classes = $this->get_schema_classes(); |
49 | 49 | |
50 | 50 | // Get only the part that we need to process. |
51 | - $schema_classes = array_slice( $all_schema_classes, $offset, $limit ); |
|
51 | + $schema_classes = array_slice($all_schema_classes, $offset, $limit); |
|
52 | 52 | |
53 | 53 | // Load the Schema.org classes. |
54 | - foreach ( $schema_classes as $schema_class ) { |
|
54 | + foreach ($schema_classes as $schema_class) { |
|
55 | 55 | $slug = $schema_class['dashname']; |
56 | - $term = term_exists( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
56 | + $term = term_exists($slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
57 | 57 | |
58 | 58 | $args = array( |
59 | 59 | 'parent' => 0, |
60 | 60 | 'description' => $schema_class['description'], |
61 | 61 | 'slug' => $schema_class['dashname'], |
62 | 62 | ); |
63 | - if ( null !== $term ) { |
|
64 | - wp_update_term( $term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
63 | + if (null !== $term) { |
|
64 | + wp_update_term($term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args); |
|
65 | 65 | } else { |
66 | - $term = wp_insert_term( $schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
66 | + $term = wp_insert_term($schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | // Update the parents/children relationship. |
70 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY ); |
|
71 | - foreach ( $schema_class['children'] as $child ) { |
|
72 | - add_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname'] ); |
|
70 | + delete_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY); |
|
71 | + foreach ($schema_class['children'] as $child) { |
|
72 | + add_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname']); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | // Update the term name. |
76 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY ); |
|
77 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name'] ); |
|
76 | + delete_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY); |
|
77 | + update_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name']); |
|
78 | 78 | |
79 | 79 | // Update the term URI. |
80 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY ); |
|
81 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}" ); |
|
80 | + delete_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY); |
|
81 | + update_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}"); |
|
82 | 82 | |
83 | 83 | } |
84 | 84 | |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | return array( |
90 | 90 | 'next' => $next, |
91 | 91 | 'limit' => $limit, |
92 | - 'complete' => ( 0 === $remaining ), |
|
92 | + 'complete' => (0 === $remaining), |
|
93 | 93 | 'remaining' => $remaining, |
94 | 94 | ); |
95 | 95 | } |
@@ -103,10 +103,10 @@ discard block |
||
103 | 103 | public function count() { |
104 | 104 | |
105 | 105 | // Schema Classes count. |
106 | - $schema_classes_count = count( $this->get_schema_classes() ); |
|
106 | + $schema_classes_count = count($this->get_schema_classes()); |
|
107 | 107 | |
108 | 108 | // Terms count. |
109 | - $terms_count = wp_count_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
109 | + $terms_count = wp_count_terms(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
110 | 110 | |
111 | 111 | // Return the difference. |
112 | 112 | return $schema_classes_count - $terms_count; |
@@ -122,13 +122,13 @@ discard block |
||
122 | 122 | |
123 | 123 | // Load the file contents. |
124 | 124 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
125 | - $contents = file_get_contents( __DIR__ . '/schema-classes.json' ); |
|
125 | + $contents = file_get_contents(__DIR__.'/schema-classes.json'); |
|
126 | 126 | |
127 | 127 | // Decode the JSON contents. |
128 | - $json = json_decode( $contents, true ); |
|
128 | + $json = json_decode($contents, true); |
|
129 | 129 | |
130 | 130 | // Return the schema classes or an empty array. |
131 | - return isset( $json['schemaClasses'] ) ? $json['schemaClasses'] : array(); |
|
131 | + return isset($json['schemaClasses']) ? $json['schemaClasses'] : array(); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | } |
@@ -16,110 +16,110 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Schemaorg_Property_Service { |
18 | 18 | |
19 | - /** |
|
20 | - * The meta key prefix used to store properties. The `_` prefix makes these metas invisible in the |
|
21 | - * edit screen custom fields metabox. |
|
22 | - * |
|
23 | - * @since 3.20.0 |
|
24 | - */ |
|
25 | - const PREFIX = '_wl_prop_'; |
|
26 | - |
|
27 | - /** |
|
28 | - * Create a {@link Wordlift_Schemaorg_Property_Service} instance. |
|
29 | - * |
|
30 | - * @since 3.20.0 |
|
31 | - */ |
|
32 | - protected function __construct() { |
|
33 | - |
|
34 | - } |
|
35 | - |
|
36 | - private static $instance = null; |
|
37 | - |
|
38 | - /** |
|
39 | - * Get the singleton instance. |
|
40 | - * |
|
41 | - * @return \Wordlift_Schemaorg_Property_Service The singleton instance. |
|
42 | - * @since 3.20.0 |
|
43 | - */ |
|
44 | - public static function get_instance() { |
|
45 | - |
|
46 | - if ( ! isset( self::$instance ) ) { |
|
47 | - self::$instance = new self(); |
|
48 | - } |
|
49 | - |
|
50 | - return self::$instance; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Get all the properties bound to the specified post. |
|
55 | - * |
|
56 | - * @param int $post_id The post id. |
|
57 | - * |
|
58 | - * @return array An array of properties instances keyed by the property name. Each property contains a |
|
59 | - * `type` and a `value` and optionally a `language`. |
|
60 | - * } |
|
61 | - * @since 3.20.0 |
|
62 | - */ |
|
63 | - public function get_all( $post_id ) { |
|
64 | - |
|
65 | - // Get all the post metas. |
|
66 | - $post_metas = get_post_meta( $post_id ); |
|
67 | - |
|
68 | - // Cycle through them to get the Schema.org properties. |
|
69 | - $props = array(); |
|
70 | - foreach ( $post_metas as $key => $values ) { |
|
71 | - $matches = array(); |
|
72 | - |
|
73 | - // We're looking for `_wl_prop_propName_uuid_key`. |
|
74 | - if ( 1 === preg_match( '/' . self::PREFIX . '(\w+)_([\w-]+)_(\w+)/i', $key, $matches ) ) { |
|
75 | - $name = $matches[1]; |
|
76 | - $uuid = $matches[2]; |
|
77 | - $key = $matches[3]; |
|
78 | - |
|
79 | - // Record the value. |
|
80 | - $props[ $name ][ $uuid ][ $key ] = $values[0]; |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - // Remove the UUIDs. |
|
85 | - foreach ( $props as $name => $instance ) { |
|
86 | - foreach ( $instance as $uuid => $keys ) { |
|
87 | - // This way we remove the `uuid`s. |
|
88 | - $props[ $name ] = array_values( $instance ); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - // Finally return the props. |
|
93 | - return $props; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Get the meta keys for Schema.org properties associated with the specified post. |
|
98 | - * |
|
99 | - * @param int $post_id The post id. |
|
100 | - * |
|
101 | - * @return array An array of meta keys. |
|
102 | - * @since 3.20.0 |
|
103 | - */ |
|
104 | - public function get_keys( $post_id ) { |
|
105 | - |
|
106 | - // Get all the post metas to remove the `_wl_prop` ones. |
|
107 | - $post_meta = get_post_meta( $post_id ); |
|
108 | - |
|
109 | - // Get the keys. |
|
110 | - $post_meta_keys = array_unique( array_keys( $post_meta ) ); |
|
111 | - |
|
112 | - // Get only the `_wl_prop` keys. `array_values` resets the indexes. |
|
113 | - $prop_keys = array_values( |
|
114 | - array_filter( |
|
115 | - $post_meta_keys, |
|
116 | - function ( $item ) { |
|
117 | - return 0 === strpos( $item, Wordlift_Schemaorg_Property_Service::PREFIX ); |
|
118 | - } |
|
119 | - ) |
|
120 | - ); |
|
121 | - |
|
122 | - return $prop_keys; |
|
123 | - } |
|
19 | + /** |
|
20 | + * The meta key prefix used to store properties. The `_` prefix makes these metas invisible in the |
|
21 | + * edit screen custom fields metabox. |
|
22 | + * |
|
23 | + * @since 3.20.0 |
|
24 | + */ |
|
25 | + const PREFIX = '_wl_prop_'; |
|
26 | + |
|
27 | + /** |
|
28 | + * Create a {@link Wordlift_Schemaorg_Property_Service} instance. |
|
29 | + * |
|
30 | + * @since 3.20.0 |
|
31 | + */ |
|
32 | + protected function __construct() { |
|
33 | + |
|
34 | + } |
|
35 | + |
|
36 | + private static $instance = null; |
|
37 | + |
|
38 | + /** |
|
39 | + * Get the singleton instance. |
|
40 | + * |
|
41 | + * @return \Wordlift_Schemaorg_Property_Service The singleton instance. |
|
42 | + * @since 3.20.0 |
|
43 | + */ |
|
44 | + public static function get_instance() { |
|
45 | + |
|
46 | + if ( ! isset( self::$instance ) ) { |
|
47 | + self::$instance = new self(); |
|
48 | + } |
|
49 | + |
|
50 | + return self::$instance; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Get all the properties bound to the specified post. |
|
55 | + * |
|
56 | + * @param int $post_id The post id. |
|
57 | + * |
|
58 | + * @return array An array of properties instances keyed by the property name. Each property contains a |
|
59 | + * `type` and a `value` and optionally a `language`. |
|
60 | + * } |
|
61 | + * @since 3.20.0 |
|
62 | + */ |
|
63 | + public function get_all( $post_id ) { |
|
64 | + |
|
65 | + // Get all the post metas. |
|
66 | + $post_metas = get_post_meta( $post_id ); |
|
67 | + |
|
68 | + // Cycle through them to get the Schema.org properties. |
|
69 | + $props = array(); |
|
70 | + foreach ( $post_metas as $key => $values ) { |
|
71 | + $matches = array(); |
|
72 | + |
|
73 | + // We're looking for `_wl_prop_propName_uuid_key`. |
|
74 | + if ( 1 === preg_match( '/' . self::PREFIX . '(\w+)_([\w-]+)_(\w+)/i', $key, $matches ) ) { |
|
75 | + $name = $matches[1]; |
|
76 | + $uuid = $matches[2]; |
|
77 | + $key = $matches[3]; |
|
78 | + |
|
79 | + // Record the value. |
|
80 | + $props[ $name ][ $uuid ][ $key ] = $values[0]; |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + // Remove the UUIDs. |
|
85 | + foreach ( $props as $name => $instance ) { |
|
86 | + foreach ( $instance as $uuid => $keys ) { |
|
87 | + // This way we remove the `uuid`s. |
|
88 | + $props[ $name ] = array_values( $instance ); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + // Finally return the props. |
|
93 | + return $props; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Get the meta keys for Schema.org properties associated with the specified post. |
|
98 | + * |
|
99 | + * @param int $post_id The post id. |
|
100 | + * |
|
101 | + * @return array An array of meta keys. |
|
102 | + * @since 3.20.0 |
|
103 | + */ |
|
104 | + public function get_keys( $post_id ) { |
|
105 | + |
|
106 | + // Get all the post metas to remove the `_wl_prop` ones. |
|
107 | + $post_meta = get_post_meta( $post_id ); |
|
108 | + |
|
109 | + // Get the keys. |
|
110 | + $post_meta_keys = array_unique( array_keys( $post_meta ) ); |
|
111 | + |
|
112 | + // Get only the `_wl_prop` keys. `array_values` resets the indexes. |
|
113 | + $prop_keys = array_values( |
|
114 | + array_filter( |
|
115 | + $post_meta_keys, |
|
116 | + function ( $item ) { |
|
117 | + return 0 === strpos( $item, Wordlift_Schemaorg_Property_Service::PREFIX ); |
|
118 | + } |
|
119 | + ) |
|
120 | + ); |
|
121 | + |
|
122 | + return $prop_keys; |
|
123 | + } |
|
124 | 124 | |
125 | 125 | } |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | */ |
44 | 44 | public static function get_instance() { |
45 | 45 | |
46 | - if ( ! isset( self::$instance ) ) { |
|
46 | + if ( ! isset(self::$instance)) { |
|
47 | 47 | self::$instance = new self(); |
48 | 48 | } |
49 | 49 | |
@@ -60,32 +60,32 @@ discard block |
||
60 | 60 | * } |
61 | 61 | * @since 3.20.0 |
62 | 62 | */ |
63 | - public function get_all( $post_id ) { |
|
63 | + public function get_all($post_id) { |
|
64 | 64 | |
65 | 65 | // Get all the post metas. |
66 | - $post_metas = get_post_meta( $post_id ); |
|
66 | + $post_metas = get_post_meta($post_id); |
|
67 | 67 | |
68 | 68 | // Cycle through them to get the Schema.org properties. |
69 | 69 | $props = array(); |
70 | - foreach ( $post_metas as $key => $values ) { |
|
70 | + foreach ($post_metas as $key => $values) { |
|
71 | 71 | $matches = array(); |
72 | 72 | |
73 | 73 | // We're looking for `_wl_prop_propName_uuid_key`. |
74 | - if ( 1 === preg_match( '/' . self::PREFIX . '(\w+)_([\w-]+)_(\w+)/i', $key, $matches ) ) { |
|
74 | + if (1 === preg_match('/'.self::PREFIX.'(\w+)_([\w-]+)_(\w+)/i', $key, $matches)) { |
|
75 | 75 | $name = $matches[1]; |
76 | 76 | $uuid = $matches[2]; |
77 | 77 | $key = $matches[3]; |
78 | 78 | |
79 | 79 | // Record the value. |
80 | - $props[ $name ][ $uuid ][ $key ] = $values[0]; |
|
80 | + $props[$name][$uuid][$key] = $values[0]; |
|
81 | 81 | } |
82 | 82 | } |
83 | 83 | |
84 | 84 | // Remove the UUIDs. |
85 | - foreach ( $props as $name => $instance ) { |
|
86 | - foreach ( $instance as $uuid => $keys ) { |
|
85 | + foreach ($props as $name => $instance) { |
|
86 | + foreach ($instance as $uuid => $keys) { |
|
87 | 87 | // This way we remove the `uuid`s. |
88 | - $props[ $name ] = array_values( $instance ); |
|
88 | + $props[$name] = array_values($instance); |
|
89 | 89 | } |
90 | 90 | } |
91 | 91 | |
@@ -101,20 +101,20 @@ discard block |
||
101 | 101 | * @return array An array of meta keys. |
102 | 102 | * @since 3.20.0 |
103 | 103 | */ |
104 | - public function get_keys( $post_id ) { |
|
104 | + public function get_keys($post_id) { |
|
105 | 105 | |
106 | 106 | // Get all the post metas to remove the `_wl_prop` ones. |
107 | - $post_meta = get_post_meta( $post_id ); |
|
107 | + $post_meta = get_post_meta($post_id); |
|
108 | 108 | |
109 | 109 | // Get the keys. |
110 | - $post_meta_keys = array_unique( array_keys( $post_meta ) ); |
|
110 | + $post_meta_keys = array_unique(array_keys($post_meta)); |
|
111 | 111 | |
112 | 112 | // Get only the `_wl_prop` keys. `array_values` resets the indexes. |
113 | 113 | $prop_keys = array_values( |
114 | 114 | array_filter( |
115 | 115 | $post_meta_keys, |
116 | - function ( $item ) { |
|
117 | - return 0 === strpos( $item, Wordlift_Schemaorg_Property_Service::PREFIX ); |
|
116 | + function($item) { |
|
117 | + return 0 === strpos($item, Wordlift_Schemaorg_Property_Service::PREFIX); |
|
118 | 118 | } |
119 | 119 | ) |
120 | 120 | ); |
@@ -16,20 +16,20 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_NewRelic_Adapter { |
18 | 18 | |
19 | - /** |
|
20 | - * Tell NewRelic to ignore this "transaction" for the Apdex. |
|
21 | - * |
|
22 | - * @see https://github.com/insideout10/wordlift-plugin/issues/521 |
|
23 | - * |
|
24 | - * @since 3.11.3 |
|
25 | - */ |
|
26 | - public static function ignore_apdex() { |
|
19 | + /** |
|
20 | + * Tell NewRelic to ignore this "transaction" for the Apdex. |
|
21 | + * |
|
22 | + * @see https://github.com/insideout10/wordlift-plugin/issues/521 |
|
23 | + * |
|
24 | + * @since 3.11.3 |
|
25 | + */ |
|
26 | + public static function ignore_apdex() { |
|
27 | 27 | |
28 | - // Ensure PHP agent and the function are available. |
|
29 | - if ( extension_loaded( 'newrelic' ) && function_exists( 'newrelic_ignore_apdex' ) ) { |
|
30 | - newrelic_ignore_apdex(); |
|
31 | - } |
|
28 | + // Ensure PHP agent and the function are available. |
|
29 | + if ( extension_loaded( 'newrelic' ) && function_exists( 'newrelic_ignore_apdex' ) ) { |
|
30 | + newrelic_ignore_apdex(); |
|
31 | + } |
|
32 | 32 | |
33 | - } |
|
33 | + } |
|
34 | 34 | |
35 | 35 | } |
@@ -26,7 +26,7 @@ |
||
26 | 26 | public static function ignore_apdex() { |
27 | 27 | |
28 | 28 | // Ensure PHP agent and the function are available. |
29 | - if ( extension_loaded( 'newrelic' ) && function_exists( 'newrelic_ignore_apdex' ) ) { |
|
29 | + if (extension_loaded('newrelic') && function_exists('newrelic_ignore_apdex')) { |
|
30 | 30 | newrelic_ignore_apdex(); |
31 | 31 | } |
32 | 32 |
@@ -17,54 +17,54 @@ discard block |
||
17 | 17 | */ |
18 | 18 | class Wordlift_Entity_Page_Service { |
19 | 19 | |
20 | - /** |
|
21 | - * Set the entity post types as one to be included in archive pages. |
|
22 | - * |
|
23 | - * In order to have entities show up in standard WP categories (Posts categories) |
|
24 | - * we configure the `entity` post type, but we also need to alter the main |
|
25 | - * WP query (which by default queries posts only) to include the `entities`. |
|
26 | - * |
|
27 | - * @since 3.12.0 |
|
28 | - * |
|
29 | - * @param WP_Query $query WP's {@link WP_Query} instance. |
|
30 | - */ |
|
31 | - public function pre_get_posts( $query ) { |
|
32 | - |
|
33 | - // Only for the main query, avoid problems with widgets and what not. |
|
34 | - if ( ! $query->is_main_query() ) { |
|
35 | - return; |
|
36 | - } |
|
37 | - |
|
38 | - // We don't want to alter the query if we're in the admin UI, if this is |
|
39 | - // not a entity type achieve query, or if the `suppress_filters` is set. |
|
40 | - // |
|
41 | - // Note that it is unlikely for `suppress_filter` to be set on the front |
|
42 | - // end, but let's be safe if it is set the calling code assumes no |
|
43 | - // modifications of queries. |
|
44 | - |
|
45 | - // Ignore admin side request, requests for which filters should be |
|
46 | - // suppressed, and when we are not on a entity type archive page. |
|
47 | - if ( is_admin() || |
|
48 | - ! is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) || |
|
49 | - ! empty( $query->query_vars['suppress_filters'] ) |
|
50 | - ) { |
|
51 | - return; |
|
52 | - } |
|
53 | - |
|
54 | - // Events should be sorted by start date in descending order. |
|
55 | - if ( is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ) { |
|
56 | - |
|
57 | - // Update the query to use the start time meta and desc order. |
|
58 | - $meta_query = array( |
|
59 | - array( |
|
60 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
61 | - ), |
|
62 | - ); |
|
63 | - $query->set( 'meta_query', $meta_query ); |
|
64 | - $query->set( 'orderby', 'meta_value' ); |
|
65 | - $query->set( 'order', 'DESC' ); |
|
66 | - } else { |
|
67 | - /* |
|
20 | + /** |
|
21 | + * Set the entity post types as one to be included in archive pages. |
|
22 | + * |
|
23 | + * In order to have entities show up in standard WP categories (Posts categories) |
|
24 | + * we configure the `entity` post type, but we also need to alter the main |
|
25 | + * WP query (which by default queries posts only) to include the `entities`. |
|
26 | + * |
|
27 | + * @since 3.12.0 |
|
28 | + * |
|
29 | + * @param WP_Query $query WP's {@link WP_Query} instance. |
|
30 | + */ |
|
31 | + public function pre_get_posts( $query ) { |
|
32 | + |
|
33 | + // Only for the main query, avoid problems with widgets and what not. |
|
34 | + if ( ! $query->is_main_query() ) { |
|
35 | + return; |
|
36 | + } |
|
37 | + |
|
38 | + // We don't want to alter the query if we're in the admin UI, if this is |
|
39 | + // not a entity type achieve query, or if the `suppress_filters` is set. |
|
40 | + // |
|
41 | + // Note that it is unlikely for `suppress_filter` to be set on the front |
|
42 | + // end, but let's be safe if it is set the calling code assumes no |
|
43 | + // modifications of queries. |
|
44 | + |
|
45 | + // Ignore admin side request, requests for which filters should be |
|
46 | + // suppressed, and when we are not on a entity type archive page. |
|
47 | + if ( is_admin() || |
|
48 | + ! is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) || |
|
49 | + ! empty( $query->query_vars['suppress_filters'] ) |
|
50 | + ) { |
|
51 | + return; |
|
52 | + } |
|
53 | + |
|
54 | + // Events should be sorted by start date in descending order. |
|
55 | + if ( is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ) { |
|
56 | + |
|
57 | + // Update the query to use the start time meta and desc order. |
|
58 | + $meta_query = array( |
|
59 | + array( |
|
60 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
61 | + ), |
|
62 | + ); |
|
63 | + $query->set( 'meta_query', $meta_query ); |
|
64 | + $query->set( 'orderby', 'meta_value' ); |
|
65 | + $query->set( 'order', 'DESC' ); |
|
66 | + } else { |
|
67 | + /* |
|
68 | 68 | * All other entity types should be sorted by their connectivity. |
69 | 69 | * For this we need to query the relationship table which has |
70 | 70 | * to be done by manipulating the SQL generated for the query. |
@@ -72,73 +72,73 @@ discard block |
||
72 | 72 | * additional filters to handle it. |
73 | 73 | */ |
74 | 74 | |
75 | - add_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
76 | - add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
77 | - add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Filter handler that sets the join part of a query to include the |
|
83 | - * relationship table to be able to use it in the sorting. |
|
84 | - * |
|
85 | - * @since 3.15.0 |
|
86 | - * |
|
87 | - * @param string $join_statement The join part of the SQL statement which is used for the query. |
|
88 | - * |
|
89 | - * @return string An join SQL which add the relationships table to the join. |
|
90 | - */ |
|
91 | - public function posts_join( $join_statement ) { |
|
92 | - |
|
93 | - global $wpdb; |
|
94 | - |
|
95 | - $join_statement .= " LEFT JOIN {$wpdb->prefix}wl_relation_instances ri " |
|
96 | - . " ON (ri.object_id = {$wpdb->posts}.ID)"; |
|
97 | - |
|
98 | - // Remove to make sure it will not run agan in other context. |
|
99 | - remove_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
100 | - |
|
101 | - return $join_statement; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Filter handler that sets the groupby part of a query to include the |
|
106 | - * relationship table to be able to use it in the sorting. |
|
107 | - * |
|
108 | - * @since 3.15.0 |
|
109 | - * |
|
110 | - * @param string $groupby_statement The groupby part of the SQL statement which is used for the query. |
|
111 | - * |
|
112 | - * @return string A groupby SQL which add the relationships table to the join. |
|
113 | - */ |
|
114 | - public function posts_groupby( $groupby_statement ) { |
|
115 | - |
|
116 | - $groupby_statement = 'ri.object_id, ' . $groupby_statement; |
|
117 | - |
|
118 | - // Remove to make sure it will not run agan in other context. |
|
119 | - remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
120 | - |
|
121 | - return $groupby_statement; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Filter handler that sets the orderby part of a query to sort by number of |
|
126 | - * relationships. |
|
127 | - * |
|
128 | - * @since 3.15.0 |
|
129 | - * |
|
130 | - * @param string $orderby_statement The orderby part of the SQL statement which is used for the query. |
|
131 | - * |
|
132 | - * @return string An orderby SQL which sorts by the number of relationships |
|
133 | - */ |
|
134 | - public function posts_orderby( $orderby_statement ) { |
|
135 | - |
|
136 | - $orderby_statement = 'COUNT( ri.object_id ) DESC, ' . $orderby_statement; |
|
137 | - |
|
138 | - // Remove to make sure it will not run agan in other context. |
|
139 | - remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
140 | - |
|
141 | - return $orderby_statement; |
|
142 | - } |
|
75 | + add_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
76 | + add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
77 | + add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Filter handler that sets the join part of a query to include the |
|
83 | + * relationship table to be able to use it in the sorting. |
|
84 | + * |
|
85 | + * @since 3.15.0 |
|
86 | + * |
|
87 | + * @param string $join_statement The join part of the SQL statement which is used for the query. |
|
88 | + * |
|
89 | + * @return string An join SQL which add the relationships table to the join. |
|
90 | + */ |
|
91 | + public function posts_join( $join_statement ) { |
|
92 | + |
|
93 | + global $wpdb; |
|
94 | + |
|
95 | + $join_statement .= " LEFT JOIN {$wpdb->prefix}wl_relation_instances ri " |
|
96 | + . " ON (ri.object_id = {$wpdb->posts}.ID)"; |
|
97 | + |
|
98 | + // Remove to make sure it will not run agan in other context. |
|
99 | + remove_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
100 | + |
|
101 | + return $join_statement; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Filter handler that sets the groupby part of a query to include the |
|
106 | + * relationship table to be able to use it in the sorting. |
|
107 | + * |
|
108 | + * @since 3.15.0 |
|
109 | + * |
|
110 | + * @param string $groupby_statement The groupby part of the SQL statement which is used for the query. |
|
111 | + * |
|
112 | + * @return string A groupby SQL which add the relationships table to the join. |
|
113 | + */ |
|
114 | + public function posts_groupby( $groupby_statement ) { |
|
115 | + |
|
116 | + $groupby_statement = 'ri.object_id, ' . $groupby_statement; |
|
117 | + |
|
118 | + // Remove to make sure it will not run agan in other context. |
|
119 | + remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
120 | + |
|
121 | + return $groupby_statement; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Filter handler that sets the orderby part of a query to sort by number of |
|
126 | + * relationships. |
|
127 | + * |
|
128 | + * @since 3.15.0 |
|
129 | + * |
|
130 | + * @param string $orderby_statement The orderby part of the SQL statement which is used for the query. |
|
131 | + * |
|
132 | + * @return string An orderby SQL which sorts by the number of relationships |
|
133 | + */ |
|
134 | + public function posts_orderby( $orderby_statement ) { |
|
135 | + |
|
136 | + $orderby_statement = 'COUNT( ri.object_id ) DESC, ' . $orderby_statement; |
|
137 | + |
|
138 | + // Remove to make sure it will not run agan in other context. |
|
139 | + remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
140 | + |
|
141 | + return $orderby_statement; |
|
142 | + } |
|
143 | 143 | |
144 | 144 | } |
@@ -28,10 +28,10 @@ discard block |
||
28 | 28 | * |
29 | 29 | * @param WP_Query $query WP's {@link WP_Query} instance. |
30 | 30 | */ |
31 | - public function pre_get_posts( $query ) { |
|
31 | + public function pre_get_posts($query) { |
|
32 | 32 | |
33 | 33 | // Only for the main query, avoid problems with widgets and what not. |
34 | - if ( ! $query->is_main_query() ) { |
|
34 | + if ( ! $query->is_main_query()) { |
|
35 | 35 | return; |
36 | 36 | } |
37 | 37 | |
@@ -44,15 +44,15 @@ discard block |
||
44 | 44 | |
45 | 45 | // Ignore admin side request, requests for which filters should be |
46 | 46 | // suppressed, and when we are not on a entity type archive page. |
47 | - if ( is_admin() || |
|
48 | - ! is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) || |
|
49 | - ! empty( $query->query_vars['suppress_filters'] ) |
|
47 | + if (is_admin() || |
|
48 | + ! is_tax(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME) || |
|
49 | + ! empty($query->query_vars['suppress_filters']) |
|
50 | 50 | ) { |
51 | 51 | return; |
52 | 52 | } |
53 | 53 | |
54 | 54 | // Events should be sorted by start date in descending order. |
55 | - if ( is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ) { |
|
55 | + if (is_tax(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, 'event')) { |
|
56 | 56 | |
57 | 57 | // Update the query to use the start time meta and desc order. |
58 | 58 | $meta_query = array( |
@@ -60,9 +60,9 @@ discard block |
||
60 | 60 | 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
61 | 61 | ), |
62 | 62 | ); |
63 | - $query->set( 'meta_query', $meta_query ); |
|
64 | - $query->set( 'orderby', 'meta_value' ); |
|
65 | - $query->set( 'order', 'DESC' ); |
|
63 | + $query->set('meta_query', $meta_query); |
|
64 | + $query->set('orderby', 'meta_value'); |
|
65 | + $query->set('order', 'DESC'); |
|
66 | 66 | } else { |
67 | 67 | /* |
68 | 68 | * All other entity types should be sorted by their connectivity. |
@@ -72,9 +72,9 @@ discard block |
||
72 | 72 | * additional filters to handle it. |
73 | 73 | */ |
74 | 74 | |
75 | - add_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
76 | - add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
77 | - add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
75 | + add_filter('posts_join', array($this, 'posts_join')); |
|
76 | + add_filter('posts_groupby', array($this, 'posts_groupby')); |
|
77 | + add_filter('posts_orderby', array($this, 'posts_orderby')); |
|
78 | 78 | } |
79 | 79 | } |
80 | 80 | |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | * |
89 | 89 | * @return string An join SQL which add the relationships table to the join. |
90 | 90 | */ |
91 | - public function posts_join( $join_statement ) { |
|
91 | + public function posts_join($join_statement) { |
|
92 | 92 | |
93 | 93 | global $wpdb; |
94 | 94 | |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | . " ON (ri.object_id = {$wpdb->posts}.ID)"; |
97 | 97 | |
98 | 98 | // Remove to make sure it will not run agan in other context. |
99 | - remove_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
99 | + remove_filter('posts_join', array($this, 'posts_join')); |
|
100 | 100 | |
101 | 101 | return $join_statement; |
102 | 102 | } |
@@ -111,12 +111,12 @@ discard block |
||
111 | 111 | * |
112 | 112 | * @return string A groupby SQL which add the relationships table to the join. |
113 | 113 | */ |
114 | - public function posts_groupby( $groupby_statement ) { |
|
114 | + public function posts_groupby($groupby_statement) { |
|
115 | 115 | |
116 | - $groupby_statement = 'ri.object_id, ' . $groupby_statement; |
|
116 | + $groupby_statement = 'ri.object_id, '.$groupby_statement; |
|
117 | 117 | |
118 | 118 | // Remove to make sure it will not run agan in other context. |
119 | - remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
119 | + remove_filter('posts_groupby', array($this, 'posts_groupby')); |
|
120 | 120 | |
121 | 121 | return $groupby_statement; |
122 | 122 | } |
@@ -131,12 +131,12 @@ discard block |
||
131 | 131 | * |
132 | 132 | * @return string An orderby SQL which sorts by the number of relationships |
133 | 133 | */ |
134 | - public function posts_orderby( $orderby_statement ) { |
|
134 | + public function posts_orderby($orderby_statement) { |
|
135 | 135 | |
136 | - $orderby_statement = 'COUNT( ri.object_id ) DESC, ' . $orderby_statement; |
|
136 | + $orderby_statement = 'COUNT( ri.object_id ) DESC, '.$orderby_statement; |
|
137 | 137 | |
138 | 138 | // Remove to make sure it will not run agan in other context. |
139 | - remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
139 | + remove_filter('posts_orderby', array($this, 'posts_orderby')); |
|
140 | 140 | |
141 | 141 | return $orderby_statement; |
142 | 142 | } |
@@ -17,98 +17,98 @@ discard block |
||
17 | 17 | */ |
18 | 18 | class Wordlift_Sample_Data_Service { |
19 | 19 | |
20 | - /** |
|
21 | - * An array of sample data. |
|
22 | - * |
|
23 | - * @since 3.12.0 |
|
24 | - * @var array $samples An array of sample data. |
|
25 | - */ |
|
26 | - private $samples = array( |
|
27 | - array( |
|
28 | - 'post' => array( |
|
29 | - 'post_name' => 'praesent_imperdiet_odio_sed_lectus_vulputate_finibus', |
|
30 | - 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
31 | - 'post_content' => 'Praesent imperdiet odio sed lectus vulputate finibus. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.', |
|
32 | - 'post_type' => 'entity', |
|
33 | - 'post_status' => 'publish', |
|
34 | - ), |
|
35 | - 'entity_type_uri' => 'http://schema.org/Event', |
|
36 | - ), |
|
37 | - array( |
|
38 | - 'post' => array( |
|
39 | - 'post_name' => 'nullam_tempor_lectus_sit_amet_tincidunt_euismod', |
|
40 | - 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
41 | - 'post_content' => '<span id="urn:enhancement-da554278-9522-2d83-76ad-8129d2292cb3" class="textannotation disambiguated wl-event" itemid="{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus">Praesent imperdiet odio sed lectus vulputate finibus</span> Nullam tempor lectus sit amet tincidunt euismod. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.', |
|
42 | - 'post_type' => 'entity', |
|
43 | - 'post_status' => 'publish', |
|
44 | - ), |
|
45 | - 'entity_type_uri' => 'http://schema.org/Place', |
|
46 | - ), |
|
47 | - array( |
|
48 | - 'post' => array( |
|
49 | - 'post_name' => 'praesent_luctus_tincidunt_odio_quis_aliquam', |
|
50 | - 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
51 | - 'post_content' => 'Praesent luctus tincidunt odio quis aliquam. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.', |
|
52 | - 'post_type' => 'entity', |
|
53 | - 'post_status' => 'publish', |
|
54 | - ), |
|
55 | - 'entity_type_uri' => 'http://schema.org/Organization', |
|
56 | - ), |
|
57 | - array( |
|
58 | - 'post' => array( |
|
59 | - 'post_name' => 'lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit', |
|
60 | - 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
61 | - 'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', |
|
62 | - 'post_type' => 'entity', |
|
63 | - 'post_status' => 'publish', |
|
64 | - ), |
|
65 | - 'entity_type_uri' => 'http://schema.org/CreativeWork', |
|
66 | - ), |
|
67 | - array( |
|
68 | - 'post' => |
|
69 | - array( |
|
70 | - 'post_name' => 'post_1', |
|
71 | - 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
72 | - 'post_content' => '<span><span id="urn:enhancement-da554278-9522-2d83-76ad-8129d2292cb3" class="textannotation disambiguated wl-event" itemid="{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus">Praesent imperdiet odio sed lectus vulputate finibus</span>. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.</span>', |
|
73 | - 'post_type' => 'post', |
|
74 | - 'post_status' => 'publish', |
|
75 | - ), |
|
76 | - ), |
|
77 | - array( |
|
78 | - 'post' => |
|
79 | - array( |
|
80 | - 'post_name' => 'post_2', |
|
81 | - 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
82 | - 'post_content' => '<span><span id="urn:local-text-annotation-p8i5o4279ex3rsbwqkrx9z5mh1ox91ae" class="textannotation disambiguated wl-place" itemid="{dataset-uri}/entity/nullam_tempor_lectus_sit_amet_tincidunt_euismod">Nullam tempor lectus sit amet tincidunt euismod</span>. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</span>', |
|
83 | - 'post_type' => 'post', |
|
84 | - 'post_status' => 'publish', |
|
85 | - ), |
|
86 | - ), |
|
87 | - array( |
|
88 | - 'post' => |
|
89 | - array( |
|
90 | - 'post_name' => 'post_3', |
|
91 | - 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
92 | - 'post_content' => '<span><span id="urn:enhancement-b3487a20-4696-b6d9-6c55-842445f5c263" class="textannotation disambiguated wl-organization" itemid="{dataset-uri}/entity/praesent_luctus_tincidunt_odio_quis_aliquam">Praesent luctus tincidunt odio quis aliquam</span>. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.</span>', |
|
93 | - 'post_type' => 'post', |
|
94 | - 'post_status' => 'publish', |
|
95 | - ), |
|
96 | - ), |
|
97 | - array( |
|
98 | - 'post' => |
|
99 | - array( |
|
100 | - 'post_name' => 'post_4', |
|
101 | - 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
102 | - 'post_content' => '<span><span id="urn:enhancement-4edc3bde-d275-22f9-8d50-0b707596b292" class="textannotation disambiguated wl-thing" itemid="{dataset-uri}/entity/lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit">Lorem ipsum dolor sit amet, consectetur adipiscing elit</span>. Proin rutrum ultrices nulla ut elementum. Nunc nec lacus tortor. Curabitur bibendum imperdiet luctus. Vivamus a faucibus dolor. Donec blandit malesuada risus. Vestibulum volutpat ut tellus sed tincidunt. Sed id tincidunt velit. Integer sed felis id libero fringilla molestie vitae id orci. Ut vel purus ullamcorper, feugiat tortor non, iaculis neque. Vivamus vitae vehicula sem. Mauris fermentum, metus id vestibulum sodales, lorem lacus efficitur ante, non vestibulum ligula ligula a turpis. Vivamus quis scelerisque massa.</span>', |
|
103 | - 'post_type' => 'post', |
|
104 | - 'post_status' => 'publish', |
|
105 | - ), |
|
106 | - ), |
|
107 | - array( |
|
108 | - 'post' => array( |
|
109 | - 'post_name' => 'post_5', |
|
110 | - 'post_title' => 'Lorem ipsum', |
|
111 | - 'post_content' => ' |
|
20 | + /** |
|
21 | + * An array of sample data. |
|
22 | + * |
|
23 | + * @since 3.12.0 |
|
24 | + * @var array $samples An array of sample data. |
|
25 | + */ |
|
26 | + private $samples = array( |
|
27 | + array( |
|
28 | + 'post' => array( |
|
29 | + 'post_name' => 'praesent_imperdiet_odio_sed_lectus_vulputate_finibus', |
|
30 | + 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
31 | + 'post_content' => 'Praesent imperdiet odio sed lectus vulputate finibus. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.', |
|
32 | + 'post_type' => 'entity', |
|
33 | + 'post_status' => 'publish', |
|
34 | + ), |
|
35 | + 'entity_type_uri' => 'http://schema.org/Event', |
|
36 | + ), |
|
37 | + array( |
|
38 | + 'post' => array( |
|
39 | + 'post_name' => 'nullam_tempor_lectus_sit_amet_tincidunt_euismod', |
|
40 | + 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
41 | + 'post_content' => '<span id="urn:enhancement-da554278-9522-2d83-76ad-8129d2292cb3" class="textannotation disambiguated wl-event" itemid="{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus">Praesent imperdiet odio sed lectus vulputate finibus</span> Nullam tempor lectus sit amet tincidunt euismod. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.', |
|
42 | + 'post_type' => 'entity', |
|
43 | + 'post_status' => 'publish', |
|
44 | + ), |
|
45 | + 'entity_type_uri' => 'http://schema.org/Place', |
|
46 | + ), |
|
47 | + array( |
|
48 | + 'post' => array( |
|
49 | + 'post_name' => 'praesent_luctus_tincidunt_odio_quis_aliquam', |
|
50 | + 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
51 | + 'post_content' => 'Praesent luctus tincidunt odio quis aliquam. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.', |
|
52 | + 'post_type' => 'entity', |
|
53 | + 'post_status' => 'publish', |
|
54 | + ), |
|
55 | + 'entity_type_uri' => 'http://schema.org/Organization', |
|
56 | + ), |
|
57 | + array( |
|
58 | + 'post' => array( |
|
59 | + 'post_name' => 'lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit', |
|
60 | + 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
61 | + 'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', |
|
62 | + 'post_type' => 'entity', |
|
63 | + 'post_status' => 'publish', |
|
64 | + ), |
|
65 | + 'entity_type_uri' => 'http://schema.org/CreativeWork', |
|
66 | + ), |
|
67 | + array( |
|
68 | + 'post' => |
|
69 | + array( |
|
70 | + 'post_name' => 'post_1', |
|
71 | + 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
72 | + 'post_content' => '<span><span id="urn:enhancement-da554278-9522-2d83-76ad-8129d2292cb3" class="textannotation disambiguated wl-event" itemid="{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus">Praesent imperdiet odio sed lectus vulputate finibus</span>. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.</span>', |
|
73 | + 'post_type' => 'post', |
|
74 | + 'post_status' => 'publish', |
|
75 | + ), |
|
76 | + ), |
|
77 | + array( |
|
78 | + 'post' => |
|
79 | + array( |
|
80 | + 'post_name' => 'post_2', |
|
81 | + 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
82 | + 'post_content' => '<span><span id="urn:local-text-annotation-p8i5o4279ex3rsbwqkrx9z5mh1ox91ae" class="textannotation disambiguated wl-place" itemid="{dataset-uri}/entity/nullam_tempor_lectus_sit_amet_tincidunt_euismod">Nullam tempor lectus sit amet tincidunt euismod</span>. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</span>', |
|
83 | + 'post_type' => 'post', |
|
84 | + 'post_status' => 'publish', |
|
85 | + ), |
|
86 | + ), |
|
87 | + array( |
|
88 | + 'post' => |
|
89 | + array( |
|
90 | + 'post_name' => 'post_3', |
|
91 | + 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
92 | + 'post_content' => '<span><span id="urn:enhancement-b3487a20-4696-b6d9-6c55-842445f5c263" class="textannotation disambiguated wl-organization" itemid="{dataset-uri}/entity/praesent_luctus_tincidunt_odio_quis_aliquam">Praesent luctus tincidunt odio quis aliquam</span>. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.</span>', |
|
93 | + 'post_type' => 'post', |
|
94 | + 'post_status' => 'publish', |
|
95 | + ), |
|
96 | + ), |
|
97 | + array( |
|
98 | + 'post' => |
|
99 | + array( |
|
100 | + 'post_name' => 'post_4', |
|
101 | + 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
102 | + 'post_content' => '<span><span id="urn:enhancement-4edc3bde-d275-22f9-8d50-0b707596b292" class="textannotation disambiguated wl-thing" itemid="{dataset-uri}/entity/lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit">Lorem ipsum dolor sit amet, consectetur adipiscing elit</span>. Proin rutrum ultrices nulla ut elementum. Nunc nec lacus tortor. Curabitur bibendum imperdiet luctus. Vivamus a faucibus dolor. Donec blandit malesuada risus. Vestibulum volutpat ut tellus sed tincidunt. Sed id tincidunt velit. Integer sed felis id libero fringilla molestie vitae id orci. Ut vel purus ullamcorper, feugiat tortor non, iaculis neque. Vivamus vitae vehicula sem. Mauris fermentum, metus id vestibulum sodales, lorem lacus efficitur ante, non vestibulum ligula ligula a turpis. Vivamus quis scelerisque massa.</span>', |
|
103 | + 'post_type' => 'post', |
|
104 | + 'post_status' => 'publish', |
|
105 | + ), |
|
106 | + ), |
|
107 | + array( |
|
108 | + 'post' => array( |
|
109 | + 'post_name' => 'post_5', |
|
110 | + 'post_title' => 'Lorem ipsum', |
|
111 | + 'post_content' => ' |
|
112 | 112 | <span id="urn:enhancement-28cb4112-64cf-bd49-ef97-a2ee54727de7" class="textannotation disambiguated wl-thing" itemid="{dataset-uri}/entity/lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit">Lorem ipsum</span> dolor sit amet, consectetur adipiscing elit. Proin rutrum ultrices nulla ut elementum. Nunc nec lacus tortor. Curabitur bibendum imperdiet luctus. Vivamus a faucibus dolor. Donec blandit malesuada risus. Vestibulum volutpat ut tellus sed tincidunt. Sed id tincidunt velit. Integer sed felis id libero fringilla molestie vitae id orci. Ut vel purus ullamcorper, feugiat tortor non, iaculis neque. Vivamus vitae vehicula sem. Mauris fermentum, metus id vestibulum sodales, lorem lacus efficitur ante, non vestibulum ligula ligula a turpis. Vivamus quis scelerisque massa. |
113 | 113 | |
114 | 114 | [wl_navigator] |
@@ -123,242 +123,242 @@ discard block |
||
123 | 123 | |
124 | 124 | <span id="urn:local-text-annotation-v0kqdtx685n6cg9jrfvl67amkhm28hxh" class="textannotation disambiguated wl-event" itemid="{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus">Praesent imperdiet odio sed lectus vulputate finibus</span>. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus. |
125 | 125 | ', |
126 | - 'post_type' => 'post', |
|
127 | - 'post_status' => 'publish', |
|
128 | - ), |
|
129 | - ), |
|
130 | - ); |
|
131 | - |
|
132 | - /** |
|
133 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
134 | - * |
|
135 | - * @since 3.12.0 |
|
136 | - * @access private |
|
137 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
138 | - */ |
|
139 | - private $entity_type_service; |
|
140 | - |
|
141 | - /** |
|
142 | - * The {@link Wordlift_User_Service} instance. |
|
143 | - * |
|
144 | - * @since 3.16.0 |
|
145 | - * @access private |
|
146 | - * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
147 | - */ |
|
148 | - private $user_service; |
|
149 | - |
|
150 | - /** |
|
151 | - * Create a {@link Wordlift_Sample_Data_Service} instance. |
|
152 | - * |
|
153 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
154 | - * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
155 | - * |
|
156 | - * @since 3.12.0 |
|
157 | - */ |
|
158 | - protected function __construct( $entity_type_service, $user_service ) { |
|
159 | - |
|
160 | - $this->entity_type_service = $entity_type_service; |
|
161 | - $this->user_service = $user_service; |
|
162 | - |
|
163 | - } |
|
164 | - |
|
165 | - private static $instance = null; |
|
166 | - |
|
167 | - public static function get_instance() { |
|
168 | - |
|
169 | - if ( ! isset( self::$instance ) ) { |
|
170 | - self::$instance = new self( Wordlift_Entity_Type_Service::get_instance(), Wordlift_User_Service::get_instance() ); |
|
171 | - } |
|
172 | - |
|
173 | - return self::$instance; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Create sample data in this WordPress instance. |
|
178 | - * |
|
179 | - * @since 3.12.0 |
|
180 | - */ |
|
181 | - public function create() { |
|
182 | - |
|
183 | - // Get the source image path. |
|
184 | - $source = plugin_dir_path( __DIR__ ) . 'images/rome.png'; |
|
185 | - |
|
186 | - // Create an attachment with the local file. |
|
187 | - $attachment_id = $this->create_attachment_from_local_file( $source ); |
|
188 | - |
|
189 | - // Add a flag to signal the attachment is sample data and allow easy delete |
|
190 | - // afterwards. |
|
191 | - add_post_meta( $attachment_id, '_wl_sample_data', 1, true ); |
|
192 | - |
|
193 | - // Get the dataset URI, used for replacements in the `post_content`. |
|
194 | - $dataset_uri = untrailingslashit( Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ); |
|
195 | - |
|
196 | - // Create the author and get its id. |
|
197 | - $author_id = $this->create_author(); |
|
198 | - |
|
199 | - // Create 4 entities. |
|
200 | - // Create 4 posts referencing each one entity. |
|
201 | - // Create 1 post referencing all the entities. |
|
202 | - foreach ( $this->samples as $sample ) { |
|
203 | - |
|
204 | - // Get the post data. |
|
205 | - $post = array_replace_recursive( |
|
206 | - $sample['post'], |
|
207 | - array( |
|
208 | - 'post_content' => str_replace( '{dataset-uri}', $dataset_uri, $sample['post']['post_content'] ), |
|
209 | - ) |
|
210 | - ); |
|
211 | - |
|
212 | - // Set the author. |
|
213 | - $post['post_author'] = $author_id; |
|
214 | - |
|
215 | - // Insert the post. |
|
216 | - $post_id = wp_insert_post( $post ); |
|
217 | - |
|
218 | - // Add a flag to signal the post is sample data and allow easy delete |
|
219 | - // afterwards. |
|
220 | - add_post_meta( $post_id, '_wl_sample_data', 1, true ); |
|
221 | - |
|
222 | - // Set the post thumbnail. |
|
223 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
224 | - |
|
225 | - // If the `entity_type_uri` property is set, set it on the post. |
|
226 | - if ( isset( $sample['entity_type_uri'] ) ) { |
|
227 | - $this->entity_type_service->set( $post_id, $sample['entity_type_uri'] ); |
|
228 | - } |
|
229 | - } |
|
230 | - |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Create an author to bind to posts. |
|
235 | - * |
|
236 | - * @return int The {@link WP_User}'s id. |
|
237 | - * @since 3.16.0 |
|
238 | - */ |
|
239 | - private function create_author() { |
|
240 | - |
|
241 | - $user_id = wp_create_user( 'wl-sample-data', wp_generate_password() ); |
|
242 | - $author_post_id = wp_insert_post( |
|
243 | - array( |
|
244 | - 'post_type' => 'entity', |
|
245 | - 'post_title' => 'WordLift Sample Data Person', |
|
246 | - ) |
|
247 | - ); |
|
248 | - // Add a flag to signal the attachment is sample data and allow easy delete |
|
249 | - // afterwards. |
|
250 | - add_post_meta( $author_post_id, '_wl_sample_data', 1, true ); |
|
251 | - |
|
252 | - $this->entity_type_service->set( $author_post_id, 'http://schema.org/Person' ); |
|
253 | - $this->user_service->set_entity( $user_id, $author_post_id ); |
|
254 | - |
|
255 | - return $user_id; |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Remove the sample data from this WordPress instance. |
|
260 | - * |
|
261 | - * @since 3.12.0 |
|
262 | - */ |
|
263 | - public function delete() { |
|
264 | - |
|
265 | - $this->delete_by_type( 'post' ); |
|
266 | - $this->delete_by_type( 'entity' ); |
|
267 | - $this->delete_by_type( 'attachment' ); |
|
268 | - |
|
269 | - // Get and delete the user. |
|
270 | - $user = get_user_by( 'login', 'wl-sample-data' ); |
|
271 | - wp_delete_user( $user->ID ); |
|
272 | - |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Remove the sample data of the specified type (e.g. `post`, `entity`, `attachment`) |
|
277 | - * from the local WordPress instance. |
|
278 | - * |
|
279 | - * @param string $type WordPress {@link WP_Post}'s type, e.g. `post`, `entity`, `attachment`. |
|
280 | - * |
|
281 | - * @since 3.12.0 |
|
282 | - */ |
|
283 | - private function delete_by_type( $type ) { |
|
284 | - |
|
285 | - $posts = get_posts( |
|
286 | - array( |
|
287 | - 'meta_key' => '_wl_sample_data', |
|
288 | - 'meta_value' => 1, |
|
289 | - 'post_status' => 'any', |
|
290 | - 'post_type' => $type, |
|
291 | - ) |
|
292 | - ); |
|
293 | - |
|
294 | - foreach ( $posts as $post ) { |
|
295 | - wp_delete_post( $post->ID, true ); |
|
296 | - } |
|
297 | - |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Create a WordPress' attachment using the specified file. |
|
302 | - * |
|
303 | - * @param string $source The source file path. |
|
304 | - * |
|
305 | - * @return int WordPress' attachment's id. |
|
306 | - * @since 3.12.0 |
|
307 | - */ |
|
308 | - private function create_attachment_from_local_file( $source ) { |
|
309 | - |
|
310 | - // Get the path to the upload directory. |
|
311 | - $upload_dir = wp_upload_dir(); |
|
312 | - $upload_path = $upload_dir['path']; |
|
313 | - |
|
314 | - // Get the destination image path. |
|
315 | - $destination = $upload_path . '/wl-sample-data.png'; |
|
316 | - |
|
317 | - // Copy the source file to the destination. |
|
318 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
319 | - @copy( $source, $destination ); |
|
320 | - |
|
321 | - return $this->create_attachment( $destination ); |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Create a WordPress attachment using the specified file in the upload folder. |
|
326 | - * |
|
327 | - * @see https://codex.wordpress.org/Function_Reference/wp_insert_attachment |
|
328 | - * |
|
329 | - * @since 3.12.0 |
|
330 | - * |
|
331 | - * @param string $filename The image filename. |
|
332 | - * |
|
333 | - * @return int The attachment id. |
|
334 | - */ |
|
335 | - private function create_attachment( $filename ) { |
|
336 | - |
|
337 | - // Check the type of file. We'll use this as the 'post_mime_type'. |
|
338 | - $filetype = wp_check_filetype( basename( $filename ), null ); |
|
339 | - |
|
340 | - // Get the path to the upload directory. |
|
341 | - $wp_upload_dir = wp_upload_dir(); |
|
342 | - |
|
343 | - // Prepare an array of post data for the attachment. |
|
344 | - $attachment = array( |
|
345 | - 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), |
|
346 | - 'post_mime_type' => $filetype['type'], |
|
347 | - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
348 | - 'post_content' => '', |
|
349 | - 'post_status' => 'inherit', |
|
350 | - ); |
|
351 | - |
|
352 | - // Insert the attachment. |
|
353 | - $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
354 | - |
|
355 | - // Generate the metadata for the attachment, and update the database record. |
|
356 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
357 | - |
|
358 | - // Update the attachment metadata. |
|
359 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
360 | - |
|
361 | - return $attachment_id; |
|
362 | - } |
|
126 | + 'post_type' => 'post', |
|
127 | + 'post_status' => 'publish', |
|
128 | + ), |
|
129 | + ), |
|
130 | + ); |
|
131 | + |
|
132 | + /** |
|
133 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
134 | + * |
|
135 | + * @since 3.12.0 |
|
136 | + * @access private |
|
137 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
138 | + */ |
|
139 | + private $entity_type_service; |
|
140 | + |
|
141 | + /** |
|
142 | + * The {@link Wordlift_User_Service} instance. |
|
143 | + * |
|
144 | + * @since 3.16.0 |
|
145 | + * @access private |
|
146 | + * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
147 | + */ |
|
148 | + private $user_service; |
|
149 | + |
|
150 | + /** |
|
151 | + * Create a {@link Wordlift_Sample_Data_Service} instance. |
|
152 | + * |
|
153 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
154 | + * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
155 | + * |
|
156 | + * @since 3.12.0 |
|
157 | + */ |
|
158 | + protected function __construct( $entity_type_service, $user_service ) { |
|
159 | + |
|
160 | + $this->entity_type_service = $entity_type_service; |
|
161 | + $this->user_service = $user_service; |
|
162 | + |
|
163 | + } |
|
164 | + |
|
165 | + private static $instance = null; |
|
166 | + |
|
167 | + public static function get_instance() { |
|
168 | + |
|
169 | + if ( ! isset( self::$instance ) ) { |
|
170 | + self::$instance = new self( Wordlift_Entity_Type_Service::get_instance(), Wordlift_User_Service::get_instance() ); |
|
171 | + } |
|
172 | + |
|
173 | + return self::$instance; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Create sample data in this WordPress instance. |
|
178 | + * |
|
179 | + * @since 3.12.0 |
|
180 | + */ |
|
181 | + public function create() { |
|
182 | + |
|
183 | + // Get the source image path. |
|
184 | + $source = plugin_dir_path( __DIR__ ) . 'images/rome.png'; |
|
185 | + |
|
186 | + // Create an attachment with the local file. |
|
187 | + $attachment_id = $this->create_attachment_from_local_file( $source ); |
|
188 | + |
|
189 | + // Add a flag to signal the attachment is sample data and allow easy delete |
|
190 | + // afterwards. |
|
191 | + add_post_meta( $attachment_id, '_wl_sample_data', 1, true ); |
|
192 | + |
|
193 | + // Get the dataset URI, used for replacements in the `post_content`. |
|
194 | + $dataset_uri = untrailingslashit( Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ); |
|
195 | + |
|
196 | + // Create the author and get its id. |
|
197 | + $author_id = $this->create_author(); |
|
198 | + |
|
199 | + // Create 4 entities. |
|
200 | + // Create 4 posts referencing each one entity. |
|
201 | + // Create 1 post referencing all the entities. |
|
202 | + foreach ( $this->samples as $sample ) { |
|
203 | + |
|
204 | + // Get the post data. |
|
205 | + $post = array_replace_recursive( |
|
206 | + $sample['post'], |
|
207 | + array( |
|
208 | + 'post_content' => str_replace( '{dataset-uri}', $dataset_uri, $sample['post']['post_content'] ), |
|
209 | + ) |
|
210 | + ); |
|
211 | + |
|
212 | + // Set the author. |
|
213 | + $post['post_author'] = $author_id; |
|
214 | + |
|
215 | + // Insert the post. |
|
216 | + $post_id = wp_insert_post( $post ); |
|
217 | + |
|
218 | + // Add a flag to signal the post is sample data and allow easy delete |
|
219 | + // afterwards. |
|
220 | + add_post_meta( $post_id, '_wl_sample_data', 1, true ); |
|
221 | + |
|
222 | + // Set the post thumbnail. |
|
223 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
224 | + |
|
225 | + // If the `entity_type_uri` property is set, set it on the post. |
|
226 | + if ( isset( $sample['entity_type_uri'] ) ) { |
|
227 | + $this->entity_type_service->set( $post_id, $sample['entity_type_uri'] ); |
|
228 | + } |
|
229 | + } |
|
230 | + |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Create an author to bind to posts. |
|
235 | + * |
|
236 | + * @return int The {@link WP_User}'s id. |
|
237 | + * @since 3.16.0 |
|
238 | + */ |
|
239 | + private function create_author() { |
|
240 | + |
|
241 | + $user_id = wp_create_user( 'wl-sample-data', wp_generate_password() ); |
|
242 | + $author_post_id = wp_insert_post( |
|
243 | + array( |
|
244 | + 'post_type' => 'entity', |
|
245 | + 'post_title' => 'WordLift Sample Data Person', |
|
246 | + ) |
|
247 | + ); |
|
248 | + // Add a flag to signal the attachment is sample data and allow easy delete |
|
249 | + // afterwards. |
|
250 | + add_post_meta( $author_post_id, '_wl_sample_data', 1, true ); |
|
251 | + |
|
252 | + $this->entity_type_service->set( $author_post_id, 'http://schema.org/Person' ); |
|
253 | + $this->user_service->set_entity( $user_id, $author_post_id ); |
|
254 | + |
|
255 | + return $user_id; |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Remove the sample data from this WordPress instance. |
|
260 | + * |
|
261 | + * @since 3.12.0 |
|
262 | + */ |
|
263 | + public function delete() { |
|
264 | + |
|
265 | + $this->delete_by_type( 'post' ); |
|
266 | + $this->delete_by_type( 'entity' ); |
|
267 | + $this->delete_by_type( 'attachment' ); |
|
268 | + |
|
269 | + // Get and delete the user. |
|
270 | + $user = get_user_by( 'login', 'wl-sample-data' ); |
|
271 | + wp_delete_user( $user->ID ); |
|
272 | + |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Remove the sample data of the specified type (e.g. `post`, `entity`, `attachment`) |
|
277 | + * from the local WordPress instance. |
|
278 | + * |
|
279 | + * @param string $type WordPress {@link WP_Post}'s type, e.g. `post`, `entity`, `attachment`. |
|
280 | + * |
|
281 | + * @since 3.12.0 |
|
282 | + */ |
|
283 | + private function delete_by_type( $type ) { |
|
284 | + |
|
285 | + $posts = get_posts( |
|
286 | + array( |
|
287 | + 'meta_key' => '_wl_sample_data', |
|
288 | + 'meta_value' => 1, |
|
289 | + 'post_status' => 'any', |
|
290 | + 'post_type' => $type, |
|
291 | + ) |
|
292 | + ); |
|
293 | + |
|
294 | + foreach ( $posts as $post ) { |
|
295 | + wp_delete_post( $post->ID, true ); |
|
296 | + } |
|
297 | + |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Create a WordPress' attachment using the specified file. |
|
302 | + * |
|
303 | + * @param string $source The source file path. |
|
304 | + * |
|
305 | + * @return int WordPress' attachment's id. |
|
306 | + * @since 3.12.0 |
|
307 | + */ |
|
308 | + private function create_attachment_from_local_file( $source ) { |
|
309 | + |
|
310 | + // Get the path to the upload directory. |
|
311 | + $upload_dir = wp_upload_dir(); |
|
312 | + $upload_path = $upload_dir['path']; |
|
313 | + |
|
314 | + // Get the destination image path. |
|
315 | + $destination = $upload_path . '/wl-sample-data.png'; |
|
316 | + |
|
317 | + // Copy the source file to the destination. |
|
318 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
319 | + @copy( $source, $destination ); |
|
320 | + |
|
321 | + return $this->create_attachment( $destination ); |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Create a WordPress attachment using the specified file in the upload folder. |
|
326 | + * |
|
327 | + * @see https://codex.wordpress.org/Function_Reference/wp_insert_attachment |
|
328 | + * |
|
329 | + * @since 3.12.0 |
|
330 | + * |
|
331 | + * @param string $filename The image filename. |
|
332 | + * |
|
333 | + * @return int The attachment id. |
|
334 | + */ |
|
335 | + private function create_attachment( $filename ) { |
|
336 | + |
|
337 | + // Check the type of file. We'll use this as the 'post_mime_type'. |
|
338 | + $filetype = wp_check_filetype( basename( $filename ), null ); |
|
339 | + |
|
340 | + // Get the path to the upload directory. |
|
341 | + $wp_upload_dir = wp_upload_dir(); |
|
342 | + |
|
343 | + // Prepare an array of post data for the attachment. |
|
344 | + $attachment = array( |
|
345 | + 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), |
|
346 | + 'post_mime_type' => $filetype['type'], |
|
347 | + 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
348 | + 'post_content' => '', |
|
349 | + 'post_status' => 'inherit', |
|
350 | + ); |
|
351 | + |
|
352 | + // Insert the attachment. |
|
353 | + $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
354 | + |
|
355 | + // Generate the metadata for the attachment, and update the database record. |
|
356 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
357 | + |
|
358 | + // Update the attachment metadata. |
|
359 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
360 | + |
|
361 | + return $attachment_id; |
|
362 | + } |
|
363 | 363 | |
364 | 364 | } |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | * |
156 | 156 | * @since 3.12.0 |
157 | 157 | */ |
158 | - protected function __construct( $entity_type_service, $user_service ) { |
|
158 | + protected function __construct($entity_type_service, $user_service) { |
|
159 | 159 | |
160 | 160 | $this->entity_type_service = $entity_type_service; |
161 | 161 | $this->user_service = $user_service; |
@@ -166,8 +166,8 @@ discard block |
||
166 | 166 | |
167 | 167 | public static function get_instance() { |
168 | 168 | |
169 | - if ( ! isset( self::$instance ) ) { |
|
170 | - self::$instance = new self( Wordlift_Entity_Type_Service::get_instance(), Wordlift_User_Service::get_instance() ); |
|
169 | + if ( ! isset(self::$instance)) { |
|
170 | + self::$instance = new self(Wordlift_Entity_Type_Service::get_instance(), Wordlift_User_Service::get_instance()); |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | return self::$instance; |
@@ -181,17 +181,17 @@ discard block |
||
181 | 181 | public function create() { |
182 | 182 | |
183 | 183 | // Get the source image path. |
184 | - $source = plugin_dir_path( __DIR__ ) . 'images/rome.png'; |
|
184 | + $source = plugin_dir_path(__DIR__).'images/rome.png'; |
|
185 | 185 | |
186 | 186 | // Create an attachment with the local file. |
187 | - $attachment_id = $this->create_attachment_from_local_file( $source ); |
|
187 | + $attachment_id = $this->create_attachment_from_local_file($source); |
|
188 | 188 | |
189 | 189 | // Add a flag to signal the attachment is sample data and allow easy delete |
190 | 190 | // afterwards. |
191 | - add_post_meta( $attachment_id, '_wl_sample_data', 1, true ); |
|
191 | + add_post_meta($attachment_id, '_wl_sample_data', 1, true); |
|
192 | 192 | |
193 | 193 | // Get the dataset URI, used for replacements in the `post_content`. |
194 | - $dataset_uri = untrailingslashit( Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ); |
|
194 | + $dataset_uri = untrailingslashit(Wordlift_Configuration_Service::get_instance()->get_dataset_uri()); |
|
195 | 195 | |
196 | 196 | // Create the author and get its id. |
197 | 197 | $author_id = $this->create_author(); |
@@ -199,13 +199,13 @@ discard block |
||
199 | 199 | // Create 4 entities. |
200 | 200 | // Create 4 posts referencing each one entity. |
201 | 201 | // Create 1 post referencing all the entities. |
202 | - foreach ( $this->samples as $sample ) { |
|
202 | + foreach ($this->samples as $sample) { |
|
203 | 203 | |
204 | 204 | // Get the post data. |
205 | 205 | $post = array_replace_recursive( |
206 | 206 | $sample['post'], |
207 | 207 | array( |
208 | - 'post_content' => str_replace( '{dataset-uri}', $dataset_uri, $sample['post']['post_content'] ), |
|
208 | + 'post_content' => str_replace('{dataset-uri}', $dataset_uri, $sample['post']['post_content']), |
|
209 | 209 | ) |
210 | 210 | ); |
211 | 211 | |
@@ -213,18 +213,18 @@ discard block |
||
213 | 213 | $post['post_author'] = $author_id; |
214 | 214 | |
215 | 215 | // Insert the post. |
216 | - $post_id = wp_insert_post( $post ); |
|
216 | + $post_id = wp_insert_post($post); |
|
217 | 217 | |
218 | 218 | // Add a flag to signal the post is sample data and allow easy delete |
219 | 219 | // afterwards. |
220 | - add_post_meta( $post_id, '_wl_sample_data', 1, true ); |
|
220 | + add_post_meta($post_id, '_wl_sample_data', 1, true); |
|
221 | 221 | |
222 | 222 | // Set the post thumbnail. |
223 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
223 | + set_post_thumbnail($post_id, $attachment_id); |
|
224 | 224 | |
225 | 225 | // If the `entity_type_uri` property is set, set it on the post. |
226 | - if ( isset( $sample['entity_type_uri'] ) ) { |
|
227 | - $this->entity_type_service->set( $post_id, $sample['entity_type_uri'] ); |
|
226 | + if (isset($sample['entity_type_uri'])) { |
|
227 | + $this->entity_type_service->set($post_id, $sample['entity_type_uri']); |
|
228 | 228 | } |
229 | 229 | } |
230 | 230 | |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | */ |
239 | 239 | private function create_author() { |
240 | 240 | |
241 | - $user_id = wp_create_user( 'wl-sample-data', wp_generate_password() ); |
|
241 | + $user_id = wp_create_user('wl-sample-data', wp_generate_password()); |
|
242 | 242 | $author_post_id = wp_insert_post( |
243 | 243 | array( |
244 | 244 | 'post_type' => 'entity', |
@@ -247,10 +247,10 @@ discard block |
||
247 | 247 | ); |
248 | 248 | // Add a flag to signal the attachment is sample data and allow easy delete |
249 | 249 | // afterwards. |
250 | - add_post_meta( $author_post_id, '_wl_sample_data', 1, true ); |
|
250 | + add_post_meta($author_post_id, '_wl_sample_data', 1, true); |
|
251 | 251 | |
252 | - $this->entity_type_service->set( $author_post_id, 'http://schema.org/Person' ); |
|
253 | - $this->user_service->set_entity( $user_id, $author_post_id ); |
|
252 | + $this->entity_type_service->set($author_post_id, 'http://schema.org/Person'); |
|
253 | + $this->user_service->set_entity($user_id, $author_post_id); |
|
254 | 254 | |
255 | 255 | return $user_id; |
256 | 256 | } |
@@ -262,13 +262,13 @@ discard block |
||
262 | 262 | */ |
263 | 263 | public function delete() { |
264 | 264 | |
265 | - $this->delete_by_type( 'post' ); |
|
266 | - $this->delete_by_type( 'entity' ); |
|
267 | - $this->delete_by_type( 'attachment' ); |
|
265 | + $this->delete_by_type('post'); |
|
266 | + $this->delete_by_type('entity'); |
|
267 | + $this->delete_by_type('attachment'); |
|
268 | 268 | |
269 | 269 | // Get and delete the user. |
270 | - $user = get_user_by( 'login', 'wl-sample-data' ); |
|
271 | - wp_delete_user( $user->ID ); |
|
270 | + $user = get_user_by('login', 'wl-sample-data'); |
|
271 | + wp_delete_user($user->ID); |
|
272 | 272 | |
273 | 273 | } |
274 | 274 | |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | * |
281 | 281 | * @since 3.12.0 |
282 | 282 | */ |
283 | - private function delete_by_type( $type ) { |
|
283 | + private function delete_by_type($type) { |
|
284 | 284 | |
285 | 285 | $posts = get_posts( |
286 | 286 | array( |
@@ -291,8 +291,8 @@ discard block |
||
291 | 291 | ) |
292 | 292 | ); |
293 | 293 | |
294 | - foreach ( $posts as $post ) { |
|
295 | - wp_delete_post( $post->ID, true ); |
|
294 | + foreach ($posts as $post) { |
|
295 | + wp_delete_post($post->ID, true); |
|
296 | 296 | } |
297 | 297 | |
298 | 298 | } |
@@ -305,20 +305,20 @@ discard block |
||
305 | 305 | * @return int WordPress' attachment's id. |
306 | 306 | * @since 3.12.0 |
307 | 307 | */ |
308 | - private function create_attachment_from_local_file( $source ) { |
|
308 | + private function create_attachment_from_local_file($source) { |
|
309 | 309 | |
310 | 310 | // Get the path to the upload directory. |
311 | 311 | $upload_dir = wp_upload_dir(); |
312 | 312 | $upload_path = $upload_dir['path']; |
313 | 313 | |
314 | 314 | // Get the destination image path. |
315 | - $destination = $upload_path . '/wl-sample-data.png'; |
|
315 | + $destination = $upload_path.'/wl-sample-data.png'; |
|
316 | 316 | |
317 | 317 | // Copy the source file to the destination. |
318 | 318 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
319 | - @copy( $source, $destination ); |
|
319 | + @copy($source, $destination); |
|
320 | 320 | |
321 | - return $this->create_attachment( $destination ); |
|
321 | + return $this->create_attachment($destination); |
|
322 | 322 | } |
323 | 323 | |
324 | 324 | /** |
@@ -332,31 +332,31 @@ discard block |
||
332 | 332 | * |
333 | 333 | * @return int The attachment id. |
334 | 334 | */ |
335 | - private function create_attachment( $filename ) { |
|
335 | + private function create_attachment($filename) { |
|
336 | 336 | |
337 | 337 | // Check the type of file. We'll use this as the 'post_mime_type'. |
338 | - $filetype = wp_check_filetype( basename( $filename ), null ); |
|
338 | + $filetype = wp_check_filetype(basename($filename), null); |
|
339 | 339 | |
340 | 340 | // Get the path to the upload directory. |
341 | 341 | $wp_upload_dir = wp_upload_dir(); |
342 | 342 | |
343 | 343 | // Prepare an array of post data for the attachment. |
344 | 344 | $attachment = array( |
345 | - 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), |
|
345 | + 'guid' => $wp_upload_dir['url'].'/'.basename($filename), |
|
346 | 346 | 'post_mime_type' => $filetype['type'], |
347 | - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
347 | + 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), |
|
348 | 348 | 'post_content' => '', |
349 | 349 | 'post_status' => 'inherit', |
350 | 350 | ); |
351 | 351 | |
352 | 352 | // Insert the attachment. |
353 | - $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
353 | + $attachment_id = wp_insert_attachment($attachment, $filename); |
|
354 | 354 | |
355 | 355 | // Generate the metadata for the attachment, and update the database record. |
356 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
356 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
357 | 357 | |
358 | 358 | // Update the attachment metadata. |
359 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
359 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
360 | 360 | |
361 | 361 | return $attachment_id; |
362 | 362 | } |
@@ -23,257 +23,257 @@ |
||
23 | 23 | */ |
24 | 24 | class Wordlift_Content_Filter_Service { |
25 | 25 | |
26 | - /** |
|
27 | - * The pattern to find entities in text. |
|
28 | - * |
|
29 | - * @since 3.8.0 |
|
30 | - */ |
|
31 | - const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
32 | - |
|
33 | - /** |
|
34 | - * A {@link Wordlift_Entity_Service} instance. |
|
35 | - * |
|
36 | - * @since 3.8.0 |
|
37 | - * @access private |
|
38 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
39 | - */ |
|
40 | - private $entity_service; |
|
41 | - |
|
42 | - /** |
|
43 | - * The `link by default` setting. |
|
44 | - * |
|
45 | - * @since 3.13.0 |
|
46 | - * @access private |
|
47 | - * @var bool True if link by default is enabled otherwise false. |
|
48 | - */ |
|
49 | - private $is_link_by_default; |
|
50 | - |
|
51 | - private $linked_entity_uris = array(); |
|
52 | - |
|
53 | - /** |
|
54 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
55 | - * |
|
56 | - * @since 3.16.3 |
|
57 | - * @access private |
|
58 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
59 | - */ |
|
60 | - private $entity_uri_service; |
|
61 | - |
|
62 | - /** |
|
63 | - * A {@link Wordlift_Log_Service} instance. |
|
64 | - * |
|
65 | - * @since 3.16.0 |
|
66 | - * |
|
67 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
68 | - */ |
|
69 | - private $log; |
|
70 | - /** |
|
71 | - * @var Object_Link_Provider |
|
72 | - */ |
|
73 | - private $object_link_provider; |
|
74 | - |
|
75 | - /** |
|
76 | - * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
77 | - * |
|
78 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
79 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
80 | - * |
|
81 | - * @since 3.8.0 |
|
82 | - */ |
|
83 | - protected function __construct( $entity_service, $entity_uri_service ) { |
|
84 | - |
|
85 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
86 | - |
|
87 | - $this->entity_service = $entity_service; |
|
88 | - $this->entity_uri_service = $entity_uri_service; |
|
89 | - $this->object_link_provider = Object_Link_Provider::get_instance(); |
|
90 | - |
|
91 | - } |
|
92 | - |
|
93 | - private static $instance = null; |
|
94 | - |
|
95 | - /** |
|
96 | - * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
97 | - * |
|
98 | - * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
99 | - * @since 3.14.2 |
|
100 | - */ |
|
101 | - public static function get_instance() { |
|
102 | - |
|
103 | - if ( ! isset( self::$instance ) ) { |
|
104 | - self::$instance = new self( Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance() ); |
|
105 | - } |
|
106 | - |
|
107 | - return self::$instance; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Mangle the content by adding links to the entity pages. This function is |
|
112 | - * hooked to the 'the_content' WP's filter. |
|
113 | - * |
|
114 | - * @param string $content The content being filtered. |
|
115 | - * |
|
116 | - * @return string The filtered content. |
|
117 | - * @since 3.8.0 |
|
118 | - */ |
|
119 | - public function the_content( $content ) { |
|
120 | - $this->log->trace( 'Filtering content [ ' . ( is_singular() ? 'yes' : 'no' ) . ' ]...' ); |
|
121 | - |
|
122 | - // Links should be added only on the front end and not for RSS. |
|
123 | - if ( is_feed() || is_admin() || is_search() ) { |
|
124 | - return $content; |
|
125 | - } |
|
126 | - |
|
127 | - // Preload the `link by default` setting. |
|
128 | - $this->is_link_by_default = Wordlift_Configuration_Service::get_instance()->is_link_by_default(); |
|
129 | - |
|
130 | - // Reset the array of of entity post ids linked from the post content. |
|
131 | - // This is used to avoid linking more the once the same post. |
|
132 | - $this->linked_entity_uris = array(); |
|
133 | - |
|
134 | - // Preload URIs. |
|
135 | - $matches = array(); |
|
136 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
137 | - |
|
138 | - // Bail out if there are no URIs. |
|
139 | - if ( empty( $matches[3] ) ) { |
|
140 | - return $content; |
|
141 | - } |
|
142 | - |
|
143 | - // Replace each match of the entity tag with the entity link. If an error |
|
144 | - // occurs fail silently returning the original content. |
|
145 | - $maybe_content = preg_replace_callback( |
|
146 | - self::PATTERN, |
|
147 | - array( |
|
148 | - $this, |
|
149 | - 'link', |
|
150 | - ), |
|
151 | - $content |
|
152 | - ); |
|
153 | - |
|
154 | - return $maybe_content ? $maybe_content : $content; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Get the entity match and replace it with a page link. |
|
159 | - * |
|
160 | - * @param array $matches An array of matches. |
|
161 | - * |
|
162 | - * @return string The replaced text with the link to the entity page. |
|
163 | - * @since 3.8.0 |
|
164 | - */ |
|
165 | - private function link( $matches ) { |
|
166 | - |
|
167 | - // Get the entity itemid URI and label. |
|
168 | - $css_class = $matches[2]; |
|
169 | - $uri = $matches[3]; |
|
170 | - $label = $matches[4]; |
|
171 | - |
|
172 | - /** |
|
173 | - * If the entity is already linked, dont send query to the db. |
|
174 | - */ |
|
175 | - if ( $this->is_already_linked( $uri ) ) { |
|
176 | - return $label; |
|
177 | - } |
|
178 | - |
|
179 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
180 | - |
|
181 | - // If the entity should not be linked and link by default is also disabled, |
|
182 | - // then don't lookup the entity on the table. |
|
183 | - if ( ! $this->is_link_by_default && ! $link ) { |
|
184 | - return $label; |
|
185 | - } |
|
186 | - |
|
187 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
188 | - $content = $content_service->get_by_entity_id_or_same_as( $uri ); |
|
189 | - |
|
190 | - // If no content is found, return the label, that is _remove the annotation_. |
|
191 | - if ( ! is_object( $content ) ) { |
|
192 | - return $label; |
|
193 | - } |
|
194 | - |
|
195 | - $object_id = $content->get_id(); |
|
196 | - $object_type = $content->get_object_type_enum(); |
|
197 | - |
|
198 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
199 | - |
|
200 | - // Don't link if links are disabled and the entity is not link or the |
|
201 | - // entity is do not link. |
|
202 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
203 | - |
|
204 | - // Return the label if it's don't link. |
|
205 | - if ( $dont_link ) { |
|
206 | - return $label; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * @since 3.32.0 |
|
211 | - * Object_ids are prefixed with object_type to prevent conflicts. |
|
212 | - */ |
|
213 | - $this->linked_entity_uris[] = $uri; |
|
214 | - |
|
215 | - // Get the link. |
|
216 | - $href = Wordlift_Post_Adapter::get_production_permalink( $object_id, $object_type ); |
|
217 | - |
|
218 | - // Bail out if the `$href` has been reset. |
|
219 | - if ( empty( $href ) ) { |
|
220 | - return $label; |
|
221 | - } |
|
222 | - |
|
223 | - return Link_Builder::create( $uri, $object_id ) |
|
224 | - ->label( $label ) |
|
225 | - ->href( $href ) |
|
226 | - ->generate_link(); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Get a string to be used as a title attribute in links to a post |
|
231 | - * |
|
232 | - * @param int $post_id The post id of the post being linked. |
|
233 | - * @param string $ignore_label A label to ignore. |
|
234 | - * |
|
235 | - * @return string The title to be used in the link. An empty string when |
|
236 | - * there is no alternative that is not the $ignore_label. |
|
237 | - * @deprecated 3.32.0 Use object link provider to get the link title for getting link |
|
238 | - * title for different types. |
|
239 | - * @since 3.15.0 |
|
240 | - * |
|
241 | - * As of 3.32.0 this method is not used anywhere in the core, this should be removed |
|
242 | - * from tests and companion plugins. |
|
243 | - */ |
|
244 | - public function get_link_title( $post_id, $ignore_label, $object_type = Object_Type_Enum::POST ) { |
|
245 | - return $this->object_link_provider->get_link_title( $post_id, $ignore_label, $object_type ); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
250 | - * the provided content. |
|
251 | - * |
|
252 | - * @param string $content The content. |
|
253 | - * |
|
254 | - * @return array An array of URIs. |
|
255 | - * @since 3.14.2 |
|
256 | - */ |
|
257 | - public function get_entity_uris( $content ) { |
|
258 | - |
|
259 | - $matches = array(); |
|
260 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
261 | - |
|
262 | - // We need to use `array_values` here in order to avoid further `json_encode` |
|
263 | - // to turn it into an object (since if the 3rd match isn't found the index |
|
264 | - // is not sequential. |
|
265 | - // |
|
266 | - // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
267 | - return array_values( array_unique( $matches[3] ) ); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * @param $entity_uri |
|
272 | - * |
|
273 | - * @return bool |
|
274 | - */ |
|
275 | - private function is_already_linked( $entity_uri ) { |
|
276 | - return in_array( $entity_uri, $this->linked_entity_uris, true ); |
|
277 | - } |
|
26 | + /** |
|
27 | + * The pattern to find entities in text. |
|
28 | + * |
|
29 | + * @since 3.8.0 |
|
30 | + */ |
|
31 | + const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
32 | + |
|
33 | + /** |
|
34 | + * A {@link Wordlift_Entity_Service} instance. |
|
35 | + * |
|
36 | + * @since 3.8.0 |
|
37 | + * @access private |
|
38 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
39 | + */ |
|
40 | + private $entity_service; |
|
41 | + |
|
42 | + /** |
|
43 | + * The `link by default` setting. |
|
44 | + * |
|
45 | + * @since 3.13.0 |
|
46 | + * @access private |
|
47 | + * @var bool True if link by default is enabled otherwise false. |
|
48 | + */ |
|
49 | + private $is_link_by_default; |
|
50 | + |
|
51 | + private $linked_entity_uris = array(); |
|
52 | + |
|
53 | + /** |
|
54 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
55 | + * |
|
56 | + * @since 3.16.3 |
|
57 | + * @access private |
|
58 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
59 | + */ |
|
60 | + private $entity_uri_service; |
|
61 | + |
|
62 | + /** |
|
63 | + * A {@link Wordlift_Log_Service} instance. |
|
64 | + * |
|
65 | + * @since 3.16.0 |
|
66 | + * |
|
67 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
68 | + */ |
|
69 | + private $log; |
|
70 | + /** |
|
71 | + * @var Object_Link_Provider |
|
72 | + */ |
|
73 | + private $object_link_provider; |
|
74 | + |
|
75 | + /** |
|
76 | + * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
77 | + * |
|
78 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
79 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
80 | + * |
|
81 | + * @since 3.8.0 |
|
82 | + */ |
|
83 | + protected function __construct( $entity_service, $entity_uri_service ) { |
|
84 | + |
|
85 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
86 | + |
|
87 | + $this->entity_service = $entity_service; |
|
88 | + $this->entity_uri_service = $entity_uri_service; |
|
89 | + $this->object_link_provider = Object_Link_Provider::get_instance(); |
|
90 | + |
|
91 | + } |
|
92 | + |
|
93 | + private static $instance = null; |
|
94 | + |
|
95 | + /** |
|
96 | + * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
97 | + * |
|
98 | + * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
99 | + * @since 3.14.2 |
|
100 | + */ |
|
101 | + public static function get_instance() { |
|
102 | + |
|
103 | + if ( ! isset( self::$instance ) ) { |
|
104 | + self::$instance = new self( Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance() ); |
|
105 | + } |
|
106 | + |
|
107 | + return self::$instance; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Mangle the content by adding links to the entity pages. This function is |
|
112 | + * hooked to the 'the_content' WP's filter. |
|
113 | + * |
|
114 | + * @param string $content The content being filtered. |
|
115 | + * |
|
116 | + * @return string The filtered content. |
|
117 | + * @since 3.8.0 |
|
118 | + */ |
|
119 | + public function the_content( $content ) { |
|
120 | + $this->log->trace( 'Filtering content [ ' . ( is_singular() ? 'yes' : 'no' ) . ' ]...' ); |
|
121 | + |
|
122 | + // Links should be added only on the front end and not for RSS. |
|
123 | + if ( is_feed() || is_admin() || is_search() ) { |
|
124 | + return $content; |
|
125 | + } |
|
126 | + |
|
127 | + // Preload the `link by default` setting. |
|
128 | + $this->is_link_by_default = Wordlift_Configuration_Service::get_instance()->is_link_by_default(); |
|
129 | + |
|
130 | + // Reset the array of of entity post ids linked from the post content. |
|
131 | + // This is used to avoid linking more the once the same post. |
|
132 | + $this->linked_entity_uris = array(); |
|
133 | + |
|
134 | + // Preload URIs. |
|
135 | + $matches = array(); |
|
136 | + preg_match_all( self::PATTERN, $content, $matches ); |
|
137 | + |
|
138 | + // Bail out if there are no URIs. |
|
139 | + if ( empty( $matches[3] ) ) { |
|
140 | + return $content; |
|
141 | + } |
|
142 | + |
|
143 | + // Replace each match of the entity tag with the entity link. If an error |
|
144 | + // occurs fail silently returning the original content. |
|
145 | + $maybe_content = preg_replace_callback( |
|
146 | + self::PATTERN, |
|
147 | + array( |
|
148 | + $this, |
|
149 | + 'link', |
|
150 | + ), |
|
151 | + $content |
|
152 | + ); |
|
153 | + |
|
154 | + return $maybe_content ? $maybe_content : $content; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Get the entity match and replace it with a page link. |
|
159 | + * |
|
160 | + * @param array $matches An array of matches. |
|
161 | + * |
|
162 | + * @return string The replaced text with the link to the entity page. |
|
163 | + * @since 3.8.0 |
|
164 | + */ |
|
165 | + private function link( $matches ) { |
|
166 | + |
|
167 | + // Get the entity itemid URI and label. |
|
168 | + $css_class = $matches[2]; |
|
169 | + $uri = $matches[3]; |
|
170 | + $label = $matches[4]; |
|
171 | + |
|
172 | + /** |
|
173 | + * If the entity is already linked, dont send query to the db. |
|
174 | + */ |
|
175 | + if ( $this->is_already_linked( $uri ) ) { |
|
176 | + return $label; |
|
177 | + } |
|
178 | + |
|
179 | + $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
180 | + |
|
181 | + // If the entity should not be linked and link by default is also disabled, |
|
182 | + // then don't lookup the entity on the table. |
|
183 | + if ( ! $this->is_link_by_default && ! $link ) { |
|
184 | + return $label; |
|
185 | + } |
|
186 | + |
|
187 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
188 | + $content = $content_service->get_by_entity_id_or_same_as( $uri ); |
|
189 | + |
|
190 | + // If no content is found, return the label, that is _remove the annotation_. |
|
191 | + if ( ! is_object( $content ) ) { |
|
192 | + return $label; |
|
193 | + } |
|
194 | + |
|
195 | + $object_id = $content->get_id(); |
|
196 | + $object_type = $content->get_object_type_enum(); |
|
197 | + |
|
198 | + $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
199 | + |
|
200 | + // Don't link if links are disabled and the entity is not link or the |
|
201 | + // entity is do not link. |
|
202 | + $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
203 | + |
|
204 | + // Return the label if it's don't link. |
|
205 | + if ( $dont_link ) { |
|
206 | + return $label; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * @since 3.32.0 |
|
211 | + * Object_ids are prefixed with object_type to prevent conflicts. |
|
212 | + */ |
|
213 | + $this->linked_entity_uris[] = $uri; |
|
214 | + |
|
215 | + // Get the link. |
|
216 | + $href = Wordlift_Post_Adapter::get_production_permalink( $object_id, $object_type ); |
|
217 | + |
|
218 | + // Bail out if the `$href` has been reset. |
|
219 | + if ( empty( $href ) ) { |
|
220 | + return $label; |
|
221 | + } |
|
222 | + |
|
223 | + return Link_Builder::create( $uri, $object_id ) |
|
224 | + ->label( $label ) |
|
225 | + ->href( $href ) |
|
226 | + ->generate_link(); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Get a string to be used as a title attribute in links to a post |
|
231 | + * |
|
232 | + * @param int $post_id The post id of the post being linked. |
|
233 | + * @param string $ignore_label A label to ignore. |
|
234 | + * |
|
235 | + * @return string The title to be used in the link. An empty string when |
|
236 | + * there is no alternative that is not the $ignore_label. |
|
237 | + * @deprecated 3.32.0 Use object link provider to get the link title for getting link |
|
238 | + * title for different types. |
|
239 | + * @since 3.15.0 |
|
240 | + * |
|
241 | + * As of 3.32.0 this method is not used anywhere in the core, this should be removed |
|
242 | + * from tests and companion plugins. |
|
243 | + */ |
|
244 | + public function get_link_title( $post_id, $ignore_label, $object_type = Object_Type_Enum::POST ) { |
|
245 | + return $this->object_link_provider->get_link_title( $post_id, $ignore_label, $object_type ); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
250 | + * the provided content. |
|
251 | + * |
|
252 | + * @param string $content The content. |
|
253 | + * |
|
254 | + * @return array An array of URIs. |
|
255 | + * @since 3.14.2 |
|
256 | + */ |
|
257 | + public function get_entity_uris( $content ) { |
|
258 | + |
|
259 | + $matches = array(); |
|
260 | + preg_match_all( self::PATTERN, $content, $matches ); |
|
261 | + |
|
262 | + // We need to use `array_values` here in order to avoid further `json_encode` |
|
263 | + // to turn it into an object (since if the 3rd match isn't found the index |
|
264 | + // is not sequential. |
|
265 | + // |
|
266 | + // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
267 | + return array_values( array_unique( $matches[3] ) ); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * @param $entity_uri |
|
272 | + * |
|
273 | + * @return bool |
|
274 | + */ |
|
275 | + private function is_already_linked( $entity_uri ) { |
|
276 | + return in_array( $entity_uri, $this->linked_entity_uris, true ); |
|
277 | + } |
|
278 | 278 | |
279 | 279 | } |
@@ -80,9 +80,9 @@ discard block |
||
80 | 80 | * |
81 | 81 | * @since 3.8.0 |
82 | 82 | */ |
83 | - protected function __construct( $entity_service, $entity_uri_service ) { |
|
83 | + protected function __construct($entity_service, $entity_uri_service) { |
|
84 | 84 | |
85 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
85 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
86 | 86 | |
87 | 87 | $this->entity_service = $entity_service; |
88 | 88 | $this->entity_uri_service = $entity_uri_service; |
@@ -100,8 +100,8 @@ discard block |
||
100 | 100 | */ |
101 | 101 | public static function get_instance() { |
102 | 102 | |
103 | - if ( ! isset( self::$instance ) ) { |
|
104 | - self::$instance = new self( Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance() ); |
|
103 | + if ( ! isset(self::$instance)) { |
|
104 | + self::$instance = new self(Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance()); |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | return self::$instance; |
@@ -116,11 +116,11 @@ discard block |
||
116 | 116 | * @return string The filtered content. |
117 | 117 | * @since 3.8.0 |
118 | 118 | */ |
119 | - public function the_content( $content ) { |
|
120 | - $this->log->trace( 'Filtering content [ ' . ( is_singular() ? 'yes' : 'no' ) . ' ]...' ); |
|
119 | + public function the_content($content) { |
|
120 | + $this->log->trace('Filtering content [ '.(is_singular() ? 'yes' : 'no').' ]...'); |
|
121 | 121 | |
122 | 122 | // Links should be added only on the front end and not for RSS. |
123 | - if ( is_feed() || is_admin() || is_search() ) { |
|
123 | + if (is_feed() || is_admin() || is_search()) { |
|
124 | 124 | return $content; |
125 | 125 | } |
126 | 126 | |
@@ -133,10 +133,10 @@ discard block |
||
133 | 133 | |
134 | 134 | // Preload URIs. |
135 | 135 | $matches = array(); |
136 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
136 | + preg_match_all(self::PATTERN, $content, $matches); |
|
137 | 137 | |
138 | 138 | // Bail out if there are no URIs. |
139 | - if ( empty( $matches[3] ) ) { |
|
139 | + if (empty($matches[3])) { |
|
140 | 140 | return $content; |
141 | 141 | } |
142 | 142 | |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | * @return string The replaced text with the link to the entity page. |
163 | 163 | * @since 3.8.0 |
164 | 164 | */ |
165 | - private function link( $matches ) { |
|
165 | + private function link($matches) { |
|
166 | 166 | |
167 | 167 | // Get the entity itemid URI and label. |
168 | 168 | $css_class = $matches[2]; |
@@ -172,37 +172,37 @@ discard block |
||
172 | 172 | /** |
173 | 173 | * If the entity is already linked, dont send query to the db. |
174 | 174 | */ |
175 | - if ( $this->is_already_linked( $uri ) ) { |
|
175 | + if ($this->is_already_linked($uri)) { |
|
176 | 176 | return $label; |
177 | 177 | } |
178 | 178 | |
179 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
179 | + $link = - 1 < strpos($css_class, 'wl-link'); |
|
180 | 180 | |
181 | 181 | // If the entity should not be linked and link by default is also disabled, |
182 | 182 | // then don't lookup the entity on the table. |
183 | - if ( ! $this->is_link_by_default && ! $link ) { |
|
183 | + if ( ! $this->is_link_by_default && ! $link) { |
|
184 | 184 | return $label; |
185 | 185 | } |
186 | 186 | |
187 | 187 | $content_service = Wordpress_Content_Service::get_instance(); |
188 | - $content = $content_service->get_by_entity_id_or_same_as( $uri ); |
|
188 | + $content = $content_service->get_by_entity_id_or_same_as($uri); |
|
189 | 189 | |
190 | 190 | // If no content is found, return the label, that is _remove the annotation_. |
191 | - if ( ! is_object( $content ) ) { |
|
191 | + if ( ! is_object($content)) { |
|
192 | 192 | return $label; |
193 | 193 | } |
194 | 194 | |
195 | 195 | $object_id = $content->get_id(); |
196 | 196 | $object_type = $content->get_object_type_enum(); |
197 | 197 | |
198 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
198 | + $no_link = - 1 < strpos($css_class, 'wl-no-link'); |
|
199 | 199 | |
200 | 200 | // Don't link if links are disabled and the entity is not link or the |
201 | 201 | // entity is do not link. |
202 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
202 | + $dont_link = ( ! $this->is_link_by_default && ! $link) || $no_link; |
|
203 | 203 | |
204 | 204 | // Return the label if it's don't link. |
205 | - if ( $dont_link ) { |
|
205 | + if ($dont_link) { |
|
206 | 206 | return $label; |
207 | 207 | } |
208 | 208 | |
@@ -213,16 +213,16 @@ discard block |
||
213 | 213 | $this->linked_entity_uris[] = $uri; |
214 | 214 | |
215 | 215 | // Get the link. |
216 | - $href = Wordlift_Post_Adapter::get_production_permalink( $object_id, $object_type ); |
|
216 | + $href = Wordlift_Post_Adapter::get_production_permalink($object_id, $object_type); |
|
217 | 217 | |
218 | 218 | // Bail out if the `$href` has been reset. |
219 | - if ( empty( $href ) ) { |
|
219 | + if (empty($href)) { |
|
220 | 220 | return $label; |
221 | 221 | } |
222 | 222 | |
223 | - return Link_Builder::create( $uri, $object_id ) |
|
224 | - ->label( $label ) |
|
225 | - ->href( $href ) |
|
223 | + return Link_Builder::create($uri, $object_id) |
|
224 | + ->label($label) |
|
225 | + ->href($href) |
|
226 | 226 | ->generate_link(); |
227 | 227 | } |
228 | 228 | |
@@ -241,8 +241,8 @@ discard block |
||
241 | 241 | * As of 3.32.0 this method is not used anywhere in the core, this should be removed |
242 | 242 | * from tests and companion plugins. |
243 | 243 | */ |
244 | - public function get_link_title( $post_id, $ignore_label, $object_type = Object_Type_Enum::POST ) { |
|
245 | - return $this->object_link_provider->get_link_title( $post_id, $ignore_label, $object_type ); |
|
244 | + public function get_link_title($post_id, $ignore_label, $object_type = Object_Type_Enum::POST) { |
|
245 | + return $this->object_link_provider->get_link_title($post_id, $ignore_label, $object_type); |
|
246 | 246 | } |
247 | 247 | |
248 | 248 | /** |
@@ -254,17 +254,17 @@ discard block |
||
254 | 254 | * @return array An array of URIs. |
255 | 255 | * @since 3.14.2 |
256 | 256 | */ |
257 | - public function get_entity_uris( $content ) { |
|
257 | + public function get_entity_uris($content) { |
|
258 | 258 | |
259 | 259 | $matches = array(); |
260 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
260 | + preg_match_all(self::PATTERN, $content, $matches); |
|
261 | 261 | |
262 | 262 | // We need to use `array_values` here in order to avoid further `json_encode` |
263 | 263 | // to turn it into an object (since if the 3rd match isn't found the index |
264 | 264 | // is not sequential. |
265 | 265 | // |
266 | 266 | // See https://github.com/insideout10/wordlift-plugin/issues/646. |
267 | - return array_values( array_unique( $matches[3] ) ); |
|
267 | + return array_values(array_unique($matches[3])); |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | /** |
@@ -272,8 +272,8 @@ discard block |
||
272 | 272 | * |
273 | 273 | * @return bool |
274 | 274 | */ |
275 | - private function is_already_linked( $entity_uri ) { |
|
276 | - return in_array( $entity_uri, $this->linked_entity_uris, true ); |
|
275 | + private function is_already_linked($entity_uri) { |
|
276 | + return in_array($entity_uri, $this->linked_entity_uris, true); |
|
277 | 277 | } |
278 | 278 | |
279 | 279 | } |
@@ -13,64 +13,64 @@ |
||
13 | 13 | */ |
14 | 14 | class Wordlift_Debug_Service { |
15 | 15 | |
16 | - /** |
|
17 | - * The {@link Wordlift_Entity_Service} instance. |
|
18 | - * |
|
19 | - * @since 3.7.2 |
|
20 | - * @access private |
|
21 | - * @var Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
22 | - */ |
|
23 | - private $entity_service; |
|
16 | + /** |
|
17 | + * The {@link Wordlift_Entity_Service} instance. |
|
18 | + * |
|
19 | + * @since 3.7.2 |
|
20 | + * @access private |
|
21 | + * @var Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
22 | + */ |
|
23 | + private $entity_service; |
|
24 | 24 | |
25 | - /** |
|
26 | - * A {@link Wordlift_Uri_Service} instance. |
|
27 | - * |
|
28 | - * @since 3.10.0 |
|
29 | - * @access private |
|
30 | - * @var \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
31 | - */ |
|
32 | - private $uri_service; |
|
25 | + /** |
|
26 | + * A {@link Wordlift_Uri_Service} instance. |
|
27 | + * |
|
28 | + * @since 3.10.0 |
|
29 | + * @access private |
|
30 | + * @var \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
31 | + */ |
|
32 | + private $uri_service; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Wordlift_Debug_Service constructor. |
|
36 | - * |
|
37 | - * @since 3.7.2 |
|
38 | - * |
|
39 | - * @param Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
40 | - * @param \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
41 | - */ |
|
42 | - public function __construct( $entity_service, $uri_service ) { |
|
34 | + /** |
|
35 | + * Wordlift_Debug_Service constructor. |
|
36 | + * |
|
37 | + * @since 3.7.2 |
|
38 | + * |
|
39 | + * @param Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
40 | + * @param \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
41 | + */ |
|
42 | + public function __construct( $entity_service, $uri_service ) { |
|
43 | 43 | |
44 | - $this->entity_service = $entity_service; |
|
45 | - $this->uri_service = $uri_service; |
|
44 | + $this->entity_service = $entity_service; |
|
45 | + $this->uri_service = $uri_service; |
|
46 | 46 | |
47 | - add_action( 'wp_ajax_wl_dump_uri', array( $this, 'dump_uri' ) ); |
|
47 | + add_action( 'wp_ajax_wl_dump_uri', array( $this, 'dump_uri' ) ); |
|
48 | 48 | |
49 | - } |
|
49 | + } |
|
50 | 50 | |
51 | - public function dump_uri() { |
|
51 | + public function dump_uri() { |
|
52 | 52 | |
53 | - if ( ! isset( $_GET['id'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
54 | - wp_send_json_error( 'id not set' ); |
|
55 | - } |
|
53 | + if ( ! isset( $_GET['id'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
54 | + wp_send_json_error( 'id not set' ); |
|
55 | + } |
|
56 | 56 | |
57 | - $post_id = (int) $_GET['id']; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
57 | + $post_id = (int) $_GET['id']; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
58 | 58 | |
59 | - $post = get_post( $post_id ); |
|
59 | + $post = get_post( $post_id ); |
|
60 | 60 | |
61 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
62 | - $build_uri = $this->uri_service->build_uri( $post->post_title, $post->post_type ); |
|
61 | + $uri = $this->entity_service->get_uri( $post_id ); |
|
62 | + $build_uri = $this->uri_service->build_uri( $post->post_title, $post->post_type ); |
|
63 | 63 | |
64 | - wp_send_json_success( |
|
65 | - array( |
|
66 | - 'uri' => $uri, |
|
67 | - 'post_title' => sprintf( '%s (%s)', $post->post_title, mb_detect_encoding( $post->post_title ) ), |
|
68 | - 'post_title_ascii' => mb_convert_encoding( $post->post_title, 'ASCII' ), |
|
69 | - 'build_uri' => $build_uri, |
|
70 | - 'build_uri_convert' => mb_convert_encoding( $build_uri, 'ASCII' ), |
|
71 | - ) |
|
72 | - ); |
|
64 | + wp_send_json_success( |
|
65 | + array( |
|
66 | + 'uri' => $uri, |
|
67 | + 'post_title' => sprintf( '%s (%s)', $post->post_title, mb_detect_encoding( $post->post_title ) ), |
|
68 | + 'post_title_ascii' => mb_convert_encoding( $post->post_title, 'ASCII' ), |
|
69 | + 'build_uri' => $build_uri, |
|
70 | + 'build_uri_convert' => mb_convert_encoding( $build_uri, 'ASCII' ), |
|
71 | + ) |
|
72 | + ); |
|
73 | 73 | |
74 | - } |
|
74 | + } |
|
75 | 75 | |
76 | 76 | } |
@@ -39,35 +39,35 @@ |
||
39 | 39 | * @param Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
40 | 40 | * @param \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
41 | 41 | */ |
42 | - public function __construct( $entity_service, $uri_service ) { |
|
42 | + public function __construct($entity_service, $uri_service) { |
|
43 | 43 | |
44 | 44 | $this->entity_service = $entity_service; |
45 | 45 | $this->uri_service = $uri_service; |
46 | 46 | |
47 | - add_action( 'wp_ajax_wl_dump_uri', array( $this, 'dump_uri' ) ); |
|
47 | + add_action('wp_ajax_wl_dump_uri', array($this, 'dump_uri')); |
|
48 | 48 | |
49 | 49 | } |
50 | 50 | |
51 | 51 | public function dump_uri() { |
52 | 52 | |
53 | - if ( ! isset( $_GET['id'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
54 | - wp_send_json_error( 'id not set' ); |
|
53 | + if ( ! isset($_GET['id'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
54 | + wp_send_json_error('id not set'); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | $post_id = (int) $_GET['id']; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
58 | 58 | |
59 | - $post = get_post( $post_id ); |
|
59 | + $post = get_post($post_id); |
|
60 | 60 | |
61 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
62 | - $build_uri = $this->uri_service->build_uri( $post->post_title, $post->post_type ); |
|
61 | + $uri = $this->entity_service->get_uri($post_id); |
|
62 | + $build_uri = $this->uri_service->build_uri($post->post_title, $post->post_type); |
|
63 | 63 | |
64 | 64 | wp_send_json_success( |
65 | 65 | array( |
66 | 66 | 'uri' => $uri, |
67 | - 'post_title' => sprintf( '%s (%s)', $post->post_title, mb_detect_encoding( $post->post_title ) ), |
|
68 | - 'post_title_ascii' => mb_convert_encoding( $post->post_title, 'ASCII' ), |
|
67 | + 'post_title' => sprintf('%s (%s)', $post->post_title, mb_detect_encoding($post->post_title)), |
|
68 | + 'post_title_ascii' => mb_convert_encoding($post->post_title, 'ASCII'), |
|
69 | 69 | 'build_uri' => $build_uri, |
70 | - 'build_uri_convert' => mb_convert_encoding( $build_uri, 'ASCII' ), |
|
70 | + 'build_uri_convert' => mb_convert_encoding($build_uri, 'ASCII'), |
|
71 | 71 | ) |
72 | 72 | ); |
73 | 73 |
@@ -11,58 +11,58 @@ |
||
11 | 11 | */ |
12 | 12 | class Wordlift_Property_Factory { |
13 | 13 | |
14 | - /** |
|
15 | - * The default {@link Wordlift_Property_Service}. |
|
16 | - * |
|
17 | - * @since 3.7.0 |
|
18 | - * @access private |
|
19 | - * @var \Wordlift_Property_Service $default_property_service The default {@link Wordlift_Property_Service}. |
|
20 | - */ |
|
21 | - private $default_property_service; |
|
14 | + /** |
|
15 | + * The default {@link Wordlift_Property_Service}. |
|
16 | + * |
|
17 | + * @since 3.7.0 |
|
18 | + * @access private |
|
19 | + * @var \Wordlift_Property_Service $default_property_service The default {@link Wordlift_Property_Service}. |
|
20 | + */ |
|
21 | + private $default_property_service; |
|
22 | 22 | |
23 | - private $property_services = array(); |
|
23 | + private $property_services = array(); |
|
24 | 24 | |
25 | - /** |
|
26 | - * Wordlift_Property_Factory constructor. |
|
27 | - * |
|
28 | - * @since 3.7.0 |
|
29 | - * |
|
30 | - * @param \Wordlift_Property_Service $default_property_service |
|
31 | - */ |
|
32 | - public function __construct( $default_property_service ) { |
|
25 | + /** |
|
26 | + * Wordlift_Property_Factory constructor. |
|
27 | + * |
|
28 | + * @since 3.7.0 |
|
29 | + * |
|
30 | + * @param \Wordlift_Property_Service $default_property_service |
|
31 | + */ |
|
32 | + public function __construct( $default_property_service ) { |
|
33 | 33 | |
34 | - $this->default_property_service = $default_property_service; |
|
34 | + $this->default_property_service = $default_property_service; |
|
35 | 35 | |
36 | - } |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Set the {@link Wordlift_Property_Service} which handles that meta key. |
|
40 | - * |
|
41 | - * @since 3.7.0 |
|
42 | - * |
|
43 | - * @param string $meta_key WordPress' meta key. |
|
44 | - * @param \Wordlift_Property_Service $property_service A {@link Wordlift_Property_Service} instance. |
|
45 | - */ |
|
46 | - public function register( $meta_key, $property_service ) { |
|
38 | + /** |
|
39 | + * Set the {@link Wordlift_Property_Service} which handles that meta key. |
|
40 | + * |
|
41 | + * @since 3.7.0 |
|
42 | + * |
|
43 | + * @param string $meta_key WordPress' meta key. |
|
44 | + * @param \Wordlift_Property_Service $property_service A {@link Wordlift_Property_Service} instance. |
|
45 | + */ |
|
46 | + public function register( $meta_key, $property_service ) { |
|
47 | 47 | |
48 | - $this->property_services[ $meta_key ] = $property_service; |
|
48 | + $this->property_services[ $meta_key ] = $property_service; |
|
49 | 49 | |
50 | - } |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Get the {@link Wordlift_Property_Service} which handles the specified meta key. |
|
54 | - * |
|
55 | - * @since 3.7.0 |
|
56 | - * |
|
57 | - * @param $meta_key |
|
58 | - * |
|
59 | - * @return \Wordlift_Property_Service The {@link Wordlift_Property_Service} which handles the specified meta key. |
|
60 | - */ |
|
61 | - public function get( $meta_key ) { |
|
52 | + /** |
|
53 | + * Get the {@link Wordlift_Property_Service} which handles the specified meta key. |
|
54 | + * |
|
55 | + * @since 3.7.0 |
|
56 | + * |
|
57 | + * @param $meta_key |
|
58 | + * |
|
59 | + * @return \Wordlift_Property_Service The {@link Wordlift_Property_Service} which handles the specified meta key. |
|
60 | + */ |
|
61 | + public function get( $meta_key ) { |
|
62 | 62 | |
63 | - $service = $this->property_services[ $meta_key ]; |
|
63 | + $service = $this->property_services[ $meta_key ]; |
|
64 | 64 | |
65 | - return $service ? $service : $this->default_property_service; |
|
66 | - } |
|
65 | + return $service ? $service : $this->default_property_service; |
|
66 | + } |
|
67 | 67 | |
68 | 68 | } |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @param \Wordlift_Property_Service $default_property_service |
31 | 31 | */ |
32 | - public function __construct( $default_property_service ) { |
|
32 | + public function __construct($default_property_service) { |
|
33 | 33 | |
34 | 34 | $this->default_property_service = $default_property_service; |
35 | 35 | |
@@ -43,9 +43,9 @@ discard block |
||
43 | 43 | * @param string $meta_key WordPress' meta key. |
44 | 44 | * @param \Wordlift_Property_Service $property_service A {@link Wordlift_Property_Service} instance. |
45 | 45 | */ |
46 | - public function register( $meta_key, $property_service ) { |
|
46 | + public function register($meta_key, $property_service) { |
|
47 | 47 | |
48 | - $this->property_services[ $meta_key ] = $property_service; |
|
48 | + $this->property_services[$meta_key] = $property_service; |
|
49 | 49 | |
50 | 50 | } |
51 | 51 | |
@@ -58,9 +58,9 @@ discard block |
||
58 | 58 | * |
59 | 59 | * @return \Wordlift_Property_Service The {@link Wordlift_Property_Service} which handles the specified meta key. |
60 | 60 | */ |
61 | - public function get( $meta_key ) { |
|
61 | + public function get($meta_key) { |
|
62 | 62 | |
63 | - $service = $this->property_services[ $meta_key ]; |
|
63 | + $service = $this->property_services[$meta_key]; |
|
64 | 64 | |
65 | 65 | return $service ? $service : $this->default_property_service; |
66 | 66 | } |