@@ -11,151 +11,151 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Jsonld_Generator { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @var \Wordlift_Entity_Type_Service |
|
| 16 | - */ |
|
| 17 | - private $entity_type_service; |
|
| 18 | - /** |
|
| 19 | - * @var \Wordlift_Property_Getter |
|
| 20 | - */ |
|
| 21 | - private $property_getter; |
|
| 22 | - /** |
|
| 23 | - * @var Type_Service |
|
| 24 | - */ |
|
| 25 | - private $term_entity_type_service; |
|
| 26 | - /** |
|
| 27 | - * @var \Wordlift_Entity_Service |
|
| 28 | - */ |
|
| 29 | - private $entity_service; |
|
| 30 | - |
|
| 31 | - public function __construct( $entity_type_service, $property_getter ) { |
|
| 32 | - $this->entity_type_service = $entity_type_service; |
|
| 33 | - $this->property_getter = $property_getter; |
|
| 34 | - $this->term_entity_type_service = Type_Service::get_instance(); |
|
| 35 | - $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - public function init() { |
|
| 39 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 43 | - $jsonld = $data['jsonld']; |
|
| 44 | - $references = $data['references']; |
|
| 45 | - |
|
| 46 | - $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 47 | - |
|
| 48 | - // Return early if we dont have the entity data |
|
| 49 | - // for the term. |
|
| 50 | - if ( ! $term_jsonld_data ) { |
|
| 51 | - return $data; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - $term_jsonld = $term_jsonld_data['jsonld']; |
|
| 55 | - |
|
| 56 | - $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 57 | - |
|
| 58 | - array_unshift( $jsonld, $term_jsonld ); |
|
| 59 | - |
|
| 60 | - return array( |
|
| 61 | - 'jsonld' => $jsonld, |
|
| 62 | - 'references' => $references, |
|
| 63 | - ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - private function get_jsonld_data_for_term( $term_id ) { |
|
| 67 | - |
|
| 68 | - $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 69 | - |
|
| 70 | - // If we don't have a dataset URI, then don't publish the term data |
|
| 71 | - // on this page. |
|
| 72 | - if ( ! $id ) { |
|
| 73 | - return false; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $references = array(); |
|
| 77 | - $term = get_term( $term_id ); |
|
| 78 | - $permalink = get_term_link( $term ); |
|
| 79 | - |
|
| 80 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 81 | - $term = get_term( $term_id ); |
|
| 82 | - $jsonld = array( |
|
| 83 | - '@context' => 'http://schema.org', |
|
| 84 | - 'name' => $term->name, |
|
| 85 | - '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 86 | - '@id' => $id, |
|
| 87 | - 'description' => $term->description, |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 91 | - return $jsonld; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - foreach ( $custom_fields as $key => $value ) { |
|
| 95 | - $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 96 | - $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 97 | - $value = $this->process_value( $value, $references ); |
|
| 98 | - if ( ! isset( $value ) || |
|
| 99 | - is_array( $value ) && empty( $value ) || |
|
| 100 | - is_string( $value ) && empty( $value ) ) { |
|
| 101 | - continue; |
|
| 102 | - } |
|
| 103 | - $jsonld[ $name ] = $value; |
|
| 104 | - |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ( $permalink ) { |
|
| 108 | - $jsonld['mainEntityOfPage'] = $permalink; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return apply_filters( |
|
| 112 | - 'wl_no_vocabulary_term_jsonld_array', |
|
| 113 | - array( |
|
| 114 | - 'jsonld' => $jsonld, |
|
| 115 | - 'references' => $references, |
|
| 116 | - ), |
|
| 117 | - $term_id |
|
| 118 | - ); |
|
| 119 | - |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - private function relative_to_schema_context( $predicate ) { |
|
| 123 | - return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - private function process_value( $value, &$references ) { |
|
| 127 | - |
|
| 128 | - if ( is_array( $value ) |
|
| 129 | - && count( $value ) > 0 |
|
| 130 | - && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 131 | - |
|
| 132 | - // All of the references from the custom fields are post references. |
|
| 133 | - $references = array_merge( |
|
| 134 | - $references, |
|
| 135 | - array_map( |
|
| 136 | - function ( $property_entity_reference ) { |
|
| 137 | - /** |
|
| 138 | - * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
|
| 139 | - */ |
|
| 140 | - return $property_entity_reference->to_reference(); |
|
| 141 | - }, |
|
| 142 | - $value |
|
| 143 | - ) |
|
| 144 | - ); |
|
| 145 | - |
|
| 146 | - return array_map( |
|
| 147 | - function ( $reference ) { |
|
| 148 | - /** |
|
| 149 | - * @var $reference \Wordlift_Property_Entity_Reference |
|
| 150 | - */ |
|
| 151 | - return array( '@id' => $reference->get_url() ); |
|
| 152 | - }, |
|
| 153 | - $value |
|
| 154 | - ); |
|
| 155 | - |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return $value; |
|
| 159 | - } |
|
| 14 | + /** |
|
| 15 | + * @var \Wordlift_Entity_Type_Service |
|
| 16 | + */ |
|
| 17 | + private $entity_type_service; |
|
| 18 | + /** |
|
| 19 | + * @var \Wordlift_Property_Getter |
|
| 20 | + */ |
|
| 21 | + private $property_getter; |
|
| 22 | + /** |
|
| 23 | + * @var Type_Service |
|
| 24 | + */ |
|
| 25 | + private $term_entity_type_service; |
|
| 26 | + /** |
|
| 27 | + * @var \Wordlift_Entity_Service |
|
| 28 | + */ |
|
| 29 | + private $entity_service; |
|
| 30 | + |
|
| 31 | + public function __construct( $entity_type_service, $property_getter ) { |
|
| 32 | + $this->entity_type_service = $entity_type_service; |
|
| 33 | + $this->property_getter = $property_getter; |
|
| 34 | + $this->term_entity_type_service = Type_Service::get_instance(); |
|
| 35 | + $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + public function init() { |
|
| 39 | + add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 43 | + $jsonld = $data['jsonld']; |
|
| 44 | + $references = $data['references']; |
|
| 45 | + |
|
| 46 | + $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 47 | + |
|
| 48 | + // Return early if we dont have the entity data |
|
| 49 | + // for the term. |
|
| 50 | + if ( ! $term_jsonld_data ) { |
|
| 51 | + return $data; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + $term_jsonld = $term_jsonld_data['jsonld']; |
|
| 55 | + |
|
| 56 | + $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 57 | + |
|
| 58 | + array_unshift( $jsonld, $term_jsonld ); |
|
| 59 | + |
|
| 60 | + return array( |
|
| 61 | + 'jsonld' => $jsonld, |
|
| 62 | + 'references' => $references, |
|
| 63 | + ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + private function get_jsonld_data_for_term( $term_id ) { |
|
| 67 | + |
|
| 68 | + $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 69 | + |
|
| 70 | + // If we don't have a dataset URI, then don't publish the term data |
|
| 71 | + // on this page. |
|
| 72 | + if ( ! $id ) { |
|
| 73 | + return false; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $references = array(); |
|
| 77 | + $term = get_term( $term_id ); |
|
| 78 | + $permalink = get_term_link( $term ); |
|
| 79 | + |
|
| 80 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 81 | + $term = get_term( $term_id ); |
|
| 82 | + $jsonld = array( |
|
| 83 | + '@context' => 'http://schema.org', |
|
| 84 | + 'name' => $term->name, |
|
| 85 | + '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 86 | + '@id' => $id, |
|
| 87 | + 'description' => $term->description, |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 91 | + return $jsonld; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + foreach ( $custom_fields as $key => $value ) { |
|
| 95 | + $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 96 | + $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 97 | + $value = $this->process_value( $value, $references ); |
|
| 98 | + if ( ! isset( $value ) || |
|
| 99 | + is_array( $value ) && empty( $value ) || |
|
| 100 | + is_string( $value ) && empty( $value ) ) { |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 103 | + $jsonld[ $name ] = $value; |
|
| 104 | + |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ( $permalink ) { |
|
| 108 | + $jsonld['mainEntityOfPage'] = $permalink; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return apply_filters( |
|
| 112 | + 'wl_no_vocabulary_term_jsonld_array', |
|
| 113 | + array( |
|
| 114 | + 'jsonld' => $jsonld, |
|
| 115 | + 'references' => $references, |
|
| 116 | + ), |
|
| 117 | + $term_id |
|
| 118 | + ); |
|
| 119 | + |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + private function relative_to_schema_context( $predicate ) { |
|
| 123 | + return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + private function process_value( $value, &$references ) { |
|
| 127 | + |
|
| 128 | + if ( is_array( $value ) |
|
| 129 | + && count( $value ) > 0 |
|
| 130 | + && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 131 | + |
|
| 132 | + // All of the references from the custom fields are post references. |
|
| 133 | + $references = array_merge( |
|
| 134 | + $references, |
|
| 135 | + array_map( |
|
| 136 | + function ( $property_entity_reference ) { |
|
| 137 | + /** |
|
| 138 | + * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
|
| 139 | + */ |
|
| 140 | + return $property_entity_reference->to_reference(); |
|
| 141 | + }, |
|
| 142 | + $value |
|
| 143 | + ) |
|
| 144 | + ); |
|
| 145 | + |
|
| 146 | + return array_map( |
|
| 147 | + function ( $reference ) { |
|
| 148 | + /** |
|
| 149 | + * @var $reference \Wordlift_Property_Entity_Reference |
|
| 150 | + */ |
|
| 151 | + return array( '@id' => $reference->get_url() ); |
|
| 152 | + }, |
|
| 153 | + $value |
|
| 154 | + ); |
|
| 155 | + |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return $value; |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | 161 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | private $entity_service; |
| 30 | 30 | |
| 31 | - public function __construct( $entity_type_service, $property_getter ) { |
|
| 31 | + public function __construct($entity_type_service, $property_getter) { |
|
| 32 | 32 | $this->entity_type_service = $entity_type_service; |
| 33 | 33 | $this->property_getter = $property_getter; |
| 34 | 34 | $this->term_entity_type_service = Type_Service::get_instance(); |
@@ -36,26 +36,26 @@ discard block |
||
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public function init() { |
| 39 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 39 | + add_filter('wl_term_jsonld_array', array($this, 'wl_term_jsonld_array'), 10, 2); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 42 | + public function wl_term_jsonld_array($data, $term_id) { |
|
| 43 | 43 | $jsonld = $data['jsonld']; |
| 44 | 44 | $references = $data['references']; |
| 45 | 45 | |
| 46 | - $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 46 | + $term_jsonld_data = $this->get_jsonld_data_for_term($term_id); |
|
| 47 | 47 | |
| 48 | 48 | // Return early if we dont have the entity data |
| 49 | 49 | // for the term. |
| 50 | - if ( ! $term_jsonld_data ) { |
|
| 50 | + if ( ! $term_jsonld_data) { |
|
| 51 | 51 | return $data; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | $term_jsonld = $term_jsonld_data['jsonld']; |
| 55 | 55 | |
| 56 | - $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 56 | + $references = array_merge($references, $term_jsonld_data['references']); |
|
| 57 | 57 | |
| 58 | - array_unshift( $jsonld, $term_jsonld ); |
|
| 58 | + array_unshift($jsonld, $term_jsonld); |
|
| 59 | 59 | |
| 60 | 60 | return array( |
| 61 | 61 | 'jsonld' => $jsonld, |
@@ -63,48 +63,48 @@ discard block |
||
| 63 | 63 | ); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - private function get_jsonld_data_for_term( $term_id ) { |
|
| 66 | + private function get_jsonld_data_for_term($term_id) { |
|
| 67 | 67 | |
| 68 | - $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 68 | + $id = $this->entity_service->get_uri($term_id, Object_Type_Enum::TERM); |
|
| 69 | 69 | |
| 70 | 70 | // If we don't have a dataset URI, then don't publish the term data |
| 71 | 71 | // on this page. |
| 72 | - if ( ! $id ) { |
|
| 72 | + if ( ! $id) { |
|
| 73 | 73 | return false; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | $references = array(); |
| 77 | - $term = get_term( $term_id ); |
|
| 78 | - $permalink = get_term_link( $term ); |
|
| 77 | + $term = get_term($term_id); |
|
| 78 | + $permalink = get_term_link($term); |
|
| 79 | 79 | |
| 80 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 81 | - $term = get_term( $term_id ); |
|
| 80 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_term($term_id); |
|
| 81 | + $term = get_term($term_id); |
|
| 82 | 82 | $jsonld = array( |
| 83 | 83 | '@context' => 'http://schema.org', |
| 84 | 84 | 'name' => $term->name, |
| 85 | - '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 85 | + '@type' => $this->term_entity_type_service->get_entity_types_labels($term_id), |
|
| 86 | 86 | '@id' => $id, |
| 87 | 87 | 'description' => $term->description, |
| 88 | 88 | ); |
| 89 | 89 | |
| 90 | - if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 90 | + if ( ! $custom_fields || ! is_array($custom_fields)) { |
|
| 91 | 91 | return $jsonld; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - foreach ( $custom_fields as $key => $value ) { |
|
| 95 | - $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 96 | - $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 97 | - $value = $this->process_value( $value, $references ); |
|
| 98 | - if ( ! isset( $value ) || |
|
| 99 | - is_array( $value ) && empty( $value ) || |
|
| 100 | - is_string( $value ) && empty( $value ) ) { |
|
| 94 | + foreach ($custom_fields as $key => $value) { |
|
| 95 | + $name = $this->relative_to_schema_context($value['predicate']); |
|
| 96 | + $value = $this->property_getter->get($term_id, $key, Object_Type_Enum::TERM); |
|
| 97 | + $value = $this->process_value($value, $references); |
|
| 98 | + if ( ! isset($value) || |
|
| 99 | + is_array($value) && empty($value) || |
|
| 100 | + is_string($value) && empty($value)) { |
|
| 101 | 101 | continue; |
| 102 | 102 | } |
| 103 | - $jsonld[ $name ] = $value; |
|
| 103 | + $jsonld[$name] = $value; |
|
| 104 | 104 | |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - if ( $permalink ) { |
|
| 107 | + if ($permalink) { |
|
| 108 | 108 | $jsonld['mainEntityOfPage'] = $permalink; |
| 109 | 109 | } |
| 110 | 110 | |
@@ -119,21 +119,21 @@ discard block |
||
| 119 | 119 | |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | - private function relative_to_schema_context( $predicate ) { |
|
| 123 | - return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 122 | + private function relative_to_schema_context($predicate) { |
|
| 123 | + return str_replace('http://schema.org/', '', $predicate); |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - private function process_value( $value, &$references ) { |
|
| 126 | + private function process_value($value, &$references) { |
|
| 127 | 127 | |
| 128 | - if ( is_array( $value ) |
|
| 129 | - && count( $value ) > 0 |
|
| 130 | - && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 128 | + if (is_array($value) |
|
| 129 | + && count($value) > 0 |
|
| 130 | + && $value[0] instanceof \Wordlift_Property_Entity_Reference) { |
|
| 131 | 131 | |
| 132 | 132 | // All of the references from the custom fields are post references. |
| 133 | 133 | $references = array_merge( |
| 134 | 134 | $references, |
| 135 | 135 | array_map( |
| 136 | - function ( $property_entity_reference ) { |
|
| 136 | + function($property_entity_reference) { |
|
| 137 | 137 | /** |
| 138 | 138 | * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
| 139 | 139 | */ |
@@ -144,11 +144,11 @@ discard block |
||
| 144 | 144 | ); |
| 145 | 145 | |
| 146 | 146 | return array_map( |
| 147 | - function ( $reference ) { |
|
| 147 | + function($reference) { |
|
| 148 | 148 | /** |
| 149 | 149 | * @var $reference \Wordlift_Property_Entity_Reference |
| 150 | 150 | */ |
| 151 | - return array( '@id' => $reference->get_url() ); |
|
| 151 | + return array('@id' => $reference->get_url()); |
|
| 152 | 152 | }, |
| 153 | 153 | $value |
| 154 | 154 | ); |
@@ -19,81 +19,81 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | function wl_entity_get_by_title( $title, $autocomplete = false, $include_alias = true, $limit = false, $schema_types = array() ) { |
| 21 | 21 | |
| 22 | - global $wpdb; |
|
| 23 | - |
|
| 24 | - $schema_type_query = ''; |
|
| 25 | - |
|
| 26 | - if ( $schema_types ) { |
|
| 27 | - $schema_type_query = ' AND t.name IN (' . join( |
|
| 28 | - ',', |
|
| 29 | - array_map( |
|
| 30 | - function ( $schema_type ) { |
|
| 31 | - return "'" . esc_sql( $schema_type ) . "'"; |
|
| 32 | - }, |
|
| 33 | - $schema_types |
|
| 34 | - ) |
|
| 35 | - ) . ')'; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - // Search by substring |
|
| 39 | - if ( $autocomplete ) { |
|
| 40 | - $title = '%' . $title . '%'; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - // The title is a LIKE query. |
|
| 44 | - $query = 'SELECT DISTINCT p.ID AS id, p.post_title AS title, t.name AS schema_type_name, t.slug AS type_slug' |
|
| 45 | - . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t" |
|
| 46 | - . ' WHERE p.post_title LIKE %s' |
|
| 47 | - . ' AND t.term_id = tt.term_id' |
|
| 48 | - . ' AND tt.taxonomy = %s' |
|
| 49 | - . ' AND tt.term_taxonomy_id = tr.term_taxonomy_id' |
|
| 50 | - . ' AND tr.object_id = p.ID' |
|
| 51 | - // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 52 | - . " AND p.post_status != 'trash'" |
|
| 53 | - . $schema_type_query; |
|
| 54 | - |
|
| 55 | - $params = array( |
|
| 56 | - $title, |
|
| 57 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 58 | - ); |
|
| 59 | - |
|
| 60 | - if ( $include_alias ) { |
|
| 61 | - |
|
| 62 | - $query .= ' UNION' |
|
| 63 | - . " SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug" |
|
| 64 | - . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t, $wpdb->postmeta m" |
|
| 65 | - . ' WHERE m.meta_key = %s AND m.meta_value LIKE %s' |
|
| 66 | - . ' AND m.post_id = p.ID' |
|
| 67 | - . ' AND t.term_id = tt.term_id' |
|
| 68 | - . ' AND tt.taxonomy = %s' |
|
| 69 | - . ' AND tt.term_taxonomy_id = tr.term_taxonomy_id' |
|
| 70 | - . ' AND tr.object_id = p.ID' |
|
| 71 | - // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 72 | - . " AND p.post_status != 'trash'" |
|
| 73 | - . $schema_type_query; |
|
| 74 | - |
|
| 75 | - $params = array_merge( |
|
| 76 | - $params, |
|
| 77 | - array( |
|
| 78 | - Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
|
| 79 | - $title, |
|
| 80 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 81 | - ) |
|
| 82 | - ); |
|
| 83 | - |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if ( $limit ) { |
|
| 87 | - $query .= ' LIMIT %d'; |
|
| 88 | - $params[] = $limit; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return $wpdb->get_results( |
|
| 92 | - $wpdb->prepare( |
|
| 93 | - $query, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 94 | - $params |
|
| 95 | - ) |
|
| 96 | - ); |
|
| 22 | + global $wpdb; |
|
| 23 | + |
|
| 24 | + $schema_type_query = ''; |
|
| 25 | + |
|
| 26 | + if ( $schema_types ) { |
|
| 27 | + $schema_type_query = ' AND t.name IN (' . join( |
|
| 28 | + ',', |
|
| 29 | + array_map( |
|
| 30 | + function ( $schema_type ) { |
|
| 31 | + return "'" . esc_sql( $schema_type ) . "'"; |
|
| 32 | + }, |
|
| 33 | + $schema_types |
|
| 34 | + ) |
|
| 35 | + ) . ')'; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + // Search by substring |
|
| 39 | + if ( $autocomplete ) { |
|
| 40 | + $title = '%' . $title . '%'; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + // The title is a LIKE query. |
|
| 44 | + $query = 'SELECT DISTINCT p.ID AS id, p.post_title AS title, t.name AS schema_type_name, t.slug AS type_slug' |
|
| 45 | + . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t" |
|
| 46 | + . ' WHERE p.post_title LIKE %s' |
|
| 47 | + . ' AND t.term_id = tt.term_id' |
|
| 48 | + . ' AND tt.taxonomy = %s' |
|
| 49 | + . ' AND tt.term_taxonomy_id = tr.term_taxonomy_id' |
|
| 50 | + . ' AND tr.object_id = p.ID' |
|
| 51 | + // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 52 | + . " AND p.post_status != 'trash'" |
|
| 53 | + . $schema_type_query; |
|
| 54 | + |
|
| 55 | + $params = array( |
|
| 56 | + $title, |
|
| 57 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 58 | + ); |
|
| 59 | + |
|
| 60 | + if ( $include_alias ) { |
|
| 61 | + |
|
| 62 | + $query .= ' UNION' |
|
| 63 | + . " SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug" |
|
| 64 | + . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t, $wpdb->postmeta m" |
|
| 65 | + . ' WHERE m.meta_key = %s AND m.meta_value LIKE %s' |
|
| 66 | + . ' AND m.post_id = p.ID' |
|
| 67 | + . ' AND t.term_id = tt.term_id' |
|
| 68 | + . ' AND tt.taxonomy = %s' |
|
| 69 | + . ' AND tt.term_taxonomy_id = tr.term_taxonomy_id' |
|
| 70 | + . ' AND tr.object_id = p.ID' |
|
| 71 | + // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 72 | + . " AND p.post_status != 'trash'" |
|
| 73 | + . $schema_type_query; |
|
| 74 | + |
|
| 75 | + $params = array_merge( |
|
| 76 | + $params, |
|
| 77 | + array( |
|
| 78 | + Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
|
| 79 | + $title, |
|
| 80 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 81 | + ) |
|
| 82 | + ); |
|
| 83 | + |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if ( $limit ) { |
|
| 87 | + $query .= ' LIMIT %d'; |
|
| 88 | + $params[] = $limit; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return $wpdb->get_results( |
|
| 92 | + $wpdb->prepare( |
|
| 93 | + $query, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 94 | + $params |
|
| 95 | + ) |
|
| 96 | + ); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | /** |
@@ -103,43 +103,43 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | function wl_entity_ajax_get_by_title() { |
| 105 | 105 | |
| 106 | - // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 107 | - // |
|
| 108 | - // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 109 | - // Get the title to search. |
|
| 110 | - if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing |
|
| 111 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 112 | - @ob_clean(); |
|
| 113 | - wp_send_json_error( 'The title parameter is required.' ); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 117 | - // |
|
| 118 | - // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 119 | - $title = sanitize_text_field( wp_unslash( $_POST['title'] ? $_POST['title'] : $_GET['title'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing |
|
| 120 | - |
|
| 121 | - // Are we searching for a specific title or for a containing title? |
|
| 122 | - $autocomplete = isset( $_GET['autocomplete'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 123 | - |
|
| 124 | - // Are we searching also for the aliases? |
|
| 125 | - $include_alias = isset( $_GET['alias'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 126 | - |
|
| 127 | - // Get the edit link. |
|
| 128 | - $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 129 | - $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 130 | - |
|
| 131 | - // Prepare the response with the edit link. |
|
| 132 | - $response = array( |
|
| 133 | - 'edit_link' => $edit_link, |
|
| 134 | - 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 135 | - ); |
|
| 136 | - |
|
| 137 | - // Clean any buffer. |
|
| 138 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 139 | - @ob_clean(); |
|
| 140 | - |
|
| 141 | - // Send the success response. |
|
| 142 | - wp_send_json_success( $response ); |
|
| 106 | + // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 107 | + // |
|
| 108 | + // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 109 | + // Get the title to search. |
|
| 110 | + if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing |
|
| 111 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 112 | + @ob_clean(); |
|
| 113 | + wp_send_json_error( 'The title parameter is required.' ); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 117 | + // |
|
| 118 | + // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 119 | + $title = sanitize_text_field( wp_unslash( $_POST['title'] ? $_POST['title'] : $_GET['title'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing |
|
| 120 | + |
|
| 121 | + // Are we searching for a specific title or for a containing title? |
|
| 122 | + $autocomplete = isset( $_GET['autocomplete'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 123 | + |
|
| 124 | + // Are we searching also for the aliases? |
|
| 125 | + $include_alias = isset( $_GET['alias'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 126 | + |
|
| 127 | + // Get the edit link. |
|
| 128 | + $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 129 | + $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 130 | + |
|
| 131 | + // Prepare the response with the edit link. |
|
| 132 | + $response = array( |
|
| 133 | + 'edit_link' => $edit_link, |
|
| 134 | + 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 135 | + ); |
|
| 136 | + |
|
| 137 | + // Clean any buffer. |
|
| 138 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 139 | + @ob_clean(); |
|
| 140 | + |
|
| 141 | + // Send the success response. |
|
| 142 | + wp_send_json_success( $response ); |
|
| 143 | 143 | |
| 144 | 144 | } |
| 145 | 145 | |
@@ -17,27 +17,27 @@ discard block |
||
| 17 | 17 | * @return array An array of WP_Post instances. |
| 18 | 18 | * @since 3.0.0 |
| 19 | 19 | */ |
| 20 | -function wl_entity_get_by_title( $title, $autocomplete = false, $include_alias = true, $limit = false, $schema_types = array() ) { |
|
| 20 | +function wl_entity_get_by_title($title, $autocomplete = false, $include_alias = true, $limit = false, $schema_types = array()) { |
|
| 21 | 21 | |
| 22 | 22 | global $wpdb; |
| 23 | 23 | |
| 24 | 24 | $schema_type_query = ''; |
| 25 | 25 | |
| 26 | - if ( $schema_types ) { |
|
| 27 | - $schema_type_query = ' AND t.name IN (' . join( |
|
| 26 | + if ($schema_types) { |
|
| 27 | + $schema_type_query = ' AND t.name IN ('.join( |
|
| 28 | 28 | ',', |
| 29 | 29 | array_map( |
| 30 | - function ( $schema_type ) { |
|
| 31 | - return "'" . esc_sql( $schema_type ) . "'"; |
|
| 30 | + function($schema_type) { |
|
| 31 | + return "'".esc_sql($schema_type)."'"; |
|
| 32 | 32 | }, |
| 33 | 33 | $schema_types |
| 34 | 34 | ) |
| 35 | - ) . ')'; |
|
| 35 | + ).')'; |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | // Search by substring |
| 39 | - if ( $autocomplete ) { |
|
| 40 | - $title = '%' . $title . '%'; |
|
| 39 | + if ($autocomplete) { |
|
| 40 | + $title = '%'.$title.'%'; |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | // The title is a LIKE query. |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 58 | 58 | ); |
| 59 | 59 | |
| 60 | - if ( $include_alias ) { |
|
| 60 | + if ($include_alias) { |
|
| 61 | 61 | |
| 62 | 62 | $query .= ' UNION' |
| 63 | 63 | . " SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug" |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - if ( $limit ) { |
|
| 86 | + if ($limit) { |
|
| 87 | 87 | $query .= ' LIMIT %d'; |
| 88 | 88 | $params[] = $limit; |
| 89 | 89 | } |
@@ -107,31 +107,31 @@ discard block |
||
| 107 | 107 | // |
| 108 | 108 | // See https://github.com/insideout10/wordlift-plugin/issues/438. |
| 109 | 109 | // Get the title to search. |
| 110 | - if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing |
|
| 110 | + if (empty($_POST['title']) && empty($_GET['title'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing |
|
| 111 | 111 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 112 | 112 | @ob_clean(); |
| 113 | - wp_send_json_error( 'The title parameter is required.' ); |
|
| 113 | + wp_send_json_error('The title parameter is required.'); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
| 117 | 117 | // |
| 118 | 118 | // See https://github.com/insideout10/wordlift-plugin/issues/438. |
| 119 | - $title = sanitize_text_field( wp_unslash( $_POST['title'] ? $_POST['title'] : $_GET['title'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing |
|
| 119 | + $title = sanitize_text_field(wp_unslash($_POST['title'] ? $_POST['title'] : $_GET['title'])); //phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing |
|
| 120 | 120 | |
| 121 | 121 | // Are we searching for a specific title or for a containing title? |
| 122 | - $autocomplete = isset( $_GET['autocomplete'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 122 | + $autocomplete = isset($_GET['autocomplete']); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 123 | 123 | |
| 124 | 124 | // Are we searching also for the aliases? |
| 125 | - $include_alias = isset( $_GET['alias'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 125 | + $include_alias = isset($_GET['alias']); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 126 | 126 | |
| 127 | 127 | // Get the edit link. |
| 128 | - $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 129 | - $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 128 | + $post_type_object = get_post_type_object(Wordlift_Entity_Service::TYPE_NAME); |
|
| 129 | + $edit_link = $post_type_object->_edit_link.'&action=edit'; |
|
| 130 | 130 | |
| 131 | 131 | // Prepare the response with the edit link. |
| 132 | 132 | $response = array( |
| 133 | 133 | 'edit_link' => $edit_link, |
| 134 | - 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 134 | + 'results' => wl_entity_get_by_title($title, $autocomplete, $include_alias), |
|
| 135 | 135 | ); |
| 136 | 136 | |
| 137 | 137 | // Clean any buffer. |
@@ -139,8 +139,8 @@ discard block |
||
| 139 | 139 | @ob_clean(); |
| 140 | 140 | |
| 141 | 141 | // Send the success response. |
| 142 | - wp_send_json_success( $response ); |
|
| 142 | + wp_send_json_success($response); |
|
| 143 | 143 | |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | -add_action( 'wp_ajax_entity_by_title', 'wl_entity_ajax_get_by_title' ); |
|
| 146 | +add_action('wp_ajax_entity_by_title', 'wl_entity_ajax_get_by_title'); |
|
@@ -5,9 +5,9 @@ |
||
| 5 | 5 | |
| 6 | 6 | // Autoloader for dependencies. |
| 7 | 7 | if ( file_exists( __DIR__ . '/third-party/vendor/scoper-autoload.php' ) ) { |
| 8 | - require __DIR__ . '/third-party/vendor/scoper-autoload.php'; |
|
| 8 | + require __DIR__ . '/third-party/vendor/scoper-autoload.php'; |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) { |
| 12 | - require __DIR__ . '/vendor/autoload.php'; |
|
| 12 | + require __DIR__ . '/vendor/autoload.php'; |
|
| 13 | 13 | } |
@@ -4,10 +4,10 @@ |
||
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | // Autoloader for dependencies. |
| 7 | -if ( file_exists( __DIR__ . '/third-party/vendor/scoper-autoload.php' ) ) { |
|
| 8 | - require __DIR__ . '/third-party/vendor/scoper-autoload.php'; |
|
| 7 | +if (file_exists(__DIR__.'/third-party/vendor/scoper-autoload.php')) { |
|
| 8 | + require __DIR__.'/third-party/vendor/scoper-autoload.php'; |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | -if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) { |
|
| 12 | - require __DIR__ . '/vendor/autoload.php'; |
|
| 11 | +if (file_exists(__DIR__.'/vendor/autoload.php')) { |
|
| 12 | + require __DIR__.'/vendor/autoload.php'; |
|
| 13 | 13 | } |
@@ -4,60 +4,60 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Context { |
| 6 | 6 | |
| 7 | - const POST = 0; |
|
| 8 | - const TERM = 1; |
|
| 9 | - const ADMIN_AJAX = 2; |
|
| 10 | - const UNKNOWN = -1; |
|
| 11 | - /** |
|
| 12 | - * @var $object_type int |
|
| 13 | - */ |
|
| 14 | - private $object_type; |
|
| 15 | - /** |
|
| 16 | - * @var $identifier int |
|
| 17 | - */ |
|
| 18 | - private $identifier; |
|
| 19 | - /** |
|
| 20 | - * @var $custom_fields Schema_Field_Group[] |
|
| 21 | - */ |
|
| 22 | - private $custom_fields; |
|
| 23 | - |
|
| 24 | - private static $pod_types_map = array( |
|
| 25 | - self::POST => 'post_type', |
|
| 26 | - self::TERM => 'taxonomy', |
|
| 27 | - ); |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param int $object_type |
|
| 31 | - * @param $identifier |
|
| 32 | - * @param $custom_fields |
|
| 33 | - */ |
|
| 34 | - public function __construct( $object_type, $identifier, $custom_fields ) { |
|
| 35 | - $this->object_type = $object_type; |
|
| 36 | - $this->identifier = $identifier; |
|
| 37 | - $this->custom_fields = $custom_fields; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @return int |
|
| 42 | - */ |
|
| 43 | - public function get_object_type() { |
|
| 44 | - return $this->object_type; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function get_pod_name() { |
|
| 48 | - if ( self::POST === $this->object_type ) { |
|
| 49 | - return get_post_type( $this->identifier ); |
|
| 50 | - } elseif ( self::TERM === $this->object_type ) { |
|
| 51 | - return get_term( $this->identifier )->taxonomy; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function get_pod_type() { |
|
| 56 | - return self::$pod_types_map[ $this->object_type ]; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function get_custom_fields() { |
|
| 60 | - return $this->custom_fields; |
|
| 61 | - } |
|
| 7 | + const POST = 0; |
|
| 8 | + const TERM = 1; |
|
| 9 | + const ADMIN_AJAX = 2; |
|
| 10 | + const UNKNOWN = -1; |
|
| 11 | + /** |
|
| 12 | + * @var $object_type int |
|
| 13 | + */ |
|
| 14 | + private $object_type; |
|
| 15 | + /** |
|
| 16 | + * @var $identifier int |
|
| 17 | + */ |
|
| 18 | + private $identifier; |
|
| 19 | + /** |
|
| 20 | + * @var $custom_fields Schema_Field_Group[] |
|
| 21 | + */ |
|
| 22 | + private $custom_fields; |
|
| 23 | + |
|
| 24 | + private static $pod_types_map = array( |
|
| 25 | + self::POST => 'post_type', |
|
| 26 | + self::TERM => 'taxonomy', |
|
| 27 | + ); |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param int $object_type |
|
| 31 | + * @param $identifier |
|
| 32 | + * @param $custom_fields |
|
| 33 | + */ |
|
| 34 | + public function __construct( $object_type, $identifier, $custom_fields ) { |
|
| 35 | + $this->object_type = $object_type; |
|
| 36 | + $this->identifier = $identifier; |
|
| 37 | + $this->custom_fields = $custom_fields; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @return int |
|
| 42 | + */ |
|
| 43 | + public function get_object_type() { |
|
| 44 | + return $this->object_type; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function get_pod_name() { |
|
| 48 | + if ( self::POST === $this->object_type ) { |
|
| 49 | + return get_post_type( $this->identifier ); |
|
| 50 | + } elseif ( self::TERM === $this->object_type ) { |
|
| 51 | + return get_term( $this->identifier )->taxonomy; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function get_pod_type() { |
|
| 56 | + return self::$pod_types_map[ $this->object_type ]; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function get_custom_fields() { |
|
| 60 | + return $this->custom_fields; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | 63 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @param $identifier |
| 32 | 32 | * @param $custom_fields |
| 33 | 33 | */ |
| 34 | - public function __construct( $object_type, $identifier, $custom_fields ) { |
|
| 34 | + public function __construct($object_type, $identifier, $custom_fields) { |
|
| 35 | 35 | $this->object_type = $object_type; |
| 36 | 36 | $this->identifier = $identifier; |
| 37 | 37 | $this->custom_fields = $custom_fields; |
@@ -45,15 +45,15 @@ discard block |
||
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | public function get_pod_name() { |
| 48 | - if ( self::POST === $this->object_type ) { |
|
| 49 | - return get_post_type( $this->identifier ); |
|
| 50 | - } elseif ( self::TERM === $this->object_type ) { |
|
| 51 | - return get_term( $this->identifier )->taxonomy; |
|
| 48 | + if (self::POST === $this->object_type) { |
|
| 49 | + return get_post_type($this->identifier); |
|
| 50 | + } elseif (self::TERM === $this->object_type) { |
|
| 51 | + return get_term($this->identifier)->taxonomy; |
|
| 52 | 52 | } |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | public function get_pod_type() { |
| 56 | - return self::$pod_types_map[ $this->object_type ]; |
|
| 56 | + return self::$pod_types_map[$this->object_type]; |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | public function get_custom_fields() { |
@@ -4,12 +4,12 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface FieldDefinition { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * Register the fields for the provided context in constructor. |
|
| 9 | - * |
|
| 10 | - * @return void |
|
| 11 | - */ |
|
| 12 | - public function register(); |
|
| 7 | + /** |
|
| 8 | + * Register the fields for the provided context in constructor. |
|
| 9 | + * |
|
| 10 | + * @return void |
|
| 11 | + */ |
|
| 12 | + public function register(); |
|
| 13 | 13 | |
| 14 | 14 | } |
| 15 | 15 | |
@@ -10,27 +10,27 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class AllPodsDefiniton extends AbstractFieldDefiniton { |
| 12 | 12 | |
| 13 | - public function register() { |
|
| 14 | - |
|
| 15 | - add_action( 'init', array( $this, 'register_on_all_valid_post_types' ) ); |
|
| 16 | - add_action( 'setup_theme', array( $this, 'register_on_all_supported_taxonomies' ) ); |
|
| 17 | - } |
|
| 18 | - |
|
| 19 | - public function register_on_all_supported_taxonomies() { |
|
| 20 | - $context = $this->schema->get(); |
|
| 21 | - $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 22 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 23 | - $this->register_pod( $taxonomy, 'taxonomy', $context ); |
|
| 24 | - |
|
| 25 | - } |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - public function register_on_all_valid_post_types() { |
|
| 29 | - $context = $this->schema->get(); |
|
| 30 | - $supported_types = \Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 31 | - foreach ( $supported_types as $supported_type ) { |
|
| 32 | - $this->register_pod( $supported_type, 'post_type', $context ); |
|
| 33 | - } |
|
| 34 | - } |
|
| 13 | + public function register() { |
|
| 14 | + |
|
| 15 | + add_action( 'init', array( $this, 'register_on_all_valid_post_types' ) ); |
|
| 16 | + add_action( 'setup_theme', array( $this, 'register_on_all_supported_taxonomies' ) ); |
|
| 17 | + } |
|
| 18 | + |
|
| 19 | + public function register_on_all_supported_taxonomies() { |
|
| 20 | + $context = $this->schema->get(); |
|
| 21 | + $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 22 | + foreach ( $taxonomies as $taxonomy ) { |
|
| 23 | + $this->register_pod( $taxonomy, 'taxonomy', $context ); |
|
| 24 | + |
|
| 25 | + } |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public function register_on_all_valid_post_types() { |
|
| 29 | + $context = $this->schema->get(); |
|
| 30 | + $supported_types = \Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 31 | + foreach ( $supported_types as $supported_type ) { |
|
| 32 | + $this->register_pod( $supported_type, 'post_type', $context ); |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | } |
@@ -12,15 +12,15 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | public function register() { |
| 14 | 14 | |
| 15 | - add_action( 'init', array( $this, 'register_on_all_valid_post_types' ) ); |
|
| 16 | - add_action( 'setup_theme', array( $this, 'register_on_all_supported_taxonomies' ) ); |
|
| 15 | + add_action('init', array($this, 'register_on_all_valid_post_types')); |
|
| 16 | + add_action('setup_theme', array($this, 'register_on_all_supported_taxonomies')); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public function register_on_all_supported_taxonomies() { |
| 20 | 20 | $context = $this->schema->get(); |
| 21 | 21 | $taxonomies = Terms_Compat::get_public_taxonomies(); |
| 22 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 23 | - $this->register_pod( $taxonomy, 'taxonomy', $context ); |
|
| 22 | + foreach ($taxonomies as $taxonomy) { |
|
| 23 | + $this->register_pod($taxonomy, 'taxonomy', $context); |
|
| 24 | 24 | |
| 25 | 25 | } |
| 26 | 26 | } |
@@ -28,8 +28,8 @@ discard block |
||
| 28 | 28 | public function register_on_all_valid_post_types() { |
| 29 | 29 | $context = $this->schema->get(); |
| 30 | 30 | $supported_types = \Wordlift_Entity_Service::valid_entity_post_types(); |
| 31 | - foreach ( $supported_types as $supported_type ) { |
|
| 32 | - $this->register_pod( $supported_type, 'post_type', $context ); |
|
| 31 | + foreach ($supported_types as $supported_type) { |
|
| 32 | + $this->register_pod($supported_type, 'post_type', $context); |
|
| 33 | 33 | } |
| 34 | 34 | } |
| 35 | 35 | |
@@ -23,419 +23,419 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * The JSON-LD context. |
|
| 28 | - * |
|
| 29 | - * @since 3.10.0 |
|
| 30 | - */ |
|
| 31 | - const CONTEXT = 'http://schema.org'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.10.0 |
|
| 37 | - * @access protected |
|
| 38 | - * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 39 | - */ |
|
| 40 | - protected $entity_type_service; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * A {@link Wordlift_User_Service} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.10.0 |
|
| 46 | - * @access private |
|
| 47 | - * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 48 | - */ |
|
| 49 | - protected $user_service; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * A {@link Wordlift_Attachment_Service} instance. |
|
| 53 | - * |
|
| 54 | - * @since 3.10.0 |
|
| 55 | - * @access private |
|
| 56 | - * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 57 | - */ |
|
| 58 | - protected $attachment_service; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @var Wordlift_Property_Getter |
|
| 62 | - */ |
|
| 63 | - private $property_getter; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 67 | - * |
|
| 68 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 69 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 70 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 71 | - * @param \Wordlift_Property_Getter $property_getter |
|
| 72 | - * |
|
| 73 | - * @since 3.10.0 |
|
| 74 | - */ |
|
| 75 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
| 76 | - $this->entity_type_service = $entity_type_service; |
|
| 77 | - $this->user_service = $user_service; |
|
| 78 | - $this->attachment_service = $attachment_service; |
|
| 79 | - $this->property_getter = $property_getter; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 84 | - * found while processing the post is set in the $references array. |
|
| 85 | - * |
|
| 86 | - * @param int $post_id The post id. |
|
| 87 | - * @param array $references An array of entity references. |
|
| 88 | - * @param array $references_infos |
|
| 89 | - * |
|
| 90 | - * @return array A JSON-LD array. |
|
| 91 | - * @since 3.10.0 |
|
| 92 | - */ |
|
| 93 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 94 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 95 | - |
|
| 96 | - // Get the post instance. |
|
| 97 | - $post = get_post( $post_id ); |
|
| 98 | - if ( null === $post ) { |
|
| 99 | - // Post not found. |
|
| 100 | - return null; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // Get the post URI @id. |
|
| 104 | - $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
| 105 | - if ( $id === null ) { |
|
| 106 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /* |
|
| 26 | + /** |
|
| 27 | + * The JSON-LD context. |
|
| 28 | + * |
|
| 29 | + * @since 3.10.0 |
|
| 30 | + */ |
|
| 31 | + const CONTEXT = 'http://schema.org'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.10.0 |
|
| 37 | + * @access protected |
|
| 38 | + * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 39 | + */ |
|
| 40 | + protected $entity_type_service; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * A {@link Wordlift_User_Service} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.10.0 |
|
| 46 | + * @access private |
|
| 47 | + * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 48 | + */ |
|
| 49 | + protected $user_service; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * A {@link Wordlift_Attachment_Service} instance. |
|
| 53 | + * |
|
| 54 | + * @since 3.10.0 |
|
| 55 | + * @access private |
|
| 56 | + * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 57 | + */ |
|
| 58 | + protected $attachment_service; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @var Wordlift_Property_Getter |
|
| 62 | + */ |
|
| 63 | + private $property_getter; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 67 | + * |
|
| 68 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 69 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 70 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 71 | + * @param \Wordlift_Property_Getter $property_getter |
|
| 72 | + * |
|
| 73 | + * @since 3.10.0 |
|
| 74 | + */ |
|
| 75 | + public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
| 76 | + $this->entity_type_service = $entity_type_service; |
|
| 77 | + $this->user_service = $user_service; |
|
| 78 | + $this->attachment_service = $attachment_service; |
|
| 79 | + $this->property_getter = $property_getter; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 84 | + * found while processing the post is set in the $references array. |
|
| 85 | + * |
|
| 86 | + * @param int $post_id The post id. |
|
| 87 | + * @param array $references An array of entity references. |
|
| 88 | + * @param array $references_infos |
|
| 89 | + * |
|
| 90 | + * @return array A JSON-LD array. |
|
| 91 | + * @since 3.10.0 |
|
| 92 | + */ |
|
| 93 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 94 | + public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 95 | + |
|
| 96 | + // Get the post instance. |
|
| 97 | + $post = get_post( $post_id ); |
|
| 98 | + if ( null === $post ) { |
|
| 99 | + // Post not found. |
|
| 100 | + return null; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // Get the post URI @id. |
|
| 104 | + $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
| 105 | + if ( $id === null ) { |
|
| 106 | + $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /* |
|
| 110 | 110 | * The `types` variable holds one or more entity types. The `type` variable isn't used anymore. |
| 111 | 111 | * |
| 112 | 112 | * @since 3.20.0 We support more than one entity type. |
| 113 | 113 | * |
| 114 | 114 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 115 | 115 | */ |
| 116 | - // Get the entity @type. We consider `post` BlogPostings. |
|
| 117 | - // $type = $this->entity_type_service->get( $post_id ); |
|
| 118 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
| 119 | - $type = self::make_one( $types ); |
|
| 120 | - |
|
| 121 | - // Prepare the response. |
|
| 122 | - $jsonld = array( |
|
| 123 | - '@context' => self::CONTEXT, |
|
| 124 | - '@id' => $id, |
|
| 125 | - '@type' => $type, |
|
| 126 | - 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
| 127 | - ); |
|
| 128 | - |
|
| 129 | - // Set the `mainEntityOfPage` property if the post has some contents. |
|
| 130 | - /* |
|
| 116 | + // Get the entity @type. We consider `post` BlogPostings. |
|
| 117 | + // $type = $this->entity_type_service->get( $post_id ); |
|
| 118 | + $types = $this->entity_type_service->get_names( $post_id ); |
|
| 119 | + $type = self::make_one( $types ); |
|
| 120 | + |
|
| 121 | + // Prepare the response. |
|
| 122 | + $jsonld = array( |
|
| 123 | + '@context' => self::CONTEXT, |
|
| 124 | + '@id' => $id, |
|
| 125 | + '@type' => $type, |
|
| 126 | + 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
| 127 | + ); |
|
| 128 | + |
|
| 129 | + // Set the `mainEntityOfPage` property if the post has some contents. |
|
| 130 | + /* |
|
| 131 | 131 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
| 132 | 132 | * because the content is written elsewhere. |
| 133 | 133 | * |
| 134 | 134 | * @since 3.20.0 |
| 135 | 135 | */ |
| 136 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 137 | - if ( ! empty( $post_content ) ) { |
|
| 138 | - // We're setting the `mainEntityOfPage` to signal which one is the |
|
| 139 | - // main entity for the specified URL. It might be however that the |
|
| 140 | - // post/page is actually about another specific entity. How WL deals |
|
| 141 | - // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
| 142 | - // |
|
| 143 | - // See http://schema.org/mainEntityOfPage |
|
| 144 | - // |
|
| 145 | - // No need to specify `'@type' => 'WebPage'. |
|
| 146 | - // |
|
| 147 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 148 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
|
| 152 | - * |
|
| 153 | - * @since 3.27.7 |
|
| 154 | - */ |
|
| 155 | - if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ), true ) ) { |
|
| 156 | - $jsonld['mainEntityOfPage'] = array( |
|
| 157 | - '@type' => 'WebPage', |
|
| 158 | - 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
| 159 | - ); |
|
| 160 | - } |
|
| 161 | - }; |
|
| 162 | - |
|
| 163 | - $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 164 | - |
|
| 165 | - // Get the entities referenced by this post and set it to the `references` |
|
| 166 | - // array so that the caller can do further processing, such as printing out |
|
| 167 | - // more of those references. |
|
| 168 | - $references_without_locations = Object_Relation_Service::get_instance() |
|
| 169 | - ->get_references( $post_id, Object_Type_Enum::POST ); |
|
| 170 | - |
|
| 171 | - /* |
|
| 136 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 137 | + if ( ! empty( $post_content ) ) { |
|
| 138 | + // We're setting the `mainEntityOfPage` to signal which one is the |
|
| 139 | + // main entity for the specified URL. It might be however that the |
|
| 140 | + // post/page is actually about another specific entity. How WL deals |
|
| 141 | + // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
| 142 | + // |
|
| 143 | + // See http://schema.org/mainEntityOfPage |
|
| 144 | + // |
|
| 145 | + // No need to specify `'@type' => 'WebPage'. |
|
| 146 | + // |
|
| 147 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 148 | + $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
|
| 152 | + * |
|
| 153 | + * @since 3.27.7 |
|
| 154 | + */ |
|
| 155 | + if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ), true ) ) { |
|
| 156 | + $jsonld['mainEntityOfPage'] = array( |
|
| 157 | + '@type' => 'WebPage', |
|
| 158 | + 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
| 159 | + ); |
|
| 160 | + } |
|
| 161 | + }; |
|
| 162 | + |
|
| 163 | + $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 164 | + |
|
| 165 | + // Get the entities referenced by this post and set it to the `references` |
|
| 166 | + // array so that the caller can do further processing, such as printing out |
|
| 167 | + // more of those references. |
|
| 168 | + $references_without_locations = Object_Relation_Service::get_instance() |
|
| 169 | + ->get_references( $post_id, Object_Type_Enum::POST ); |
|
| 170 | + |
|
| 171 | + /* |
|
| 172 | 172 | * Add the locations to the references. |
| 173 | 173 | * |
| 174 | 174 | * @since 3.19.5 |
| 175 | 175 | * |
| 176 | 176 | * @see https://github.com/insideout10/wordlift-plugin/issues/858. |
| 177 | 177 | */ |
| 178 | - // A reference to use in closure. |
|
| 179 | - $entity_type_service = $this->entity_type_service; |
|
| 180 | - $locations = array_reduce( |
|
| 181 | - $references_without_locations, |
|
| 182 | - function ( $carry, $reference ) use ( $entity_type_service ) { |
|
| 183 | - /** |
|
| 184 | - * @var $reference Reference |
|
| 185 | - */ |
|
| 186 | - // @see https://schema.org/location for the schema.org types using the `location` property. |
|
| 187 | - if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
| 188 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
| 189 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
| 190 | - |
|
| 191 | - return $carry; |
|
| 192 | - } |
|
| 193 | - $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 194 | - $post_location_references = array_map( |
|
| 195 | - function ( $post_id ) { |
|
| 196 | - return new Post_Reference( $post_id ); |
|
| 197 | - }, |
|
| 198 | - $post_location_ids |
|
| 199 | - ); |
|
| 200 | - |
|
| 201 | - return array_merge( $carry, $post_location_references ); |
|
| 202 | - }, |
|
| 203 | - array() |
|
| 204 | - ); |
|
| 205 | - |
|
| 206 | - // Merge the references with the referenced locations if any. |
|
| 207 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 208 | - $references = array_merge( $references_without_locations, $locations ); |
|
| 209 | - |
|
| 210 | - return $jsonld; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * If the provided value starts with the schema.org context, we remove the schema.org |
|
| 215 | - * part since it is set with the '@context'. |
|
| 216 | - * |
|
| 217 | - * @param string $value The property value. |
|
| 218 | - * |
|
| 219 | - * @return string The property value without the context. |
|
| 220 | - * @since 3.10.0 |
|
| 221 | - */ |
|
| 222 | - public function relative_to_context( $value ) { |
|
| 223 | - |
|
| 224 | - return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Set the images, by looking for embedded images, for images loaded via the |
|
| 229 | - * gallery and for the featured image. |
|
| 230 | - * |
|
| 231 | - * Uses the cache service to store the results of this function for a day. |
|
| 232 | - * |
|
| 233 | - * @param $attachment_service Wordlift_Attachment_Service |
|
| 234 | - * @param WP_Post $post The target {@link WP_Post}. |
|
| 235 | - * @param array $jsonld The JSON-LD array. |
|
| 236 | - * |
|
| 237 | - * @since 3.10.0 |
|
| 238 | - */ |
|
| 239 | - public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 240 | - |
|
| 241 | - // Prepare the attachment ids array. |
|
| 242 | - $ids = array(); |
|
| 243 | - |
|
| 244 | - // Set the thumbnail id as first attachment id, if any. |
|
| 245 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 246 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 247 | - $ids[] = $thumbnail_id; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - // For the time being the following is being removed since the query |
|
| 251 | - // initiated by `get_image_embeds` is consuming lots of CPU. |
|
| 252 | - // |
|
| 253 | - // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 254 | - // |
|
| 255 | - // Get the embeds, removing existing ids. |
|
| 256 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 257 | - if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
| 258 | - $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 259 | - } else { |
|
| 260 | - $embeds = array(); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - // Get the gallery, removing existing ids. |
|
| 264 | - $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 265 | - |
|
| 266 | - // Map the attachment ids to images' data structured for schema.org use. |
|
| 267 | - $images_with_sizes = array_filter( |
|
| 268 | - array_reduce( |
|
| 269 | - array_merge( $ids, $embeds, $gallery ), |
|
| 270 | - function ( $carry, $item ) { |
|
| 271 | - /* |
|
| 178 | + // A reference to use in closure. |
|
| 179 | + $entity_type_service = $this->entity_type_service; |
|
| 180 | + $locations = array_reduce( |
|
| 181 | + $references_without_locations, |
|
| 182 | + function ( $carry, $reference ) use ( $entity_type_service ) { |
|
| 183 | + /** |
|
| 184 | + * @var $reference Reference |
|
| 185 | + */ |
|
| 186 | + // @see https://schema.org/location for the schema.org types using the `location` property. |
|
| 187 | + if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
| 188 | + && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
| 189 | + && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
| 190 | + |
|
| 191 | + return $carry; |
|
| 192 | + } |
|
| 193 | + $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 194 | + $post_location_references = array_map( |
|
| 195 | + function ( $post_id ) { |
|
| 196 | + return new Post_Reference( $post_id ); |
|
| 197 | + }, |
|
| 198 | + $post_location_ids |
|
| 199 | + ); |
|
| 200 | + |
|
| 201 | + return array_merge( $carry, $post_location_references ); |
|
| 202 | + }, |
|
| 203 | + array() |
|
| 204 | + ); |
|
| 205 | + |
|
| 206 | + // Merge the references with the referenced locations if any. |
|
| 207 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 208 | + $references = array_merge( $references_without_locations, $locations ); |
|
| 209 | + |
|
| 210 | + return $jsonld; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * If the provided value starts with the schema.org context, we remove the schema.org |
|
| 215 | + * part since it is set with the '@context'. |
|
| 216 | + * |
|
| 217 | + * @param string $value The property value. |
|
| 218 | + * |
|
| 219 | + * @return string The property value without the context. |
|
| 220 | + * @since 3.10.0 |
|
| 221 | + */ |
|
| 222 | + public function relative_to_context( $value ) { |
|
| 223 | + |
|
| 224 | + return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Set the images, by looking for embedded images, for images loaded via the |
|
| 229 | + * gallery and for the featured image. |
|
| 230 | + * |
|
| 231 | + * Uses the cache service to store the results of this function for a day. |
|
| 232 | + * |
|
| 233 | + * @param $attachment_service Wordlift_Attachment_Service |
|
| 234 | + * @param WP_Post $post The target {@link WP_Post}. |
|
| 235 | + * @param array $jsonld The JSON-LD array. |
|
| 236 | + * |
|
| 237 | + * @since 3.10.0 |
|
| 238 | + */ |
|
| 239 | + public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 240 | + |
|
| 241 | + // Prepare the attachment ids array. |
|
| 242 | + $ids = array(); |
|
| 243 | + |
|
| 244 | + // Set the thumbnail id as first attachment id, if any. |
|
| 245 | + $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 246 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 247 | + $ids[] = $thumbnail_id; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + // For the time being the following is being removed since the query |
|
| 251 | + // initiated by `get_image_embeds` is consuming lots of CPU. |
|
| 252 | + // |
|
| 253 | + // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 254 | + // |
|
| 255 | + // Get the embeds, removing existing ids. |
|
| 256 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 257 | + if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
| 258 | + $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 259 | + } else { |
|
| 260 | + $embeds = array(); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + // Get the gallery, removing existing ids. |
|
| 264 | + $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 265 | + |
|
| 266 | + // Map the attachment ids to images' data structured for schema.org use. |
|
| 267 | + $images_with_sizes = array_filter( |
|
| 268 | + array_reduce( |
|
| 269 | + array_merge( $ids, $embeds, $gallery ), |
|
| 270 | + function ( $carry, $item ) { |
|
| 271 | + /* |
|
| 272 | 272 | * @todo: we're not sure that we're getting attachment data here, we |
| 273 | 273 | * should filter `false`s. |
| 274 | 274 | */ |
| 275 | 275 | |
| 276 | - $sources = array_merge( |
|
| 277 | - Wordlift_Image_Service::get_sources( $item ), |
|
| 278 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 279 | - ); |
|
| 280 | - |
|
| 281 | - $sources_with_image = array_filter( |
|
| 282 | - $sources, |
|
| 283 | - function ( $source ) { |
|
| 284 | - return ! empty( $source[0] ); |
|
| 285 | - } |
|
| 286 | - ); |
|
| 287 | - |
|
| 288 | - // Get the attachment data. |
|
| 289 | - // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
| 290 | - |
|
| 291 | - // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
| 292 | - |
|
| 293 | - // Bail if image is not found. |
|
| 294 | - // In some cases, you can delete the image from the database |
|
| 295 | - // or from uploads dir, but the image id still exists as featured image |
|
| 296 | - // or in [gallery] shortcode. |
|
| 297 | - // if ( empty( $attachment[0] ) ) { |
|
| 298 | - if ( empty( $sources_with_image ) ) { |
|
| 299 | - return $carry; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - // Merge the arrays. |
|
| 303 | - return array_merge( |
|
| 304 | - $carry, |
|
| 305 | - $sources_with_image |
|
| 306 | - ); |
|
| 307 | - }, |
|
| 308 | - // Initial array. |
|
| 309 | - |
|
| 310 | - array() |
|
| 311 | - ) |
|
| 312 | - ); |
|
| 313 | - |
|
| 314 | - // Refactor data as per schema.org specifications. |
|
| 315 | - $images = array_map( |
|
| 316 | - function ( $attachment ) { |
|
| 317 | - return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
| 318 | - array( |
|
| 319 | - '@type' => 'ImageObject', |
|
| 320 | - 'url' => $attachment[0], |
|
| 321 | - ), |
|
| 322 | - $attachment |
|
| 323 | - ); |
|
| 324 | - }, |
|
| 325 | - $images_with_sizes |
|
| 326 | - ); |
|
| 327 | - |
|
| 328 | - // Add images if present. |
|
| 329 | - if ( 0 < count( $images ) ) { |
|
| 330 | - $jsonld['image'] = $images; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * If the provided array of values contains only one value, then one single |
|
| 337 | - * value is returned, otherwise the original array is returned. |
|
| 338 | - * |
|
| 339 | - * @param array $value An array of values. |
|
| 340 | - * |
|
| 341 | - * @return mixed|array A single value or the original array. |
|
| 342 | - * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
| 343 | - * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
| 344 | - * @since 3.8.0 |
|
| 345 | - * @access private |
|
| 346 | - */ |
|
| 347 | - protected static function make_one( $value ) { |
|
| 348 | - |
|
| 349 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Process the provided array by adding the width / height if the values |
|
| 354 | - * are available and are greater than 0. |
|
| 355 | - * |
|
| 356 | - * @param array $image The `ImageObject` array. |
|
| 357 | - * @param array $attachment The attachment array. |
|
| 358 | - * |
|
| 359 | - * @return array The enriched `ImageObject` array. |
|
| 360 | - * @since 3.14.0 |
|
| 361 | - */ |
|
| 362 | - public static function set_image_size( $image, $attachment ) { |
|
| 363 | - |
|
| 364 | - // If you specify a "width" or "height" value you should leave out |
|
| 365 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 366 | - // |
|
| 367 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 368 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 369 | - $image['width'] = $attachment[1]; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 373 | - $image['height'] = $attachment[2]; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - return $image; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property |
|
| 381 | - * for the post entity type. |
|
| 382 | - * |
|
| 383 | - * @param array $jsonld The JSON-LD array. |
|
| 384 | - * @param array $fields The entity types field array. |
|
| 385 | - * @param WP_Post $post The target {@link WP_Post} instance. |
|
| 386 | - * @param array $references The references array. |
|
| 387 | - * |
|
| 388 | - * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
|
| 389 | - * the {@link Wordlift_Schema_Service} class. |
|
| 390 | - */ |
|
| 391 | - protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
| 392 | - |
|
| 393 | - // Set a reference to use in closures. |
|
| 394 | - $converter = $this; |
|
| 395 | - |
|
| 396 | - // Try each field on the entity. |
|
| 397 | - foreach ( $fields as $key => $value ) { |
|
| 398 | - |
|
| 399 | - // Get the predicate. |
|
| 400 | - $name = $this->relative_to_context( $value['predicate'] ); |
|
| 401 | - |
|
| 402 | - // Get the value, the property service will get the right extractor |
|
| 403 | - // for that property. |
|
| 404 | - $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
| 405 | - |
|
| 406 | - if ( empty( $value ) ) { |
|
| 407 | - continue; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - // Map the value to the property name. |
|
| 411 | - // If we got an array with just one value, we return that one value. |
|
| 412 | - // If we got a Wordlift_Property_Entity_Reference we get the URL. |
|
| 413 | - $jsonld[ $name ] = self::make_one( |
|
| 414 | - array_map( |
|
| 415 | - function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
| 416 | - |
|
| 417 | - if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 418 | - |
|
| 419 | - $url = $item->get_url(); |
|
| 420 | - |
|
| 421 | - // The refactored converters require the entity id. |
|
| 422 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 423 | - $references[] = $item->to_reference(); |
|
| 424 | - |
|
| 425 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 426 | - $references_infos[] = array( 'reference' => $item ); |
|
| 427 | - |
|
| 428 | - return array( '@id' => $url ); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - return $converter->relative_to_context( $item ); |
|
| 432 | - }, |
|
| 433 | - $value |
|
| 434 | - ) |
|
| 435 | - ); |
|
| 436 | - |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - } |
|
| 276 | + $sources = array_merge( |
|
| 277 | + Wordlift_Image_Service::get_sources( $item ), |
|
| 278 | + array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 279 | + ); |
|
| 280 | + |
|
| 281 | + $sources_with_image = array_filter( |
|
| 282 | + $sources, |
|
| 283 | + function ( $source ) { |
|
| 284 | + return ! empty( $source[0] ); |
|
| 285 | + } |
|
| 286 | + ); |
|
| 287 | + |
|
| 288 | + // Get the attachment data. |
|
| 289 | + // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
| 290 | + |
|
| 291 | + // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
| 292 | + |
|
| 293 | + // Bail if image is not found. |
|
| 294 | + // In some cases, you can delete the image from the database |
|
| 295 | + // or from uploads dir, but the image id still exists as featured image |
|
| 296 | + // or in [gallery] shortcode. |
|
| 297 | + // if ( empty( $attachment[0] ) ) { |
|
| 298 | + if ( empty( $sources_with_image ) ) { |
|
| 299 | + return $carry; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + // Merge the arrays. |
|
| 303 | + return array_merge( |
|
| 304 | + $carry, |
|
| 305 | + $sources_with_image |
|
| 306 | + ); |
|
| 307 | + }, |
|
| 308 | + // Initial array. |
|
| 309 | + |
|
| 310 | + array() |
|
| 311 | + ) |
|
| 312 | + ); |
|
| 313 | + |
|
| 314 | + // Refactor data as per schema.org specifications. |
|
| 315 | + $images = array_map( |
|
| 316 | + function ( $attachment ) { |
|
| 317 | + return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
| 318 | + array( |
|
| 319 | + '@type' => 'ImageObject', |
|
| 320 | + 'url' => $attachment[0], |
|
| 321 | + ), |
|
| 322 | + $attachment |
|
| 323 | + ); |
|
| 324 | + }, |
|
| 325 | + $images_with_sizes |
|
| 326 | + ); |
|
| 327 | + |
|
| 328 | + // Add images if present. |
|
| 329 | + if ( 0 < count( $images ) ) { |
|
| 330 | + $jsonld['image'] = $images; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * If the provided array of values contains only one value, then one single |
|
| 337 | + * value is returned, otherwise the original array is returned. |
|
| 338 | + * |
|
| 339 | + * @param array $value An array of values. |
|
| 340 | + * |
|
| 341 | + * @return mixed|array A single value or the original array. |
|
| 342 | + * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
| 343 | + * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
| 344 | + * @since 3.8.0 |
|
| 345 | + * @access private |
|
| 346 | + */ |
|
| 347 | + protected static function make_one( $value ) { |
|
| 348 | + |
|
| 349 | + return 1 === count( $value ) ? $value[0] : $value; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Process the provided array by adding the width / height if the values |
|
| 354 | + * are available and are greater than 0. |
|
| 355 | + * |
|
| 356 | + * @param array $image The `ImageObject` array. |
|
| 357 | + * @param array $attachment The attachment array. |
|
| 358 | + * |
|
| 359 | + * @return array The enriched `ImageObject` array. |
|
| 360 | + * @since 3.14.0 |
|
| 361 | + */ |
|
| 362 | + public static function set_image_size( $image, $attachment ) { |
|
| 363 | + |
|
| 364 | + // If you specify a "width" or "height" value you should leave out |
|
| 365 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 366 | + // |
|
| 367 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 368 | + if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 369 | + $image['width'] = $attachment[1]; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 373 | + $image['height'] = $attachment[2]; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + return $image; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property |
|
| 381 | + * for the post entity type. |
|
| 382 | + * |
|
| 383 | + * @param array $jsonld The JSON-LD array. |
|
| 384 | + * @param array $fields The entity types field array. |
|
| 385 | + * @param WP_Post $post The target {@link WP_Post} instance. |
|
| 386 | + * @param array $references The references array. |
|
| 387 | + * |
|
| 388 | + * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
|
| 389 | + * the {@link Wordlift_Schema_Service} class. |
|
| 390 | + */ |
|
| 391 | + protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
| 392 | + |
|
| 393 | + // Set a reference to use in closures. |
|
| 394 | + $converter = $this; |
|
| 395 | + |
|
| 396 | + // Try each field on the entity. |
|
| 397 | + foreach ( $fields as $key => $value ) { |
|
| 398 | + |
|
| 399 | + // Get the predicate. |
|
| 400 | + $name = $this->relative_to_context( $value['predicate'] ); |
|
| 401 | + |
|
| 402 | + // Get the value, the property service will get the right extractor |
|
| 403 | + // for that property. |
|
| 404 | + $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
| 405 | + |
|
| 406 | + if ( empty( $value ) ) { |
|
| 407 | + continue; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + // Map the value to the property name. |
|
| 411 | + // If we got an array with just one value, we return that one value. |
|
| 412 | + // If we got a Wordlift_Property_Entity_Reference we get the URL. |
|
| 413 | + $jsonld[ $name ] = self::make_one( |
|
| 414 | + array_map( |
|
| 415 | + function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
| 416 | + |
|
| 417 | + if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 418 | + |
|
| 419 | + $url = $item->get_url(); |
|
| 420 | + |
|
| 421 | + // The refactored converters require the entity id. |
|
| 422 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 423 | + $references[] = $item->to_reference(); |
|
| 424 | + |
|
| 425 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 426 | + $references_infos[] = array( 'reference' => $item ); |
|
| 427 | + |
|
| 428 | + return array( '@id' => $url ); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + return $converter->relative_to_context( $item ); |
|
| 432 | + }, |
|
| 433 | + $value |
|
| 434 | + ) |
|
| 435 | + ); |
|
| 436 | + |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + } |
|
| 440 | 440 | |
| 441 | 441 | } |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | * |
| 73 | 73 | * @since 3.10.0 |
| 74 | 74 | */ |
| 75 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
| 75 | + public function __construct($entity_type_service, $user_service, $attachment_service, $property_getter) { |
|
| 76 | 76 | $this->entity_type_service = $entity_type_service; |
| 77 | 77 | $this->user_service = $user_service; |
| 78 | 78 | $this->attachment_service = $attachment_service; |
@@ -91,19 +91,19 @@ discard block |
||
| 91 | 91 | * @since 3.10.0 |
| 92 | 92 | */ |
| 93 | 93 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 94 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 94 | + public function convert($post_id, &$references = array(), &$references_infos = array()) { |
|
| 95 | 95 | |
| 96 | 96 | // Get the post instance. |
| 97 | - $post = get_post( $post_id ); |
|
| 98 | - if ( null === $post ) { |
|
| 97 | + $post = get_post($post_id); |
|
| 98 | + if (null === $post) { |
|
| 99 | 99 | // Post not found. |
| 100 | 100 | return null; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | // Get the post URI @id. |
| 104 | - $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
| 105 | - if ( $id === null ) { |
|
| 106 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 104 | + $id = Wordlift_Entity_Service::get_instance()->get_uri($post->ID); |
|
| 105 | + if ($id === null) { |
|
| 106 | + $id = 'get_uri returned null, dataset is '.wl_configuration_get_redlink_dataset_uri(); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /* |
@@ -115,15 +115,15 @@ discard block |
||
| 115 | 115 | */ |
| 116 | 116 | // Get the entity @type. We consider `post` BlogPostings. |
| 117 | 117 | // $type = $this->entity_type_service->get( $post_id ); |
| 118 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
| 119 | - $type = self::make_one( $types ); |
|
| 118 | + $types = $this->entity_type_service->get_names($post_id); |
|
| 119 | + $type = self::make_one($types); |
|
| 120 | 120 | |
| 121 | 121 | // Prepare the response. |
| 122 | 122 | $jsonld = array( |
| 123 | 123 | '@context' => self::CONTEXT, |
| 124 | 124 | '@id' => $id, |
| 125 | 125 | '@type' => $type, |
| 126 | - 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
| 126 | + 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt($post), |
|
| 127 | 127 | ); |
| 128 | 128 | |
| 129 | 129 | // Set the `mainEntityOfPage` property if the post has some contents. |
@@ -133,8 +133,8 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * @since 3.20.0 |
| 135 | 135 | */ |
| 136 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 137 | - if ( ! empty( $post_content ) ) { |
|
| 136 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
| 137 | + if ( ! empty($post_content)) { |
|
| 138 | 138 | // We're setting the `mainEntityOfPage` to signal which one is the |
| 139 | 139 | // main entity for the specified URL. It might be however that the |
| 140 | 140 | // post/page is actually about another specific entity. How WL deals |
@@ -145,28 +145,28 @@ discard block |
||
| 145 | 145 | // No need to specify `'@type' => 'WebPage'. |
| 146 | 146 | // |
| 147 | 147 | // See https://github.com/insideout10/wordlift-plugin/issues/451. |
| 148 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 148 | + $jsonld['mainEntityOfPage'] = get_the_permalink($post->ID); |
|
| 149 | 149 | |
| 150 | 150 | /** |
| 151 | 151 | * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
| 152 | 152 | * |
| 153 | 153 | * @since 3.27.7 |
| 154 | 154 | */ |
| 155 | - if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ), true ) ) { |
|
| 155 | + if (in_array($type, array('Occupation', 'OccupationAggregationByEmployer'), true)) { |
|
| 156 | 156 | $jsonld['mainEntityOfPage'] = array( |
| 157 | 157 | '@type' => 'WebPage', |
| 158 | - 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
| 158 | + 'lastReviewed' => get_post_time('Y-m-d\TH:i:sP', true, $post, false), |
|
| 159 | 159 | ); |
| 160 | 160 | } |
| 161 | 161 | }; |
| 162 | 162 | |
| 163 | - $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 163 | + $this->set_images($this->attachment_service, $post, $jsonld); |
|
| 164 | 164 | |
| 165 | 165 | // Get the entities referenced by this post and set it to the `references` |
| 166 | 166 | // array so that the caller can do further processing, such as printing out |
| 167 | 167 | // more of those references. |
| 168 | 168 | $references_without_locations = Object_Relation_Service::get_instance() |
| 169 | - ->get_references( $post_id, Object_Type_Enum::POST ); |
|
| 169 | + ->get_references($post_id, Object_Type_Enum::POST); |
|
| 170 | 170 | |
| 171 | 171 | /* |
| 172 | 172 | * Add the locations to the references. |
@@ -179,33 +179,33 @@ discard block |
||
| 179 | 179 | $entity_type_service = $this->entity_type_service; |
| 180 | 180 | $locations = array_reduce( |
| 181 | 181 | $references_without_locations, |
| 182 | - function ( $carry, $reference ) use ( $entity_type_service ) { |
|
| 182 | + function($carry, $reference) use ($entity_type_service) { |
|
| 183 | 183 | /** |
| 184 | 184 | * @var $reference Reference |
| 185 | 185 | */ |
| 186 | 186 | // @see https://schema.org/location for the schema.org types using the `location` property. |
| 187 | - if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
| 188 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
| 189 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
| 187 | + if ( ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Action') |
|
| 188 | + && ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Event') |
|
| 189 | + && ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Organization')) { |
|
| 190 | 190 | |
| 191 | 191 | return $carry; |
| 192 | 192 | } |
| 193 | - $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 193 | + $post_location_ids = get_post_meta($reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION); |
|
| 194 | 194 | $post_location_references = array_map( |
| 195 | - function ( $post_id ) { |
|
| 196 | - return new Post_Reference( $post_id ); |
|
| 195 | + function($post_id) { |
|
| 196 | + return new Post_Reference($post_id); |
|
| 197 | 197 | }, |
| 198 | 198 | $post_location_ids |
| 199 | 199 | ); |
| 200 | 200 | |
| 201 | - return array_merge( $carry, $post_location_references ); |
|
| 201 | + return array_merge($carry, $post_location_references); |
|
| 202 | 202 | }, |
| 203 | 203 | array() |
| 204 | 204 | ); |
| 205 | 205 | |
| 206 | 206 | // Merge the references with the referenced locations if any. |
| 207 | 207 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 208 | - $references = array_merge( $references_without_locations, $locations ); |
|
| 208 | + $references = array_merge($references_without_locations, $locations); |
|
| 209 | 209 | |
| 210 | 210 | return $jsonld; |
| 211 | 211 | } |
@@ -219,9 +219,9 @@ discard block |
||
| 219 | 219 | * @return string The property value without the context. |
| 220 | 220 | * @since 3.10.0 |
| 221 | 221 | */ |
| 222 | - public function relative_to_context( $value ) { |
|
| 222 | + public function relative_to_context($value) { |
|
| 223 | 223 | |
| 224 | - return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 224 | + return 0 === strpos($value, self::CONTEXT.'/') ? substr($value, strlen(self::CONTEXT) + 1) : $value; |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | /** |
@@ -236,14 +236,14 @@ discard block |
||
| 236 | 236 | * |
| 237 | 237 | * @since 3.10.0 |
| 238 | 238 | */ |
| 239 | - public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 239 | + public static function set_images($attachment_service, $post, &$jsonld) { |
|
| 240 | 240 | |
| 241 | 241 | // Prepare the attachment ids array. |
| 242 | 242 | $ids = array(); |
| 243 | 243 | |
| 244 | 244 | // Set the thumbnail id as first attachment id, if any. |
| 245 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 246 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 245 | + $thumbnail_id = get_post_thumbnail_id($post->ID); |
|
| 246 | + if ('' !== $thumbnail_id && 0 !== $thumbnail_id) { |
|
| 247 | 247 | $ids[] = $thumbnail_id; |
| 248 | 248 | } |
| 249 | 249 | |
@@ -254,34 +254,34 @@ discard block |
||
| 254 | 254 | // |
| 255 | 255 | // Get the embeds, removing existing ids. |
| 256 | 256 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 257 | - if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
| 258 | - $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 257 | + if (apply_filters('wl_feature__enable__image-embeds', false)) { |
|
| 258 | + $embeds = array_diff($attachment_service->get_image_embeds($post->post_content), $ids); |
|
| 259 | 259 | } else { |
| 260 | 260 | $embeds = array(); |
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | // Get the gallery, removing existing ids. |
| 264 | - $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 264 | + $gallery = array_diff($attachment_service->get_gallery($post), $ids, $embeds); |
|
| 265 | 265 | |
| 266 | 266 | // Map the attachment ids to images' data structured for schema.org use. |
| 267 | 267 | $images_with_sizes = array_filter( |
| 268 | 268 | array_reduce( |
| 269 | - array_merge( $ids, $embeds, $gallery ), |
|
| 270 | - function ( $carry, $item ) { |
|
| 269 | + array_merge($ids, $embeds, $gallery), |
|
| 270 | + function($carry, $item) { |
|
| 271 | 271 | /* |
| 272 | 272 | * @todo: we're not sure that we're getting attachment data here, we |
| 273 | 273 | * should filter `false`s. |
| 274 | 274 | */ |
| 275 | 275 | |
| 276 | 276 | $sources = array_merge( |
| 277 | - Wordlift_Image_Service::get_sources( $item ), |
|
| 278 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 277 | + Wordlift_Image_Service::get_sources($item), |
|
| 278 | + array(wp_get_attachment_image_src($item, 'full')) |
|
| 279 | 279 | ); |
| 280 | 280 | |
| 281 | 281 | $sources_with_image = array_filter( |
| 282 | 282 | $sources, |
| 283 | - function ( $source ) { |
|
| 284 | - return ! empty( $source[0] ); |
|
| 283 | + function($source) { |
|
| 284 | + return ! empty($source[0]); |
|
| 285 | 285 | } |
| 286 | 286 | ); |
| 287 | 287 | |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | // or from uploads dir, but the image id still exists as featured image |
| 296 | 296 | // or in [gallery] shortcode. |
| 297 | 297 | // if ( empty( $attachment[0] ) ) { |
| 298 | - if ( empty( $sources_with_image ) ) { |
|
| 298 | + if (empty($sources_with_image)) { |
|
| 299 | 299 | return $carry; |
| 300 | 300 | } |
| 301 | 301 | |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | |
| 314 | 314 | // Refactor data as per schema.org specifications. |
| 315 | 315 | $images = array_map( |
| 316 | - function ( $attachment ) { |
|
| 316 | + function($attachment) { |
|
| 317 | 317 | return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
| 318 | 318 | array( |
| 319 | 319 | '@type' => 'ImageObject', |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | ); |
| 327 | 327 | |
| 328 | 328 | // Add images if present. |
| 329 | - if ( 0 < count( $images ) ) { |
|
| 329 | + if (0 < count($images)) { |
|
| 330 | 330 | $jsonld['image'] = $images; |
| 331 | 331 | } |
| 332 | 332 | |
@@ -344,9 +344,9 @@ discard block |
||
| 344 | 344 | * @since 3.8.0 |
| 345 | 345 | * @access private |
| 346 | 346 | */ |
| 347 | - protected static function make_one( $value ) { |
|
| 347 | + protected static function make_one($value) { |
|
| 348 | 348 | |
| 349 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 349 | + return 1 === count($value) ? $value[0] : $value; |
|
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | /** |
@@ -359,17 +359,17 @@ discard block |
||
| 359 | 359 | * @return array The enriched `ImageObject` array. |
| 360 | 360 | * @since 3.14.0 |
| 361 | 361 | */ |
| 362 | - public static function set_image_size( $image, $attachment ) { |
|
| 362 | + public static function set_image_size($image, $attachment) { |
|
| 363 | 363 | |
| 364 | 364 | // If you specify a "width" or "height" value you should leave out |
| 365 | 365 | // 'px'. For example: "width":"4608px" should be "width":"4608". |
| 366 | 366 | // |
| 367 | 367 | // See https://github.com/insideout10/wordlift-plugin/issues/451. |
| 368 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 368 | + if (isset($attachment[1]) && is_numeric($attachment[1]) && 0 < $attachment[1]) { |
|
| 369 | 369 | $image['width'] = $attachment[1]; |
| 370 | 370 | } |
| 371 | 371 | |
| 372 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 372 | + if (isset($attachment[2]) && is_numeric($attachment[2]) && 0 < $attachment[2]) { |
|
| 373 | 373 | $image['height'] = $attachment[2]; |
| 374 | 374 | } |
| 375 | 375 | |
@@ -388,33 +388,33 @@ discard block |
||
| 388 | 388 | * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
| 389 | 389 | * the {@link Wordlift_Schema_Service} class. |
| 390 | 390 | */ |
| 391 | - protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
| 391 | + protected function process_type_custom_fields(&$jsonld, $fields, $post, &$references, &$references_infos) { |
|
| 392 | 392 | |
| 393 | 393 | // Set a reference to use in closures. |
| 394 | 394 | $converter = $this; |
| 395 | 395 | |
| 396 | 396 | // Try each field on the entity. |
| 397 | - foreach ( $fields as $key => $value ) { |
|
| 397 | + foreach ($fields as $key => $value) { |
|
| 398 | 398 | |
| 399 | 399 | // Get the predicate. |
| 400 | - $name = $this->relative_to_context( $value['predicate'] ); |
|
| 400 | + $name = $this->relative_to_context($value['predicate']); |
|
| 401 | 401 | |
| 402 | 402 | // Get the value, the property service will get the right extractor |
| 403 | 403 | // for that property. |
| 404 | - $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
| 404 | + $value = $this->property_getter->get($post->ID, $key, Object_Type_Enum::POST); |
|
| 405 | 405 | |
| 406 | - if ( empty( $value ) ) { |
|
| 406 | + if (empty($value)) { |
|
| 407 | 407 | continue; |
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | // Map the value to the property name. |
| 411 | 411 | // If we got an array with just one value, we return that one value. |
| 412 | 412 | // If we got a Wordlift_Property_Entity_Reference we get the URL. |
| 413 | - $jsonld[ $name ] = self::make_one( |
|
| 413 | + $jsonld[$name] = self::make_one( |
|
| 414 | 414 | array_map( |
| 415 | - function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
| 415 | + function($item) use ($converter, &$references, &$references_infos) { |
|
| 416 | 416 | |
| 417 | - if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 417 | + if ($item instanceof Wordlift_Property_Entity_Reference) { |
|
| 418 | 418 | |
| 419 | 419 | $url = $item->get_url(); |
| 420 | 420 | |
@@ -423,12 +423,12 @@ discard block |
||
| 423 | 423 | $references[] = $item->to_reference(); |
| 424 | 424 | |
| 425 | 425 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
| 426 | - $references_infos[] = array( 'reference' => $item ); |
|
| 426 | + $references_infos[] = array('reference' => $item); |
|
| 427 | 427 | |
| 428 | - return array( '@id' => $url ); |
|
| 428 | + return array('@id' => $url); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | - return $converter->relative_to_context( $item ); |
|
| 431 | + return $converter->relative_to_context($item); |
|
| 432 | 432 | }, |
| 433 | 433 | $value |
| 434 | 434 | ) |
@@ -4,99 +4,99 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Schema { |
| 6 | 6 | |
| 7 | - public function get_context_type() { |
|
| 8 | - |
|
| 9 | - if ( isset( $_REQUEST['post'] ) || isset( $_REQUEST['post_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 10 | - return Context::POST; |
|
| 11 | - } |
|
| 12 | - if ( isset( $_REQUEST['tag_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 13 | - return Context::TERM; |
|
| 14 | - } |
|
| 15 | - |
|
| 16 | - if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { |
|
| 17 | - return Context::ADMIN_AJAX; |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - return Context::UNKNOWN; |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @return Context |
|
| 25 | - */ |
|
| 26 | - public function get() { |
|
| 27 | - // we need to identify the context to filter the results. |
|
| 28 | - |
|
| 29 | - $identifier = isset( $_REQUEST['post'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 30 | - : sanitize_text_field( wp_unslash( isset( $_REQUEST['post_ID'] ) ? $_REQUEST['post_ID'] : '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 31 | - |
|
| 32 | - if ( $identifier ) { |
|
| 33 | - // If post identifier, get schema. |
|
| 34 | - return new Context( Context::POST, $identifier, $this->get_fields_for_post( $identifier ) ); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - $identifier = isset( $_REQUEST['tag_ID'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tag_ID'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 38 | - |
|
| 39 | - if ( $identifier ) { |
|
| 40 | - // If post identifier, get schema. |
|
| 41 | - return new Context( Context::TERM, $identifier, $this->get_fields_for_term( $identifier ) ); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - if ( is_admin() && defined( 'DOING_AJAX' ) ) { |
|
| 45 | - return new Context( Context::ADMIN_AJAX, null, $this->get_all_fields() ); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - return new Context( Context::UNKNOWN, null, null ); |
|
| 49 | - |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @return Schema_Field_Group[] |
|
| 54 | - */ |
|
| 55 | - private function get_fields_for_post( $identifier ) { |
|
| 56 | - $types = \Wordlift_Entity_Type_Service::get_instance()->get_names( $identifier ); |
|
| 57 | - $schema_classes = \Wordlift_Schema_Service::get_instance(); |
|
| 58 | - |
|
| 59 | - return array_map( |
|
| 60 | - function ( $schema_type ) use ( $schema_classes ) { |
|
| 61 | - $data = $schema_classes->get_schema( strtolower( $schema_type ) ); |
|
| 62 | - |
|
| 63 | - return new Schema_Field_Group( $schema_type, $data['custom_fields'] ); |
|
| 64 | - }, |
|
| 65 | - $types |
|
| 66 | - ); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return Schema_Field_Group[] |
|
| 71 | - */ |
|
| 72 | - private function get_fields_for_term( $identifier ) { |
|
| 73 | - $term_entity_types = get_term_meta( (int) $identifier, \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 74 | - $schema_classes = \Wordlift_Schema_Service::get_instance(); |
|
| 75 | - |
|
| 76 | - return array_map( |
|
| 77 | - function ( $schema_type ) use ( $schema_classes ) { |
|
| 78 | - $data = $schema_classes->get_schema( strtolower( $schema_type ) ); |
|
| 79 | - |
|
| 80 | - return new Schema_Field_Group( $schema_type, $data['custom_fields'] ); |
|
| 81 | - }, |
|
| 82 | - $term_entity_types |
|
| 83 | - ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - private function get_all_fields() { |
|
| 87 | - $schema_classes = \Wordlift_Schema_Service::get_instance(); |
|
| 88 | - $all_schema_slugs = $schema_classes->get_all_schema_slugs(); |
|
| 89 | - |
|
| 90 | - return array_filter( |
|
| 91 | - array_map( |
|
| 92 | - function ( $schema_type ) use ( $schema_classes ) { |
|
| 93 | - $data = $schema_classes->get_schema( strtolower( $schema_type ) ); |
|
| 94 | - |
|
| 95 | - return new Schema_Field_Group( $schema_type, $data['custom_fields'] ); |
|
| 96 | - }, |
|
| 97 | - $all_schema_slugs |
|
| 98 | - ) |
|
| 99 | - ); |
|
| 100 | - } |
|
| 7 | + public function get_context_type() { |
|
| 8 | + |
|
| 9 | + if ( isset( $_REQUEST['post'] ) || isset( $_REQUEST['post_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 10 | + return Context::POST; |
|
| 11 | + } |
|
| 12 | + if ( isset( $_REQUEST['tag_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 13 | + return Context::TERM; |
|
| 14 | + } |
|
| 15 | + |
|
| 16 | + if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { |
|
| 17 | + return Context::ADMIN_AJAX; |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + return Context::UNKNOWN; |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @return Context |
|
| 25 | + */ |
|
| 26 | + public function get() { |
|
| 27 | + // we need to identify the context to filter the results. |
|
| 28 | + |
|
| 29 | + $identifier = isset( $_REQUEST['post'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 30 | + : sanitize_text_field( wp_unslash( isset( $_REQUEST['post_ID'] ) ? $_REQUEST['post_ID'] : '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 31 | + |
|
| 32 | + if ( $identifier ) { |
|
| 33 | + // If post identifier, get schema. |
|
| 34 | + return new Context( Context::POST, $identifier, $this->get_fields_for_post( $identifier ) ); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + $identifier = isset( $_REQUEST['tag_ID'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tag_ID'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 38 | + |
|
| 39 | + if ( $identifier ) { |
|
| 40 | + // If post identifier, get schema. |
|
| 41 | + return new Context( Context::TERM, $identifier, $this->get_fields_for_term( $identifier ) ); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + if ( is_admin() && defined( 'DOING_AJAX' ) ) { |
|
| 45 | + return new Context( Context::ADMIN_AJAX, null, $this->get_all_fields() ); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + return new Context( Context::UNKNOWN, null, null ); |
|
| 49 | + |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @return Schema_Field_Group[] |
|
| 54 | + */ |
|
| 55 | + private function get_fields_for_post( $identifier ) { |
|
| 56 | + $types = \Wordlift_Entity_Type_Service::get_instance()->get_names( $identifier ); |
|
| 57 | + $schema_classes = \Wordlift_Schema_Service::get_instance(); |
|
| 58 | + |
|
| 59 | + return array_map( |
|
| 60 | + function ( $schema_type ) use ( $schema_classes ) { |
|
| 61 | + $data = $schema_classes->get_schema( strtolower( $schema_type ) ); |
|
| 62 | + |
|
| 63 | + return new Schema_Field_Group( $schema_type, $data['custom_fields'] ); |
|
| 64 | + }, |
|
| 65 | + $types |
|
| 66 | + ); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return Schema_Field_Group[] |
|
| 71 | + */ |
|
| 72 | + private function get_fields_for_term( $identifier ) { |
|
| 73 | + $term_entity_types = get_term_meta( (int) $identifier, \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 74 | + $schema_classes = \Wordlift_Schema_Service::get_instance(); |
|
| 75 | + |
|
| 76 | + return array_map( |
|
| 77 | + function ( $schema_type ) use ( $schema_classes ) { |
|
| 78 | + $data = $schema_classes->get_schema( strtolower( $schema_type ) ); |
|
| 79 | + |
|
| 80 | + return new Schema_Field_Group( $schema_type, $data['custom_fields'] ); |
|
| 81 | + }, |
|
| 82 | + $term_entity_types |
|
| 83 | + ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + private function get_all_fields() { |
|
| 87 | + $schema_classes = \Wordlift_Schema_Service::get_instance(); |
|
| 88 | + $all_schema_slugs = $schema_classes->get_all_schema_slugs(); |
|
| 89 | + |
|
| 90 | + return array_filter( |
|
| 91 | + array_map( |
|
| 92 | + function ( $schema_type ) use ( $schema_classes ) { |
|
| 93 | + $data = $schema_classes->get_schema( strtolower( $schema_type ) ); |
|
| 94 | + |
|
| 95 | + return new Schema_Field_Group( $schema_type, $data['custom_fields'] ); |
|
| 96 | + }, |
|
| 97 | + $all_schema_slugs |
|
| 98 | + ) |
|
| 99 | + ); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | 102 | } |
@@ -6,14 +6,14 @@ discard block |
||
| 6 | 6 | |
| 7 | 7 | public function get_context_type() { |
| 8 | 8 | |
| 9 | - if ( isset( $_REQUEST['post'] ) || isset( $_REQUEST['post_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 9 | + if (isset($_REQUEST['post']) || isset($_REQUEST['post_ID'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 10 | 10 | return Context::POST; |
| 11 | 11 | } |
| 12 | - if ( isset( $_REQUEST['tag_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 12 | + if (isset($_REQUEST['tag_ID'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 13 | 13 | return Context::TERM; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { |
|
| 16 | + if (is_admin() && ! defined('DOING_AJAX')) { |
|
| 17 | 17 | return Context::ADMIN_AJAX; |
| 18 | 18 | } |
| 19 | 19 | |
@@ -26,41 +26,41 @@ discard block |
||
| 26 | 26 | public function get() { |
| 27 | 27 | // we need to identify the context to filter the results. |
| 28 | 28 | |
| 29 | - $identifier = isset( $_REQUEST['post'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 30 | - : sanitize_text_field( wp_unslash( isset( $_REQUEST['post_ID'] ) ? $_REQUEST['post_ID'] : '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 29 | + $identifier = isset($_REQUEST['post']) ? sanitize_text_field(wp_unslash($_REQUEST['post'])) // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 30 | + : sanitize_text_field(wp_unslash(isset($_REQUEST['post_ID']) ? $_REQUEST['post_ID'] : '')); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 31 | 31 | |
| 32 | - if ( $identifier ) { |
|
| 32 | + if ($identifier) { |
|
| 33 | 33 | // If post identifier, get schema. |
| 34 | - return new Context( Context::POST, $identifier, $this->get_fields_for_post( $identifier ) ); |
|
| 34 | + return new Context(Context::POST, $identifier, $this->get_fields_for_post($identifier)); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - $identifier = isset( $_REQUEST['tag_ID'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tag_ID'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 37 | + $identifier = isset($_REQUEST['tag_ID']) ? sanitize_text_field(wp_unslash($_REQUEST['tag_ID'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 38 | 38 | |
| 39 | - if ( $identifier ) { |
|
| 39 | + if ($identifier) { |
|
| 40 | 40 | // If post identifier, get schema. |
| 41 | - return new Context( Context::TERM, $identifier, $this->get_fields_for_term( $identifier ) ); |
|
| 41 | + return new Context(Context::TERM, $identifier, $this->get_fields_for_term($identifier)); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - if ( is_admin() && defined( 'DOING_AJAX' ) ) { |
|
| 45 | - return new Context( Context::ADMIN_AJAX, null, $this->get_all_fields() ); |
|
| 44 | + if (is_admin() && defined('DOING_AJAX')) { |
|
| 45 | + return new Context(Context::ADMIN_AJAX, null, $this->get_all_fields()); |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - return new Context( Context::UNKNOWN, null, null ); |
|
| 48 | + return new Context(Context::UNKNOWN, null, null); |
|
| 49 | 49 | |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * @return Schema_Field_Group[] |
| 54 | 54 | */ |
| 55 | - private function get_fields_for_post( $identifier ) { |
|
| 56 | - $types = \Wordlift_Entity_Type_Service::get_instance()->get_names( $identifier ); |
|
| 55 | + private function get_fields_for_post($identifier) { |
|
| 56 | + $types = \Wordlift_Entity_Type_Service::get_instance()->get_names($identifier); |
|
| 57 | 57 | $schema_classes = \Wordlift_Schema_Service::get_instance(); |
| 58 | 58 | |
| 59 | 59 | return array_map( |
| 60 | - function ( $schema_type ) use ( $schema_classes ) { |
|
| 61 | - $data = $schema_classes->get_schema( strtolower( $schema_type ) ); |
|
| 60 | + function($schema_type) use ($schema_classes) { |
|
| 61 | + $data = $schema_classes->get_schema(strtolower($schema_type)); |
|
| 62 | 62 | |
| 63 | - return new Schema_Field_Group( $schema_type, $data['custom_fields'] ); |
|
| 63 | + return new Schema_Field_Group($schema_type, $data['custom_fields']); |
|
| 64 | 64 | }, |
| 65 | 65 | $types |
| 66 | 66 | ); |
@@ -69,15 +69,15 @@ discard block |
||
| 69 | 69 | /** |
| 70 | 70 | * @return Schema_Field_Group[] |
| 71 | 71 | */ |
| 72 | - private function get_fields_for_term( $identifier ) { |
|
| 73 | - $term_entity_types = get_term_meta( (int) $identifier, \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 72 | + private function get_fields_for_term($identifier) { |
|
| 73 | + $term_entity_types = get_term_meta((int) $identifier, \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 74 | 74 | $schema_classes = \Wordlift_Schema_Service::get_instance(); |
| 75 | 75 | |
| 76 | 76 | return array_map( |
| 77 | - function ( $schema_type ) use ( $schema_classes ) { |
|
| 78 | - $data = $schema_classes->get_schema( strtolower( $schema_type ) ); |
|
| 77 | + function($schema_type) use ($schema_classes) { |
|
| 78 | + $data = $schema_classes->get_schema(strtolower($schema_type)); |
|
| 79 | 79 | |
| 80 | - return new Schema_Field_Group( $schema_type, $data['custom_fields'] ); |
|
| 80 | + return new Schema_Field_Group($schema_type, $data['custom_fields']); |
|
| 81 | 81 | }, |
| 82 | 82 | $term_entity_types |
| 83 | 83 | ); |
@@ -89,10 +89,10 @@ discard block |
||
| 89 | 89 | |
| 90 | 90 | return array_filter( |
| 91 | 91 | array_map( |
| 92 | - function ( $schema_type ) use ( $schema_classes ) { |
|
| 93 | - $data = $schema_classes->get_schema( strtolower( $schema_type ) ); |
|
| 92 | + function($schema_type) use ($schema_classes) { |
|
| 93 | + $data = $schema_classes->get_schema(strtolower($schema_type)); |
|
| 94 | 94 | |
| 95 | - return new Schema_Field_Group( $schema_type, $data['custom_fields'] ); |
|
| 95 | + return new Schema_Field_Group($schema_type, $data['custom_fields']); |
|
| 96 | 96 | }, |
| 97 | 97 | $all_schema_slugs |
| 98 | 98 | ) |
@@ -7,196 +7,196 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Filters { |
| 9 | 9 | |
| 10 | - const FIELD_NAME = 'wlentity'; |
|
| 11 | - |
|
| 12 | - public function __construct() { |
|
| 13 | - |
|
| 14 | - pods_register_related_object( self::FIELD_NAME, 'WordLift Entity', array( 'simple' => false ) ); |
|
| 15 | - add_filter( 'pods_form_ui_field_pick_ajax', array( $this, 'ajax_filter' ), 10, 4 ); |
|
| 16 | - add_filter( 'pods_api_get_table_info', array( $this, 'table_info_filter' ), 10, 6 ); |
|
| 17 | - add_filter( 'pods_field_pick_object_data', array( $this, 'field_options_filter' ), 10, 7 ); |
|
| 18 | - add_filter( 'pods_field_dfv_data', array( $this, 'data_filter' ), 10, 2 ); |
|
| 19 | - add_filter( 'pods_field_pick_data_ajax', array( $this, 'admin_ajax_filter' ), 10, 4 ); |
|
| 20 | - |
|
| 21 | - add_action( |
|
| 22 | - 'pods_meta_save_taxonomy', |
|
| 23 | - function ( $data, $pod, $id, $groups, $term_id ) { |
|
| 24 | - $this->save_field( 'term', $term_id, $groups ); |
|
| 25 | - }, |
|
| 26 | - 10, |
|
| 27 | - 5 |
|
| 28 | - ); |
|
| 29 | - |
|
| 30 | - add_action( |
|
| 31 | - 'pods_meta_save_post', |
|
| 32 | - function ( $data, $pod, $id, $groups ) { |
|
| 33 | - $this->save_field( 'post', $id, $groups ); |
|
| 34 | - }, |
|
| 35 | - 10, |
|
| 36 | - 4 |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - private function save_field( $type, $identifier, $groups ) { |
|
| 42 | - |
|
| 43 | - $entity_fields = $this->filter_entity_fields( $groups ); |
|
| 44 | - |
|
| 45 | - foreach ( $entity_fields as $entity_field ) { |
|
| 46 | - delete_metadata( $type, $identifier, $entity_field ); |
|
| 47 | - $key = sprintf( 'pods_meta_%s', $entity_field ); |
|
| 48 | - |
|
| 49 | - $data = filter_var_array( $_REQUEST, array( $key => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 50 | - |
|
| 51 | - if ( ! $data ) { |
|
| 52 | - continue; |
|
| 53 | - } |
|
| 54 | - $values = $data[ $key ]; |
|
| 55 | - |
|
| 56 | - foreach ( $values as $value ) { |
|
| 57 | - add_metadata( $type, $identifier, $entity_field, $value ); |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - private function filter_entity_fields( $groups ) { |
|
| 64 | - |
|
| 65 | - $pods = json_decode( wp_json_encode( $groups ), true ); |
|
| 66 | - |
|
| 67 | - $fields = array_reduce( |
|
| 68 | - $pods, |
|
| 69 | - function ( $carry, $item ) { |
|
| 70 | - return array_merge( $carry, $item['fields'] ); |
|
| 71 | - }, |
|
| 72 | - array() |
|
| 73 | - ); |
|
| 74 | - |
|
| 75 | - return array_map( |
|
| 76 | - function ( $item ) { |
|
| 77 | - return $item['name']; |
|
| 78 | - }, |
|
| 79 | - array_filter( |
|
| 80 | - $fields, |
|
| 81 | - function ( $item ) { |
|
| 82 | - return is_array( $item ) && isset( $item['pick_object'] ) && self::FIELD_NAME === $item['pick_object']; |
|
| 83 | - } |
|
| 84 | - ) |
|
| 85 | - ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - public function wl_pods_transform_data_for_pick_field( $item ) { |
|
| 89 | - |
|
| 90 | - $content = $item->get_content(); |
|
| 91 | - |
|
| 92 | - return array( |
|
| 93 | - 'id' => sprintf( '%s_%d', Object_Type_Enum::to_string( $content->get_object_type_enum() ), $content->get_id() ), |
|
| 94 | - 'icon' => 'https:\/\/wordlift.localhost\/wp-content\/plugins\/wordlift\/images\/svg\/wl-vocabulary-icon.svg', |
|
| 95 | - 'name' => $item->get_title() . ' (' . $item->get_schema_type() . ')', |
|
| 96 | - 'edit_link' => $content->get_edit_link(), |
|
| 97 | - 'link' => $content->get_permalink(), |
|
| 98 | - 'selected' => false, |
|
| 99 | - ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - public function admin_ajax_filter( $data, $name, $_, $field ) { |
|
| 103 | - |
|
| 104 | - if ( ( ! $field instanceof \Pods\Whatsit\Field ) || $field->get_arg( 'pick_object', false ) !== self::FIELD_NAME ) { |
|
| 105 | - return $data; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $query = sanitize_text_field( wp_unslash( isset( $_REQUEST['query'] ) ? $_REQUEST['query'] : '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 109 | - $query_service = Entity_Query_Service::get_instance(); |
|
| 110 | - |
|
| 111 | - return array_map( |
|
| 112 | - array( |
|
| 113 | - $this, |
|
| 114 | - 'wl_pods_transform_data_for_pick_field', |
|
| 115 | - ), |
|
| 116 | - $query_service->query( $query, $field->get_arg( 'supported_schema_types', array( 'Thing' ) ) ) |
|
| 117 | - ); |
|
| 118 | - |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function data_filter( $data, $args ) { |
|
| 122 | - |
|
| 123 | - $args_arr = json_decode( wp_json_encode( $args ), true ); |
|
| 124 | - $field_data = $args_arr['options']; |
|
| 125 | - |
|
| 126 | - if ( ! isset( $field_data['pick_object'] ) || self::FIELD_NAME !== $field_data['pick_object'] ) { |
|
| 127 | - return $data; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if ( ! isset( $args_arr['pod']['data']['pod_data']['type'] ) |
|
| 131 | - || ! is_string( $args_arr['pod']['data']['pod_data']['type'] ) ) { |
|
| 132 | - return $data; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $name = $field_data['name']; |
|
| 136 | - $identifier = $args->id; |
|
| 137 | - $type = $args_arr['pod']['data']['pod_data']['type']; |
|
| 10 | + const FIELD_NAME = 'wlentity'; |
|
| 11 | + |
|
| 12 | + public function __construct() { |
|
| 13 | + |
|
| 14 | + pods_register_related_object( self::FIELD_NAME, 'WordLift Entity', array( 'simple' => false ) ); |
|
| 15 | + add_filter( 'pods_form_ui_field_pick_ajax', array( $this, 'ajax_filter' ), 10, 4 ); |
|
| 16 | + add_filter( 'pods_api_get_table_info', array( $this, 'table_info_filter' ), 10, 6 ); |
|
| 17 | + add_filter( 'pods_field_pick_object_data', array( $this, 'field_options_filter' ), 10, 7 ); |
|
| 18 | + add_filter( 'pods_field_dfv_data', array( $this, 'data_filter' ), 10, 2 ); |
|
| 19 | + add_filter( 'pods_field_pick_data_ajax', array( $this, 'admin_ajax_filter' ), 10, 4 ); |
|
| 20 | + |
|
| 21 | + add_action( |
|
| 22 | + 'pods_meta_save_taxonomy', |
|
| 23 | + function ( $data, $pod, $id, $groups, $term_id ) { |
|
| 24 | + $this->save_field( 'term', $term_id, $groups ); |
|
| 25 | + }, |
|
| 26 | + 10, |
|
| 27 | + 5 |
|
| 28 | + ); |
|
| 29 | + |
|
| 30 | + add_action( |
|
| 31 | + 'pods_meta_save_post', |
|
| 32 | + function ( $data, $pod, $id, $groups ) { |
|
| 33 | + $this->save_field( 'post', $id, $groups ); |
|
| 34 | + }, |
|
| 35 | + 10, |
|
| 36 | + 4 |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + private function save_field( $type, $identifier, $groups ) { |
|
| 42 | + |
|
| 43 | + $entity_fields = $this->filter_entity_fields( $groups ); |
|
| 44 | + |
|
| 45 | + foreach ( $entity_fields as $entity_field ) { |
|
| 46 | + delete_metadata( $type, $identifier, $entity_field ); |
|
| 47 | + $key = sprintf( 'pods_meta_%s', $entity_field ); |
|
| 48 | + |
|
| 49 | + $data = filter_var_array( $_REQUEST, array( $key => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 50 | + |
|
| 51 | + if ( ! $data ) { |
|
| 52 | + continue; |
|
| 53 | + } |
|
| 54 | + $values = $data[ $key ]; |
|
| 55 | + |
|
| 56 | + foreach ( $values as $value ) { |
|
| 57 | + add_metadata( $type, $identifier, $entity_field, $value ); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + private function filter_entity_fields( $groups ) { |
|
| 64 | + |
|
| 65 | + $pods = json_decode( wp_json_encode( $groups ), true ); |
|
| 66 | + |
|
| 67 | + $fields = array_reduce( |
|
| 68 | + $pods, |
|
| 69 | + function ( $carry, $item ) { |
|
| 70 | + return array_merge( $carry, $item['fields'] ); |
|
| 71 | + }, |
|
| 72 | + array() |
|
| 73 | + ); |
|
| 74 | + |
|
| 75 | + return array_map( |
|
| 76 | + function ( $item ) { |
|
| 77 | + return $item['name']; |
|
| 78 | + }, |
|
| 79 | + array_filter( |
|
| 80 | + $fields, |
|
| 81 | + function ( $item ) { |
|
| 82 | + return is_array( $item ) && isset( $item['pick_object'] ) && self::FIELD_NAME === $item['pick_object']; |
|
| 83 | + } |
|
| 84 | + ) |
|
| 85 | + ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + public function wl_pods_transform_data_for_pick_field( $item ) { |
|
| 89 | + |
|
| 90 | + $content = $item->get_content(); |
|
| 91 | + |
|
| 92 | + return array( |
|
| 93 | + 'id' => sprintf( '%s_%d', Object_Type_Enum::to_string( $content->get_object_type_enum() ), $content->get_id() ), |
|
| 94 | + 'icon' => 'https:\/\/wordlift.localhost\/wp-content\/plugins\/wordlift\/images\/svg\/wl-vocabulary-icon.svg', |
|
| 95 | + 'name' => $item->get_title() . ' (' . $item->get_schema_type() . ')', |
|
| 96 | + 'edit_link' => $content->get_edit_link(), |
|
| 97 | + 'link' => $content->get_permalink(), |
|
| 98 | + 'selected' => false, |
|
| 99 | + ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + public function admin_ajax_filter( $data, $name, $_, $field ) { |
|
| 103 | + |
|
| 104 | + if ( ( ! $field instanceof \Pods\Whatsit\Field ) || $field->get_arg( 'pick_object', false ) !== self::FIELD_NAME ) { |
|
| 105 | + return $data; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $query = sanitize_text_field( wp_unslash( isset( $_REQUEST['query'] ) ? $_REQUEST['query'] : '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 109 | + $query_service = Entity_Query_Service::get_instance(); |
|
| 110 | + |
|
| 111 | + return array_map( |
|
| 112 | + array( |
|
| 113 | + $this, |
|
| 114 | + 'wl_pods_transform_data_for_pick_field', |
|
| 115 | + ), |
|
| 116 | + $query_service->query( $query, $field->get_arg( 'supported_schema_types', array( 'Thing' ) ) ) |
|
| 117 | + ); |
|
| 118 | + |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function data_filter( $data, $args ) { |
|
| 122 | + |
|
| 123 | + $args_arr = json_decode( wp_json_encode( $args ), true ); |
|
| 124 | + $field_data = $args_arr['options']; |
|
| 125 | + |
|
| 126 | + if ( ! isset( $field_data['pick_object'] ) || self::FIELD_NAME !== $field_data['pick_object'] ) { |
|
| 127 | + return $data; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if ( ! isset( $args_arr['pod']['data']['pod_data']['type'] ) |
|
| 131 | + || ! is_string( $args_arr['pod']['data']['pod_data']['type'] ) ) { |
|
| 132 | + return $data; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $name = $field_data['name']; |
|
| 136 | + $identifier = $args->id; |
|
| 137 | + $type = $args_arr['pod']['data']['pod_data']['type']; |
|
| 138 | 138 | |
| 139 | - if ( 'post_type' === $type ) { |
|
| 140 | - $data['fieldValue'] = get_post_meta( $identifier, $name ); |
|
| 141 | - } elseif ( 'taxonomy' === $type ) { |
|
| 142 | - $data['fieldValue'] = get_term_meta( $identifier, $name ); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - return $data; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - public function ajax_filter( $result, $name, $value, $field_options ) { |
|
| 149 | - |
|
| 150 | - if ( ! isset( $field_options['pick_object'] ) ) { |
|
| 151 | - return $result; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - return self::FIELD_NAME === $field_options['pick_object']; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - public function table_info_filter( $info, $object_type, $object, $name, $pod, $field ) { |
|
| 158 | - |
|
| 159 | - if ( $field === null || self::FIELD_NAME !== $field->get_arg( 'pick_object', false ) ) { |
|
| 160 | - return $info; |
|
| 161 | - } |
|
| 162 | - // We need to return an non empty array here to prevent pods from querying a table. |
|
| 163 | - // This is necessary to prevent errors on ui. |
|
| 164 | - return array( 'foo' => 'bar' ); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - public function field_options_filter( $_, $name, $value, $options, $pod, $id, $object_params ) { |
|
| 168 | - |
|
| 169 | - $object_params = json_decode( wp_json_encode( $object_params ), true ); |
|
| 170 | - |
|
| 171 | - $query_service = Entity_Query_Service::get_instance(); |
|
| 172 | - |
|
| 173 | - if ( is_array( $object_params ) && isset( $object_params['options']['pick_object'] ) |
|
| 174 | - && is_string( $object_params['options']['pick_object'] ) |
|
| 175 | - && self::FIELD_NAME === $object_params['options']['pick_object'] |
|
| 176 | - && isset( $object_params['pod']['data']['pod_data']['type'] ) |
|
| 177 | - && is_string( $object_params['pod']['data']['pod_data']['type'] ) ) { |
|
| 178 | - |
|
| 179 | - $type = $object_params['pod']['data']['pod_data']['type']; |
|
| 180 | - $linked_entities = array(); |
|
| 181 | - if ( 'post_type' === $type ) { |
|
| 182 | - $linked_entities = get_post_meta( $id, $name ); |
|
| 183 | - } elseif ( 'taxonomy' === $type ) { |
|
| 184 | - $linked_entities = get_term_meta( $id, $name ); |
|
| 185 | - } |
|
| 186 | - $data = array(); |
|
| 187 | - $linked_entities = $query_service->get( $linked_entities ); |
|
| 188 | - foreach ( $linked_entities as $linked_entity ) { |
|
| 189 | - $content = $linked_entity->get_content(); |
|
| 190 | - $id = sprintf( '%s_%d', Object_Type_Enum::to_string( $content->get_object_type_enum() ), $content->get_id() ); |
|
| 191 | - $text = $linked_entity->get_title() . ' (' . $linked_entity->get_schema_type() . ')'; |
|
| 192 | - $data[ $id ] = $text; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - return $data; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return $_; |
|
| 139 | + if ( 'post_type' === $type ) { |
|
| 140 | + $data['fieldValue'] = get_post_meta( $identifier, $name ); |
|
| 141 | + } elseif ( 'taxonomy' === $type ) { |
|
| 142 | + $data['fieldValue'] = get_term_meta( $identifier, $name ); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + return $data; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + public function ajax_filter( $result, $name, $value, $field_options ) { |
|
| 149 | + |
|
| 150 | + if ( ! isset( $field_options['pick_object'] ) ) { |
|
| 151 | + return $result; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + return self::FIELD_NAME === $field_options['pick_object']; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + public function table_info_filter( $info, $object_type, $object, $name, $pod, $field ) { |
|
| 158 | + |
|
| 159 | + if ( $field === null || self::FIELD_NAME !== $field->get_arg( 'pick_object', false ) ) { |
|
| 160 | + return $info; |
|
| 161 | + } |
|
| 162 | + // We need to return an non empty array here to prevent pods from querying a table. |
|
| 163 | + // This is necessary to prevent errors on ui. |
|
| 164 | + return array( 'foo' => 'bar' ); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + public function field_options_filter( $_, $name, $value, $options, $pod, $id, $object_params ) { |
|
| 168 | + |
|
| 169 | + $object_params = json_decode( wp_json_encode( $object_params ), true ); |
|
| 170 | + |
|
| 171 | + $query_service = Entity_Query_Service::get_instance(); |
|
| 172 | + |
|
| 173 | + if ( is_array( $object_params ) && isset( $object_params['options']['pick_object'] ) |
|
| 174 | + && is_string( $object_params['options']['pick_object'] ) |
|
| 175 | + && self::FIELD_NAME === $object_params['options']['pick_object'] |
|
| 176 | + && isset( $object_params['pod']['data']['pod_data']['type'] ) |
|
| 177 | + && is_string( $object_params['pod']['data']['pod_data']['type'] ) ) { |
|
| 178 | + |
|
| 179 | + $type = $object_params['pod']['data']['pod_data']['type']; |
|
| 180 | + $linked_entities = array(); |
|
| 181 | + if ( 'post_type' === $type ) { |
|
| 182 | + $linked_entities = get_post_meta( $id, $name ); |
|
| 183 | + } elseif ( 'taxonomy' === $type ) { |
|
| 184 | + $linked_entities = get_term_meta( $id, $name ); |
|
| 185 | + } |
|
| 186 | + $data = array(); |
|
| 187 | + $linked_entities = $query_service->get( $linked_entities ); |
|
| 188 | + foreach ( $linked_entities as $linked_entity ) { |
|
| 189 | + $content = $linked_entity->get_content(); |
|
| 190 | + $id = sprintf( '%s_%d', Object_Type_Enum::to_string( $content->get_object_type_enum() ), $content->get_id() ); |
|
| 191 | + $text = $linked_entity->get_title() . ' (' . $linked_entity->get_schema_type() . ')'; |
|
| 192 | + $data[ $id ] = $text; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + return $data; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return $_; |
|
| 199 | 199 | |
| 200 | - } |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | 202 | } |
@@ -11,17 +11,17 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | public function __construct() { |
| 13 | 13 | |
| 14 | - pods_register_related_object( self::FIELD_NAME, 'WordLift Entity', array( 'simple' => false ) ); |
|
| 15 | - add_filter( 'pods_form_ui_field_pick_ajax', array( $this, 'ajax_filter' ), 10, 4 ); |
|
| 16 | - add_filter( 'pods_api_get_table_info', array( $this, 'table_info_filter' ), 10, 6 ); |
|
| 17 | - add_filter( 'pods_field_pick_object_data', array( $this, 'field_options_filter' ), 10, 7 ); |
|
| 18 | - add_filter( 'pods_field_dfv_data', array( $this, 'data_filter' ), 10, 2 ); |
|
| 19 | - add_filter( 'pods_field_pick_data_ajax', array( $this, 'admin_ajax_filter' ), 10, 4 ); |
|
| 14 | + pods_register_related_object(self::FIELD_NAME, 'WordLift Entity', array('simple' => false)); |
|
| 15 | + add_filter('pods_form_ui_field_pick_ajax', array($this, 'ajax_filter'), 10, 4); |
|
| 16 | + add_filter('pods_api_get_table_info', array($this, 'table_info_filter'), 10, 6); |
|
| 17 | + add_filter('pods_field_pick_object_data', array($this, 'field_options_filter'), 10, 7); |
|
| 18 | + add_filter('pods_field_dfv_data', array($this, 'data_filter'), 10, 2); |
|
| 19 | + add_filter('pods_field_pick_data_ajax', array($this, 'admin_ajax_filter'), 10, 4); |
|
| 20 | 20 | |
| 21 | 21 | add_action( |
| 22 | 22 | 'pods_meta_save_taxonomy', |
| 23 | - function ( $data, $pod, $id, $groups, $term_id ) { |
|
| 24 | - $this->save_field( 'term', $term_id, $groups ); |
|
| 23 | + function($data, $pod, $id, $groups, $term_id) { |
|
| 24 | + $this->save_field('term', $term_id, $groups); |
|
| 25 | 25 | }, |
| 26 | 26 | 10, |
| 27 | 27 | 5 |
@@ -29,8 +29,8 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | add_action( |
| 31 | 31 | 'pods_meta_save_post', |
| 32 | - function ( $data, $pod, $id, $groups ) { |
|
| 33 | - $this->save_field( 'post', $id, $groups ); |
|
| 32 | + function($data, $pod, $id, $groups) { |
|
| 33 | + $this->save_field('post', $id, $groups); |
|
| 34 | 34 | }, |
| 35 | 35 | 10, |
| 36 | 36 | 4 |
@@ -38,74 +38,74 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - private function save_field( $type, $identifier, $groups ) { |
|
| 41 | + private function save_field($type, $identifier, $groups) { |
|
| 42 | 42 | |
| 43 | - $entity_fields = $this->filter_entity_fields( $groups ); |
|
| 43 | + $entity_fields = $this->filter_entity_fields($groups); |
|
| 44 | 44 | |
| 45 | - foreach ( $entity_fields as $entity_field ) { |
|
| 46 | - delete_metadata( $type, $identifier, $entity_field ); |
|
| 47 | - $key = sprintf( 'pods_meta_%s', $entity_field ); |
|
| 45 | + foreach ($entity_fields as $entity_field) { |
|
| 46 | + delete_metadata($type, $identifier, $entity_field); |
|
| 47 | + $key = sprintf('pods_meta_%s', $entity_field); |
|
| 48 | 48 | |
| 49 | - $data = filter_var_array( $_REQUEST, array( $key => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 49 | + $data = filter_var_array($_REQUEST, array($key => array('flags' => FILTER_REQUIRE_ARRAY))); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 50 | 50 | |
| 51 | - if ( ! $data ) { |
|
| 51 | + if ( ! $data) { |
|
| 52 | 52 | continue; |
| 53 | 53 | } |
| 54 | - $values = $data[ $key ]; |
|
| 54 | + $values = $data[$key]; |
|
| 55 | 55 | |
| 56 | - foreach ( $values as $value ) { |
|
| 57 | - add_metadata( $type, $identifier, $entity_field, $value ); |
|
| 56 | + foreach ($values as $value) { |
|
| 57 | + add_metadata($type, $identifier, $entity_field, $value); |
|
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - private function filter_entity_fields( $groups ) { |
|
| 63 | + private function filter_entity_fields($groups) { |
|
| 64 | 64 | |
| 65 | - $pods = json_decode( wp_json_encode( $groups ), true ); |
|
| 65 | + $pods = json_decode(wp_json_encode($groups), true); |
|
| 66 | 66 | |
| 67 | 67 | $fields = array_reduce( |
| 68 | 68 | $pods, |
| 69 | - function ( $carry, $item ) { |
|
| 70 | - return array_merge( $carry, $item['fields'] ); |
|
| 69 | + function($carry, $item) { |
|
| 70 | + return array_merge($carry, $item['fields']); |
|
| 71 | 71 | }, |
| 72 | 72 | array() |
| 73 | 73 | ); |
| 74 | 74 | |
| 75 | 75 | return array_map( |
| 76 | - function ( $item ) { |
|
| 76 | + function($item) { |
|
| 77 | 77 | return $item['name']; |
| 78 | 78 | }, |
| 79 | 79 | array_filter( |
| 80 | 80 | $fields, |
| 81 | - function ( $item ) { |
|
| 82 | - return is_array( $item ) && isset( $item['pick_object'] ) && self::FIELD_NAME === $item['pick_object']; |
|
| 81 | + function($item) { |
|
| 82 | + return is_array($item) && isset($item['pick_object']) && self::FIELD_NAME === $item['pick_object']; |
|
| 83 | 83 | } |
| 84 | 84 | ) |
| 85 | 85 | ); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - public function wl_pods_transform_data_for_pick_field( $item ) { |
|
| 88 | + public function wl_pods_transform_data_for_pick_field($item) { |
|
| 89 | 89 | |
| 90 | 90 | $content = $item->get_content(); |
| 91 | 91 | |
| 92 | 92 | return array( |
| 93 | - 'id' => sprintf( '%s_%d', Object_Type_Enum::to_string( $content->get_object_type_enum() ), $content->get_id() ), |
|
| 93 | + 'id' => sprintf('%s_%d', Object_Type_Enum::to_string($content->get_object_type_enum()), $content->get_id()), |
|
| 94 | 94 | 'icon' => 'https:\/\/wordlift.localhost\/wp-content\/plugins\/wordlift\/images\/svg\/wl-vocabulary-icon.svg', |
| 95 | - 'name' => $item->get_title() . ' (' . $item->get_schema_type() . ')', |
|
| 95 | + 'name' => $item->get_title().' ('.$item->get_schema_type().')', |
|
| 96 | 96 | 'edit_link' => $content->get_edit_link(), |
| 97 | 97 | 'link' => $content->get_permalink(), |
| 98 | 98 | 'selected' => false, |
| 99 | 99 | ); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - public function admin_ajax_filter( $data, $name, $_, $field ) { |
|
| 102 | + public function admin_ajax_filter($data, $name, $_, $field) { |
|
| 103 | 103 | |
| 104 | - if ( ( ! $field instanceof \Pods\Whatsit\Field ) || $field->get_arg( 'pick_object', false ) !== self::FIELD_NAME ) { |
|
| 104 | + if (( ! $field instanceof \Pods\Whatsit\Field) || $field->get_arg('pick_object', false) !== self::FIELD_NAME) { |
|
| 105 | 105 | return $data; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - $query = sanitize_text_field( wp_unslash( isset( $_REQUEST['query'] ) ? $_REQUEST['query'] : '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 108 | + $query = sanitize_text_field(wp_unslash(isset($_REQUEST['query']) ? $_REQUEST['query'] : '')); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 109 | 109 | $query_service = Entity_Query_Service::get_instance(); |
| 110 | 110 | |
| 111 | 111 | return array_map( |
@@ -113,22 +113,22 @@ discard block |
||
| 113 | 113 | $this, |
| 114 | 114 | 'wl_pods_transform_data_for_pick_field', |
| 115 | 115 | ), |
| 116 | - $query_service->query( $query, $field->get_arg( 'supported_schema_types', array( 'Thing' ) ) ) |
|
| 116 | + $query_service->query($query, $field->get_arg('supported_schema_types', array('Thing'))) |
|
| 117 | 117 | ); |
| 118 | 118 | |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - public function data_filter( $data, $args ) { |
|
| 121 | + public function data_filter($data, $args) { |
|
| 122 | 122 | |
| 123 | - $args_arr = json_decode( wp_json_encode( $args ), true ); |
|
| 123 | + $args_arr = json_decode(wp_json_encode($args), true); |
|
| 124 | 124 | $field_data = $args_arr['options']; |
| 125 | 125 | |
| 126 | - if ( ! isset( $field_data['pick_object'] ) || self::FIELD_NAME !== $field_data['pick_object'] ) { |
|
| 126 | + if ( ! isset($field_data['pick_object']) || self::FIELD_NAME !== $field_data['pick_object']) { |
|
| 127 | 127 | return $data; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | - if ( ! isset( $args_arr['pod']['data']['pod_data']['type'] ) |
|
| 131 | - || ! is_string( $args_arr['pod']['data']['pod_data']['type'] ) ) { |
|
| 130 | + if ( ! isset($args_arr['pod']['data']['pod_data']['type']) |
|
| 131 | + || ! is_string($args_arr['pod']['data']['pod_data']['type'])) { |
|
| 132 | 132 | return $data; |
| 133 | 133 | } |
| 134 | 134 | |
@@ -136,60 +136,60 @@ discard block |
||
| 136 | 136 | $identifier = $args->id; |
| 137 | 137 | $type = $args_arr['pod']['data']['pod_data']['type']; |
| 138 | 138 | |
| 139 | - if ( 'post_type' === $type ) { |
|
| 140 | - $data['fieldValue'] = get_post_meta( $identifier, $name ); |
|
| 141 | - } elseif ( 'taxonomy' === $type ) { |
|
| 142 | - $data['fieldValue'] = get_term_meta( $identifier, $name ); |
|
| 139 | + if ('post_type' === $type) { |
|
| 140 | + $data['fieldValue'] = get_post_meta($identifier, $name); |
|
| 141 | + } elseif ('taxonomy' === $type) { |
|
| 142 | + $data['fieldValue'] = get_term_meta($identifier, $name); |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | return $data; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - public function ajax_filter( $result, $name, $value, $field_options ) { |
|
| 148 | + public function ajax_filter($result, $name, $value, $field_options) { |
|
| 149 | 149 | |
| 150 | - if ( ! isset( $field_options['pick_object'] ) ) { |
|
| 150 | + if ( ! isset($field_options['pick_object'])) { |
|
| 151 | 151 | return $result; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | return self::FIELD_NAME === $field_options['pick_object']; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - public function table_info_filter( $info, $object_type, $object, $name, $pod, $field ) { |
|
| 157 | + public function table_info_filter($info, $object_type, $object, $name, $pod, $field) { |
|
| 158 | 158 | |
| 159 | - if ( $field === null || self::FIELD_NAME !== $field->get_arg( 'pick_object', false ) ) { |
|
| 159 | + if ($field === null || self::FIELD_NAME !== $field->get_arg('pick_object', false)) { |
|
| 160 | 160 | return $info; |
| 161 | 161 | } |
| 162 | 162 | // We need to return an non empty array here to prevent pods from querying a table. |
| 163 | 163 | // This is necessary to prevent errors on ui. |
| 164 | - return array( 'foo' => 'bar' ); |
|
| 164 | + return array('foo' => 'bar'); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - public function field_options_filter( $_, $name, $value, $options, $pod, $id, $object_params ) { |
|
| 167 | + public function field_options_filter($_, $name, $value, $options, $pod, $id, $object_params) { |
|
| 168 | 168 | |
| 169 | - $object_params = json_decode( wp_json_encode( $object_params ), true ); |
|
| 169 | + $object_params = json_decode(wp_json_encode($object_params), true); |
|
| 170 | 170 | |
| 171 | 171 | $query_service = Entity_Query_Service::get_instance(); |
| 172 | 172 | |
| 173 | - if ( is_array( $object_params ) && isset( $object_params['options']['pick_object'] ) |
|
| 174 | - && is_string( $object_params['options']['pick_object'] ) |
|
| 173 | + if (is_array($object_params) && isset($object_params['options']['pick_object']) |
|
| 174 | + && is_string($object_params['options']['pick_object']) |
|
| 175 | 175 | && self::FIELD_NAME === $object_params['options']['pick_object'] |
| 176 | - && isset( $object_params['pod']['data']['pod_data']['type'] ) |
|
| 177 | - && is_string( $object_params['pod']['data']['pod_data']['type'] ) ) { |
|
| 176 | + && isset($object_params['pod']['data']['pod_data']['type']) |
|
| 177 | + && is_string($object_params['pod']['data']['pod_data']['type'])) { |
|
| 178 | 178 | |
| 179 | 179 | $type = $object_params['pod']['data']['pod_data']['type']; |
| 180 | 180 | $linked_entities = array(); |
| 181 | - if ( 'post_type' === $type ) { |
|
| 182 | - $linked_entities = get_post_meta( $id, $name ); |
|
| 183 | - } elseif ( 'taxonomy' === $type ) { |
|
| 184 | - $linked_entities = get_term_meta( $id, $name ); |
|
| 181 | + if ('post_type' === $type) { |
|
| 182 | + $linked_entities = get_post_meta($id, $name); |
|
| 183 | + } elseif ('taxonomy' === $type) { |
|
| 184 | + $linked_entities = get_term_meta($id, $name); |
|
| 185 | 185 | } |
| 186 | 186 | $data = array(); |
| 187 | - $linked_entities = $query_service->get( $linked_entities ); |
|
| 188 | - foreach ( $linked_entities as $linked_entity ) { |
|
| 187 | + $linked_entities = $query_service->get($linked_entities); |
|
| 188 | + foreach ($linked_entities as $linked_entity) { |
|
| 189 | 189 | $content = $linked_entity->get_content(); |
| 190 | - $id = sprintf( '%s_%d', Object_Type_Enum::to_string( $content->get_object_type_enum() ), $content->get_id() ); |
|
| 191 | - $text = $linked_entity->get_title() . ' (' . $linked_entity->get_schema_type() . ')'; |
|
| 192 | - $data[ $id ] = $text; |
|
| 190 | + $id = sprintf('%s_%d', Object_Type_Enum::to_string($content->get_object_type_enum()), $content->get_id()); |
|
| 191 | + $text = $linked_entity->get_title().' ('.$linked_entity->get_schema_type().')'; |
|
| 192 | + $data[$id] = $text; |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | return $data; |