@@ -8,24 +8,24 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Term_Jsonld { |
| 10 | 10 | |
| 11 | - public function init() { |
|
| 12 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 13 | - } |
|
| 11 | + public function init() { |
|
| 12 | + add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - public function wl_term_jsonld_array( $jsonld_array, $term_id ) { |
|
| 15 | + public function wl_term_jsonld_array( $jsonld_array, $term_id ) { |
|
| 16 | 16 | |
| 17 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term_id ); |
|
| 17 | + $entities = Jsonld_Utils::get_matched_entities_for_term( $term_id ); |
|
| 18 | 18 | |
| 19 | - if ( count( $entities ) > 0 ) { |
|
| 20 | - $entity = array_shift( $entities ); |
|
| 21 | - $entity['@context'] = 'http://schema.org'; |
|
| 22 | - $entity['@id'] = get_term_link( $term_id ) . "/#id"; |
|
| 23 | - $entity['url'] = get_term_link( $term_id ); |
|
| 24 | - $entity['mainEntityOfPage'] = get_term_link( $term_id ); |
|
| 25 | - $jsonld_array['jsonld'][] = $entity; |
|
| 26 | - } |
|
| 19 | + if ( count( $entities ) > 0 ) { |
|
| 20 | + $entity = array_shift( $entities ); |
|
| 21 | + $entity['@context'] = 'http://schema.org'; |
|
| 22 | + $entity['@id'] = get_term_link( $term_id ) . "/#id"; |
|
| 23 | + $entity['url'] = get_term_link( $term_id ); |
|
| 24 | + $entity['mainEntityOfPage'] = get_term_link( $term_id ); |
|
| 25 | + $jsonld_array['jsonld'][] = $entity; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - return $jsonld_array; |
|
| 29 | - } |
|
| 28 | + return $jsonld_array; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | 31 | } |
| 32 | 32 | \ No newline at end of file |
@@ -9,19 +9,19 @@ |
||
| 9 | 9 | class Term_Jsonld { |
| 10 | 10 | |
| 11 | 11 | public function init() { |
| 12 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 12 | + add_filter('wl_term_jsonld_array', array($this, 'wl_term_jsonld_array'), 10, 2); |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | - public function wl_term_jsonld_array( $jsonld_array, $term_id ) { |
|
| 15 | + public function wl_term_jsonld_array($jsonld_array, $term_id) { |
|
| 16 | 16 | |
| 17 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term_id ); |
|
| 17 | + $entities = Jsonld_Utils::get_matched_entities_for_term($term_id); |
|
| 18 | 18 | |
| 19 | - if ( count( $entities ) > 0 ) { |
|
| 20 | - $entity = array_shift( $entities ); |
|
| 19 | + if (count($entities) > 0) { |
|
| 20 | + $entity = array_shift($entities); |
|
| 21 | 21 | $entity['@context'] = 'http://schema.org'; |
| 22 | - $entity['@id'] = get_term_link( $term_id ) . "/#id"; |
|
| 23 | - $entity['url'] = get_term_link( $term_id ); |
|
| 24 | - $entity['mainEntityOfPage'] = get_term_link( $term_id ); |
|
| 22 | + $entity['@id'] = get_term_link($term_id)."/#id"; |
|
| 23 | + $entity['url'] = get_term_link($term_id); |
|
| 24 | + $entity['mainEntityOfPage'] = get_term_link($term_id); |
|
| 25 | 25 | $jsonld_array['jsonld'][] = $entity; |
| 26 | 26 | } |
| 27 | 27 | |
@@ -12,142 +12,142 @@ |
||
| 12 | 12 | |
| 13 | 13 | class Jsonld_Generator { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var \Wordlift_Entity_Type_Service |
|
| 17 | - */ |
|
| 18 | - private $entity_type_service; |
|
| 19 | - /** |
|
| 20 | - * @var \Wordlift_Property_Getter |
|
| 21 | - */ |
|
| 22 | - private $property_getter; |
|
| 23 | - /** |
|
| 24 | - * @var Type_Service |
|
| 25 | - */ |
|
| 26 | - private $term_entity_type_service; |
|
| 27 | - /** |
|
| 28 | - * @var \Wordlift_Entity_Service |
|
| 29 | - */ |
|
| 30 | - private $entity_service; |
|
| 31 | - |
|
| 32 | - public function __construct( $entity_type_service, $property_getter ) { |
|
| 33 | - $this->entity_type_service = $entity_type_service; |
|
| 34 | - $this->property_getter = $property_getter; |
|
| 35 | - $this->term_entity_type_service = Type_Service::get_instance(); |
|
| 36 | - $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - public function init() { |
|
| 40 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 44 | - $jsonld = $data['jsonld']; |
|
| 45 | - $references = $data['references']; |
|
| 46 | - |
|
| 47 | - $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 48 | - |
|
| 49 | - // Return early if we dont have the entity data |
|
| 50 | - // for the term. |
|
| 51 | - if ( ! $term_jsonld_data ) { |
|
| 52 | - return $data; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $term_jsonld = $term_jsonld_data['jsonld']; |
|
| 56 | - |
|
| 57 | - $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - array_unshift( $jsonld, $term_jsonld ); |
|
| 61 | - |
|
| 62 | - return array( |
|
| 63 | - 'jsonld' => $jsonld, |
|
| 64 | - 'references' => $references |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - private function get_jsonld_data_for_term( $term_id ) { |
|
| 69 | - |
|
| 70 | - $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 71 | - |
|
| 72 | - // If we don't have a dataset URI, then don't publish the term data |
|
| 73 | - // on this page. |
|
| 74 | - if ( ! $id ) { |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - $references = array(); |
|
| 79 | - $term = get_term( $term_id ); |
|
| 80 | - $permalink = get_term_link( $term ); |
|
| 81 | - |
|
| 82 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 83 | - $term = get_term( $term_id ); |
|
| 84 | - $jsonld = array( |
|
| 85 | - '@context' => 'http://schema.org', |
|
| 86 | - 'name' => $term->name, |
|
| 87 | - '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 88 | - '@id' => $id, |
|
| 89 | - 'description' => $term->description, |
|
| 90 | - ); |
|
| 91 | - |
|
| 92 | - if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 93 | - return $jsonld; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - foreach ( $custom_fields as $key => $value ) { |
|
| 97 | - $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 98 | - $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 99 | - $value = $this->process_value( $value, $references ); |
|
| 100 | - if ( ! isset( $value ) || |
|
| 101 | - is_array( $value ) && empty( $value ) || |
|
| 102 | - is_string( $value ) && empty( $value ) ) { |
|
| 103 | - continue; |
|
| 104 | - } |
|
| 105 | - $jsonld[ $name ] = $value; |
|
| 106 | - |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - if ( $permalink ) { |
|
| 110 | - $jsonld['mainEntityOfPage'] = $permalink; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return apply_filters( 'wl_no_vocabulary_term_jsonld_array', array( |
|
| 114 | - 'jsonld' => $jsonld, |
|
| 115 | - 'references' => $references |
|
| 116 | - ), $term_id ); |
|
| 117 | - |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - private function relative_to_schema_context( $predicate ) { |
|
| 121 | - return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - private function process_value( $value, &$references ) { |
|
| 125 | - |
|
| 126 | - if ( is_array( $value ) |
|
| 127 | - && count( $value ) > 0 |
|
| 128 | - && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 15 | + /** |
|
| 16 | + * @var \Wordlift_Entity_Type_Service |
|
| 17 | + */ |
|
| 18 | + private $entity_type_service; |
|
| 19 | + /** |
|
| 20 | + * @var \Wordlift_Property_Getter |
|
| 21 | + */ |
|
| 22 | + private $property_getter; |
|
| 23 | + /** |
|
| 24 | + * @var Type_Service |
|
| 25 | + */ |
|
| 26 | + private $term_entity_type_service; |
|
| 27 | + /** |
|
| 28 | + * @var \Wordlift_Entity_Service |
|
| 29 | + */ |
|
| 30 | + private $entity_service; |
|
| 31 | + |
|
| 32 | + public function __construct( $entity_type_service, $property_getter ) { |
|
| 33 | + $this->entity_type_service = $entity_type_service; |
|
| 34 | + $this->property_getter = $property_getter; |
|
| 35 | + $this->term_entity_type_service = Type_Service::get_instance(); |
|
| 36 | + $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + public function init() { |
|
| 40 | + add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 44 | + $jsonld = $data['jsonld']; |
|
| 45 | + $references = $data['references']; |
|
| 46 | + |
|
| 47 | + $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 48 | + |
|
| 49 | + // Return early if we dont have the entity data |
|
| 50 | + // for the term. |
|
| 51 | + if ( ! $term_jsonld_data ) { |
|
| 52 | + return $data; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $term_jsonld = $term_jsonld_data['jsonld']; |
|
| 56 | + |
|
| 57 | + $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + array_unshift( $jsonld, $term_jsonld ); |
|
| 61 | + |
|
| 62 | + return array( |
|
| 63 | + 'jsonld' => $jsonld, |
|
| 64 | + 'references' => $references |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + private function get_jsonld_data_for_term( $term_id ) { |
|
| 69 | + |
|
| 70 | + $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 71 | + |
|
| 72 | + // If we don't have a dataset URI, then don't publish the term data |
|
| 73 | + // on this page. |
|
| 74 | + if ( ! $id ) { |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + $references = array(); |
|
| 79 | + $term = get_term( $term_id ); |
|
| 80 | + $permalink = get_term_link( $term ); |
|
| 81 | + |
|
| 82 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 83 | + $term = get_term( $term_id ); |
|
| 84 | + $jsonld = array( |
|
| 85 | + '@context' => 'http://schema.org', |
|
| 86 | + 'name' => $term->name, |
|
| 87 | + '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 88 | + '@id' => $id, |
|
| 89 | + 'description' => $term->description, |
|
| 90 | + ); |
|
| 91 | + |
|
| 92 | + if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 93 | + return $jsonld; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + foreach ( $custom_fields as $key => $value ) { |
|
| 97 | + $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 98 | + $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 99 | + $value = $this->process_value( $value, $references ); |
|
| 100 | + if ( ! isset( $value ) || |
|
| 101 | + is_array( $value ) && empty( $value ) || |
|
| 102 | + is_string( $value ) && empty( $value ) ) { |
|
| 103 | + continue; |
|
| 104 | + } |
|
| 105 | + $jsonld[ $name ] = $value; |
|
| 106 | + |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + if ( $permalink ) { |
|
| 110 | + $jsonld['mainEntityOfPage'] = $permalink; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return apply_filters( 'wl_no_vocabulary_term_jsonld_array', array( |
|
| 114 | + 'jsonld' => $jsonld, |
|
| 115 | + 'references' => $references |
|
| 116 | + ), $term_id ); |
|
| 117 | + |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + private function relative_to_schema_context( $predicate ) { |
|
| 121 | + return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + private function process_value( $value, &$references ) { |
|
| 125 | + |
|
| 126 | + if ( is_array( $value ) |
|
| 127 | + && count( $value ) > 0 |
|
| 128 | + && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 129 | 129 | |
| 130 | - // All of the references from the custom fields are post references. |
|
| 131 | - $references = array_merge( $references, array_map( function ( $property_entity_reference ) { |
|
| 132 | - /** |
|
| 133 | - * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
|
| 134 | - */ |
|
| 135 | - return new Post_Reference( $property_entity_reference->get_id() ); |
|
| 136 | - }, $value ) ); |
|
| 130 | + // All of the references from the custom fields are post references. |
|
| 131 | + $references = array_merge( $references, array_map( function ( $property_entity_reference ) { |
|
| 132 | + /** |
|
| 133 | + * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
|
| 134 | + */ |
|
| 135 | + return new Post_Reference( $property_entity_reference->get_id() ); |
|
| 136 | + }, $value ) ); |
|
| 137 | 137 | |
| 138 | 138 | |
| 139 | - $that = $this; |
|
| 139 | + $that = $this; |
|
| 140 | 140 | |
| 141 | - return array_map( function ( $reference ) use ( $that ) { |
|
| 142 | - /** |
|
| 143 | - * @var $reference \Wordlift_Property_Entity_Reference |
|
| 144 | - */ |
|
| 145 | - return array( '@id' => $that->entity_service->get_uri( $reference->get_id() ) ); |
|
| 146 | - }, $value ); |
|
| 141 | + return array_map( function ( $reference ) use ( $that ) { |
|
| 142 | + /** |
|
| 143 | + * @var $reference \Wordlift_Property_Entity_Reference |
|
| 144 | + */ |
|
| 145 | + return array( '@id' => $that->entity_service->get_uri( $reference->get_id() ) ); |
|
| 146 | + }, $value ); |
|
| 147 | 147 | |
| 148 | - } |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - return $value; |
|
| 151 | - } |
|
| 150 | + return $value; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | 153 | } |
| 154 | 154 | \ No newline at end of file |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | private $entity_service; |
| 31 | 31 | |
| 32 | - public function __construct( $entity_type_service, $property_getter ) { |
|
| 32 | + public function __construct($entity_type_service, $property_getter) { |
|
| 33 | 33 | $this->entity_type_service = $entity_type_service; |
| 34 | 34 | $this->property_getter = $property_getter; |
| 35 | 35 | $this->term_entity_type_service = Type_Service::get_instance(); |
@@ -37,27 +37,27 @@ discard block |
||
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | public function init() { |
| 40 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 40 | + add_filter('wl_term_jsonld_array', array($this, 'wl_term_jsonld_array'), 10, 2); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 43 | + public function wl_term_jsonld_array($data, $term_id) { |
|
| 44 | 44 | $jsonld = $data['jsonld']; |
| 45 | 45 | $references = $data['references']; |
| 46 | 46 | |
| 47 | - $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 47 | + $term_jsonld_data = $this->get_jsonld_data_for_term($term_id); |
|
| 48 | 48 | |
| 49 | 49 | // Return early if we dont have the entity data |
| 50 | 50 | // for the term. |
| 51 | - if ( ! $term_jsonld_data ) { |
|
| 51 | + if ( ! $term_jsonld_data) { |
|
| 52 | 52 | return $data; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | $term_jsonld = $term_jsonld_data['jsonld']; |
| 56 | 56 | |
| 57 | - $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 57 | + $references = array_merge($references, $term_jsonld_data['references']); |
|
| 58 | 58 | |
| 59 | 59 | |
| 60 | - array_unshift( $jsonld, $term_jsonld ); |
|
| 60 | + array_unshift($jsonld, $term_jsonld); |
|
| 61 | 61 | |
| 62 | 62 | return array( |
| 63 | 63 | 'jsonld' => $jsonld, |
@@ -65,85 +65,85 @@ discard block |
||
| 65 | 65 | ); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - private function get_jsonld_data_for_term( $term_id ) { |
|
| 68 | + private function get_jsonld_data_for_term($term_id) { |
|
| 69 | 69 | |
| 70 | - $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 70 | + $id = $this->entity_service->get_uri($term_id, Object_Type_Enum::TERM); |
|
| 71 | 71 | |
| 72 | 72 | // If we don't have a dataset URI, then don't publish the term data |
| 73 | 73 | // on this page. |
| 74 | - if ( ! $id ) { |
|
| 74 | + if ( ! $id) { |
|
| 75 | 75 | return false; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | $references = array(); |
| 79 | - $term = get_term( $term_id ); |
|
| 80 | - $permalink = get_term_link( $term ); |
|
| 79 | + $term = get_term($term_id); |
|
| 80 | + $permalink = get_term_link($term); |
|
| 81 | 81 | |
| 82 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 83 | - $term = get_term( $term_id ); |
|
| 82 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_term($term_id); |
|
| 83 | + $term = get_term($term_id); |
|
| 84 | 84 | $jsonld = array( |
| 85 | 85 | '@context' => 'http://schema.org', |
| 86 | 86 | 'name' => $term->name, |
| 87 | - '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 87 | + '@type' => $this->term_entity_type_service->get_entity_types_labels($term_id), |
|
| 88 | 88 | '@id' => $id, |
| 89 | 89 | 'description' => $term->description, |
| 90 | 90 | ); |
| 91 | 91 | |
| 92 | - if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 92 | + if ( ! $custom_fields || ! is_array($custom_fields)) { |
|
| 93 | 93 | return $jsonld; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - foreach ( $custom_fields as $key => $value ) { |
|
| 97 | - $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 98 | - $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 99 | - $value = $this->process_value( $value, $references ); |
|
| 100 | - if ( ! isset( $value ) || |
|
| 101 | - is_array( $value ) && empty( $value ) || |
|
| 102 | - is_string( $value ) && empty( $value ) ) { |
|
| 96 | + foreach ($custom_fields as $key => $value) { |
|
| 97 | + $name = $this->relative_to_schema_context($value['predicate']); |
|
| 98 | + $value = $this->property_getter->get($term_id, $key, Object_Type_Enum::TERM); |
|
| 99 | + $value = $this->process_value($value, $references); |
|
| 100 | + if ( ! isset($value) || |
|
| 101 | + is_array($value) && empty($value) || |
|
| 102 | + is_string($value) && empty($value)) { |
|
| 103 | 103 | continue; |
| 104 | 104 | } |
| 105 | - $jsonld[ $name ] = $value; |
|
| 105 | + $jsonld[$name] = $value; |
|
| 106 | 106 | |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - if ( $permalink ) { |
|
| 109 | + if ($permalink) { |
|
| 110 | 110 | $jsonld['mainEntityOfPage'] = $permalink; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - return apply_filters( 'wl_no_vocabulary_term_jsonld_array', array( |
|
| 113 | + return apply_filters('wl_no_vocabulary_term_jsonld_array', array( |
|
| 114 | 114 | 'jsonld' => $jsonld, |
| 115 | 115 | 'references' => $references |
| 116 | - ), $term_id ); |
|
| 116 | + ), $term_id); |
|
| 117 | 117 | |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - private function relative_to_schema_context( $predicate ) { |
|
| 121 | - return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 120 | + private function relative_to_schema_context($predicate) { |
|
| 121 | + return str_replace('http://schema.org/', '', $predicate); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - private function process_value( $value, &$references ) { |
|
| 124 | + private function process_value($value, &$references) { |
|
| 125 | 125 | |
| 126 | - if ( is_array( $value ) |
|
| 127 | - && count( $value ) > 0 |
|
| 128 | - && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 126 | + if (is_array($value) |
|
| 127 | + && count($value) > 0 |
|
| 128 | + && $value[0] instanceof \Wordlift_Property_Entity_Reference) { |
|
| 129 | 129 | |
| 130 | 130 | // All of the references from the custom fields are post references. |
| 131 | - $references = array_merge( $references, array_map( function ( $property_entity_reference ) { |
|
| 131 | + $references = array_merge($references, array_map(function($property_entity_reference) { |
|
| 132 | 132 | /** |
| 133 | 133 | * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
| 134 | 134 | */ |
| 135 | - return new Post_Reference( $property_entity_reference->get_id() ); |
|
| 136 | - }, $value ) ); |
|
| 135 | + return new Post_Reference($property_entity_reference->get_id()); |
|
| 136 | + }, $value)); |
|
| 137 | 137 | |
| 138 | 138 | |
| 139 | 139 | $that = $this; |
| 140 | 140 | |
| 141 | - return array_map( function ( $reference ) use ( $that ) { |
|
| 141 | + return array_map(function($reference) use ($that) { |
|
| 142 | 142 | /** |
| 143 | 143 | * @var $reference \Wordlift_Property_Entity_Reference |
| 144 | 144 | */ |
| 145 | - return array( '@id' => $that->entity_service->get_uri( $reference->get_id() ) ); |
|
| 146 | - }, $value ); |
|
| 145 | + return array('@id' => $that->entity_service->get_uri($reference->get_id())); |
|
| 146 | + }, $value); |
|
| 147 | 147 | |
| 148 | 148 | } |
| 149 | 149 | |
@@ -22,142 +22,142 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | class Wl_Metabox_Field_sameas extends Wl_Metabox_Field { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @inheritdoc |
|
| 27 | - */ |
|
| 28 | - public function __construct( $args, $id, $type ) { |
|
| 29 | - parent::__construct( $args['sameas'], $id, $type ); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @inheritdoc |
|
| 34 | - */ |
|
| 35 | - public function save_data( $values ) { |
|
| 36 | - // The autocomplete select may send JSON arrays in input values. |
|
| 37 | - |
|
| 38 | - // Only use mb_* functions when mbstring is available. |
|
| 39 | - // |
|
| 40 | - // See https://github.com/insideout10/wordlift-plugin/issues/693. |
|
| 41 | - if ( extension_loaded( 'mbstring' ) ) { |
|
| 42 | - mb_regex_encoding( 'UTF-8' ); |
|
| 43 | - |
|
| 44 | - $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 45 | - return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 46 | - }, array() ); |
|
| 47 | - } else { |
|
| 48 | - $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 49 | - return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) ); |
|
| 50 | - }, array() ); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - // Convert all escaped special characters to their original. |
|
| 54 | - $merged = array_map( 'urldecode', $merged ); |
|
| 55 | - |
|
| 56 | - $merged = $this->filter_urls( $merged ); |
|
| 57 | - |
|
| 58 | - parent::save_data( $merged ); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @inheritdoc |
|
| 63 | - */ |
|
| 64 | - public function sanitize_data_filter( $value ) { |
|
| 65 | - |
|
| 66 | - // Call our sanitizer helper. |
|
| 67 | - return Wordlift_Sanitizer::sanitize_url( $value ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @inheritdoc |
|
| 72 | - */ |
|
| 73 | - protected function get_heading_html() { |
|
| 74 | - |
|
| 75 | - // Add the select html fragment after the heading. |
|
| 76 | - return parent::get_heading_html() |
|
| 77 | - . $this->get_select_html(); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Get the select html fragment. |
|
| 82 | - * |
|
| 83 | - * @return string The html fragment. |
|
| 84 | - * @since 3.15.0 |
|
| 85 | - */ |
|
| 86 | - private function get_select_html() { |
|
| 87 | - // Return an element where the new Autocomplete Select will attach to. |
|
| 88 | - return '<p>' |
|
| 89 | - . esc_html__( 'Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift' ) |
|
| 90 | - . '<div id="wl-metabox-field-sameas"></div></p>'; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @inheritdoc |
|
| 95 | - */ |
|
| 96 | - protected function get_add_button_html( $count ) { |
|
| 97 | - |
|
| 98 | - $placeholder = esc_attr_x( 'Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift' ); |
|
| 99 | - |
|
| 100 | - return |
|
| 101 | - '<button type="button" class="wl-add-input wl-add-input--link">' . esc_html__( 'Click here to manually add URLs', 'wordlift' ) . '</button>' |
|
| 102 | - . '<div style="display: none;"><div class="wl-input-wrapper">' |
|
| 103 | - . "<input type='text' id='$this->meta_name' name='wl_metaboxes[$this->meta_name][]' placeholder='$placeholder' />" |
|
| 104 | - . '<button type="button" class="wl-remove-input wl-remove-input--sameas"></button>' |
|
| 105 | - . '</div></div>' |
|
| 106 | - . '<fieldset id="wl-input-container">' . $this->get_stored_values_html( $count ) . '</fieldset>' |
|
| 107 | - . parent::get_add_custom_button_html( $count, 'Add Another URL', 'hide' ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @inheritdoc |
|
| 112 | - */ |
|
| 113 | - protected function get_stored_values_html( &$count ) { |
|
| 114 | - |
|
| 115 | - return parent::get_stored_values_html( $count ); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @inheritdoc |
|
| 120 | - */ |
|
| 121 | - public function html() { |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Filter: wl_feature__enable__metabox-sameas. |
|
| 125 | - * |
|
| 126 | - * @param bool whether the sameAs metabox should be shown, defaults to true. |
|
| 127 | - * |
|
| 128 | - * @return bool |
|
| 129 | - * @since 3.29.1 |
|
| 130 | - */ |
|
| 131 | - if ( apply_filters( 'wl_feature__enable__metabox-sameas', true ) ) { |
|
| 132 | - |
|
| 133 | - // Open main <div> for the Field. |
|
| 134 | - $html = $this->html_wrapper_open(); |
|
| 135 | - |
|
| 136 | - // Label. |
|
| 137 | - $html .= $this->get_heading_html(); |
|
| 138 | - |
|
| 139 | - // print nonce. |
|
| 140 | - $html .= $this->html_nonce(); |
|
| 141 | - |
|
| 142 | - // print data loaded from DB. |
|
| 143 | - $count = 0; |
|
| 144 | - |
|
| 145 | - // If cardinality allows it, print button to add new values. |
|
| 146 | - $html .= $this->get_add_button_html( $count ); |
|
| 147 | - |
|
| 148 | - // Close the HTML wrapper. |
|
| 149 | - $html .= $this->html_wrapper_close(); |
|
| 150 | - |
|
| 151 | - return $html; |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @inheritdoc |
|
| 157 | - */ |
|
| 158 | - public function html_input( $value ) { |
|
| 159 | - @ob_start(); |
|
| 160 | - ?> |
|
| 25 | + /** |
|
| 26 | + * @inheritdoc |
|
| 27 | + */ |
|
| 28 | + public function __construct( $args, $id, $type ) { |
|
| 29 | + parent::__construct( $args['sameas'], $id, $type ); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @inheritdoc |
|
| 34 | + */ |
|
| 35 | + public function save_data( $values ) { |
|
| 36 | + // The autocomplete select may send JSON arrays in input values. |
|
| 37 | + |
|
| 38 | + // Only use mb_* functions when mbstring is available. |
|
| 39 | + // |
|
| 40 | + // See https://github.com/insideout10/wordlift-plugin/issues/693. |
|
| 41 | + if ( extension_loaded( 'mbstring' ) ) { |
|
| 42 | + mb_regex_encoding( 'UTF-8' ); |
|
| 43 | + |
|
| 44 | + $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 45 | + return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 46 | + }, array() ); |
|
| 47 | + } else { |
|
| 48 | + $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 49 | + return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) ); |
|
| 50 | + }, array() ); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + // Convert all escaped special characters to their original. |
|
| 54 | + $merged = array_map( 'urldecode', $merged ); |
|
| 55 | + |
|
| 56 | + $merged = $this->filter_urls( $merged ); |
|
| 57 | + |
|
| 58 | + parent::save_data( $merged ); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @inheritdoc |
|
| 63 | + */ |
|
| 64 | + public function sanitize_data_filter( $value ) { |
|
| 65 | + |
|
| 66 | + // Call our sanitizer helper. |
|
| 67 | + return Wordlift_Sanitizer::sanitize_url( $value ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @inheritdoc |
|
| 72 | + */ |
|
| 73 | + protected function get_heading_html() { |
|
| 74 | + |
|
| 75 | + // Add the select html fragment after the heading. |
|
| 76 | + return parent::get_heading_html() |
|
| 77 | + . $this->get_select_html(); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Get the select html fragment. |
|
| 82 | + * |
|
| 83 | + * @return string The html fragment. |
|
| 84 | + * @since 3.15.0 |
|
| 85 | + */ |
|
| 86 | + private function get_select_html() { |
|
| 87 | + // Return an element where the new Autocomplete Select will attach to. |
|
| 88 | + return '<p>' |
|
| 89 | + . esc_html__( 'Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift' ) |
|
| 90 | + . '<div id="wl-metabox-field-sameas"></div></p>'; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @inheritdoc |
|
| 95 | + */ |
|
| 96 | + protected function get_add_button_html( $count ) { |
|
| 97 | + |
|
| 98 | + $placeholder = esc_attr_x( 'Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift' ); |
|
| 99 | + |
|
| 100 | + return |
|
| 101 | + '<button type="button" class="wl-add-input wl-add-input--link">' . esc_html__( 'Click here to manually add URLs', 'wordlift' ) . '</button>' |
|
| 102 | + . '<div style="display: none;"><div class="wl-input-wrapper">' |
|
| 103 | + . "<input type='text' id='$this->meta_name' name='wl_metaboxes[$this->meta_name][]' placeholder='$placeholder' />" |
|
| 104 | + . '<button type="button" class="wl-remove-input wl-remove-input--sameas"></button>' |
|
| 105 | + . '</div></div>' |
|
| 106 | + . '<fieldset id="wl-input-container">' . $this->get_stored_values_html( $count ) . '</fieldset>' |
|
| 107 | + . parent::get_add_custom_button_html( $count, 'Add Another URL', 'hide' ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @inheritdoc |
|
| 112 | + */ |
|
| 113 | + protected function get_stored_values_html( &$count ) { |
|
| 114 | + |
|
| 115 | + return parent::get_stored_values_html( $count ); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @inheritdoc |
|
| 120 | + */ |
|
| 121 | + public function html() { |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Filter: wl_feature__enable__metabox-sameas. |
|
| 125 | + * |
|
| 126 | + * @param bool whether the sameAs metabox should be shown, defaults to true. |
|
| 127 | + * |
|
| 128 | + * @return bool |
|
| 129 | + * @since 3.29.1 |
|
| 130 | + */ |
|
| 131 | + if ( apply_filters( 'wl_feature__enable__metabox-sameas', true ) ) { |
|
| 132 | + |
|
| 133 | + // Open main <div> for the Field. |
|
| 134 | + $html = $this->html_wrapper_open(); |
|
| 135 | + |
|
| 136 | + // Label. |
|
| 137 | + $html .= $this->get_heading_html(); |
|
| 138 | + |
|
| 139 | + // print nonce. |
|
| 140 | + $html .= $this->html_nonce(); |
|
| 141 | + |
|
| 142 | + // print data loaded from DB. |
|
| 143 | + $count = 0; |
|
| 144 | + |
|
| 145 | + // If cardinality allows it, print button to add new values. |
|
| 146 | + $html .= $this->get_add_button_html( $count ); |
|
| 147 | + |
|
| 148 | + // Close the HTML wrapper. |
|
| 149 | + $html .= $this->html_wrapper_close(); |
|
| 150 | + |
|
| 151 | + return $html; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @inheritdoc |
|
| 157 | + */ |
|
| 158 | + public function html_input( $value ) { |
|
| 159 | + @ob_start(); |
|
| 160 | + ?> |
|
| 161 | 161 | <div class="wl-input-wrapper wl-input-wrapper-readonly"> |
| 162 | 162 | <input |
| 163 | 163 | type="text" |
@@ -171,29 +171,29 @@ discard block |
||
| 171 | 171 | </div> |
| 172 | 172 | <?php |
| 173 | 173 | |
| 174 | - $html = ob_get_clean(); |
|
| 175 | - |
|
| 176 | - return $html; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @param array $urls |
|
| 181 | - * |
|
| 182 | - * @return array |
|
| 183 | - */ |
|
| 184 | - private function filter_urls( $urls ) { |
|
| 185 | - $configuration_service = Wordlift_Configuration_Service::get_instance(); |
|
| 186 | - $dataset_uri = $configuration_service->get_dataset_uri(); |
|
| 187 | - |
|
| 188 | - return array_filter( $urls, function ( $url ) use ( $dataset_uri ) { |
|
| 189 | - $url_validation = filter_var( $url, FILTER_VALIDATE_URL ); |
|
| 190 | - if ( null === $dataset_uri ) { |
|
| 191 | - return $url_validation; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - // URLs should not start with local dataset uri. |
|
| 195 | - return $url_validation && ( empty( $dataset_uri ) || 0 !== strpos( $url, $dataset_uri ) ); |
|
| 196 | - } ); |
|
| 197 | - } |
|
| 174 | + $html = ob_get_clean(); |
|
| 175 | + |
|
| 176 | + return $html; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @param array $urls |
|
| 181 | + * |
|
| 182 | + * @return array |
|
| 183 | + */ |
|
| 184 | + private function filter_urls( $urls ) { |
|
| 185 | + $configuration_service = Wordlift_Configuration_Service::get_instance(); |
|
| 186 | + $dataset_uri = $configuration_service->get_dataset_uri(); |
|
| 187 | + |
|
| 188 | + return array_filter( $urls, function ( $url ) use ( $dataset_uri ) { |
|
| 189 | + $url_validation = filter_var( $url, FILTER_VALIDATE_URL ); |
|
| 190 | + if ( null === $dataset_uri ) { |
|
| 191 | + return $url_validation; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + // URLs should not start with local dataset uri. |
|
| 195 | + return $url_validation && ( empty( $dataset_uri ) || 0 !== strpos( $url, $dataset_uri ) ); |
|
| 196 | + } ); |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | 199 | } |
@@ -25,46 +25,46 @@ discard block |
||
| 25 | 25 | /** |
| 26 | 26 | * @inheritdoc |
| 27 | 27 | */ |
| 28 | - public function __construct( $args, $id, $type ) { |
|
| 29 | - parent::__construct( $args['sameas'], $id, $type ); |
|
| 28 | + public function __construct($args, $id, $type) { |
|
| 29 | + parent::__construct($args['sameas'], $id, $type); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | 33 | * @inheritdoc |
| 34 | 34 | */ |
| 35 | - public function save_data( $values ) { |
|
| 35 | + public function save_data($values) { |
|
| 36 | 36 | // The autocomplete select may send JSON arrays in input values. |
| 37 | 37 | |
| 38 | 38 | // Only use mb_* functions when mbstring is available. |
| 39 | 39 | // |
| 40 | 40 | // See https://github.com/insideout10/wordlift-plugin/issues/693. |
| 41 | - if ( extension_loaded( 'mbstring' ) ) { |
|
| 42 | - mb_regex_encoding( 'UTF-8' ); |
|
| 41 | + if (extension_loaded('mbstring')) { |
|
| 42 | + mb_regex_encoding('UTF-8'); |
|
| 43 | 43 | |
| 44 | - $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 45 | - return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 46 | - }, array() ); |
|
| 44 | + $merged = array_reduce((array) $values, function($carry, $item) { |
|
| 45 | + return array_merge($carry, mb_split("\x{2063}", wp_unslash($item))); |
|
| 46 | + }, array()); |
|
| 47 | 47 | } else { |
| 48 | - $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 49 | - return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) ); |
|
| 50 | - }, array() ); |
|
| 48 | + $merged = array_reduce((array) $values, function($carry, $item) { |
|
| 49 | + return array_merge($carry, preg_split("/\x{2063}/u", wp_unslash($item))); |
|
| 50 | + }, array()); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // Convert all escaped special characters to their original. |
| 54 | - $merged = array_map( 'urldecode', $merged ); |
|
| 54 | + $merged = array_map('urldecode', $merged); |
|
| 55 | 55 | |
| 56 | - $merged = $this->filter_urls( $merged ); |
|
| 56 | + $merged = $this->filter_urls($merged); |
|
| 57 | 57 | |
| 58 | - parent::save_data( $merged ); |
|
| 58 | + parent::save_data($merged); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
| 62 | 62 | * @inheritdoc |
| 63 | 63 | */ |
| 64 | - public function sanitize_data_filter( $value ) { |
|
| 64 | + public function sanitize_data_filter($value) { |
|
| 65 | 65 | |
| 66 | 66 | // Call our sanitizer helper. |
| 67 | - return Wordlift_Sanitizer::sanitize_url( $value ); |
|
| 67 | + return Wordlift_Sanitizer::sanitize_url($value); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -86,33 +86,33 @@ discard block |
||
| 86 | 86 | private function get_select_html() { |
| 87 | 87 | // Return an element where the new Autocomplete Select will attach to. |
| 88 | 88 | return '<p>' |
| 89 | - . esc_html__( 'Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift' ) |
|
| 89 | + . esc_html__('Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift') |
|
| 90 | 90 | . '<div id="wl-metabox-field-sameas"></div></p>'; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | 94 | * @inheritdoc |
| 95 | 95 | */ |
| 96 | - protected function get_add_button_html( $count ) { |
|
| 96 | + protected function get_add_button_html($count) { |
|
| 97 | 97 | |
| 98 | - $placeholder = esc_attr_x( 'Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift' ); |
|
| 98 | + $placeholder = esc_attr_x('Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift'); |
|
| 99 | 99 | |
| 100 | 100 | return |
| 101 | - '<button type="button" class="wl-add-input wl-add-input--link">' . esc_html__( 'Click here to manually add URLs', 'wordlift' ) . '</button>' |
|
| 101 | + '<button type="button" class="wl-add-input wl-add-input--link">'.esc_html__('Click here to manually add URLs', 'wordlift').'</button>' |
|
| 102 | 102 | . '<div style="display: none;"><div class="wl-input-wrapper">' |
| 103 | 103 | . "<input type='text' id='$this->meta_name' name='wl_metaboxes[$this->meta_name][]' placeholder='$placeholder' />" |
| 104 | 104 | . '<button type="button" class="wl-remove-input wl-remove-input--sameas"></button>' |
| 105 | 105 | . '</div></div>' |
| 106 | - . '<fieldset id="wl-input-container">' . $this->get_stored_values_html( $count ) . '</fieldset>' |
|
| 107 | - . parent::get_add_custom_button_html( $count, 'Add Another URL', 'hide' ); |
|
| 106 | + . '<fieldset id="wl-input-container">'.$this->get_stored_values_html($count).'</fieldset>' |
|
| 107 | + . parent::get_add_custom_button_html($count, 'Add Another URL', 'hide'); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
| 111 | 111 | * @inheritdoc |
| 112 | 112 | */ |
| 113 | - protected function get_stored_values_html( &$count ) { |
|
| 113 | + protected function get_stored_values_html(&$count) { |
|
| 114 | 114 | |
| 115 | - return parent::get_stored_values_html( $count ); |
|
| 115 | + return parent::get_stored_values_html($count); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | /** |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | * @return bool |
| 129 | 129 | * @since 3.29.1 |
| 130 | 130 | */ |
| 131 | - if ( apply_filters( 'wl_feature__enable__metabox-sameas', true ) ) { |
|
| 131 | + if (apply_filters('wl_feature__enable__metabox-sameas', true)) { |
|
| 132 | 132 | |
| 133 | 133 | // Open main <div> for the Field. |
| 134 | 134 | $html = $this->html_wrapper_open(); |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | $count = 0; |
| 144 | 144 | |
| 145 | 145 | // If cardinality allows it, print button to add new values. |
| 146 | - $html .= $this->get_add_button_html( $count ); |
|
| 146 | + $html .= $this->get_add_button_html($count); |
|
| 147 | 147 | |
| 148 | 148 | // Close the HTML wrapper. |
| 149 | 149 | $html .= $this->html_wrapper_close(); |
@@ -155,16 +155,16 @@ discard block |
||
| 155 | 155 | /** |
| 156 | 156 | * @inheritdoc |
| 157 | 157 | */ |
| 158 | - public function html_input( $value ) { |
|
| 158 | + public function html_input($value) { |
|
| 159 | 159 | @ob_start(); |
| 160 | 160 | ?> |
| 161 | 161 | <div class="wl-input-wrapper wl-input-wrapper-readonly"> |
| 162 | 162 | <input |
| 163 | 163 | type="text" |
| 164 | 164 | readonly="readonly" |
| 165 | - id="<?php echo esc_attr( $this->meta_name ); ?>" |
|
| 166 | - name="wl_metaboxes[<?php echo esc_attr( $this->meta_name ); ?>][]" |
|
| 167 | - value="<?php echo esc_attr( $value ); ?>" |
|
| 165 | + id="<?php echo esc_attr($this->meta_name); ?>" |
|
| 166 | + name="wl_metaboxes[<?php echo esc_attr($this->meta_name); ?>][]" |
|
| 167 | + value="<?php echo esc_attr($value); ?>" |
|
| 168 | 168 | /> |
| 169 | 169 | |
| 170 | 170 | <button class="wl-remove-input wl-remove-input--sameas"></button> |
@@ -181,18 +181,18 @@ discard block |
||
| 181 | 181 | * |
| 182 | 182 | * @return array |
| 183 | 183 | */ |
| 184 | - private function filter_urls( $urls ) { |
|
| 184 | + private function filter_urls($urls) { |
|
| 185 | 185 | $configuration_service = Wordlift_Configuration_Service::get_instance(); |
| 186 | 186 | $dataset_uri = $configuration_service->get_dataset_uri(); |
| 187 | 187 | |
| 188 | - return array_filter( $urls, function ( $url ) use ( $dataset_uri ) { |
|
| 189 | - $url_validation = filter_var( $url, FILTER_VALIDATE_URL ); |
|
| 190 | - if ( null === $dataset_uri ) { |
|
| 188 | + return array_filter($urls, function($url) use ($dataset_uri) { |
|
| 189 | + $url_validation = filter_var($url, FILTER_VALIDATE_URL); |
|
| 190 | + if (null === $dataset_uri) { |
|
| 191 | 191 | return $url_validation; |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | // URLs should not start with local dataset uri. |
| 195 | - return $url_validation && ( empty( $dataset_uri ) || 0 !== strpos( $url, $dataset_uri ) ); |
|
| 195 | + return $url_validation && (empty($dataset_uri) || 0 !== strpos($url, $dataset_uri)); |
|
| 196 | 196 | } ); |
| 197 | 197 | } |
| 198 | 198 | |
@@ -21,499 +21,499 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Entity_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The Log service. |
|
| 26 | - * |
|
| 27 | - * @since 3.2.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 30 | - */ |
|
| 31 | - private $log; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.15.0 |
|
| 37 | - * @access private |
|
| 38 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 39 | - */ |
|
| 40 | - private $relation_service; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.16.3 |
|
| 46 | - * @access private |
|
| 47 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 48 | - */ |
|
| 49 | - private $entity_uri_service; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The entity post type name. |
|
| 53 | - * |
|
| 54 | - * @since 3.1.0 |
|
| 55 | - */ |
|
| 56 | - const TYPE_NAME = 'entity'; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * The alternative label meta key. |
|
| 60 | - * |
|
| 61 | - * @since 3.2.0 |
|
| 62 | - */ |
|
| 63 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * The alternative label input template. |
|
| 67 | - * |
|
| 68 | - * @since 3.2.0 |
|
| 69 | - */ |
|
| 70 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 71 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 24 | + /** |
|
| 25 | + * The Log service. |
|
| 26 | + * |
|
| 27 | + * @since 3.2.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 30 | + */ |
|
| 31 | + private $log; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.15.0 |
|
| 37 | + * @access private |
|
| 38 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 39 | + */ |
|
| 40 | + private $relation_service; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.16.3 |
|
| 46 | + * @access private |
|
| 47 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 48 | + */ |
|
| 49 | + private $entity_uri_service; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The entity post type name. |
|
| 53 | + * |
|
| 54 | + * @since 3.1.0 |
|
| 55 | + */ |
|
| 56 | + const TYPE_NAME = 'entity'; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * The alternative label meta key. |
|
| 60 | + * |
|
| 61 | + * @since 3.2.0 |
|
| 62 | + */ |
|
| 63 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * The alternative label input template. |
|
| 67 | + * |
|
| 68 | + * @since 3.2.0 |
|
| 69 | + */ |
|
| 70 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 71 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 72 | 72 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 73 | 73 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 74 | 74 | <button class="button wl-delete-button">%s</button> |
| 75 | 75 | </div>'; |
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Create a Wordlift_Entity_Service instance. |
|
| 79 | - * |
|
| 80 | - * @throws Exception if the `$content_service` is not of the `Content_Service` type. |
|
| 81 | - * @since 3.2.0 |
|
| 82 | - */ |
|
| 83 | - protected function __construct() { |
|
| 84 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 85 | - |
|
| 86 | - $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
| 87 | - $this->relation_service = Wordlift_Relation_Service::get_instance(); |
|
| 88 | - |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * A singleton instance of the Entity service. |
|
| 93 | - * |
|
| 94 | - * @since 3.2.0 |
|
| 95 | - * @access private |
|
| 96 | - * @var Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 97 | - */ |
|
| 98 | - private static $instance = null; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get the singleton instance of the Entity service. |
|
| 102 | - * |
|
| 103 | - * @return Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 104 | - * @since 3.2.0 |
|
| 105 | - */ |
|
| 106 | - public static function get_instance() { |
|
| 107 | - |
|
| 108 | - if ( ! isset( self::$instance ) ) { |
|
| 109 | - self::$instance = new self(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return self::$instance; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 117 | - * something which is not an article. |
|
| 118 | - * |
|
| 119 | - * @param int $post_id A post id. |
|
| 120 | - * |
|
| 121 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 122 | - * @since 3.1.0 |
|
| 123 | - * |
|
| 124 | - */ |
|
| 125 | - public function is_entity( $post_id ) { |
|
| 126 | - |
|
| 127 | - // Improve performance by giving for granted that a product is an entity. |
|
| 128 | - if ( 'product' === get_post_type( $post_id ) ) { |
|
| 129 | - return true; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 133 | - |
|
| 134 | - if ( is_wp_error( $terms ) ) { |
|
| 135 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 136 | - |
|
| 137 | - return false; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if ( empty( $terms ) ) { |
|
| 141 | - return false; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /* |
|
| 77 | + /** |
|
| 78 | + * Create a Wordlift_Entity_Service instance. |
|
| 79 | + * |
|
| 80 | + * @throws Exception if the `$content_service` is not of the `Content_Service` type. |
|
| 81 | + * @since 3.2.0 |
|
| 82 | + */ |
|
| 83 | + protected function __construct() { |
|
| 84 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 85 | + |
|
| 86 | + $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
| 87 | + $this->relation_service = Wordlift_Relation_Service::get_instance(); |
|
| 88 | + |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * A singleton instance of the Entity service. |
|
| 93 | + * |
|
| 94 | + * @since 3.2.0 |
|
| 95 | + * @access private |
|
| 96 | + * @var Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 97 | + */ |
|
| 98 | + private static $instance = null; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get the singleton instance of the Entity service. |
|
| 102 | + * |
|
| 103 | + * @return Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 104 | + * @since 3.2.0 |
|
| 105 | + */ |
|
| 106 | + public static function get_instance() { |
|
| 107 | + |
|
| 108 | + if ( ! isset( self::$instance ) ) { |
|
| 109 | + self::$instance = new self(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return self::$instance; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 117 | + * something which is not an article. |
|
| 118 | + * |
|
| 119 | + * @param int $post_id A post id. |
|
| 120 | + * |
|
| 121 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 122 | + * @since 3.1.0 |
|
| 123 | + * |
|
| 124 | + */ |
|
| 125 | + public function is_entity( $post_id ) { |
|
| 126 | + |
|
| 127 | + // Improve performance by giving for granted that a product is an entity. |
|
| 128 | + if ( 'product' === get_post_type( $post_id ) ) { |
|
| 129 | + return true; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 133 | + |
|
| 134 | + if ( is_wp_error( $terms ) ) { |
|
| 135 | + $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 136 | + |
|
| 137 | + return false; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if ( empty( $terms ) ) { |
|
| 141 | + return false; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /* |
|
| 145 | 145 | * We don't consider an `article` to be an entity. |
| 146 | 146 | * |
| 147 | 147 | * @since 3.20.0 At least one associated mustn't be an `article`. |
| 148 | 148 | * |
| 149 | 149 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 150 | 150 | */ |
| 151 | - foreach ( $terms as $term ) { |
|
| 152 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 153 | - return true; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return false; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Get the proper classification scope for a given entity post |
|
| 162 | - * |
|
| 163 | - * @param integer $post_id An entity post id. |
|
| 164 | - * |
|
| 165 | - * @param string $default The default classification scope, `what` if not |
|
| 166 | - * provided. |
|
| 167 | - * |
|
| 168 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 169 | - * @since 3.5.0 |
|
| 170 | - * |
|
| 171 | - */ |
|
| 172 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 173 | - |
|
| 174 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 175 | - return $default; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - // Retrieve the entity type |
|
| 179 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 180 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 181 | - // Retrieve classification boxes configuration |
|
| 182 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 183 | - foreach ( $classification_boxes as $cb ) { |
|
| 184 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 185 | - return $cb['id']; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - return $default; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Check whether a {@link WP_Post} is used. |
|
| 194 | - * |
|
| 195 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 196 | - * |
|
| 197 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 198 | - */ |
|
| 199 | - public function is_used( $post_id ) { |
|
| 200 | - |
|
| 201 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 202 | - return null; |
|
| 203 | - } |
|
| 204 | - // Retrieve the post |
|
| 205 | - $entity = get_post( $post_id ); |
|
| 206 | - |
|
| 207 | - global $wpdb; |
|
| 208 | - // Retrieve Wordlift relation instances table name |
|
| 209 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 210 | - |
|
| 211 | - // Check is it's referenced / related to another post / entity |
|
| 212 | - $stmt = $wpdb->prepare( |
|
| 213 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 214 | - $entity->ID |
|
| 215 | - ); |
|
| 216 | - |
|
| 217 | - // Perform the query |
|
| 218 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 219 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 220 | - if ( 0 < $relation_instances ) { |
|
| 221 | - return true; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - // Check if the entity uri is used as meta_value |
|
| 225 | - $stmt = $wpdb->prepare( |
|
| 226 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 227 | - $entity->ID, |
|
| 228 | - wl_get_entity_uri( $entity->ID ) |
|
| 229 | - ); |
|
| 230 | - // Perform the query |
|
| 231 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 232 | - |
|
| 233 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 234 | - if ( 0 < $meta_instances ) { |
|
| 235 | - return true; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - // If we are here, it means the current entity is not used at the moment |
|
| 239 | - return false; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 244 | - * |
|
| 245 | - * @param string $uri The entity URI. |
|
| 246 | - * |
|
| 247 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 248 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 249 | - * |
|
| 250 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 251 | - * @since 3.2.0 |
|
| 252 | - * |
|
| 253 | - */ |
|
| 254 | - public function get_entity_post_by_uri( $uri ) { |
|
| 255 | - |
|
| 256 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 261 | - * we check that the post we're saving is the current post. |
|
| 262 | - * |
|
| 263 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 264 | - * |
|
| 265 | - * @since 3.2.0 |
|
| 266 | - * |
|
| 267 | - * @param int $post_id Post ID. |
|
| 268 | - * @param WP_Post $post Post object. |
|
| 269 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 270 | - */ |
|
| 271 | - public function save_post( $post_id, $post, $update ) { |
|
| 272 | - |
|
| 273 | - // Avoid doing anything if post is autosave or a revision. |
|
| 274 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 275 | - return; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // We're setting the alternative label that have been provided via the UI |
|
| 279 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 280 | - // programmatically by some other function: we need to check therefore if |
|
| 281 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 282 | - // |
|
| 283 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 284 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 285 | - return; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - // Get the alt labels from the request (or empty array). |
|
| 289 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 290 | - |
|
| 291 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 292 | - // This is via classic editor, so set the alternative labels. |
|
| 293 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Set the alternative labels. |
|
| 301 | - * |
|
| 302 | - * @param int $post_id The post id. |
|
| 303 | - * @param array $alt_labels An array of labels. |
|
| 304 | - * |
|
| 305 | - * @since 3.2.0 |
|
| 306 | - * |
|
| 307 | - */ |
|
| 308 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 309 | - |
|
| 310 | - // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 311 | - // instead of post id. |
|
| 312 | - if ( ! is_numeric( $post_id ) ) { |
|
| 313 | - return; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - // Force $alt_labels to be an array |
|
| 317 | - if ( ! is_array( $alt_labels ) ) { |
|
| 318 | - $alt_labels = array( $alt_labels ); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 322 | - |
|
| 323 | - // Delete all the existing alternate labels. |
|
| 324 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 325 | - |
|
| 326 | - // Save only unique synonymns. |
|
| 327 | - $alt_labels = array_unique( $alt_labels ); |
|
| 328 | - |
|
| 329 | - // Set the alternative labels. |
|
| 330 | - foreach ( $alt_labels as $alt_label ) { |
|
| 331 | - |
|
| 332 | - // Strip html code from synonym. |
|
| 333 | - $alt_label = wp_strip_all_tags( $alt_label ); |
|
| 334 | - |
|
| 335 | - if ( ! empty( $alt_label ) ) { |
|
| 336 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
| 343 | - |
|
| 344 | - $merged_labels = $this->get_alternative_labels( $post_id ); |
|
| 345 | - |
|
| 346 | - // Append new synonyms to the end. |
|
| 347 | - $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
| 348 | - |
|
| 349 | - $this->set_alternative_labels( $post_id, $merged_labels ); |
|
| 350 | - |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Retrieve the alternate labels. |
|
| 355 | - * |
|
| 356 | - * @param int $post_id Post id. |
|
| 357 | - * |
|
| 358 | - * @return mixed An array of alternative labels. |
|
| 359 | - * @since 3.2.0 |
|
| 360 | - * |
|
| 361 | - */ |
|
| 362 | - public function get_alternative_labels( $post_id ) { |
|
| 363 | - |
|
| 364 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 369 | - * |
|
| 370 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 371 | - * @param int $object_type The object type {@link Object_Type_Enum} |
|
| 372 | - * |
|
| 373 | - * @return array An array with the entity title and labels. |
|
| 374 | - * @since 3.12.0 |
|
| 375 | - */ |
|
| 376 | - public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 377 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
| 378 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - // Term Reference dont have synonyms yet. |
|
| 382 | - return array(); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 387 | - * |
|
| 388 | - * @param WP_Post $post Post object. |
|
| 389 | - * |
|
| 390 | - * @since 3.2.0 |
|
| 391 | - * |
|
| 392 | - */ |
|
| 393 | - public function edit_form_before_permalink( $post ) { |
|
| 394 | - |
|
| 395 | - // If it's not an entity, return. |
|
| 396 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 397 | - return; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - // If disabled by filter, return. |
|
| 401 | - if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 402 | - return; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - // Print the input template. |
|
| 406 | - Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 407 | - |
|
| 408 | - // Print all the currently set alternative labels. |
|
| 409 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 410 | - |
|
| 411 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 412 | - |
|
| 413 | - }; |
|
| 414 | - |
|
| 415 | - // Print the button. |
|
| 416 | - Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 417 | - |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 421 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
| 422 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 423 | - $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
|
| 424 | - |
|
| 425 | - if ( ! isset( $entity_id ) || |
|
| 426 | - ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
| 427 | - $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
| 428 | - try { |
|
| 429 | - $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
| 430 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 431 | - } catch ( Exception $e ) { |
|
| 432 | - return null; |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - return $entity_id; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Get the alternative label input HTML code. |
|
| 441 | - * |
|
| 442 | - * @param string $value The input value. |
|
| 443 | - * |
|
| 444 | - * @return string The input HTML code. |
|
| 445 | - * @since 3.2.0 |
|
| 446 | - * |
|
| 447 | - */ |
|
| 448 | - private function get_alternative_label_input( $value = '' ) { |
|
| 449 | - |
|
| 450 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Get the number of entity posts published in this blog. |
|
| 455 | - * |
|
| 456 | - * @return int The number of published entity posts. |
|
| 457 | - * @since 3.6.0 |
|
| 458 | - * |
|
| 459 | - */ |
|
| 460 | - public function count() { |
|
| 461 | - global $wpdb; |
|
| 462 | - |
|
| 463 | - // Try to get the count from the transient. |
|
| 464 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 465 | - if ( false !== $count ) { |
|
| 466 | - return $count; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - // Query the count. |
|
| 470 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 471 | - "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 472 | - . " FROM {$wpdb->term_relationships} tr" |
|
| 473 | - . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 474 | - . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 475 | - . " INNER JOIN {$wpdb->terms} t" |
|
| 476 | - . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 477 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 478 | - 'article' |
|
| 479 | - ) ); |
|
| 480 | - |
|
| 481 | - // Store the count in cache. |
|
| 482 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 483 | - |
|
| 484 | - return $count; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 489 | - * call. |
|
| 490 | - * |
|
| 491 | - * @param array $args The arguments for a `get_posts` call. |
|
| 492 | - * |
|
| 493 | - * @return array The arguments for a `get_posts` call. |
|
| 494 | - * @since 3.15.0 |
|
| 495 | - * |
|
| 496 | - */ |
|
| 497 | - public static function add_criterias( $args ) { |
|
| 498 | - |
|
| 499 | - // Build an optimal tax-query. |
|
| 500 | - $tax_query = array( |
|
| 501 | - 'relation' => 'AND', |
|
| 502 | - array( |
|
| 503 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 504 | - 'operator' => 'EXISTS', |
|
| 505 | - ), |
|
| 506 | - array( |
|
| 507 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 508 | - 'field' => 'slug', |
|
| 509 | - 'terms' => 'article', |
|
| 510 | - 'operator' => 'NOT IN', |
|
| 511 | - ), |
|
| 512 | - ); |
|
| 513 | - |
|
| 514 | - return $args + array( |
|
| 515 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 516 | - /* |
|
| 151 | + foreach ( $terms as $term ) { |
|
| 152 | + if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 153 | + return true; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return false; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Get the proper classification scope for a given entity post |
|
| 162 | + * |
|
| 163 | + * @param integer $post_id An entity post id. |
|
| 164 | + * |
|
| 165 | + * @param string $default The default classification scope, `what` if not |
|
| 166 | + * provided. |
|
| 167 | + * |
|
| 168 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 169 | + * @since 3.5.0 |
|
| 170 | + * |
|
| 171 | + */ |
|
| 172 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 173 | + |
|
| 174 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 175 | + return $default; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + // Retrieve the entity type |
|
| 179 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 180 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 181 | + // Retrieve classification boxes configuration |
|
| 182 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 183 | + foreach ( $classification_boxes as $cb ) { |
|
| 184 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 185 | + return $cb['id']; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + return $default; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Check whether a {@link WP_Post} is used. |
|
| 194 | + * |
|
| 195 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 196 | + * |
|
| 197 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 198 | + */ |
|
| 199 | + public function is_used( $post_id ) { |
|
| 200 | + |
|
| 201 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 202 | + return null; |
|
| 203 | + } |
|
| 204 | + // Retrieve the post |
|
| 205 | + $entity = get_post( $post_id ); |
|
| 206 | + |
|
| 207 | + global $wpdb; |
|
| 208 | + // Retrieve Wordlift relation instances table name |
|
| 209 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 210 | + |
|
| 211 | + // Check is it's referenced / related to another post / entity |
|
| 212 | + $stmt = $wpdb->prepare( |
|
| 213 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 214 | + $entity->ID |
|
| 215 | + ); |
|
| 216 | + |
|
| 217 | + // Perform the query |
|
| 218 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 219 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 220 | + if ( 0 < $relation_instances ) { |
|
| 221 | + return true; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + // Check if the entity uri is used as meta_value |
|
| 225 | + $stmt = $wpdb->prepare( |
|
| 226 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 227 | + $entity->ID, |
|
| 228 | + wl_get_entity_uri( $entity->ID ) |
|
| 229 | + ); |
|
| 230 | + // Perform the query |
|
| 231 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 232 | + |
|
| 233 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 234 | + if ( 0 < $meta_instances ) { |
|
| 235 | + return true; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + // If we are here, it means the current entity is not used at the moment |
|
| 239 | + return false; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 244 | + * |
|
| 245 | + * @param string $uri The entity URI. |
|
| 246 | + * |
|
| 247 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 248 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 249 | + * |
|
| 250 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 251 | + * @since 3.2.0 |
|
| 252 | + * |
|
| 253 | + */ |
|
| 254 | + public function get_entity_post_by_uri( $uri ) { |
|
| 255 | + |
|
| 256 | + return $this->entity_uri_service->get_entity( $uri ); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 261 | + * we check that the post we're saving is the current post. |
|
| 262 | + * |
|
| 263 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 264 | + * |
|
| 265 | + * @since 3.2.0 |
|
| 266 | + * |
|
| 267 | + * @param int $post_id Post ID. |
|
| 268 | + * @param WP_Post $post Post object. |
|
| 269 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 270 | + */ |
|
| 271 | + public function save_post( $post_id, $post, $update ) { |
|
| 272 | + |
|
| 273 | + // Avoid doing anything if post is autosave or a revision. |
|
| 274 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 275 | + return; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // We're setting the alternative label that have been provided via the UI |
|
| 279 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 280 | + // programmatically by some other function: we need to check therefore if |
|
| 281 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 282 | + // |
|
| 283 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 284 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 285 | + return; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + // Get the alt labels from the request (or empty array). |
|
| 289 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 290 | + |
|
| 291 | + if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 292 | + // This is via classic editor, so set the alternative labels. |
|
| 293 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Set the alternative labels. |
|
| 301 | + * |
|
| 302 | + * @param int $post_id The post id. |
|
| 303 | + * @param array $alt_labels An array of labels. |
|
| 304 | + * |
|
| 305 | + * @since 3.2.0 |
|
| 306 | + * |
|
| 307 | + */ |
|
| 308 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 309 | + |
|
| 310 | + // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 311 | + // instead of post id. |
|
| 312 | + if ( ! is_numeric( $post_id ) ) { |
|
| 313 | + return; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + // Force $alt_labels to be an array |
|
| 317 | + if ( ! is_array( $alt_labels ) ) { |
|
| 318 | + $alt_labels = array( $alt_labels ); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 322 | + |
|
| 323 | + // Delete all the existing alternate labels. |
|
| 324 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 325 | + |
|
| 326 | + // Save only unique synonymns. |
|
| 327 | + $alt_labels = array_unique( $alt_labels ); |
|
| 328 | + |
|
| 329 | + // Set the alternative labels. |
|
| 330 | + foreach ( $alt_labels as $alt_label ) { |
|
| 331 | + |
|
| 332 | + // Strip html code from synonym. |
|
| 333 | + $alt_label = wp_strip_all_tags( $alt_label ); |
|
| 334 | + |
|
| 335 | + if ( ! empty( $alt_label ) ) { |
|
| 336 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
| 343 | + |
|
| 344 | + $merged_labels = $this->get_alternative_labels( $post_id ); |
|
| 345 | + |
|
| 346 | + // Append new synonyms to the end. |
|
| 347 | + $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
| 348 | + |
|
| 349 | + $this->set_alternative_labels( $post_id, $merged_labels ); |
|
| 350 | + |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Retrieve the alternate labels. |
|
| 355 | + * |
|
| 356 | + * @param int $post_id Post id. |
|
| 357 | + * |
|
| 358 | + * @return mixed An array of alternative labels. |
|
| 359 | + * @since 3.2.0 |
|
| 360 | + * |
|
| 361 | + */ |
|
| 362 | + public function get_alternative_labels( $post_id ) { |
|
| 363 | + |
|
| 364 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 369 | + * |
|
| 370 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 371 | + * @param int $object_type The object type {@link Object_Type_Enum} |
|
| 372 | + * |
|
| 373 | + * @return array An array with the entity title and labels. |
|
| 374 | + * @since 3.12.0 |
|
| 375 | + */ |
|
| 376 | + public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 377 | + if ( $object_type === Object_Type_Enum::POST ) { |
|
| 378 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + // Term Reference dont have synonyms yet. |
|
| 382 | + return array(); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 387 | + * |
|
| 388 | + * @param WP_Post $post Post object. |
|
| 389 | + * |
|
| 390 | + * @since 3.2.0 |
|
| 391 | + * |
|
| 392 | + */ |
|
| 393 | + public function edit_form_before_permalink( $post ) { |
|
| 394 | + |
|
| 395 | + // If it's not an entity, return. |
|
| 396 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 397 | + return; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + // If disabled by filter, return. |
|
| 401 | + if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 402 | + return; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + // Print the input template. |
|
| 406 | + Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 407 | + |
|
| 408 | + // Print all the currently set alternative labels. |
|
| 409 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 410 | + |
|
| 411 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 412 | + |
|
| 413 | + }; |
|
| 414 | + |
|
| 415 | + // Print the button. |
|
| 416 | + Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 417 | + |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 421 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
| 422 | + $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 423 | + $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
|
| 424 | + |
|
| 425 | + if ( ! isset( $entity_id ) || |
|
| 426 | + ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
| 427 | + $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
| 428 | + try { |
|
| 429 | + $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
| 430 | + $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 431 | + } catch ( Exception $e ) { |
|
| 432 | + return null; |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + return $entity_id; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Get the alternative label input HTML code. |
|
| 441 | + * |
|
| 442 | + * @param string $value The input value. |
|
| 443 | + * |
|
| 444 | + * @return string The input HTML code. |
|
| 445 | + * @since 3.2.0 |
|
| 446 | + * |
|
| 447 | + */ |
|
| 448 | + private function get_alternative_label_input( $value = '' ) { |
|
| 449 | + |
|
| 450 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Get the number of entity posts published in this blog. |
|
| 455 | + * |
|
| 456 | + * @return int The number of published entity posts. |
|
| 457 | + * @since 3.6.0 |
|
| 458 | + * |
|
| 459 | + */ |
|
| 460 | + public function count() { |
|
| 461 | + global $wpdb; |
|
| 462 | + |
|
| 463 | + // Try to get the count from the transient. |
|
| 464 | + $count = get_transient( '_wl_entity_service__count' ); |
|
| 465 | + if ( false !== $count ) { |
|
| 466 | + return $count; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + // Query the count. |
|
| 470 | + $count = $wpdb->get_var( $wpdb->prepare( |
|
| 471 | + "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 472 | + . " FROM {$wpdb->term_relationships} tr" |
|
| 473 | + . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 474 | + . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 475 | + . " INNER JOIN {$wpdb->terms} t" |
|
| 476 | + . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 477 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 478 | + 'article' |
|
| 479 | + ) ); |
|
| 480 | + |
|
| 481 | + // Store the count in cache. |
|
| 482 | + set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 483 | + |
|
| 484 | + return $count; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 489 | + * call. |
|
| 490 | + * |
|
| 491 | + * @param array $args The arguments for a `get_posts` call. |
|
| 492 | + * |
|
| 493 | + * @return array The arguments for a `get_posts` call. |
|
| 494 | + * @since 3.15.0 |
|
| 495 | + * |
|
| 496 | + */ |
|
| 497 | + public static function add_criterias( $args ) { |
|
| 498 | + |
|
| 499 | + // Build an optimal tax-query. |
|
| 500 | + $tax_query = array( |
|
| 501 | + 'relation' => 'AND', |
|
| 502 | + array( |
|
| 503 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 504 | + 'operator' => 'EXISTS', |
|
| 505 | + ), |
|
| 506 | + array( |
|
| 507 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 508 | + 'field' => 'slug', |
|
| 509 | + 'terms' => 'article', |
|
| 510 | + 'operator' => 'NOT IN', |
|
| 511 | + ), |
|
| 512 | + ); |
|
| 513 | + |
|
| 514 | + return $args + array( |
|
| 515 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 516 | + /* |
|
| 517 | 517 | * Ensure compatibility with Polylang. |
| 518 | 518 | * |
| 519 | 519 | * @see https://github.com/insideout10/wordlift-plugin/issues/855. |
@@ -521,102 +521,102 @@ discard block |
||
| 521 | 521 | * |
| 522 | 522 | * @since 3.19.5 |
| 523 | 523 | */ |
| 524 | - 'lang' => '', |
|
| 525 | - 'tax_query' => $tax_query, |
|
| 526 | - ); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Create a new entity. |
|
| 531 | - * |
|
| 532 | - * @param string $name The entity name. |
|
| 533 | - * @param string $type_uri The entity's type URI. |
|
| 534 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 535 | - * @param string $status The post status, by default 'publish'. |
|
| 536 | - * |
|
| 537 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 538 | - * @since 3.9.0 |
|
| 539 | - * |
|
| 540 | - */ |
|
| 541 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 542 | - |
|
| 543 | - // Create an entity for the publisher. |
|
| 544 | - $post_id = @wp_insert_post( array( |
|
| 545 | - 'post_type' => self::TYPE_NAME, |
|
| 546 | - 'post_title' => $name, |
|
| 547 | - 'post_status' => $status, |
|
| 548 | - 'post_content' => '', |
|
| 549 | - ) ); |
|
| 550 | - |
|
| 551 | - // Return the error if any. |
|
| 552 | - if ( is_wp_error( $post_id ) ) { |
|
| 553 | - return $post_id; |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - // Set the entity logo. |
|
| 557 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 558 | - set_post_thumbnail( $post_id, $logo ); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - // Set the entity type. |
|
| 562 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 563 | - |
|
| 564 | - return $post_id; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /** |
|
| 568 | - * Get the entities related to the one with the specified id. By default only |
|
| 569 | - * published entities will be returned. |
|
| 570 | - * |
|
| 571 | - * @param int $id The post id. |
|
| 572 | - * @param string $post_status The target post status (default = publish). |
|
| 573 | - * |
|
| 574 | - * @return array An array of post ids. |
|
| 575 | - * @since 3.10.0 |
|
| 576 | - * |
|
| 577 | - */ |
|
| 578 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 579 | - |
|
| 580 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * Get the list of entities. |
|
| 585 | - * |
|
| 586 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 587 | - * |
|
| 588 | - * @return array An array of entity posts. |
|
| 589 | - * @since 3.12.2 |
|
| 590 | - * |
|
| 591 | - */ |
|
| 592 | - public function get( $params = array() ) { |
|
| 593 | - |
|
| 594 | - // Set the defaults. |
|
| 595 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 596 | - |
|
| 597 | - // Merge the defaults with the provided parameters. |
|
| 598 | - $args = wp_parse_args( $params, $defaults ); |
|
| 599 | - |
|
| 600 | - // Call the `get_posts` function. |
|
| 601 | - return get_posts( $args ); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - /** |
|
| 605 | - * The list of post type names which can be used for entities |
|
| 606 | - * |
|
| 607 | - * Criteria is that the post type is public. The list of valid post types |
|
| 608 | - * can be overridden with a filter. |
|
| 609 | - * |
|
| 610 | - * @return array Array containing the names of the valid post types. |
|
| 611 | - * @since 3.15.0 |
|
| 612 | - * |
|
| 613 | - */ |
|
| 614 | - static function valid_entity_post_types() { |
|
| 615 | - |
|
| 616 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 617 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 618 | - |
|
| 619 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 620 | - } |
|
| 524 | + 'lang' => '', |
|
| 525 | + 'tax_query' => $tax_query, |
|
| 526 | + ); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Create a new entity. |
|
| 531 | + * |
|
| 532 | + * @param string $name The entity name. |
|
| 533 | + * @param string $type_uri The entity's type URI. |
|
| 534 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 535 | + * @param string $status The post status, by default 'publish'. |
|
| 536 | + * |
|
| 537 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 538 | + * @since 3.9.0 |
|
| 539 | + * |
|
| 540 | + */ |
|
| 541 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 542 | + |
|
| 543 | + // Create an entity for the publisher. |
|
| 544 | + $post_id = @wp_insert_post( array( |
|
| 545 | + 'post_type' => self::TYPE_NAME, |
|
| 546 | + 'post_title' => $name, |
|
| 547 | + 'post_status' => $status, |
|
| 548 | + 'post_content' => '', |
|
| 549 | + ) ); |
|
| 550 | + |
|
| 551 | + // Return the error if any. |
|
| 552 | + if ( is_wp_error( $post_id ) ) { |
|
| 553 | + return $post_id; |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + // Set the entity logo. |
|
| 557 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 558 | + set_post_thumbnail( $post_id, $logo ); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + // Set the entity type. |
|
| 562 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 563 | + |
|
| 564 | + return $post_id; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /** |
|
| 568 | + * Get the entities related to the one with the specified id. By default only |
|
| 569 | + * published entities will be returned. |
|
| 570 | + * |
|
| 571 | + * @param int $id The post id. |
|
| 572 | + * @param string $post_status The target post status (default = publish). |
|
| 573 | + * |
|
| 574 | + * @return array An array of post ids. |
|
| 575 | + * @since 3.10.0 |
|
| 576 | + * |
|
| 577 | + */ |
|
| 578 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 579 | + |
|
| 580 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * Get the list of entities. |
|
| 585 | + * |
|
| 586 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 587 | + * |
|
| 588 | + * @return array An array of entity posts. |
|
| 589 | + * @since 3.12.2 |
|
| 590 | + * |
|
| 591 | + */ |
|
| 592 | + public function get( $params = array() ) { |
|
| 593 | + |
|
| 594 | + // Set the defaults. |
|
| 595 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 596 | + |
|
| 597 | + // Merge the defaults with the provided parameters. |
|
| 598 | + $args = wp_parse_args( $params, $defaults ); |
|
| 599 | + |
|
| 600 | + // Call the `get_posts` function. |
|
| 601 | + return get_posts( $args ); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + /** |
|
| 605 | + * The list of post type names which can be used for entities |
|
| 606 | + * |
|
| 607 | + * Criteria is that the post type is public. The list of valid post types |
|
| 608 | + * can be overridden with a filter. |
|
| 609 | + * |
|
| 610 | + * @return array Array containing the names of the valid post types. |
|
| 611 | + * @since 3.15.0 |
|
| 612 | + * |
|
| 613 | + */ |
|
| 614 | + static function valid_entity_post_types() { |
|
| 615 | + |
|
| 616 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 617 | + $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 618 | + |
|
| 619 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 620 | + } |
|
| 621 | 621 | |
| 622 | 622 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @since 3.2.0 |
| 82 | 82 | */ |
| 83 | 83 | protected function __construct() { |
| 84 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 84 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 85 | 85 | |
| 86 | 86 | $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
| 87 | 87 | $this->relation_service = Wordlift_Relation_Service::get_instance(); |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | public static function get_instance() { |
| 107 | 107 | |
| 108 | - if ( ! isset( self::$instance ) ) { |
|
| 108 | + if ( ! isset(self::$instance)) { |
|
| 109 | 109 | self::$instance = new self(); |
| 110 | 110 | } |
| 111 | 111 | |
@@ -122,22 +122,22 @@ discard block |
||
| 122 | 122 | * @since 3.1.0 |
| 123 | 123 | * |
| 124 | 124 | */ |
| 125 | - public function is_entity( $post_id ) { |
|
| 125 | + public function is_entity($post_id) { |
|
| 126 | 126 | |
| 127 | 127 | // Improve performance by giving for granted that a product is an entity. |
| 128 | - if ( 'product' === get_post_type( $post_id ) ) { |
|
| 128 | + if ('product' === get_post_type($post_id)) { |
|
| 129 | 129 | return true; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 132 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 133 | 133 | |
| 134 | - if ( is_wp_error( $terms ) ) { |
|
| 135 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 134 | + if (is_wp_error($terms)) { |
|
| 135 | + $this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message()); |
|
| 136 | 136 | |
| 137 | 137 | return false; |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | - if ( empty( $terms ) ) { |
|
| 140 | + if (empty($terms)) { |
|
| 141 | 141 | return false; |
| 142 | 142 | } |
| 143 | 143 | |
@@ -148,8 +148,8 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 150 | 150 | */ |
| 151 | - foreach ( $terms as $term ) { |
|
| 152 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 151 | + foreach ($terms as $term) { |
|
| 152 | + if (1 !== preg_match('~(^|-)article$~', $term->slug)) { |
|
| 153 | 153 | return true; |
| 154 | 154 | } |
| 155 | 155 | } |
@@ -169,19 +169,19 @@ discard block |
||
| 169 | 169 | * @since 3.5.0 |
| 170 | 170 | * |
| 171 | 171 | */ |
| 172 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 172 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 173 | 173 | |
| 174 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 174 | + if (false === $this->is_entity($post_id)) { |
|
| 175 | 175 | return $default; |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | // Retrieve the entity type |
| 179 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 180 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 179 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 180 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 181 | 181 | // Retrieve classification boxes configuration |
| 182 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 183 | - foreach ( $classification_boxes as $cb ) { |
|
| 184 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 182 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 183 | + foreach ($classification_boxes as $cb) { |
|
| 184 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 185 | 185 | return $cb['id']; |
| 186 | 186 | } |
| 187 | 187 | } |
@@ -196,13 +196,13 @@ discard block |
||
| 196 | 196 | * |
| 197 | 197 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 198 | 198 | */ |
| 199 | - public function is_used( $post_id ) { |
|
| 199 | + public function is_used($post_id) { |
|
| 200 | 200 | |
| 201 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 201 | + if (false === $this->is_entity($post_id)) { |
|
| 202 | 202 | return null; |
| 203 | 203 | } |
| 204 | 204 | // Retrieve the post |
| 205 | - $entity = get_post( $post_id ); |
|
| 205 | + $entity = get_post($post_id); |
|
| 206 | 206 | |
| 207 | 207 | global $wpdb; |
| 208 | 208 | // Retrieve Wordlift relation instances table name |
@@ -215,9 +215,9 @@ discard block |
||
| 215 | 215 | ); |
| 216 | 216 | |
| 217 | 217 | // Perform the query |
| 218 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 218 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 219 | 219 | // If there is at least one relation instance for the current entity, then it's used |
| 220 | - if ( 0 < $relation_instances ) { |
|
| 220 | + if (0 < $relation_instances) { |
|
| 221 | 221 | return true; |
| 222 | 222 | } |
| 223 | 223 | |
@@ -225,13 +225,13 @@ discard block |
||
| 225 | 225 | $stmt = $wpdb->prepare( |
| 226 | 226 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 227 | 227 | $entity->ID, |
| 228 | - wl_get_entity_uri( $entity->ID ) |
|
| 228 | + wl_get_entity_uri($entity->ID) |
|
| 229 | 229 | ); |
| 230 | 230 | // Perform the query |
| 231 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 231 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 232 | 232 | |
| 233 | 233 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 234 | - if ( 0 < $meta_instances ) { |
|
| 234 | + if (0 < $meta_instances) { |
|
| 235 | 235 | return true; |
| 236 | 236 | } |
| 237 | 237 | |
@@ -251,9 +251,9 @@ discard block |
||
| 251 | 251 | * @since 3.2.0 |
| 252 | 252 | * |
| 253 | 253 | */ |
| 254 | - public function get_entity_post_by_uri( $uri ) { |
|
| 254 | + public function get_entity_post_by_uri($uri) { |
|
| 255 | 255 | |
| 256 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 256 | + return $this->entity_uri_service->get_entity($uri); |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | /** |
@@ -268,10 +268,10 @@ discard block |
||
| 268 | 268 | * @param WP_Post $post Post object. |
| 269 | 269 | * @param bool $update Whether this is an existing post being updated or not. |
| 270 | 270 | */ |
| 271 | - public function save_post( $post_id, $post, $update ) { |
|
| 271 | + public function save_post($post_id, $post, $update) { |
|
| 272 | 272 | |
| 273 | 273 | // Avoid doing anything if post is autosave or a revision. |
| 274 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 274 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 275 | 275 | return; |
| 276 | 276 | } |
| 277 | 277 | |
@@ -281,16 +281,16 @@ discard block |
||
| 281 | 281 | // the $post_id in the save_post call matches the post id set in the request. |
| 282 | 282 | // |
| 283 | 283 | // If this is not the current post being saved or if it's not an entity, return. |
| 284 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 284 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 285 | 285 | return; |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | // Get the alt labels from the request (or empty array). |
| 289 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 289 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? (array) $_REQUEST['wl_alternative_label'] : array(); |
|
| 290 | 290 | |
| 291 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 291 | + if (( ! empty($_POST['content']) && ! empty($_POST['post_content'])) || isset($_REQUEST['wl_alternative_label'])) { |
|
| 292 | 292 | // This is via classic editor, so set the alternative labels. |
| 293 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 293 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | |
@@ -305,48 +305,48 @@ discard block |
||
| 305 | 305 | * @since 3.2.0 |
| 306 | 306 | * |
| 307 | 307 | */ |
| 308 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 308 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 309 | 309 | |
| 310 | 310 | // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
| 311 | 311 | // instead of post id. |
| 312 | - if ( ! is_numeric( $post_id ) ) { |
|
| 312 | + if ( ! is_numeric($post_id)) { |
|
| 313 | 313 | return; |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | // Force $alt_labels to be an array |
| 317 | - if ( ! is_array( $alt_labels ) ) { |
|
| 318 | - $alt_labels = array( $alt_labels ); |
|
| 317 | + if ( ! is_array($alt_labels)) { |
|
| 318 | + $alt_labels = array($alt_labels); |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 321 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 322 | 322 | |
| 323 | 323 | // Delete all the existing alternate labels. |
| 324 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 324 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 325 | 325 | |
| 326 | 326 | // Save only unique synonymns. |
| 327 | - $alt_labels = array_unique( $alt_labels ); |
|
| 327 | + $alt_labels = array_unique($alt_labels); |
|
| 328 | 328 | |
| 329 | 329 | // Set the alternative labels. |
| 330 | - foreach ( $alt_labels as $alt_label ) { |
|
| 330 | + foreach ($alt_labels as $alt_label) { |
|
| 331 | 331 | |
| 332 | 332 | // Strip html code from synonym. |
| 333 | - $alt_label = wp_strip_all_tags( $alt_label ); |
|
| 333 | + $alt_label = wp_strip_all_tags($alt_label); |
|
| 334 | 334 | |
| 335 | - if ( ! empty( $alt_label ) ) { |
|
| 336 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 335 | + if ( ! empty($alt_label)) { |
|
| 336 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label); |
|
| 337 | 337 | } |
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
| 342 | + public function append_alternative_labels($post_id, $labels_to_append) { |
|
| 343 | 343 | |
| 344 | - $merged_labels = $this->get_alternative_labels( $post_id ); |
|
| 344 | + $merged_labels = $this->get_alternative_labels($post_id); |
|
| 345 | 345 | |
| 346 | 346 | // Append new synonyms to the end. |
| 347 | - $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
| 347 | + $merged_labels = array_merge($merged_labels, $labels_to_append); |
|
| 348 | 348 | |
| 349 | - $this->set_alternative_labels( $post_id, $merged_labels ); |
|
| 349 | + $this->set_alternative_labels($post_id, $merged_labels); |
|
| 350 | 350 | |
| 351 | 351 | } |
| 352 | 352 | |
@@ -359,9 +359,9 @@ discard block |
||
| 359 | 359 | * @since 3.2.0 |
| 360 | 360 | * |
| 361 | 361 | */ |
| 362 | - public function get_alternative_labels( $post_id ) { |
|
| 362 | + public function get_alternative_labels($post_id) { |
|
| 363 | 363 | |
| 364 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 364 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | /** |
@@ -373,9 +373,9 @@ discard block |
||
| 373 | 373 | * @return array An array with the entity title and labels. |
| 374 | 374 | * @since 3.12.0 |
| 375 | 375 | */ |
| 376 | - public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 377 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
| 378 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 376 | + public function get_labels($post_id, $object_type = Object_Type_Enum::POST) { |
|
| 377 | + if ($object_type === Object_Type_Enum::POST) { |
|
| 378 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | // Term Reference dont have synonyms yet. |
@@ -390,45 +390,45 @@ discard block |
||
| 390 | 390 | * @since 3.2.0 |
| 391 | 391 | * |
| 392 | 392 | */ |
| 393 | - public function edit_form_before_permalink( $post ) { |
|
| 393 | + public function edit_form_before_permalink($post) { |
|
| 394 | 394 | |
| 395 | 395 | // If it's not an entity, return. |
| 396 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 396 | + if ( ! $this->is_entity($post->ID)) { |
|
| 397 | 397 | return; |
| 398 | 398 | } |
| 399 | 399 | |
| 400 | 400 | // If disabled by filter, return. |
| 401 | - if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 401 | + if ( ! apply_filters('wl_feature__enable__add-synonyms', true)) { |
|
| 402 | 402 | return; |
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | // Print the input template. |
| 406 | - Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 406 | + Wordlift_UI_Service::print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 407 | 407 | |
| 408 | 408 | // Print all the currently set alternative labels. |
| 409 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 409 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 410 | 410 | |
| 411 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 411 | + echo $this->get_alternative_label_input($alt_label); |
|
| 412 | 412 | |
| 413 | 413 | }; |
| 414 | 414 | |
| 415 | 415 | // Print the button. |
| 416 | - Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 416 | + Wordlift_UI_Service::print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 417 | 417 | |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 420 | + public function get_uri($object_id, $type = Object_Type_Enum::POST) { |
|
| 421 | 421 | $content_service = Wordpress_Content_Service::get_instance(); |
| 422 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 422 | + $entity_id = $content_service->get_entity_id(new Wordpress_Content_Id($object_id, $type)); |
|
| 423 | 423 | $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
| 424 | 424 | |
| 425 | - if ( ! isset( $entity_id ) || |
|
| 426 | - ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
| 427 | - $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
| 425 | + if ( ! isset($entity_id) || |
|
| 426 | + ( ! empty($dataset_uri) && 0 !== strpos($entity_id, $dataset_uri))) { |
|
| 427 | + $rel_uri = Entity_Uri_Generator::create_uri($type, $object_id); |
|
| 428 | 428 | try { |
| 429 | - $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
| 430 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 431 | - } catch ( Exception $e ) { |
|
| 429 | + $content_service->set_entity_id(new Wordpress_Content_Id($object_id, $type), $rel_uri); |
|
| 430 | + $entity_id = $content_service->get_entity_id(new Wordpress_Content_Id($object_id, $type)); |
|
| 431 | + } catch (Exception $e) { |
|
| 432 | 432 | return null; |
| 433 | 433 | } |
| 434 | 434 | } |
@@ -445,9 +445,9 @@ discard block |
||
| 445 | 445 | * @since 3.2.0 |
| 446 | 446 | * |
| 447 | 447 | */ |
| 448 | - private function get_alternative_label_input( $value = '' ) { |
|
| 448 | + private function get_alternative_label_input($value = '') { |
|
| 449 | 449 | |
| 450 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 450 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 451 | 451 | } |
| 452 | 452 | |
| 453 | 453 | /** |
@@ -461,13 +461,13 @@ discard block |
||
| 461 | 461 | global $wpdb; |
| 462 | 462 | |
| 463 | 463 | // Try to get the count from the transient. |
| 464 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 465 | - if ( false !== $count ) { |
|
| 464 | + $count = get_transient('_wl_entity_service__count'); |
|
| 465 | + if (false !== $count) { |
|
| 466 | 466 | return $count; |
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | // Query the count. |
| 470 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 470 | + $count = $wpdb->get_var($wpdb->prepare( |
|
| 471 | 471 | "SELECT COUNT( DISTINCT( tr.object_id ) )" |
| 472 | 472 | . " FROM {$wpdb->term_relationships} tr" |
| 473 | 473 | . " INNER JOIN {$wpdb->term_taxonomy} tt" |
@@ -476,10 +476,10 @@ discard block |
||
| 476 | 476 | . " ON t.term_id = tt.term_id AND t.name != %s", |
| 477 | 477 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 478 | 478 | 'article' |
| 479 | - ) ); |
|
| 479 | + )); |
|
| 480 | 480 | |
| 481 | 481 | // Store the count in cache. |
| 482 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 482 | + set_transient('_wl_entity_service__count', $count, 900); |
|
| 483 | 483 | |
| 484 | 484 | return $count; |
| 485 | 485 | } |
@@ -494,7 +494,7 @@ discard block |
||
| 494 | 494 | * @since 3.15.0 |
| 495 | 495 | * |
| 496 | 496 | */ |
| 497 | - public static function add_criterias( $args ) { |
|
| 497 | + public static function add_criterias($args) { |
|
| 498 | 498 | |
| 499 | 499 | // Build an optimal tax-query. |
| 500 | 500 | $tax_query = array( |
@@ -538,28 +538,28 @@ discard block |
||
| 538 | 538 | * @since 3.9.0 |
| 539 | 539 | * |
| 540 | 540 | */ |
| 541 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 541 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 542 | 542 | |
| 543 | 543 | // Create an entity for the publisher. |
| 544 | - $post_id = @wp_insert_post( array( |
|
| 544 | + $post_id = @wp_insert_post(array( |
|
| 545 | 545 | 'post_type' => self::TYPE_NAME, |
| 546 | 546 | 'post_title' => $name, |
| 547 | 547 | 'post_status' => $status, |
| 548 | 548 | 'post_content' => '', |
| 549 | - ) ); |
|
| 549 | + )); |
|
| 550 | 550 | |
| 551 | 551 | // Return the error if any. |
| 552 | - if ( is_wp_error( $post_id ) ) { |
|
| 552 | + if (is_wp_error($post_id)) { |
|
| 553 | 553 | return $post_id; |
| 554 | 554 | } |
| 555 | 555 | |
| 556 | 556 | // Set the entity logo. |
| 557 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 558 | - set_post_thumbnail( $post_id, $logo ); |
|
| 557 | + if ($logo && is_numeric($logo)) { |
|
| 558 | + set_post_thumbnail($post_id, $logo); |
|
| 559 | 559 | } |
| 560 | 560 | |
| 561 | 561 | // Set the entity type. |
| 562 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 562 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 563 | 563 | |
| 564 | 564 | return $post_id; |
| 565 | 565 | } |
@@ -575,9 +575,9 @@ discard block |
||
| 575 | 575 | * @since 3.10.0 |
| 576 | 576 | * |
| 577 | 577 | */ |
| 578 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 578 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 579 | 579 | |
| 580 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 580 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 581 | 581 | } |
| 582 | 582 | |
| 583 | 583 | /** |
@@ -589,16 +589,16 @@ discard block |
||
| 589 | 589 | * @since 3.12.2 |
| 590 | 590 | * |
| 591 | 591 | */ |
| 592 | - public function get( $params = array() ) { |
|
| 592 | + public function get($params = array()) { |
|
| 593 | 593 | |
| 594 | 594 | // Set the defaults. |
| 595 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 595 | + $defaults = array('post_type' => 'entity'); |
|
| 596 | 596 | |
| 597 | 597 | // Merge the defaults with the provided parameters. |
| 598 | - $args = wp_parse_args( $params, $defaults ); |
|
| 598 | + $args = wp_parse_args($params, $defaults); |
|
| 599 | 599 | |
| 600 | 600 | // Call the `get_posts` function. |
| 601 | - return get_posts( $args ); |
|
| 601 | + return get_posts($args); |
|
| 602 | 602 | } |
| 603 | 603 | |
| 604 | 604 | /** |
@@ -614,9 +614,9 @@ discard block |
||
| 614 | 614 | static function valid_entity_post_types() { |
| 615 | 615 | |
| 616 | 616 | // Ignore builtins in the call to avoid getting attachments. |
| 617 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 617 | + $post_types = array('post', 'page', self::TYPE_NAME, 'product'); |
|
| 618 | 618 | |
| 619 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 619 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 620 | 620 | } |
| 621 | 621 | |
| 622 | 622 | } |
@@ -16,35 +16,35 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Url_Property_Service extends Wordlift_Simple_Property_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The meta key for the schema:url property. |
|
| 21 | - */ |
|
| 22 | - const META_KEY = 'wl_schema_url'; |
|
| 19 | + /** |
|
| 20 | + * The meta key for the schema:url property. |
|
| 21 | + */ |
|
| 22 | + const META_KEY = 'wl_schema_url'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get the URLs associated with the post. |
|
| 26 | - * |
|
| 27 | - * @param int $id The post id. |
|
| 28 | - * @param string $meta_key The meta key. |
|
| 29 | - * |
|
| 30 | - * @return array An array of URLs. |
|
| 31 | - */ |
|
| 32 | - public function get( $id, $meta_key, $type ) { |
|
| 24 | + /** |
|
| 25 | + * Get the URLs associated with the post. |
|
| 26 | + * |
|
| 27 | + * @param int $id The post id. |
|
| 28 | + * @param string $meta_key The meta key. |
|
| 29 | + * |
|
| 30 | + * @return array An array of URLs. |
|
| 31 | + */ |
|
| 32 | + public function get( $id, $meta_key, $type ) { |
|
| 33 | 33 | |
| 34 | - // Get the meta values and push the <permalink> to |
|
| 35 | - // ensure that default url will be added to the schema:url's. |
|
| 36 | - /* |
|
| 34 | + // Get the meta values and push the <permalink> to |
|
| 35 | + // ensure that default url will be added to the schema:url's. |
|
| 36 | + /* |
|
| 37 | 37 | * Do not add `<permalink>` if one or more URLs have been provided by the editor. |
| 38 | 38 | * |
| 39 | 39 | * @see https://github.com/insideout10/wordlift-plugin/issues/913 |
| 40 | 40 | * |
| 41 | 41 | * @since 3.21.1 |
| 42 | 42 | */ |
| 43 | - $urls = array_filter( parent::get( $id, $meta_key, $type ) ?: array( '<permalink>' ) ); |
|
| 43 | + $urls = array_filter( parent::get( $id, $meta_key, $type ) ?: array( '<permalink>' ) ); |
|
| 44 | 44 | |
| 45 | - // Convert <permalink> in actual permalink values. |
|
| 46 | - return array_map( function ( $item ) use ( $id, $type ) { |
|
| 47 | - /* |
|
| 45 | + // Convert <permalink> in actual permalink values. |
|
| 46 | + return array_map( function ( $item ) use ( $id, $type ) { |
|
| 47 | + /* |
|
| 48 | 48 | * If `<permalink>` get the production permalink. |
| 49 | 49 | * |
| 50 | 50 | * @since 3.20.0 |
@@ -52,20 +52,20 @@ discard block |
||
| 52 | 52 | * @see https://github.com/insideout10/wordlift-plugin/issues/850. |
| 53 | 53 | */ |
| 54 | 54 | |
| 55 | - if ( '<permalink>' !== $item ) { |
|
| 56 | - return $item; |
|
| 57 | - } |
|
| 55 | + if ( '<permalink>' !== $item ) { |
|
| 56 | + return $item; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - // Permalinks. |
|
| 60 | - switch ( $type ) { |
|
| 61 | - case Object_Type_Enum::POST: |
|
| 62 | - return Wordlift_Post_Adapter::get_production_permalink( $id ); |
|
| 63 | - case Object_Type_Enum::TERM: |
|
| 64 | - return get_term_link( $id ); |
|
| 65 | - default: |
|
| 66 | - return $item; |
|
| 67 | - } |
|
| 68 | - }, array_unique( $urls ) ); |
|
| 69 | - } |
|
| 59 | + // Permalinks. |
|
| 60 | + switch ( $type ) { |
|
| 61 | + case Object_Type_Enum::POST: |
|
| 62 | + return Wordlift_Post_Adapter::get_production_permalink( $id ); |
|
| 63 | + case Object_Type_Enum::TERM: |
|
| 64 | + return get_term_link( $id ); |
|
| 65 | + default: |
|
| 66 | + return $item; |
|
| 67 | + } |
|
| 68 | + }, array_unique( $urls ) ); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | } |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * |
| 30 | 30 | * @return array An array of URLs. |
| 31 | 31 | */ |
| 32 | - public function get( $id, $meta_key, $type ) { |
|
| 32 | + public function get($id, $meta_key, $type) { |
|
| 33 | 33 | |
| 34 | 34 | // Get the meta values and push the <permalink> to |
| 35 | 35 | // ensure that default url will be added to the schema:url's. |
@@ -40,10 +40,10 @@ discard block |
||
| 40 | 40 | * |
| 41 | 41 | * @since 3.21.1 |
| 42 | 42 | */ |
| 43 | - $urls = array_filter( parent::get( $id, $meta_key, $type ) ?: array( '<permalink>' ) ); |
|
| 43 | + $urls = array_filter(parent::get($id, $meta_key, $type) ?: array('<permalink>')); |
|
| 44 | 44 | |
| 45 | 45 | // Convert <permalink> in actual permalink values. |
| 46 | - return array_map( function ( $item ) use ( $id, $type ) { |
|
| 46 | + return array_map(function($item) use ($id, $type) { |
|
| 47 | 47 | /* |
| 48 | 48 | * If `<permalink>` get the production permalink. |
| 49 | 49 | * |
@@ -52,20 +52,20 @@ discard block |
||
| 52 | 52 | * @see https://github.com/insideout10/wordlift-plugin/issues/850. |
| 53 | 53 | */ |
| 54 | 54 | |
| 55 | - if ( '<permalink>' !== $item ) { |
|
| 55 | + if ('<permalink>' !== $item) { |
|
| 56 | 56 | return $item; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | // Permalinks. |
| 60 | - switch ( $type ) { |
|
| 60 | + switch ($type) { |
|
| 61 | 61 | case Object_Type_Enum::POST: |
| 62 | - return Wordlift_Post_Adapter::get_production_permalink( $id ); |
|
| 62 | + return Wordlift_Post_Adapter::get_production_permalink($id); |
|
| 63 | 63 | case Object_Type_Enum::TERM: |
| 64 | - return get_term_link( $id ); |
|
| 64 | + return get_term_link($id); |
|
| 65 | 65 | default: |
| 66 | 66 | return $item; |
| 67 | 67 | } |
| 68 | - }, array_unique( $urls ) ); |
|
| 68 | + }, array_unique($urls)); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | } |