@@ -16,259 +16,259 @@ |
||
| 16 | 16 | * @since 3.25.0 |
| 17 | 17 | */ |
| 18 | 18 | class Jsonld_Converter { |
| 19 | - /** |
|
| 20 | - * Enumerations for the field types. |
|
| 21 | - * Enumerations for the field types. |
|
| 22 | - */ |
|
| 23 | - const FIELD_TYPE_TEXT_FIELD = 'text'; |
|
| 24 | - const FIELD_TYPE_CUSTOM_FIELD = 'custom_field'; |
|
| 25 | - const FIELD_TYPE_ACF = 'acf'; |
|
| 26 | - /** |
|
| 27 | - * The {@link Mappings_Validator} instance to test. |
|
| 28 | - * |
|
| 29 | - * @since 3.25.0 |
|
| 30 | - * @access private |
|
| 31 | - * @var Mappings_Validator $validator The {@link Mappings_Validator} instance. |
|
| 32 | - */ |
|
| 33 | - private $validator; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 37 | - * |
|
| 38 | - * @since 3.25.0 |
|
| 39 | - * @access private |
|
| 40 | - * @var Mappings_Transform_Functions_Registry $transform_functions_registry The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 41 | - */ |
|
| 42 | - private $transform_functions_registry; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Initialize all dependencies required. |
|
| 46 | - * |
|
| 47 | - * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
|
| 48 | - * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
|
| 49 | - */ |
|
| 50 | - public function __construct( $validator, $transform_functions_registry ) { |
|
| 51 | - |
|
| 52 | - $this->validator = $validator; |
|
| 53 | - $this->transform_functions_registry = $transform_functions_registry; |
|
| 54 | - |
|
| 55 | - // Hook to refactor the JSON-LD. |
|
| 56 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 57 | - add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 58 | - |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Hook to `wl_post_jsonld_array` and `wl_entity_jsonld_array`. |
|
| 63 | - * |
|
| 64 | - * Receive the JSON-LD and the references in the array along with the post ID and transform them according to |
|
| 65 | - * the configuration. |
|
| 66 | - * |
|
| 67 | - * @param array $value { |
|
| 68 | - * The array containing the JSON-LD and the references. |
|
| 69 | - * |
|
| 70 | - * @type array $jsonld The JSON-LD array. |
|
| 71 | - * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter). |
|
| 72 | - * } |
|
| 73 | - * |
|
| 74 | - * @param int $post_id The post ID. |
|
| 75 | - * |
|
| 76 | - * @return array An array with the updated JSON-LD and references. |
|
| 77 | - */ |
|
| 78 | - public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 79 | - |
|
| 80 | - $jsonld = $value['jsonld']; |
|
| 81 | - $references = $value['references']; |
|
| 82 | - |
|
| 83 | - return array( |
|
| 84 | - 'jsonld' => $this->wl_post_jsonld( $jsonld, $post_id, $references ), |
|
| 85 | - 'references' => $references, |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Returns JSON-LD data after applying transformation functions. |
|
| 91 | - * |
|
| 92 | - * @param array $jsonld The JSON-LD structure. |
|
| 93 | - * @param int $post_id The {@link WP_Post} id. |
|
| 94 | - * @param array $references An array of post references. |
|
| 95 | - * |
|
| 96 | - * @return array the new refactored array structure. |
|
| 97 | - * @since 3.25.0 |
|
| 98 | - */ |
|
| 99 | - private function wl_post_jsonld( $jsonld, $post_id, &$references ) { |
|
| 100 | - |
|
| 101 | - // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
|
| 102 | - // instance may be reused afterwards. |
|
| 103 | - |
|
| 104 | - $properties = $this->validator->validate( $post_id ); |
|
| 105 | - $nested_properties = array(); |
|
| 106 | - |
|
| 107 | - foreach ( $properties as $property ) { |
|
| 108 | - // If the property has the character '/' in the property name then it is a nested property. |
|
| 109 | - if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 110 | - $nested_properties[] = $property; |
|
| 111 | - continue; |
|
| 112 | - } |
|
| 113 | - $property_transformed_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 114 | - if ( false !== $property_transformed_data ) { |
|
| 115 | - $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $post_id, $references ); |
|
| 120 | - |
|
| 121 | - return $jsonld; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Get the property data by applying the transformation function |
|
| 126 | - * |
|
| 127 | - * @param $property |
|
| 128 | - * @param $jsonld |
|
| 129 | - * @param $post_id |
|
| 130 | - * @param $references |
|
| 131 | - * |
|
| 132 | - * @return array|bool|null |
|
| 133 | - */ |
|
| 134 | - public function get_property_data( $property, $jsonld, $post_id, &$references ) { |
|
| 135 | - $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 136 | - $data = $this->get_data_from_data_source( $post_id, $property ); |
|
| 137 | - if ( null !== $transform_instance ) { |
|
| 138 | - $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 139 | - if ( null !== $transform_data ) { |
|
| 140 | - return $this->make_single( $transform_data ); |
|
| 141 | - } |
|
| 142 | - } else { |
|
| 143 | - return $this->make_single( $data ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Process all the nested properties. |
|
| 151 | - * |
|
| 152 | - * @param $nested_properties array |
|
| 153 | - * @param $jsonld array |
|
| 154 | - * |
|
| 155 | - * @return array |
|
| 156 | - */ |
|
| 157 | - public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references ) { |
|
| 158 | - foreach ( $nested_properties as $property ) { |
|
| 159 | - $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 160 | - if ( false === $property_data ) { |
|
| 161 | - // No need to create nested levels. |
|
| 162 | - continue; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $keys = explode( '/', $property['property_name'] ); |
|
| 166 | - // end is the last level of the nested property. |
|
| 167 | - $end = array_pop( $keys ); |
|
| 168 | - $current_property_pointer = &$jsonld; |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Once we find all the nested levels from the property name |
|
| 172 | - * loop through it and create associative array if the levels |
|
| 173 | - * didnt exist. |
|
| 174 | - */ |
|
| 175 | - while ( count( $keys ) > 0 ) { |
|
| 176 | - $key = array_shift( $keys ); |
|
| 177 | - if ( $key === "" ) { |
|
| 178 | - continue; |
|
| 179 | - } |
|
| 180 | - if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 181 | - $current_property_pointer[ $key ] = array(); |
|
| 182 | - } |
|
| 183 | - // We are setting the pointer to the current key, so that at the end |
|
| 184 | - // we can add the data at last level. |
|
| 185 | - $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 186 | - } |
|
| 187 | - $current_property_pointer[ $end ] = $property_data; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $jsonld; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Returns data from data source. |
|
| 196 | - * |
|
| 197 | - * @param int $post_id Id of the post. |
|
| 198 | - * @param array $property_data The property data for the post_id. |
|
| 199 | - * |
|
| 200 | - * @return array Returns key, value array, if the value is not found, then it returns null. |
|
| 201 | - */ |
|
| 202 | - final public function get_data_from_data_source( $post_id, $property_data ) { |
|
| 203 | - $value = $property_data['field_name']; |
|
| 204 | - |
|
| 205 | - // Do 1 to 1 mapping and return result. |
|
| 206 | - switch ( $property_data['field_type'] ) { |
|
| 207 | - case self::FIELD_TYPE_ACF: |
|
| 208 | - if ( ! function_exists( 'get_field' ) || ! function_exists( 'get_field_object' ) ) { |
|
| 209 | - return array(); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return $this->get_data_for_acf_field( $property_data['field_name'], $post_id ); |
|
| 213 | - |
|
| 214 | - case self::FIELD_TYPE_CUSTOM_FIELD: |
|
| 215 | - |
|
| 216 | - return array_map( 'wp_strip_all_tags', get_post_meta( $post_id, $value ) ); |
|
| 217 | - |
|
| 218 | - default: |
|
| 219 | - return $value; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Gets data from acf, format the data if it is a repeater field. |
|
| 226 | - * |
|
| 227 | - * @param $field_name |
|
| 228 | - * @param $post_id |
|
| 229 | - * |
|
| 230 | - * @return array|mixed |
|
| 231 | - */ |
|
| 232 | - public function get_data_for_acf_field( $field_name, $post_id ) { |
|
| 233 | - $field_data = get_field_object( $field_name, $post_id ); |
|
| 234 | - $data = get_field( $field_name, $post_id ); |
|
| 235 | - |
|
| 236 | - // only process if it is a repeater field, else return the data. |
|
| 237 | - if ( is_array( $field_data ) && array_key_exists( 'type', $field_data ) |
|
| 238 | - && $field_data['type'] === 'repeater' ) { |
|
| 239 | - // check if we have only one sub field, currently we only support one subfield. |
|
| 240 | - if ( is_array( $data ) && count( $data ) > 0 && count( array_keys( $data[0] ) === 1 ) ) { |
|
| 241 | - $repeater_formatted_data = array(); |
|
| 242 | - foreach ( $data as $item ) { |
|
| 243 | - $repeater_formatted_data = array_merge( $repeater_formatted_data, array_values( $item ) ); |
|
| 244 | - } |
|
| 245 | - // Remove non unique values. |
|
| 246 | - $repeater_formatted_data = array_unique( $repeater_formatted_data ); |
|
| 247 | - // Remove empty values |
|
| 248 | - $repeater_formatted_data = array_filter( $repeater_formatted_data, 'strlen' ); |
|
| 249 | - |
|
| 250 | - // re-index all the values. |
|
| 251 | - return array_values( $repeater_formatted_data ); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - // Return normal acf data if it is not a repeater field. |
|
| 256 | - return $data; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - private function make_single( $value ) { |
|
| 260 | - |
|
| 261 | - $values = (array) $value; |
|
| 262 | - |
|
| 263 | - if ( empty( $values ) ) { |
|
| 264 | - return null; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 268 | - return current( $values ); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - return $values; |
|
| 272 | - } |
|
| 19 | + /** |
|
| 20 | + * Enumerations for the field types. |
|
| 21 | + * Enumerations for the field types. |
|
| 22 | + */ |
|
| 23 | + const FIELD_TYPE_TEXT_FIELD = 'text'; |
|
| 24 | + const FIELD_TYPE_CUSTOM_FIELD = 'custom_field'; |
|
| 25 | + const FIELD_TYPE_ACF = 'acf'; |
|
| 26 | + /** |
|
| 27 | + * The {@link Mappings_Validator} instance to test. |
|
| 28 | + * |
|
| 29 | + * @since 3.25.0 |
|
| 30 | + * @access private |
|
| 31 | + * @var Mappings_Validator $validator The {@link Mappings_Validator} instance. |
|
| 32 | + */ |
|
| 33 | + private $validator; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 37 | + * |
|
| 38 | + * @since 3.25.0 |
|
| 39 | + * @access private |
|
| 40 | + * @var Mappings_Transform_Functions_Registry $transform_functions_registry The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 41 | + */ |
|
| 42 | + private $transform_functions_registry; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Initialize all dependencies required. |
|
| 46 | + * |
|
| 47 | + * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
|
| 48 | + * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
|
| 49 | + */ |
|
| 50 | + public function __construct( $validator, $transform_functions_registry ) { |
|
| 51 | + |
|
| 52 | + $this->validator = $validator; |
|
| 53 | + $this->transform_functions_registry = $transform_functions_registry; |
|
| 54 | + |
|
| 55 | + // Hook to refactor the JSON-LD. |
|
| 56 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 57 | + add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 58 | + |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Hook to `wl_post_jsonld_array` and `wl_entity_jsonld_array`. |
|
| 63 | + * |
|
| 64 | + * Receive the JSON-LD and the references in the array along with the post ID and transform them according to |
|
| 65 | + * the configuration. |
|
| 66 | + * |
|
| 67 | + * @param array $value { |
|
| 68 | + * The array containing the JSON-LD and the references. |
|
| 69 | + * |
|
| 70 | + * @type array $jsonld The JSON-LD array. |
|
| 71 | + * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter). |
|
| 72 | + * } |
|
| 73 | + * |
|
| 74 | + * @param int $post_id The post ID. |
|
| 75 | + * |
|
| 76 | + * @return array An array with the updated JSON-LD and references. |
|
| 77 | + */ |
|
| 78 | + public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 79 | + |
|
| 80 | + $jsonld = $value['jsonld']; |
|
| 81 | + $references = $value['references']; |
|
| 82 | + |
|
| 83 | + return array( |
|
| 84 | + 'jsonld' => $this->wl_post_jsonld( $jsonld, $post_id, $references ), |
|
| 85 | + 'references' => $references, |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Returns JSON-LD data after applying transformation functions. |
|
| 91 | + * |
|
| 92 | + * @param array $jsonld The JSON-LD structure. |
|
| 93 | + * @param int $post_id The {@link WP_Post} id. |
|
| 94 | + * @param array $references An array of post references. |
|
| 95 | + * |
|
| 96 | + * @return array the new refactored array structure. |
|
| 97 | + * @since 3.25.0 |
|
| 98 | + */ |
|
| 99 | + private function wl_post_jsonld( $jsonld, $post_id, &$references ) { |
|
| 100 | + |
|
| 101 | + // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
|
| 102 | + // instance may be reused afterwards. |
|
| 103 | + |
|
| 104 | + $properties = $this->validator->validate( $post_id ); |
|
| 105 | + $nested_properties = array(); |
|
| 106 | + |
|
| 107 | + foreach ( $properties as $property ) { |
|
| 108 | + // If the property has the character '/' in the property name then it is a nested property. |
|
| 109 | + if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 110 | + $nested_properties[] = $property; |
|
| 111 | + continue; |
|
| 112 | + } |
|
| 113 | + $property_transformed_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 114 | + if ( false !== $property_transformed_data ) { |
|
| 115 | + $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $post_id, $references ); |
|
| 120 | + |
|
| 121 | + return $jsonld; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Get the property data by applying the transformation function |
|
| 126 | + * |
|
| 127 | + * @param $property |
|
| 128 | + * @param $jsonld |
|
| 129 | + * @param $post_id |
|
| 130 | + * @param $references |
|
| 131 | + * |
|
| 132 | + * @return array|bool|null |
|
| 133 | + */ |
|
| 134 | + public function get_property_data( $property, $jsonld, $post_id, &$references ) { |
|
| 135 | + $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 136 | + $data = $this->get_data_from_data_source( $post_id, $property ); |
|
| 137 | + if ( null !== $transform_instance ) { |
|
| 138 | + $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 139 | + if ( null !== $transform_data ) { |
|
| 140 | + return $this->make_single( $transform_data ); |
|
| 141 | + } |
|
| 142 | + } else { |
|
| 143 | + return $this->make_single( $data ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Process all the nested properties. |
|
| 151 | + * |
|
| 152 | + * @param $nested_properties array |
|
| 153 | + * @param $jsonld array |
|
| 154 | + * |
|
| 155 | + * @return array |
|
| 156 | + */ |
|
| 157 | + public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references ) { |
|
| 158 | + foreach ( $nested_properties as $property ) { |
|
| 159 | + $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 160 | + if ( false === $property_data ) { |
|
| 161 | + // No need to create nested levels. |
|
| 162 | + continue; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $keys = explode( '/', $property['property_name'] ); |
|
| 166 | + // end is the last level of the nested property. |
|
| 167 | + $end = array_pop( $keys ); |
|
| 168 | + $current_property_pointer = &$jsonld; |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Once we find all the nested levels from the property name |
|
| 172 | + * loop through it and create associative array if the levels |
|
| 173 | + * didnt exist. |
|
| 174 | + */ |
|
| 175 | + while ( count( $keys ) > 0 ) { |
|
| 176 | + $key = array_shift( $keys ); |
|
| 177 | + if ( $key === "" ) { |
|
| 178 | + continue; |
|
| 179 | + } |
|
| 180 | + if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 181 | + $current_property_pointer[ $key ] = array(); |
|
| 182 | + } |
|
| 183 | + // We are setting the pointer to the current key, so that at the end |
|
| 184 | + // we can add the data at last level. |
|
| 185 | + $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 186 | + } |
|
| 187 | + $current_property_pointer[ $end ] = $property_data; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $jsonld; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Returns data from data source. |
|
| 196 | + * |
|
| 197 | + * @param int $post_id Id of the post. |
|
| 198 | + * @param array $property_data The property data for the post_id. |
|
| 199 | + * |
|
| 200 | + * @return array Returns key, value array, if the value is not found, then it returns null. |
|
| 201 | + */ |
|
| 202 | + final public function get_data_from_data_source( $post_id, $property_data ) { |
|
| 203 | + $value = $property_data['field_name']; |
|
| 204 | + |
|
| 205 | + // Do 1 to 1 mapping and return result. |
|
| 206 | + switch ( $property_data['field_type'] ) { |
|
| 207 | + case self::FIELD_TYPE_ACF: |
|
| 208 | + if ( ! function_exists( 'get_field' ) || ! function_exists( 'get_field_object' ) ) { |
|
| 209 | + return array(); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return $this->get_data_for_acf_field( $property_data['field_name'], $post_id ); |
|
| 213 | + |
|
| 214 | + case self::FIELD_TYPE_CUSTOM_FIELD: |
|
| 215 | + |
|
| 216 | + return array_map( 'wp_strip_all_tags', get_post_meta( $post_id, $value ) ); |
|
| 217 | + |
|
| 218 | + default: |
|
| 219 | + return $value; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Gets data from acf, format the data if it is a repeater field. |
|
| 226 | + * |
|
| 227 | + * @param $field_name |
|
| 228 | + * @param $post_id |
|
| 229 | + * |
|
| 230 | + * @return array|mixed |
|
| 231 | + */ |
|
| 232 | + public function get_data_for_acf_field( $field_name, $post_id ) { |
|
| 233 | + $field_data = get_field_object( $field_name, $post_id ); |
|
| 234 | + $data = get_field( $field_name, $post_id ); |
|
| 235 | + |
|
| 236 | + // only process if it is a repeater field, else return the data. |
|
| 237 | + if ( is_array( $field_data ) && array_key_exists( 'type', $field_data ) |
|
| 238 | + && $field_data['type'] === 'repeater' ) { |
|
| 239 | + // check if we have only one sub field, currently we only support one subfield. |
|
| 240 | + if ( is_array( $data ) && count( $data ) > 0 && count( array_keys( $data[0] ) === 1 ) ) { |
|
| 241 | + $repeater_formatted_data = array(); |
|
| 242 | + foreach ( $data as $item ) { |
|
| 243 | + $repeater_formatted_data = array_merge( $repeater_formatted_data, array_values( $item ) ); |
|
| 244 | + } |
|
| 245 | + // Remove non unique values. |
|
| 246 | + $repeater_formatted_data = array_unique( $repeater_formatted_data ); |
|
| 247 | + // Remove empty values |
|
| 248 | + $repeater_formatted_data = array_filter( $repeater_formatted_data, 'strlen' ); |
|
| 249 | + |
|
| 250 | + // re-index all the values. |
|
| 251 | + return array_values( $repeater_formatted_data ); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + // Return normal acf data if it is not a repeater field. |
|
| 256 | + return $data; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + private function make_single( $value ) { |
|
| 260 | + |
|
| 261 | + $values = (array) $value; |
|
| 262 | + |
|
| 263 | + if ( empty( $values ) ) { |
|
| 264 | + return null; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 268 | + return current( $values ); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + return $values; |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | 274 | } |
@@ -47,14 +47,14 @@ discard block |
||
| 47 | 47 | * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
| 48 | 48 | * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
| 49 | 49 | */ |
| 50 | - public function __construct( $validator, $transform_functions_registry ) { |
|
| 50 | + public function __construct($validator, $transform_functions_registry) { |
|
| 51 | 51 | |
| 52 | 52 | $this->validator = $validator; |
| 53 | 53 | $this->transform_functions_registry = $transform_functions_registry; |
| 54 | 54 | |
| 55 | 55 | // Hook to refactor the JSON-LD. |
| 56 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 57 | - add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 56 | + add_filter('wl_post_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 2); |
|
| 57 | + add_filter('wl_entity_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 3); |
|
| 58 | 58 | |
| 59 | 59 | } |
| 60 | 60 | |
@@ -75,13 +75,13 @@ discard block |
||
| 75 | 75 | * |
| 76 | 76 | * @return array An array with the updated JSON-LD and references. |
| 77 | 77 | */ |
| 78 | - public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 78 | + public function wl_post_jsonld_array($value, $post_id) { |
|
| 79 | 79 | |
| 80 | 80 | $jsonld = $value['jsonld']; |
| 81 | 81 | $references = $value['references']; |
| 82 | 82 | |
| 83 | 83 | return array( |
| 84 | - 'jsonld' => $this->wl_post_jsonld( $jsonld, $post_id, $references ), |
|
| 84 | + 'jsonld' => $this->wl_post_jsonld($jsonld, $post_id, $references), |
|
| 85 | 85 | 'references' => $references, |
| 86 | 86 | ); |
| 87 | 87 | } |
@@ -96,27 +96,27 @@ discard block |
||
| 96 | 96 | * @return array the new refactored array structure. |
| 97 | 97 | * @since 3.25.0 |
| 98 | 98 | */ |
| 99 | - private function wl_post_jsonld( $jsonld, $post_id, &$references ) { |
|
| 99 | + private function wl_post_jsonld($jsonld, $post_id, &$references) { |
|
| 100 | 100 | |
| 101 | 101 | // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
| 102 | 102 | // instance may be reused afterwards. |
| 103 | 103 | |
| 104 | - $properties = $this->validator->validate( $post_id ); |
|
| 104 | + $properties = $this->validator->validate($post_id); |
|
| 105 | 105 | $nested_properties = array(); |
| 106 | 106 | |
| 107 | - foreach ( $properties as $property ) { |
|
| 107 | + foreach ($properties as $property) { |
|
| 108 | 108 | // If the property has the character '/' in the property name then it is a nested property. |
| 109 | - if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 109 | + if (strpos($property['property_name'], '/') !== false) { |
|
| 110 | 110 | $nested_properties[] = $property; |
| 111 | 111 | continue; |
| 112 | 112 | } |
| 113 | - $property_transformed_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 114 | - if ( false !== $property_transformed_data ) { |
|
| 115 | - $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 113 | + $property_transformed_data = $this->get_property_data($property, $jsonld, $post_id, $references); |
|
| 114 | + if (false !== $property_transformed_data) { |
|
| 115 | + $jsonld[$property['property_name']] = $property_transformed_data; |
|
| 116 | 116 | } |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $post_id, $references ); |
|
| 119 | + $jsonld = $this->process_nested_properties($nested_properties, $jsonld, $post_id, $references); |
|
| 120 | 120 | |
| 121 | 121 | return $jsonld; |
| 122 | 122 | } |
@@ -131,16 +131,16 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @return array|bool|null |
| 133 | 133 | */ |
| 134 | - public function get_property_data( $property, $jsonld, $post_id, &$references ) { |
|
| 135 | - $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 136 | - $data = $this->get_data_from_data_source( $post_id, $property ); |
|
| 137 | - if ( null !== $transform_instance ) { |
|
| 138 | - $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 139 | - if ( null !== $transform_data ) { |
|
| 140 | - return $this->make_single( $transform_data ); |
|
| 134 | + public function get_property_data($property, $jsonld, $post_id, &$references) { |
|
| 135 | + $transform_instance = $this->transform_functions_registry->get_transform_function($property['transform_function']); |
|
| 136 | + $data = $this->get_data_from_data_source($post_id, $property); |
|
| 137 | + if (null !== $transform_instance) { |
|
| 138 | + $transform_data = $transform_instance->transform_data($data, $jsonld, $references, $post_id); |
|
| 139 | + if (null !== $transform_data) { |
|
| 140 | + return $this->make_single($transform_data); |
|
| 141 | 141 | } |
| 142 | 142 | } else { |
| 143 | - return $this->make_single( $data ); |
|
| 143 | + return $this->make_single($data); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | return false; |
@@ -154,17 +154,17 @@ discard block |
||
| 154 | 154 | * |
| 155 | 155 | * @return array |
| 156 | 156 | */ |
| 157 | - public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references ) { |
|
| 158 | - foreach ( $nested_properties as $property ) { |
|
| 159 | - $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 160 | - if ( false === $property_data ) { |
|
| 157 | + public function process_nested_properties($nested_properties, $jsonld, $post_id, &$references) { |
|
| 158 | + foreach ($nested_properties as $property) { |
|
| 159 | + $property_data = $this->get_property_data($property, $jsonld, $post_id, $references); |
|
| 160 | + if (false === $property_data) { |
|
| 161 | 161 | // No need to create nested levels. |
| 162 | 162 | continue; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - $keys = explode( '/', $property['property_name'] ); |
|
| 165 | + $keys = explode('/', $property['property_name']); |
|
| 166 | 166 | // end is the last level of the nested property. |
| 167 | - $end = array_pop( $keys ); |
|
| 167 | + $end = array_pop($keys); |
|
| 168 | 168 | $current_property_pointer = &$jsonld; |
| 169 | 169 | |
| 170 | 170 | /** |
@@ -172,19 +172,19 @@ discard block |
||
| 172 | 172 | * loop through it and create associative array if the levels |
| 173 | 173 | * didnt exist. |
| 174 | 174 | */ |
| 175 | - while ( count( $keys ) > 0 ) { |
|
| 176 | - $key = array_shift( $keys ); |
|
| 177 | - if ( $key === "" ) { |
|
| 175 | + while (count($keys) > 0) { |
|
| 176 | + $key = array_shift($keys); |
|
| 177 | + if ($key === "") { |
|
| 178 | 178 | continue; |
| 179 | 179 | } |
| 180 | - if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 181 | - $current_property_pointer[ $key ] = array(); |
|
| 180 | + if ( ! array_key_exists($key, $current_property_pointer)) { |
|
| 181 | + $current_property_pointer[$key] = array(); |
|
| 182 | 182 | } |
| 183 | 183 | // We are setting the pointer to the current key, so that at the end |
| 184 | 184 | // we can add the data at last level. |
| 185 | - $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 185 | + $current_property_pointer = &$current_property_pointer[$key]; |
|
| 186 | 186 | } |
| 187 | - $current_property_pointer[ $end ] = $property_data; |
|
| 187 | + $current_property_pointer[$end] = $property_data; |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | return $jsonld; |
@@ -199,21 +199,21 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @return array Returns key, value array, if the value is not found, then it returns null. |
| 201 | 201 | */ |
| 202 | - final public function get_data_from_data_source( $post_id, $property_data ) { |
|
| 202 | + final public function get_data_from_data_source($post_id, $property_data) { |
|
| 203 | 203 | $value = $property_data['field_name']; |
| 204 | 204 | |
| 205 | 205 | // Do 1 to 1 mapping and return result. |
| 206 | - switch ( $property_data['field_type'] ) { |
|
| 206 | + switch ($property_data['field_type']) { |
|
| 207 | 207 | case self::FIELD_TYPE_ACF: |
| 208 | - if ( ! function_exists( 'get_field' ) || ! function_exists( 'get_field_object' ) ) { |
|
| 208 | + if ( ! function_exists('get_field') || ! function_exists('get_field_object')) { |
|
| 209 | 209 | return array(); |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | - return $this->get_data_for_acf_field( $property_data['field_name'], $post_id ); |
|
| 212 | + return $this->get_data_for_acf_field($property_data['field_name'], $post_id); |
|
| 213 | 213 | |
| 214 | 214 | case self::FIELD_TYPE_CUSTOM_FIELD: |
| 215 | 215 | |
| 216 | - return array_map( 'wp_strip_all_tags', get_post_meta( $post_id, $value ) ); |
|
| 216 | + return array_map('wp_strip_all_tags', get_post_meta($post_id, $value)); |
|
| 217 | 217 | |
| 218 | 218 | default: |
| 219 | 219 | return $value; |
@@ -229,26 +229,26 @@ discard block |
||
| 229 | 229 | * |
| 230 | 230 | * @return array|mixed |
| 231 | 231 | */ |
| 232 | - public function get_data_for_acf_field( $field_name, $post_id ) { |
|
| 233 | - $field_data = get_field_object( $field_name, $post_id ); |
|
| 234 | - $data = get_field( $field_name, $post_id ); |
|
| 232 | + public function get_data_for_acf_field($field_name, $post_id) { |
|
| 233 | + $field_data = get_field_object($field_name, $post_id); |
|
| 234 | + $data = get_field($field_name, $post_id); |
|
| 235 | 235 | |
| 236 | 236 | // only process if it is a repeater field, else return the data. |
| 237 | - if ( is_array( $field_data ) && array_key_exists( 'type', $field_data ) |
|
| 238 | - && $field_data['type'] === 'repeater' ) { |
|
| 237 | + if (is_array($field_data) && array_key_exists('type', $field_data) |
|
| 238 | + && $field_data['type'] === 'repeater') { |
|
| 239 | 239 | // check if we have only one sub field, currently we only support one subfield. |
| 240 | - if ( is_array( $data ) && count( $data ) > 0 && count( array_keys( $data[0] ) === 1 ) ) { |
|
| 240 | + if (is_array($data) && count($data) > 0 && count(array_keys($data[0]) === 1)) { |
|
| 241 | 241 | $repeater_formatted_data = array(); |
| 242 | - foreach ( $data as $item ) { |
|
| 243 | - $repeater_formatted_data = array_merge( $repeater_formatted_data, array_values( $item ) ); |
|
| 242 | + foreach ($data as $item) { |
|
| 243 | + $repeater_formatted_data = array_merge($repeater_formatted_data, array_values($item)); |
|
| 244 | 244 | } |
| 245 | 245 | // Remove non unique values. |
| 246 | - $repeater_formatted_data = array_unique( $repeater_formatted_data ); |
|
| 246 | + $repeater_formatted_data = array_unique($repeater_formatted_data); |
|
| 247 | 247 | // Remove empty values |
| 248 | - $repeater_formatted_data = array_filter( $repeater_formatted_data, 'strlen' ); |
|
| 248 | + $repeater_formatted_data = array_filter($repeater_formatted_data, 'strlen'); |
|
| 249 | 249 | |
| 250 | 250 | // re-index all the values. |
| 251 | - return array_values( $repeater_formatted_data ); |
|
| 251 | + return array_values($repeater_formatted_data); |
|
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | |
@@ -256,16 +256,16 @@ discard block |
||
| 256 | 256 | return $data; |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | - private function make_single( $value ) { |
|
| 259 | + private function make_single($value) { |
|
| 260 | 260 | |
| 261 | 261 | $values = (array) $value; |
| 262 | 262 | |
| 263 | - if ( empty( $values ) ) { |
|
| 263 | + if (empty($values)) { |
|
| 264 | 264 | return null; |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 268 | - return current( $values ); |
|
| 267 | + if (1 === count($values) && 0 === key($values)) { |
|
| 268 | + return current($values); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | return $values; |
@@ -16,506 +16,506 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Entity_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The Log service. |
|
| 21 | - * |
|
| 22 | - * @since 3.2.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The UI service. |
|
| 30 | - * |
|
| 31 | - * @since 3.2.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 34 | - */ |
|
| 35 | - private $ui_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.15.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $relation_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 48 | - * |
|
| 49 | - * @since 3.16.3 |
|
| 50 | - * @access private |
|
| 51 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 52 | - */ |
|
| 53 | - private $entity_uri_service; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The entity post type name. |
|
| 57 | - * |
|
| 58 | - * @since 3.1.0 |
|
| 59 | - */ |
|
| 60 | - const TYPE_NAME = 'entity'; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * The alternative label meta key. |
|
| 64 | - * |
|
| 65 | - * @since 3.2.0 |
|
| 66 | - */ |
|
| 67 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The alternative label input template. |
|
| 71 | - * |
|
| 72 | - * @since 3.2.0 |
|
| 73 | - */ |
|
| 74 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 75 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 19 | + /** |
|
| 20 | + * The Log service. |
|
| 21 | + * |
|
| 22 | + * @since 3.2.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The UI service. |
|
| 30 | + * |
|
| 31 | + * @since 3.2.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 34 | + */ |
|
| 35 | + private $ui_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.15.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $relation_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 48 | + * |
|
| 49 | + * @since 3.16.3 |
|
| 50 | + * @access private |
|
| 51 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 52 | + */ |
|
| 53 | + private $entity_uri_service; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The entity post type name. |
|
| 57 | + * |
|
| 58 | + * @since 3.1.0 |
|
| 59 | + */ |
|
| 60 | + const TYPE_NAME = 'entity'; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * The alternative label meta key. |
|
| 64 | + * |
|
| 65 | + * @since 3.2.0 |
|
| 66 | + */ |
|
| 67 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The alternative label input template. |
|
| 71 | + * |
|
| 72 | + * @since 3.2.0 |
|
| 73 | + */ |
|
| 74 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 75 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 76 | 76 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 77 | 77 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 78 | 78 | <button class="button wl-delete-button">%s</button> |
| 79 | 79 | </div>'; |
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * A singleton instance of the Entity service. |
|
| 83 | - * |
|
| 84 | - * @since 3.2.0 |
|
| 85 | - * @access private |
|
| 86 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 87 | - */ |
|
| 88 | - private static $instance; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Create a Wordlift_Entity_Service instance. |
|
| 92 | - * |
|
| 93 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 94 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 95 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 96 | - * |
|
| 97 | - * @since 3.2.0 |
|
| 98 | - * |
|
| 99 | - */ |
|
| 100 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 101 | - |
|
| 102 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 103 | - |
|
| 104 | - $this->ui_service = $ui_service; |
|
| 105 | - $this->relation_service = $relation_service; |
|
| 106 | - $this->entity_uri_service = $entity_uri_service; |
|
| 107 | - |
|
| 108 | - // Set the singleton instance. |
|
| 109 | - self::$instance = $this; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Get the singleton instance of the Entity service. |
|
| 114 | - * |
|
| 115 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 116 | - * @since 3.2.0 |
|
| 117 | - */ |
|
| 118 | - public static function get_instance() { |
|
| 119 | - |
|
| 120 | - return self::$instance; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 125 | - * something which is not an article. |
|
| 126 | - * |
|
| 127 | - * @param int $post_id A post id. |
|
| 128 | - * |
|
| 129 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 130 | - * @since 3.1.0 |
|
| 131 | - * |
|
| 132 | - */ |
|
| 133 | - public function is_entity( $post_id ) { |
|
| 134 | - |
|
| 135 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 136 | - |
|
| 137 | - if ( is_wp_error( $terms ) ) { |
|
| 138 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 139 | - |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - if ( empty( $terms ) ) { |
|
| 144 | - return false; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /* |
|
| 81 | + /** |
|
| 82 | + * A singleton instance of the Entity service. |
|
| 83 | + * |
|
| 84 | + * @since 3.2.0 |
|
| 85 | + * @access private |
|
| 86 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 87 | + */ |
|
| 88 | + private static $instance; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Create a Wordlift_Entity_Service instance. |
|
| 92 | + * |
|
| 93 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 94 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 95 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 96 | + * |
|
| 97 | + * @since 3.2.0 |
|
| 98 | + * |
|
| 99 | + */ |
|
| 100 | + public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 101 | + |
|
| 102 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 103 | + |
|
| 104 | + $this->ui_service = $ui_service; |
|
| 105 | + $this->relation_service = $relation_service; |
|
| 106 | + $this->entity_uri_service = $entity_uri_service; |
|
| 107 | + |
|
| 108 | + // Set the singleton instance. |
|
| 109 | + self::$instance = $this; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Get the singleton instance of the Entity service. |
|
| 114 | + * |
|
| 115 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 116 | + * @since 3.2.0 |
|
| 117 | + */ |
|
| 118 | + public static function get_instance() { |
|
| 119 | + |
|
| 120 | + return self::$instance; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 125 | + * something which is not an article. |
|
| 126 | + * |
|
| 127 | + * @param int $post_id A post id. |
|
| 128 | + * |
|
| 129 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 130 | + * @since 3.1.0 |
|
| 131 | + * |
|
| 132 | + */ |
|
| 133 | + public function is_entity( $post_id ) { |
|
| 134 | + |
|
| 135 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 136 | + |
|
| 137 | + if ( is_wp_error( $terms ) ) { |
|
| 138 | + $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 139 | + |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + if ( empty( $terms ) ) { |
|
| 144 | + return false; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /* |
|
| 148 | 148 | * We don't consider an `article` to be an entity. |
| 149 | 149 | * |
| 150 | 150 | * @since 3.20.0 At least one associated mustn't be an `article`. |
| 151 | 151 | * |
| 152 | 152 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 153 | 153 | */ |
| 154 | - foreach ( $terms as $term ) { |
|
| 155 | - if ( 'article' !== $term->slug ) { |
|
| 156 | - return true; |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return false; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Get the proper classification scope for a given entity post |
|
| 165 | - * |
|
| 166 | - * @param integer $post_id An entity post id. |
|
| 167 | - * |
|
| 168 | - * @param string $default The default classification scope, `what` if not |
|
| 169 | - * provided. |
|
| 170 | - * |
|
| 171 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 172 | - * @since 3.5.0 |
|
| 173 | - * |
|
| 174 | - */ |
|
| 175 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 176 | - |
|
| 177 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 178 | - return $default; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // Retrieve the entity type |
|
| 182 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 183 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 184 | - // Retrieve classification boxes configuration |
|
| 185 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 186 | - foreach ( $classification_boxes as $cb ) { |
|
| 187 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 188 | - return $cb['id']; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - return $default; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Check whether a {@link WP_Post} is used. |
|
| 197 | - * |
|
| 198 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 199 | - * |
|
| 200 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 201 | - */ |
|
| 202 | - public function is_used( $post_id ) { |
|
| 203 | - |
|
| 204 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 205 | - return null; |
|
| 206 | - } |
|
| 207 | - // Retrieve the post |
|
| 208 | - $entity = get_post( $post_id ); |
|
| 209 | - |
|
| 210 | - global $wpdb; |
|
| 211 | - // Retrieve Wordlift relation instances table name |
|
| 212 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 213 | - |
|
| 214 | - // Check is it's referenced / related to another post / entity |
|
| 215 | - $stmt = $wpdb->prepare( |
|
| 216 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 217 | - $entity->ID |
|
| 218 | - ); |
|
| 219 | - |
|
| 220 | - // Perform the query |
|
| 221 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 222 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 223 | - if ( 0 < $relation_instances ) { |
|
| 224 | - return true; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // Check if the entity uri is used as meta_value |
|
| 228 | - $stmt = $wpdb->prepare( |
|
| 229 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 230 | - $entity->ID, |
|
| 231 | - wl_get_entity_uri( $entity->ID ) |
|
| 232 | - ); |
|
| 233 | - // Perform the query |
|
| 234 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 235 | - |
|
| 236 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 237 | - if ( 0 < $meta_instances ) { |
|
| 238 | - return true; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - // If we are here, it means the current entity is not used at the moment |
|
| 242 | - return false; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 247 | - * |
|
| 248 | - * @param string $uri The entity URI. |
|
| 249 | - * |
|
| 250 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 251 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 252 | - * |
|
| 253 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 254 | - * @since 3.2.0 |
|
| 255 | - * |
|
| 256 | - */ |
|
| 257 | - public function get_entity_post_by_uri( $uri ) { |
|
| 258 | - |
|
| 259 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 264 | - * we check that the post we're saving is the current post. |
|
| 265 | - * |
|
| 266 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 267 | - * |
|
| 268 | - * @since 3.2.0 |
|
| 269 | - * |
|
| 270 | - * @param int $post_id Post ID. |
|
| 271 | - * @param WP_Post $post Post object. |
|
| 272 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 273 | - */ |
|
| 274 | - public function save_post( $post_id, $post, $update ) { |
|
| 275 | - |
|
| 276 | - // Avoid doing anything if post is autosave or a revision. |
|
| 277 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 278 | - return; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - // We're setting the alternative label that have been provided via the UI |
|
| 282 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 283 | - // programmatically by some other function: we need to check therefore if |
|
| 284 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 285 | - // |
|
| 286 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 287 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 288 | - return; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - // Get the alt labels from the request (or empty array). |
|
| 292 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 293 | - |
|
| 294 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 295 | - // This is via classic editor, so set the alternative labels. |
|
| 296 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Set the alternative labels. |
|
| 304 | - * |
|
| 305 | - * @param int $post_id The post id. |
|
| 306 | - * @param array $alt_labels An array of labels. |
|
| 307 | - * |
|
| 308 | - * @since 3.2.0 |
|
| 309 | - * |
|
| 310 | - */ |
|
| 311 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 312 | - |
|
| 313 | - // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 314 | - // instead of post id. |
|
| 315 | - if ( ! is_numeric( $post_id ) ) { |
|
| 316 | - return; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - // Force $alt_labels to be an array |
|
| 320 | - if ( ! is_array( $alt_labels ) ) { |
|
| 321 | - $alt_labels = array( $alt_labels ); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 325 | - |
|
| 326 | - // Delete all the existing alternate labels. |
|
| 327 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 328 | - |
|
| 329 | - // Set the alternative labels. |
|
| 330 | - foreach ( $alt_labels as $alt_label ) { |
|
| 331 | - if ( ! empty( $alt_label ) ) { |
|
| 332 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Retrieve the alternate labels. |
|
| 340 | - * |
|
| 341 | - * @param int $post_id Post id. |
|
| 342 | - * |
|
| 343 | - * @return mixed An array of alternative labels. |
|
| 344 | - * @since 3.2.0 |
|
| 345 | - * |
|
| 346 | - */ |
|
| 347 | - public function get_alternative_labels( $post_id ) { |
|
| 348 | - |
|
| 349 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 354 | - * |
|
| 355 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 356 | - * |
|
| 357 | - * @return array An array with the entity title and labels. |
|
| 358 | - * @since 3.12.0 |
|
| 359 | - * |
|
| 360 | - */ |
|
| 361 | - public function get_labels( $post_id ) { |
|
| 362 | - |
|
| 363 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 368 | - * |
|
| 369 | - * @param WP_Post $post Post object. |
|
| 370 | - * |
|
| 371 | - * @since 3.2.0 |
|
| 372 | - * |
|
| 373 | - */ |
|
| 374 | - public function edit_form_before_permalink( $post ) { |
|
| 375 | - |
|
| 376 | - // If it's not an entity, return. |
|
| 377 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 378 | - return; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - // Print the input template. |
|
| 382 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 383 | - |
|
| 384 | - // Print all the currently set alternative labels. |
|
| 385 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 386 | - |
|
| 387 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 388 | - |
|
| 389 | - }; |
|
| 390 | - |
|
| 391 | - // Print the button. |
|
| 392 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 393 | - |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Get the URI for the entity with the specified post id. |
|
| 398 | - * |
|
| 399 | - * @param int $post_id The entity post id. |
|
| 400 | - * |
|
| 401 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 402 | - * @since 3.6.0 |
|
| 403 | - * |
|
| 404 | - */ |
|
| 405 | - public function get_uri( $post_id ) { |
|
| 406 | - |
|
| 407 | - // If a null is given, nothing to do |
|
| 408 | - if ( is_null( $post_id ) ) { |
|
| 409 | - return null; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
| 413 | - |
|
| 414 | - // If the dataset uri is not properly configured, null is returned |
|
| 415 | - if ( empty( $dataset_uri ) ) { |
|
| 416 | - return null; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 420 | - |
|
| 421 | - /* |
|
| 154 | + foreach ( $terms as $term ) { |
|
| 155 | + if ( 'article' !== $term->slug ) { |
|
| 156 | + return true; |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Get the proper classification scope for a given entity post |
|
| 165 | + * |
|
| 166 | + * @param integer $post_id An entity post id. |
|
| 167 | + * |
|
| 168 | + * @param string $default The default classification scope, `what` if not |
|
| 169 | + * provided. |
|
| 170 | + * |
|
| 171 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 172 | + * @since 3.5.0 |
|
| 173 | + * |
|
| 174 | + */ |
|
| 175 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 176 | + |
|
| 177 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 178 | + return $default; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // Retrieve the entity type |
|
| 182 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 183 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 184 | + // Retrieve classification boxes configuration |
|
| 185 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 186 | + foreach ( $classification_boxes as $cb ) { |
|
| 187 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 188 | + return $cb['id']; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + return $default; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Check whether a {@link WP_Post} is used. |
|
| 197 | + * |
|
| 198 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 199 | + * |
|
| 200 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 201 | + */ |
|
| 202 | + public function is_used( $post_id ) { |
|
| 203 | + |
|
| 204 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 205 | + return null; |
|
| 206 | + } |
|
| 207 | + // Retrieve the post |
|
| 208 | + $entity = get_post( $post_id ); |
|
| 209 | + |
|
| 210 | + global $wpdb; |
|
| 211 | + // Retrieve Wordlift relation instances table name |
|
| 212 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 213 | + |
|
| 214 | + // Check is it's referenced / related to another post / entity |
|
| 215 | + $stmt = $wpdb->prepare( |
|
| 216 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 217 | + $entity->ID |
|
| 218 | + ); |
|
| 219 | + |
|
| 220 | + // Perform the query |
|
| 221 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 222 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 223 | + if ( 0 < $relation_instances ) { |
|
| 224 | + return true; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // Check if the entity uri is used as meta_value |
|
| 228 | + $stmt = $wpdb->prepare( |
|
| 229 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 230 | + $entity->ID, |
|
| 231 | + wl_get_entity_uri( $entity->ID ) |
|
| 232 | + ); |
|
| 233 | + // Perform the query |
|
| 234 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 235 | + |
|
| 236 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 237 | + if ( 0 < $meta_instances ) { |
|
| 238 | + return true; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + // If we are here, it means the current entity is not used at the moment |
|
| 242 | + return false; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 247 | + * |
|
| 248 | + * @param string $uri The entity URI. |
|
| 249 | + * |
|
| 250 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 251 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 252 | + * |
|
| 253 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 254 | + * @since 3.2.0 |
|
| 255 | + * |
|
| 256 | + */ |
|
| 257 | + public function get_entity_post_by_uri( $uri ) { |
|
| 258 | + |
|
| 259 | + return $this->entity_uri_service->get_entity( $uri ); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 264 | + * we check that the post we're saving is the current post. |
|
| 265 | + * |
|
| 266 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 267 | + * |
|
| 268 | + * @since 3.2.0 |
|
| 269 | + * |
|
| 270 | + * @param int $post_id Post ID. |
|
| 271 | + * @param WP_Post $post Post object. |
|
| 272 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 273 | + */ |
|
| 274 | + public function save_post( $post_id, $post, $update ) { |
|
| 275 | + |
|
| 276 | + // Avoid doing anything if post is autosave or a revision. |
|
| 277 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 278 | + return; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + // We're setting the alternative label that have been provided via the UI |
|
| 282 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 283 | + // programmatically by some other function: we need to check therefore if |
|
| 284 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 285 | + // |
|
| 286 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 287 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 288 | + return; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + // Get the alt labels from the request (or empty array). |
|
| 292 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 293 | + |
|
| 294 | + if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 295 | + // This is via classic editor, so set the alternative labels. |
|
| 296 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Set the alternative labels. |
|
| 304 | + * |
|
| 305 | + * @param int $post_id The post id. |
|
| 306 | + * @param array $alt_labels An array of labels. |
|
| 307 | + * |
|
| 308 | + * @since 3.2.0 |
|
| 309 | + * |
|
| 310 | + */ |
|
| 311 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 312 | + |
|
| 313 | + // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 314 | + // instead of post id. |
|
| 315 | + if ( ! is_numeric( $post_id ) ) { |
|
| 316 | + return; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + // Force $alt_labels to be an array |
|
| 320 | + if ( ! is_array( $alt_labels ) ) { |
|
| 321 | + $alt_labels = array( $alt_labels ); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 325 | + |
|
| 326 | + // Delete all the existing alternate labels. |
|
| 327 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 328 | + |
|
| 329 | + // Set the alternative labels. |
|
| 330 | + foreach ( $alt_labels as $alt_label ) { |
|
| 331 | + if ( ! empty( $alt_label ) ) { |
|
| 332 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Retrieve the alternate labels. |
|
| 340 | + * |
|
| 341 | + * @param int $post_id Post id. |
|
| 342 | + * |
|
| 343 | + * @return mixed An array of alternative labels. |
|
| 344 | + * @since 3.2.0 |
|
| 345 | + * |
|
| 346 | + */ |
|
| 347 | + public function get_alternative_labels( $post_id ) { |
|
| 348 | + |
|
| 349 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 354 | + * |
|
| 355 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 356 | + * |
|
| 357 | + * @return array An array with the entity title and labels. |
|
| 358 | + * @since 3.12.0 |
|
| 359 | + * |
|
| 360 | + */ |
|
| 361 | + public function get_labels( $post_id ) { |
|
| 362 | + |
|
| 363 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 368 | + * |
|
| 369 | + * @param WP_Post $post Post object. |
|
| 370 | + * |
|
| 371 | + * @since 3.2.0 |
|
| 372 | + * |
|
| 373 | + */ |
|
| 374 | + public function edit_form_before_permalink( $post ) { |
|
| 375 | + |
|
| 376 | + // If it's not an entity, return. |
|
| 377 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 378 | + return; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + // Print the input template. |
|
| 382 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 383 | + |
|
| 384 | + // Print all the currently set alternative labels. |
|
| 385 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 386 | + |
|
| 387 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 388 | + |
|
| 389 | + }; |
|
| 390 | + |
|
| 391 | + // Print the button. |
|
| 392 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 393 | + |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Get the URI for the entity with the specified post id. |
|
| 398 | + * |
|
| 399 | + * @param int $post_id The entity post id. |
|
| 400 | + * |
|
| 401 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 402 | + * @since 3.6.0 |
|
| 403 | + * |
|
| 404 | + */ |
|
| 405 | + public function get_uri( $post_id ) { |
|
| 406 | + |
|
| 407 | + // If a null is given, nothing to do |
|
| 408 | + if ( is_null( $post_id ) ) { |
|
| 409 | + return null; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
| 413 | + |
|
| 414 | + // If the dataset uri is not properly configured, null is returned |
|
| 415 | + if ( empty( $dataset_uri ) ) { |
|
| 416 | + return null; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 420 | + |
|
| 421 | + /* |
|
| 422 | 422 | * Consider the URI invalid if it doesn't start with the dataset URI. |
| 423 | 423 | * |
| 424 | 424 | * @see https://github.com/insideout10/wordlift-plugin/issues/996 |
| 425 | 425 | */ |
| 426 | - if ( 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 427 | - $uri = null; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - // Set the URI if it isn't set yet. |
|
| 431 | - $post_status = get_post_status( $post_id ); |
|
| 432 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 433 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 434 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - return $uri; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Get the alternative label input HTML code. |
|
| 443 | - * |
|
| 444 | - * @param string $value The input value. |
|
| 445 | - * |
|
| 446 | - * @return string The input HTML code. |
|
| 447 | - * @since 3.2.0 |
|
| 448 | - * |
|
| 449 | - */ |
|
| 450 | - private function get_alternative_label_input( $value = '' ) { |
|
| 451 | - |
|
| 452 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Get the number of entity posts published in this blog. |
|
| 457 | - * |
|
| 458 | - * @return int The number of published entity posts. |
|
| 459 | - * @since 3.6.0 |
|
| 460 | - * |
|
| 461 | - */ |
|
| 462 | - public function count() { |
|
| 463 | - global $wpdb; |
|
| 464 | - |
|
| 465 | - // Try to get the count from the transient. |
|
| 466 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 467 | - if ( false !== $count ) { |
|
| 468 | - return $count; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - // Query the count. |
|
| 472 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 473 | - "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 474 | - . " FROM {$wpdb->term_relationships} tr" |
|
| 475 | - . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 476 | - . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 477 | - . " INNER JOIN {$wpdb->terms} t" |
|
| 478 | - . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 479 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 480 | - 'article' |
|
| 481 | - ) ); |
|
| 482 | - |
|
| 483 | - // Store the count in cache. |
|
| 484 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 485 | - |
|
| 486 | - return $count; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 491 | - * call. |
|
| 492 | - * |
|
| 493 | - * @param array $args The arguments for a `get_posts` call. |
|
| 494 | - * |
|
| 495 | - * @return array The arguments for a `get_posts` call. |
|
| 496 | - * @since 3.15.0 |
|
| 497 | - * |
|
| 498 | - */ |
|
| 499 | - public static function add_criterias( $args ) { |
|
| 500 | - |
|
| 501 | - // Build an optimal tax-query. |
|
| 502 | - $tax_query = array( |
|
| 503 | - 'relation' => 'AND', |
|
| 504 | - array( |
|
| 505 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 506 | - 'operator' => 'EXISTS', |
|
| 507 | - ), |
|
| 508 | - array( |
|
| 509 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 510 | - 'field' => 'slug', |
|
| 511 | - 'terms' => 'article', |
|
| 512 | - 'operator' => 'NOT IN', |
|
| 513 | - ), |
|
| 514 | - ); |
|
| 515 | - |
|
| 516 | - return $args + array( |
|
| 517 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 518 | - /* |
|
| 426 | + if ( 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 427 | + $uri = null; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + // Set the URI if it isn't set yet. |
|
| 431 | + $post_status = get_post_status( $post_id ); |
|
| 432 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 433 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 434 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + return $uri; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Get the alternative label input HTML code. |
|
| 443 | + * |
|
| 444 | + * @param string $value The input value. |
|
| 445 | + * |
|
| 446 | + * @return string The input HTML code. |
|
| 447 | + * @since 3.2.0 |
|
| 448 | + * |
|
| 449 | + */ |
|
| 450 | + private function get_alternative_label_input( $value = '' ) { |
|
| 451 | + |
|
| 452 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Get the number of entity posts published in this blog. |
|
| 457 | + * |
|
| 458 | + * @return int The number of published entity posts. |
|
| 459 | + * @since 3.6.0 |
|
| 460 | + * |
|
| 461 | + */ |
|
| 462 | + public function count() { |
|
| 463 | + global $wpdb; |
|
| 464 | + |
|
| 465 | + // Try to get the count from the transient. |
|
| 466 | + $count = get_transient( '_wl_entity_service__count' ); |
|
| 467 | + if ( false !== $count ) { |
|
| 468 | + return $count; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + // Query the count. |
|
| 472 | + $count = $wpdb->get_var( $wpdb->prepare( |
|
| 473 | + "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 474 | + . " FROM {$wpdb->term_relationships} tr" |
|
| 475 | + . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 476 | + . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 477 | + . " INNER JOIN {$wpdb->terms} t" |
|
| 478 | + . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 479 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 480 | + 'article' |
|
| 481 | + ) ); |
|
| 482 | + |
|
| 483 | + // Store the count in cache. |
|
| 484 | + set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 485 | + |
|
| 486 | + return $count; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 491 | + * call. |
|
| 492 | + * |
|
| 493 | + * @param array $args The arguments for a `get_posts` call. |
|
| 494 | + * |
|
| 495 | + * @return array The arguments for a `get_posts` call. |
|
| 496 | + * @since 3.15.0 |
|
| 497 | + * |
|
| 498 | + */ |
|
| 499 | + public static function add_criterias( $args ) { |
|
| 500 | + |
|
| 501 | + // Build an optimal tax-query. |
|
| 502 | + $tax_query = array( |
|
| 503 | + 'relation' => 'AND', |
|
| 504 | + array( |
|
| 505 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 506 | + 'operator' => 'EXISTS', |
|
| 507 | + ), |
|
| 508 | + array( |
|
| 509 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 510 | + 'field' => 'slug', |
|
| 511 | + 'terms' => 'article', |
|
| 512 | + 'operator' => 'NOT IN', |
|
| 513 | + ), |
|
| 514 | + ); |
|
| 515 | + |
|
| 516 | + return $args + array( |
|
| 517 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 518 | + /* |
|
| 519 | 519 | * Ensure compatibility with Polylang. |
| 520 | 520 | * |
| 521 | 521 | * @see https://github.com/insideout10/wordlift-plugin/issues/855. |
@@ -523,102 +523,102 @@ discard block |
||
| 523 | 523 | * |
| 524 | 524 | * @since 3.19.5 |
| 525 | 525 | */ |
| 526 | - 'lang' => '', |
|
| 527 | - 'tax_query' => $tax_query, |
|
| 528 | - ); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * Create a new entity. |
|
| 533 | - * |
|
| 534 | - * @param string $name The entity name. |
|
| 535 | - * @param string $type_uri The entity's type URI. |
|
| 536 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 537 | - * @param string $status The post status, by default 'publish'. |
|
| 538 | - * |
|
| 539 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 540 | - * @since 3.9.0 |
|
| 541 | - * |
|
| 542 | - */ |
|
| 543 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 544 | - |
|
| 545 | - // Create an entity for the publisher. |
|
| 546 | - $post_id = @wp_insert_post( array( |
|
| 547 | - 'post_type' => self::TYPE_NAME, |
|
| 548 | - 'post_title' => $name, |
|
| 549 | - 'post_status' => $status, |
|
| 550 | - 'post_content' => '', |
|
| 551 | - ) ); |
|
| 552 | - |
|
| 553 | - // Return the error if any. |
|
| 554 | - if ( is_wp_error( $post_id ) ) { |
|
| 555 | - return $post_id; |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - // Set the entity logo. |
|
| 559 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 560 | - set_post_thumbnail( $post_id, $logo ); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - // Set the entity type. |
|
| 564 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 565 | - |
|
| 566 | - return $post_id; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Get the entities related to the one with the specified id. By default only |
|
| 571 | - * published entities will be returned. |
|
| 572 | - * |
|
| 573 | - * @param int $id The post id. |
|
| 574 | - * @param string $post_status The target post status (default = publish). |
|
| 575 | - * |
|
| 576 | - * @return array An array of post ids. |
|
| 577 | - * @since 3.10.0 |
|
| 578 | - * |
|
| 579 | - */ |
|
| 580 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 581 | - |
|
| 582 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - /** |
|
| 586 | - * Get the list of entities. |
|
| 587 | - * |
|
| 588 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 589 | - * |
|
| 590 | - * @return array An array of entity posts. |
|
| 591 | - * @since 3.12.2 |
|
| 592 | - * |
|
| 593 | - */ |
|
| 594 | - public function get( $params = array() ) { |
|
| 595 | - |
|
| 596 | - // Set the defaults. |
|
| 597 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 598 | - |
|
| 599 | - // Merge the defaults with the provided parameters. |
|
| 600 | - $args = wp_parse_args( $params, $defaults ); |
|
| 601 | - |
|
| 602 | - // Call the `get_posts` function. |
|
| 603 | - return get_posts( $args ); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * The list of post type names which can be used for entities |
|
| 608 | - * |
|
| 609 | - * Criteria is that the post type is public. The list of valid post types |
|
| 610 | - * can be overridden with a filter. |
|
| 611 | - * |
|
| 612 | - * @return array Array containing the names of the valid post types. |
|
| 613 | - * @since 3.15.0 |
|
| 614 | - * |
|
| 615 | - */ |
|
| 616 | - static function valid_entity_post_types() { |
|
| 617 | - |
|
| 618 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 619 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 620 | - |
|
| 621 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 622 | - } |
|
| 526 | + 'lang' => '', |
|
| 527 | + 'tax_query' => $tax_query, |
|
| 528 | + ); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * Create a new entity. |
|
| 533 | + * |
|
| 534 | + * @param string $name The entity name. |
|
| 535 | + * @param string $type_uri The entity's type URI. |
|
| 536 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 537 | + * @param string $status The post status, by default 'publish'. |
|
| 538 | + * |
|
| 539 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 540 | + * @since 3.9.0 |
|
| 541 | + * |
|
| 542 | + */ |
|
| 543 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 544 | + |
|
| 545 | + // Create an entity for the publisher. |
|
| 546 | + $post_id = @wp_insert_post( array( |
|
| 547 | + 'post_type' => self::TYPE_NAME, |
|
| 548 | + 'post_title' => $name, |
|
| 549 | + 'post_status' => $status, |
|
| 550 | + 'post_content' => '', |
|
| 551 | + ) ); |
|
| 552 | + |
|
| 553 | + // Return the error if any. |
|
| 554 | + if ( is_wp_error( $post_id ) ) { |
|
| 555 | + return $post_id; |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + // Set the entity logo. |
|
| 559 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 560 | + set_post_thumbnail( $post_id, $logo ); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + // Set the entity type. |
|
| 564 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 565 | + |
|
| 566 | + return $post_id; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Get the entities related to the one with the specified id. By default only |
|
| 571 | + * published entities will be returned. |
|
| 572 | + * |
|
| 573 | + * @param int $id The post id. |
|
| 574 | + * @param string $post_status The target post status (default = publish). |
|
| 575 | + * |
|
| 576 | + * @return array An array of post ids. |
|
| 577 | + * @since 3.10.0 |
|
| 578 | + * |
|
| 579 | + */ |
|
| 580 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 581 | + |
|
| 582 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + /** |
|
| 586 | + * Get the list of entities. |
|
| 587 | + * |
|
| 588 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 589 | + * |
|
| 590 | + * @return array An array of entity posts. |
|
| 591 | + * @since 3.12.2 |
|
| 592 | + * |
|
| 593 | + */ |
|
| 594 | + public function get( $params = array() ) { |
|
| 595 | + |
|
| 596 | + // Set the defaults. |
|
| 597 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 598 | + |
|
| 599 | + // Merge the defaults with the provided parameters. |
|
| 600 | + $args = wp_parse_args( $params, $defaults ); |
|
| 601 | + |
|
| 602 | + // Call the `get_posts` function. |
|
| 603 | + return get_posts( $args ); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * The list of post type names which can be used for entities |
|
| 608 | + * |
|
| 609 | + * Criteria is that the post type is public. The list of valid post types |
|
| 610 | + * can be overridden with a filter. |
|
| 611 | + * |
|
| 612 | + * @return array Array containing the names of the valid post types. |
|
| 613 | + * @since 3.15.0 |
|
| 614 | + * |
|
| 615 | + */ |
|
| 616 | + static function valid_entity_post_types() { |
|
| 617 | + |
|
| 618 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 619 | + $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 620 | + |
|
| 621 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 622 | + } |
|
| 623 | 623 | |
| 624 | 624 | } |
@@ -97,9 +97,9 @@ discard block |
||
| 97 | 97 | * @since 3.2.0 |
| 98 | 98 | * |
| 99 | 99 | */ |
| 100 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 100 | + public function __construct($ui_service, $relation_service, $entity_uri_service) { |
|
| 101 | 101 | |
| 102 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 102 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 103 | 103 | |
| 104 | 104 | $this->ui_service = $ui_service; |
| 105 | 105 | $this->relation_service = $relation_service; |
@@ -130,17 +130,17 @@ discard block |
||
| 130 | 130 | * @since 3.1.0 |
| 131 | 131 | * |
| 132 | 132 | */ |
| 133 | - public function is_entity( $post_id ) { |
|
| 133 | + public function is_entity($post_id) { |
|
| 134 | 134 | |
| 135 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 135 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 136 | 136 | |
| 137 | - if ( is_wp_error( $terms ) ) { |
|
| 138 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 137 | + if (is_wp_error($terms)) { |
|
| 138 | + $this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message()); |
|
| 139 | 139 | |
| 140 | 140 | return false; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - if ( empty( $terms ) ) { |
|
| 143 | + if (empty($terms)) { |
|
| 144 | 144 | return false; |
| 145 | 145 | } |
| 146 | 146 | |
@@ -151,8 +151,8 @@ discard block |
||
| 151 | 151 | * |
| 152 | 152 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 153 | 153 | */ |
| 154 | - foreach ( $terms as $term ) { |
|
| 155 | - if ( 'article' !== $term->slug ) { |
|
| 154 | + foreach ($terms as $term) { |
|
| 155 | + if ('article' !== $term->slug) { |
|
| 156 | 156 | return true; |
| 157 | 157 | } |
| 158 | 158 | } |
@@ -172,19 +172,19 @@ discard block |
||
| 172 | 172 | * @since 3.5.0 |
| 173 | 173 | * |
| 174 | 174 | */ |
| 175 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 175 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 176 | 176 | |
| 177 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 177 | + if (false === $this->is_entity($post_id)) { |
|
| 178 | 178 | return $default; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | // Retrieve the entity type |
| 182 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 183 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 182 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 183 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 184 | 184 | // Retrieve classification boxes configuration |
| 185 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 186 | - foreach ( $classification_boxes as $cb ) { |
|
| 187 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 185 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 186 | + foreach ($classification_boxes as $cb) { |
|
| 187 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 188 | 188 | return $cb['id']; |
| 189 | 189 | } |
| 190 | 190 | } |
@@ -199,13 +199,13 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 201 | 201 | */ |
| 202 | - public function is_used( $post_id ) { |
|
| 202 | + public function is_used($post_id) { |
|
| 203 | 203 | |
| 204 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 204 | + if (false === $this->is_entity($post_id)) { |
|
| 205 | 205 | return null; |
| 206 | 206 | } |
| 207 | 207 | // Retrieve the post |
| 208 | - $entity = get_post( $post_id ); |
|
| 208 | + $entity = get_post($post_id); |
|
| 209 | 209 | |
| 210 | 210 | global $wpdb; |
| 211 | 211 | // Retrieve Wordlift relation instances table name |
@@ -218,9 +218,9 @@ discard block |
||
| 218 | 218 | ); |
| 219 | 219 | |
| 220 | 220 | // Perform the query |
| 221 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 221 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 222 | 222 | // If there is at least one relation instance for the current entity, then it's used |
| 223 | - if ( 0 < $relation_instances ) { |
|
| 223 | + if (0 < $relation_instances) { |
|
| 224 | 224 | return true; |
| 225 | 225 | } |
| 226 | 226 | |
@@ -228,13 +228,13 @@ discard block |
||
| 228 | 228 | $stmt = $wpdb->prepare( |
| 229 | 229 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 230 | 230 | $entity->ID, |
| 231 | - wl_get_entity_uri( $entity->ID ) |
|
| 231 | + wl_get_entity_uri($entity->ID) |
|
| 232 | 232 | ); |
| 233 | 233 | // Perform the query |
| 234 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 234 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 235 | 235 | |
| 236 | 236 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 237 | - if ( 0 < $meta_instances ) { |
|
| 237 | + if (0 < $meta_instances) { |
|
| 238 | 238 | return true; |
| 239 | 239 | } |
| 240 | 240 | |
@@ -254,9 +254,9 @@ discard block |
||
| 254 | 254 | * @since 3.2.0 |
| 255 | 255 | * |
| 256 | 256 | */ |
| 257 | - public function get_entity_post_by_uri( $uri ) { |
|
| 257 | + public function get_entity_post_by_uri($uri) { |
|
| 258 | 258 | |
| 259 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 259 | + return $this->entity_uri_service->get_entity($uri); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | /** |
@@ -271,10 +271,10 @@ discard block |
||
| 271 | 271 | * @param WP_Post $post Post object. |
| 272 | 272 | * @param bool $update Whether this is an existing post being updated or not. |
| 273 | 273 | */ |
| 274 | - public function save_post( $post_id, $post, $update ) { |
|
| 274 | + public function save_post($post_id, $post, $update) { |
|
| 275 | 275 | |
| 276 | 276 | // Avoid doing anything if post is autosave or a revision. |
| 277 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 277 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 278 | 278 | return; |
| 279 | 279 | } |
| 280 | 280 | |
@@ -284,16 +284,16 @@ discard block |
||
| 284 | 284 | // the $post_id in the save_post call matches the post id set in the request. |
| 285 | 285 | // |
| 286 | 286 | // If this is not the current post being saved or if it's not an entity, return. |
| 287 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 287 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 288 | 288 | return; |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | // Get the alt labels from the request (or empty array). |
| 292 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 292 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 293 | 293 | |
| 294 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 294 | + if (( ! empty($_POST['content']) && ! empty($_POST['post_content'])) || isset($_REQUEST['wl_alternative_label'])) { |
|
| 295 | 295 | // This is via classic editor, so set the alternative labels. |
| 296 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 296 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | |
@@ -308,28 +308,28 @@ discard block |
||
| 308 | 308 | * @since 3.2.0 |
| 309 | 309 | * |
| 310 | 310 | */ |
| 311 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 311 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 312 | 312 | |
| 313 | 313 | // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
| 314 | 314 | // instead of post id. |
| 315 | - if ( ! is_numeric( $post_id ) ) { |
|
| 315 | + if ( ! is_numeric($post_id)) { |
|
| 316 | 316 | return; |
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | // Force $alt_labels to be an array |
| 320 | - if ( ! is_array( $alt_labels ) ) { |
|
| 321 | - $alt_labels = array( $alt_labels ); |
|
| 320 | + if ( ! is_array($alt_labels)) { |
|
| 321 | + $alt_labels = array($alt_labels); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 324 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 325 | 325 | |
| 326 | 326 | // Delete all the existing alternate labels. |
| 327 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 327 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 328 | 328 | |
| 329 | 329 | // Set the alternative labels. |
| 330 | - foreach ( $alt_labels as $alt_label ) { |
|
| 331 | - if ( ! empty( $alt_label ) ) { |
|
| 332 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 330 | + foreach ($alt_labels as $alt_label) { |
|
| 331 | + if ( ! empty($alt_label)) { |
|
| 332 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 333 | 333 | } |
| 334 | 334 | } |
| 335 | 335 | |
@@ -344,9 +344,9 @@ discard block |
||
| 344 | 344 | * @since 3.2.0 |
| 345 | 345 | * |
| 346 | 346 | */ |
| 347 | - public function get_alternative_labels( $post_id ) { |
|
| 347 | + public function get_alternative_labels($post_id) { |
|
| 348 | 348 | |
| 349 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 349 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | /** |
@@ -358,9 +358,9 @@ discard block |
||
| 358 | 358 | * @since 3.12.0 |
| 359 | 359 | * |
| 360 | 360 | */ |
| 361 | - public function get_labels( $post_id ) { |
|
| 361 | + public function get_labels($post_id) { |
|
| 362 | 362 | |
| 363 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 363 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | /** |
@@ -371,25 +371,25 @@ discard block |
||
| 371 | 371 | * @since 3.2.0 |
| 372 | 372 | * |
| 373 | 373 | */ |
| 374 | - public function edit_form_before_permalink( $post ) { |
|
| 374 | + public function edit_form_before_permalink($post) { |
|
| 375 | 375 | |
| 376 | 376 | // If it's not an entity, return. |
| 377 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 377 | + if ( ! $this->is_entity($post->ID)) { |
|
| 378 | 378 | return; |
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | // Print the input template. |
| 382 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 382 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 383 | 383 | |
| 384 | 384 | // Print all the currently set alternative labels. |
| 385 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 385 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 386 | 386 | |
| 387 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 387 | + echo $this->get_alternative_label_input($alt_label); |
|
| 388 | 388 | |
| 389 | 389 | }; |
| 390 | 390 | |
| 391 | 391 | // Print the button. |
| 392 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 392 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 393 | 393 | |
| 394 | 394 | } |
| 395 | 395 | |
@@ -402,36 +402,36 @@ discard block |
||
| 402 | 402 | * @since 3.6.0 |
| 403 | 403 | * |
| 404 | 404 | */ |
| 405 | - public function get_uri( $post_id ) { |
|
| 405 | + public function get_uri($post_id) { |
|
| 406 | 406 | |
| 407 | 407 | // If a null is given, nothing to do |
| 408 | - if ( is_null( $post_id ) ) { |
|
| 408 | + if (is_null($post_id)) { |
|
| 409 | 409 | return null; |
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
| 413 | 413 | |
| 414 | 414 | // If the dataset uri is not properly configured, null is returned |
| 415 | - if ( empty( $dataset_uri ) ) { |
|
| 415 | + if (empty($dataset_uri)) { |
|
| 416 | 416 | return null; |
| 417 | 417 | } |
| 418 | 418 | |
| 419 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 419 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 420 | 420 | |
| 421 | 421 | /* |
| 422 | 422 | * Consider the URI invalid if it doesn't start with the dataset URI. |
| 423 | 423 | * |
| 424 | 424 | * @see https://github.com/insideout10/wordlift-plugin/issues/996 |
| 425 | 425 | */ |
| 426 | - if ( 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 426 | + if (0 !== strpos($uri, $dataset_uri)) { |
|
| 427 | 427 | $uri = null; |
| 428 | 428 | } |
| 429 | 429 | |
| 430 | 430 | // Set the URI if it isn't set yet. |
| 431 | - $post_status = get_post_status( $post_id ); |
|
| 432 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 433 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 434 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 431 | + $post_status = get_post_status($post_id); |
|
| 432 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 433 | + $uri = wl_build_entity_uri($post_id); |
|
| 434 | + wl_set_entity_uri($post_id, $uri); |
|
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | return $uri; |
@@ -447,9 +447,9 @@ discard block |
||
| 447 | 447 | * @since 3.2.0 |
| 448 | 448 | * |
| 449 | 449 | */ |
| 450 | - private function get_alternative_label_input( $value = '' ) { |
|
| 450 | + private function get_alternative_label_input($value = '') { |
|
| 451 | 451 | |
| 452 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 452 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | /** |
@@ -463,13 +463,13 @@ discard block |
||
| 463 | 463 | global $wpdb; |
| 464 | 464 | |
| 465 | 465 | // Try to get the count from the transient. |
| 466 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 467 | - if ( false !== $count ) { |
|
| 466 | + $count = get_transient('_wl_entity_service__count'); |
|
| 467 | + if (false !== $count) { |
|
| 468 | 468 | return $count; |
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | // Query the count. |
| 472 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 472 | + $count = $wpdb->get_var($wpdb->prepare( |
|
| 473 | 473 | "SELECT COUNT( DISTINCT( tr.object_id ) )" |
| 474 | 474 | . " FROM {$wpdb->term_relationships} tr" |
| 475 | 475 | . " INNER JOIN {$wpdb->term_taxonomy} tt" |
@@ -478,10 +478,10 @@ discard block |
||
| 478 | 478 | . " ON t.term_id = tt.term_id AND t.name != %s", |
| 479 | 479 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 480 | 480 | 'article' |
| 481 | - ) ); |
|
| 481 | + )); |
|
| 482 | 482 | |
| 483 | 483 | // Store the count in cache. |
| 484 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 484 | + set_transient('_wl_entity_service__count', $count, 900); |
|
| 485 | 485 | |
| 486 | 486 | return $count; |
| 487 | 487 | } |
@@ -496,7 +496,7 @@ discard block |
||
| 496 | 496 | * @since 3.15.0 |
| 497 | 497 | * |
| 498 | 498 | */ |
| 499 | - public static function add_criterias( $args ) { |
|
| 499 | + public static function add_criterias($args) { |
|
| 500 | 500 | |
| 501 | 501 | // Build an optimal tax-query. |
| 502 | 502 | $tax_query = array( |
@@ -540,28 +540,28 @@ discard block |
||
| 540 | 540 | * @since 3.9.0 |
| 541 | 541 | * |
| 542 | 542 | */ |
| 543 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 543 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 544 | 544 | |
| 545 | 545 | // Create an entity for the publisher. |
| 546 | - $post_id = @wp_insert_post( array( |
|
| 546 | + $post_id = @wp_insert_post(array( |
|
| 547 | 547 | 'post_type' => self::TYPE_NAME, |
| 548 | 548 | 'post_title' => $name, |
| 549 | 549 | 'post_status' => $status, |
| 550 | 550 | 'post_content' => '', |
| 551 | - ) ); |
|
| 551 | + )); |
|
| 552 | 552 | |
| 553 | 553 | // Return the error if any. |
| 554 | - if ( is_wp_error( $post_id ) ) { |
|
| 554 | + if (is_wp_error($post_id)) { |
|
| 555 | 555 | return $post_id; |
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | // Set the entity logo. |
| 559 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 560 | - set_post_thumbnail( $post_id, $logo ); |
|
| 559 | + if ($logo && is_numeric($logo)) { |
|
| 560 | + set_post_thumbnail($post_id, $logo); |
|
| 561 | 561 | } |
| 562 | 562 | |
| 563 | 563 | // Set the entity type. |
| 564 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 564 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 565 | 565 | |
| 566 | 566 | return $post_id; |
| 567 | 567 | } |
@@ -577,9 +577,9 @@ discard block |
||
| 577 | 577 | * @since 3.10.0 |
| 578 | 578 | * |
| 579 | 579 | */ |
| 580 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 580 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 581 | 581 | |
| 582 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 582 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 583 | 583 | } |
| 584 | 584 | |
| 585 | 585 | /** |
@@ -591,16 +591,16 @@ discard block |
||
| 591 | 591 | * @since 3.12.2 |
| 592 | 592 | * |
| 593 | 593 | */ |
| 594 | - public function get( $params = array() ) { |
|
| 594 | + public function get($params = array()) { |
|
| 595 | 595 | |
| 596 | 596 | // Set the defaults. |
| 597 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 597 | + $defaults = array('post_type' => 'entity'); |
|
| 598 | 598 | |
| 599 | 599 | // Merge the defaults with the provided parameters. |
| 600 | - $args = wp_parse_args( $params, $defaults ); |
|
| 600 | + $args = wp_parse_args($params, $defaults); |
|
| 601 | 601 | |
| 602 | 602 | // Call the `get_posts` function. |
| 603 | - return get_posts( $args ); |
|
| 603 | + return get_posts($args); |
|
| 604 | 604 | } |
| 605 | 605 | |
| 606 | 606 | /** |
@@ -616,9 +616,9 @@ discard block |
||
| 616 | 616 | static function valid_entity_post_types() { |
| 617 | 617 | |
| 618 | 618 | // Ignore builtins in the call to avoid getting attachments. |
| 619 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 619 | + $post_types = array('post', 'page', self::TYPE_NAME, 'product'); |
|
| 620 | 620 | |
| 621 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 621 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 622 | 622 | } |
| 623 | 623 | |
| 624 | 624 | } |
@@ -15,82 +15,82 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Acf_Mappings { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Acf_Mappings constructor. |
|
| 20 | - * |
|
| 21 | - * Hooks `wl_mappings_field_types` to our own function to declare support for ACF fields. |
|
| 22 | - */ |
|
| 23 | - public function __construct() { |
|
| 24 | - |
|
| 25 | - $that = $this; |
|
| 26 | - add_action( 'plugins_loaded', function () use ( $that ) { |
|
| 27 | - $that->add_acf_option_to_mappings_ui(); |
|
| 28 | - } ); |
|
| 29 | - |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - private function add_acf_option_to_mappings_ui() { |
|
| 33 | - // Bail out if ACF is not available. |
|
| 34 | - if ( ! function_exists( 'acf_get_field_groups' ) ) { |
|
| 35 | - return array(); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - add_filter( 'wl_mappings_field_types', array( $this, 'wl_mappings_field_types', ) ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Hook to `wl_mappings_field_types` to declare support for ACF fields. |
|
| 43 | - * |
|
| 44 | - * @param array $field_types An array of field types. |
|
| 45 | - * |
|
| 46 | - * @return array The array with our ACF declaration. |
|
| 47 | - */ |
|
| 48 | - public function wl_mappings_field_types( $field_types ) { |
|
| 49 | - |
|
| 50 | - $field_types[] = array( |
|
| 51 | - 'field_type' => 'acf', |
|
| 52 | - 'label' => __( 'ACF', 'wordlift' ), |
|
| 53 | - 'value' => self::get_acf_options(), |
|
| 54 | - ); |
|
| 55 | - |
|
| 56 | - return $field_types; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Returns array of acf options. |
|
| 61 | - * |
|
| 62 | - * The array is composed by all the ACF groups and their fields hierarchy. |
|
| 63 | - * |
|
| 64 | - * @return array ACF options array. |
|
| 65 | - */ |
|
| 66 | - private static function get_acf_options() { |
|
| 67 | - |
|
| 68 | - $acf_options = array(); |
|
| 69 | - |
|
| 70 | - // Get all the ACF field groups. |
|
| 71 | - $field_groups = acf_get_field_groups(); |
|
| 72 | - |
|
| 73 | - foreach ( $field_groups as $field_group ) { |
|
| 74 | - $group_name = $field_group['title']; |
|
| 75 | - $group_key = $field_group['key']; |
|
| 76 | - |
|
| 77 | - $group_fields = acf_get_fields( $group_key ); |
|
| 78 | - |
|
| 79 | - $group_options = array(); |
|
| 80 | - foreach ( $group_fields as $group_field ) { |
|
| 81 | - $group_options[] = array( |
|
| 82 | - 'label' => $group_field['label'], |
|
| 83 | - 'value' => $group_field['key'], |
|
| 84 | - ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $acf_options[] = array( |
|
| 88 | - 'group_name' => $group_name, |
|
| 89 | - 'group_options' => $group_options, |
|
| 90 | - ); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - return $acf_options; |
|
| 94 | - } |
|
| 18 | + /** |
|
| 19 | + * Acf_Mappings constructor. |
|
| 20 | + * |
|
| 21 | + * Hooks `wl_mappings_field_types` to our own function to declare support for ACF fields. |
|
| 22 | + */ |
|
| 23 | + public function __construct() { |
|
| 24 | + |
|
| 25 | + $that = $this; |
|
| 26 | + add_action( 'plugins_loaded', function () use ( $that ) { |
|
| 27 | + $that->add_acf_option_to_mappings_ui(); |
|
| 28 | + } ); |
|
| 29 | + |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + private function add_acf_option_to_mappings_ui() { |
|
| 33 | + // Bail out if ACF is not available. |
|
| 34 | + if ( ! function_exists( 'acf_get_field_groups' ) ) { |
|
| 35 | + return array(); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + add_filter( 'wl_mappings_field_types', array( $this, 'wl_mappings_field_types', ) ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Hook to `wl_mappings_field_types` to declare support for ACF fields. |
|
| 43 | + * |
|
| 44 | + * @param array $field_types An array of field types. |
|
| 45 | + * |
|
| 46 | + * @return array The array with our ACF declaration. |
|
| 47 | + */ |
|
| 48 | + public function wl_mappings_field_types( $field_types ) { |
|
| 49 | + |
|
| 50 | + $field_types[] = array( |
|
| 51 | + 'field_type' => 'acf', |
|
| 52 | + 'label' => __( 'ACF', 'wordlift' ), |
|
| 53 | + 'value' => self::get_acf_options(), |
|
| 54 | + ); |
|
| 55 | + |
|
| 56 | + return $field_types; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Returns array of acf options. |
|
| 61 | + * |
|
| 62 | + * The array is composed by all the ACF groups and their fields hierarchy. |
|
| 63 | + * |
|
| 64 | + * @return array ACF options array. |
|
| 65 | + */ |
|
| 66 | + private static function get_acf_options() { |
|
| 67 | + |
|
| 68 | + $acf_options = array(); |
|
| 69 | + |
|
| 70 | + // Get all the ACF field groups. |
|
| 71 | + $field_groups = acf_get_field_groups(); |
|
| 72 | + |
|
| 73 | + foreach ( $field_groups as $field_group ) { |
|
| 74 | + $group_name = $field_group['title']; |
|
| 75 | + $group_key = $field_group['key']; |
|
| 76 | + |
|
| 77 | + $group_fields = acf_get_fields( $group_key ); |
|
| 78 | + |
|
| 79 | + $group_options = array(); |
|
| 80 | + foreach ( $group_fields as $group_field ) { |
|
| 81 | + $group_options[] = array( |
|
| 82 | + 'label' => $group_field['label'], |
|
| 83 | + 'value' => $group_field['key'], |
|
| 84 | + ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $acf_options[] = array( |
|
| 88 | + 'group_name' => $group_name, |
|
| 89 | + 'group_options' => $group_options, |
|
| 90 | + ); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + return $acf_options; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | 96 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | public function __construct() { |
| 24 | 24 | |
| 25 | 25 | $that = $this; |
| 26 | - add_action( 'plugins_loaded', function () use ( $that ) { |
|
| 26 | + add_action('plugins_loaded', function() use ($that) { |
|
| 27 | 27 | $that->add_acf_option_to_mappings_ui(); |
| 28 | 28 | } ); |
| 29 | 29 | |
@@ -31,11 +31,11 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | private function add_acf_option_to_mappings_ui() { |
| 33 | 33 | // Bail out if ACF is not available. |
| 34 | - if ( ! function_exists( 'acf_get_field_groups' ) ) { |
|
| 34 | + if ( ! function_exists('acf_get_field_groups')) { |
|
| 35 | 35 | return array(); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - add_filter( 'wl_mappings_field_types', array( $this, 'wl_mappings_field_types', ) ); |
|
| 38 | + add_filter('wl_mappings_field_types', array($this, 'wl_mappings_field_types',)); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
@@ -45,11 +45,11 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @return array The array with our ACF declaration. |
| 47 | 47 | */ |
| 48 | - public function wl_mappings_field_types( $field_types ) { |
|
| 48 | + public function wl_mappings_field_types($field_types) { |
|
| 49 | 49 | |
| 50 | 50 | $field_types[] = array( |
| 51 | 51 | 'field_type' => 'acf', |
| 52 | - 'label' => __( 'ACF', 'wordlift' ), |
|
| 52 | + 'label' => __('ACF', 'wordlift'), |
|
| 53 | 53 | 'value' => self::get_acf_options(), |
| 54 | 54 | ); |
| 55 | 55 | |
@@ -70,14 +70,14 @@ discard block |
||
| 70 | 70 | // Get all the ACF field groups. |
| 71 | 71 | $field_groups = acf_get_field_groups(); |
| 72 | 72 | |
| 73 | - foreach ( $field_groups as $field_group ) { |
|
| 73 | + foreach ($field_groups as $field_group) { |
|
| 74 | 74 | $group_name = $field_group['title']; |
| 75 | 75 | $group_key = $field_group['key']; |
| 76 | 76 | |
| 77 | - $group_fields = acf_get_fields( $group_key ); |
|
| 77 | + $group_fields = acf_get_fields($group_key); |
|
| 78 | 78 | |
| 79 | 79 | $group_options = array(); |
| 80 | - foreach ( $group_fields as $group_field ) { |
|
| 80 | + foreach ($group_fields as $group_field) { |
|
| 81 | 81 | $group_options[] = array( |
| 82 | 82 | 'label' => $group_field['label'], |
| 83 | 83 | 'value' => $group_field['key'], |
@@ -20,147 +20,147 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | class Jsonld_Endpoint { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * The {@link Wordlift_Jsonld_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
| 27 | - */ |
|
| 28 | - private $jsonld_service; |
|
| 29 | - /** |
|
| 30 | - * @var \Wordlift_Entity_Uri_Service |
|
| 31 | - */ |
|
| 32 | - private $entity_uri_service; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Jsonld_Endpoint constructor. |
|
| 36 | - * |
|
| 37 | - * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 38 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
| 39 | - */ |
|
| 40 | - public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 41 | - |
|
| 42 | - $this->jsonld_service = $jsonld_service; |
|
| 43 | - $this->entity_uri_service = $entity_uri_service; |
|
| 44 | - |
|
| 45 | - // PHP 5.3 compatibility. |
|
| 46 | - $that = $this; |
|
| 47 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 48 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 49 | - 'methods' => WP_REST_Server::READABLE, |
|
| 50 | - 'callback' => array( $that, 'jsonld_using_post_id' ), |
|
| 51 | - 'args' => array( |
|
| 52 | - 'id' => array( |
|
| 53 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 54 | - return is_numeric( $param ); |
|
| 55 | - }, |
|
| 56 | - 'sanitize_callback' => 'absint', |
|
| 57 | - ), |
|
| 58 | - ) |
|
| 59 | - ) ); |
|
| 60 | - |
|
| 61 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 62 | - 'methods' => 'GET', |
|
| 63 | - 'callback' => array( $that, 'jsonld_using_item_id' ), |
|
| 64 | - ) ); |
|
| 65 | - |
|
| 66 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 67 | - 'methods' => 'GET', |
|
| 68 | - 'callback' => array( $that, 'jsonld_using_post_meta' ), |
|
| 69 | - ) ); |
|
| 70 | - |
|
| 71 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 72 | - 'methods' => 'GET', |
|
| 73 | - 'callback' => array( $that, 'jsonld_using_get_page_by_path' ), |
|
| 74 | - ) ); |
|
| 75 | - |
|
| 76 | - } ); |
|
| 77 | - |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Callback for the JSON-LD request. |
|
| 82 | - * |
|
| 83 | - * @param array $request { |
|
| 84 | - * The request array. |
|
| 85 | - * |
|
| 86 | - * @type int $id The post id. |
|
| 87 | - * } |
|
| 88 | - * |
|
| 89 | - * @return WP_REST_Response |
|
| 90 | - * @throws \Exception |
|
| 91 | - */ |
|
| 92 | - public function jsonld_using_post_id( $request ) { |
|
| 93 | - |
|
| 94 | - $post_id = $request['id']; |
|
| 95 | - $is_homepage = ( 0 === $post_id ); |
|
| 96 | - |
|
| 97 | - // Send the generated JSON-LD. |
|
| 98 | - $data = $this->jsonld_service->get_jsonld( $is_homepage, $post_id ); |
|
| 99 | - |
|
| 100 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Provide a JSON-LD given the itemId. |
|
| 105 | - * |
|
| 106 | - * @param array $request { |
|
| 107 | - * The request array. |
|
| 108 | - * |
|
| 109 | - * @type string $item_id The entity item id. |
|
| 110 | - * } |
|
| 111 | - * |
|
| 112 | - * @return WP_REST_Response |
|
| 113 | - * @throws \Exception |
|
| 114 | - */ |
|
| 115 | - public function jsonld_using_item_id( $request ) { |
|
| 116 | - |
|
| 117 | - $item_id = 'http://' . $request['item_id']; |
|
| 118 | - $post = $this->entity_uri_service->get_entity( $item_id ); |
|
| 119 | - |
|
| 120 | - if ( ! is_a( $post, 'WP_Post' ) ) { |
|
| 121 | - return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - public function jsonld_using_get_page_by_path( $request ) { |
|
| 128 | - |
|
| 129 | - $post_name = $request['post_name']; |
|
| 130 | - $post_type = $request['post_type']; |
|
| 131 | - |
|
| 132 | - global $wpdb; |
|
| 133 | - |
|
| 134 | - $sql = " |
|
| 23 | + /** |
|
| 24 | + * The {@link Wordlift_Jsonld_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
| 27 | + */ |
|
| 28 | + private $jsonld_service; |
|
| 29 | + /** |
|
| 30 | + * @var \Wordlift_Entity_Uri_Service |
|
| 31 | + */ |
|
| 32 | + private $entity_uri_service; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Jsonld_Endpoint constructor. |
|
| 36 | + * |
|
| 37 | + * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 38 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
| 39 | + */ |
|
| 40 | + public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 41 | + |
|
| 42 | + $this->jsonld_service = $jsonld_service; |
|
| 43 | + $this->entity_uri_service = $entity_uri_service; |
|
| 44 | + |
|
| 45 | + // PHP 5.3 compatibility. |
|
| 46 | + $that = $this; |
|
| 47 | + add_action( 'rest_api_init', function () use ( $that ) { |
|
| 48 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 49 | + 'methods' => WP_REST_Server::READABLE, |
|
| 50 | + 'callback' => array( $that, 'jsonld_using_post_id' ), |
|
| 51 | + 'args' => array( |
|
| 52 | + 'id' => array( |
|
| 53 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 54 | + return is_numeric( $param ); |
|
| 55 | + }, |
|
| 56 | + 'sanitize_callback' => 'absint', |
|
| 57 | + ), |
|
| 58 | + ) |
|
| 59 | + ) ); |
|
| 60 | + |
|
| 61 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 62 | + 'methods' => 'GET', |
|
| 63 | + 'callback' => array( $that, 'jsonld_using_item_id' ), |
|
| 64 | + ) ); |
|
| 65 | + |
|
| 66 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 67 | + 'methods' => 'GET', |
|
| 68 | + 'callback' => array( $that, 'jsonld_using_post_meta' ), |
|
| 69 | + ) ); |
|
| 70 | + |
|
| 71 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 72 | + 'methods' => 'GET', |
|
| 73 | + 'callback' => array( $that, 'jsonld_using_get_page_by_path' ), |
|
| 74 | + ) ); |
|
| 75 | + |
|
| 76 | + } ); |
|
| 77 | + |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Callback for the JSON-LD request. |
|
| 82 | + * |
|
| 83 | + * @param array $request { |
|
| 84 | + * The request array. |
|
| 85 | + * |
|
| 86 | + * @type int $id The post id. |
|
| 87 | + * } |
|
| 88 | + * |
|
| 89 | + * @return WP_REST_Response |
|
| 90 | + * @throws \Exception |
|
| 91 | + */ |
|
| 92 | + public function jsonld_using_post_id( $request ) { |
|
| 93 | + |
|
| 94 | + $post_id = $request['id']; |
|
| 95 | + $is_homepage = ( 0 === $post_id ); |
|
| 96 | + |
|
| 97 | + // Send the generated JSON-LD. |
|
| 98 | + $data = $this->jsonld_service->get_jsonld( $is_homepage, $post_id ); |
|
| 99 | + |
|
| 100 | + return Jsonld_Response_Helper::to_response( $data ); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Provide a JSON-LD given the itemId. |
|
| 105 | + * |
|
| 106 | + * @param array $request { |
|
| 107 | + * The request array. |
|
| 108 | + * |
|
| 109 | + * @type string $item_id The entity item id. |
|
| 110 | + * } |
|
| 111 | + * |
|
| 112 | + * @return WP_REST_Response |
|
| 113 | + * @throws \Exception |
|
| 114 | + */ |
|
| 115 | + public function jsonld_using_item_id( $request ) { |
|
| 116 | + |
|
| 117 | + $item_id = 'http://' . $request['item_id']; |
|
| 118 | + $post = $this->entity_uri_service->get_entity( $item_id ); |
|
| 119 | + |
|
| 120 | + if ( ! is_a( $post, 'WP_Post' ) ) { |
|
| 121 | + return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + public function jsonld_using_get_page_by_path( $request ) { |
|
| 128 | + |
|
| 129 | + $post_name = $request['post_name']; |
|
| 130 | + $post_type = $request['post_type']; |
|
| 131 | + |
|
| 132 | + global $wpdb; |
|
| 133 | + |
|
| 134 | + $sql = " |
|
| 135 | 135 | SELECT ID |
| 136 | 136 | FROM $wpdb->posts |
| 137 | 137 | WHERE post_name = %s |
| 138 | 138 | AND post_type = %s |
| 139 | 139 | "; |
| 140 | 140 | |
| 141 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) ); |
|
| 141 | + $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) ); |
|
| 142 | 142 | |
| 143 | - if ( is_null( $post_id ) ) { |
|
| 144 | - return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 145 | - } |
|
| 143 | + if ( is_null( $post_id ) ) { |
|
| 144 | + return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 148 | - } |
|
| 147 | + return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * @param WP_REST_Request $request |
|
| 152 | - * |
|
| 153 | - * @return WP_REST_Response |
|
| 154 | - * @throws \Exception |
|
| 155 | - */ |
|
| 156 | - public function jsonld_using_post_meta( $request ) { |
|
| 150 | + /** |
|
| 151 | + * @param WP_REST_Request $request |
|
| 152 | + * |
|
| 153 | + * @return WP_REST_Response |
|
| 154 | + * @throws \Exception |
|
| 155 | + */ |
|
| 156 | + public function jsonld_using_post_meta( $request ) { |
|
| 157 | 157 | |
| 158 | - $meta_key = $request['meta_key']; |
|
| 159 | - $meta_value = current( $request->get_query_params( 'meta_value' ) ); |
|
| 158 | + $meta_key = $request['meta_key']; |
|
| 159 | + $meta_value = current( $request->get_query_params( 'meta_value' ) ); |
|
| 160 | 160 | |
| 161 | - global $wpdb; |
|
| 161 | + global $wpdb; |
|
| 162 | 162 | |
| 163 | - $sql = " |
|
| 163 | + $sql = " |
|
| 164 | 164 | SELECT post_id AS ID |
| 165 | 165 | FROM $wpdb->postmeta |
| 166 | 166 | WHERE meta_key = %s |
@@ -168,13 +168,13 @@ discard block |
||
| 168 | 168 | LIMIT 1 |
| 169 | 169 | "; |
| 170 | 170 | |
| 171 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) ); |
|
| 171 | + $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) ); |
|
| 172 | 172 | |
| 173 | - if ( is_null( $post_id ) ) { |
|
| 174 | - return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 175 | - } |
|
| 173 | + if ( is_null( $post_id ) ) { |
|
| 174 | + return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 178 | - } |
|
| 177 | + return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | 180 | } |
@@ -37,41 +37,41 @@ discard block |
||
| 37 | 37 | * @param \Wordlift_Jsonld_Service $jsonld_service |
| 38 | 38 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
| 39 | 39 | */ |
| 40 | - public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 40 | + public function __construct($jsonld_service, $entity_uri_service) { |
|
| 41 | 41 | |
| 42 | 42 | $this->jsonld_service = $jsonld_service; |
| 43 | 43 | $this->entity_uri_service = $entity_uri_service; |
| 44 | 44 | |
| 45 | 45 | // PHP 5.3 compatibility. |
| 46 | 46 | $that = $this; |
| 47 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 48 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 47 | + add_action('rest_api_init', function() use ($that) { |
|
| 48 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 49 | 49 | 'methods' => WP_REST_Server::READABLE, |
| 50 | - 'callback' => array( $that, 'jsonld_using_post_id' ), |
|
| 50 | + 'callback' => array($that, 'jsonld_using_post_id'), |
|
| 51 | 51 | 'args' => array( |
| 52 | 52 | 'id' => array( |
| 53 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 54 | - return is_numeric( $param ); |
|
| 53 | + 'validate_callback' => function($param, $request, $key) { |
|
| 54 | + return is_numeric($param); |
|
| 55 | 55 | }, |
| 56 | 56 | 'sanitize_callback' => 'absint', |
| 57 | 57 | ), |
| 58 | 58 | ) |
| 59 | - ) ); |
|
| 59 | + )); |
|
| 60 | 60 | |
| 61 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 61 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 62 | 62 | 'methods' => 'GET', |
| 63 | - 'callback' => array( $that, 'jsonld_using_item_id' ), |
|
| 64 | - ) ); |
|
| 63 | + 'callback' => array($that, 'jsonld_using_item_id'), |
|
| 64 | + )); |
|
| 65 | 65 | |
| 66 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 66 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 67 | 67 | 'methods' => 'GET', |
| 68 | - 'callback' => array( $that, 'jsonld_using_post_meta' ), |
|
| 69 | - ) ); |
|
| 68 | + 'callback' => array($that, 'jsonld_using_post_meta'), |
|
| 69 | + )); |
|
| 70 | 70 | |
| 71 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 71 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 72 | 72 | 'methods' => 'GET', |
| 73 | - 'callback' => array( $that, 'jsonld_using_get_page_by_path' ), |
|
| 74 | - ) ); |
|
| 73 | + 'callback' => array($that, 'jsonld_using_get_page_by_path'), |
|
| 74 | + )); |
|
| 75 | 75 | |
| 76 | 76 | } ); |
| 77 | 77 | |
@@ -89,15 +89,15 @@ discard block |
||
| 89 | 89 | * @return WP_REST_Response |
| 90 | 90 | * @throws \Exception |
| 91 | 91 | */ |
| 92 | - public function jsonld_using_post_id( $request ) { |
|
| 92 | + public function jsonld_using_post_id($request) { |
|
| 93 | 93 | |
| 94 | 94 | $post_id = $request['id']; |
| 95 | - $is_homepage = ( 0 === $post_id ); |
|
| 95 | + $is_homepage = (0 === $post_id); |
|
| 96 | 96 | |
| 97 | 97 | // Send the generated JSON-LD. |
| 98 | - $data = $this->jsonld_service->get_jsonld( $is_homepage, $post_id ); |
|
| 98 | + $data = $this->jsonld_service->get_jsonld($is_homepage, $post_id); |
|
| 99 | 99 | |
| 100 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 100 | + return Jsonld_Response_Helper::to_response($data); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | /** |
@@ -112,19 +112,19 @@ discard block |
||
| 112 | 112 | * @return WP_REST_Response |
| 113 | 113 | * @throws \Exception |
| 114 | 114 | */ |
| 115 | - public function jsonld_using_item_id( $request ) { |
|
| 115 | + public function jsonld_using_item_id($request) { |
|
| 116 | 116 | |
| 117 | - $item_id = 'http://' . $request['item_id']; |
|
| 118 | - $post = $this->entity_uri_service->get_entity( $item_id ); |
|
| 117 | + $item_id = 'http://'.$request['item_id']; |
|
| 118 | + $post = $this->entity_uri_service->get_entity($item_id); |
|
| 119 | 119 | |
| 120 | - if ( ! is_a( $post, 'WP_Post' ) ) { |
|
| 121 | - return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 120 | + if ( ! is_a($post, 'WP_Post')) { |
|
| 121 | + return new WP_REST_Response(esc_html("$item_id not found."), 404, array('Content-Type' => 'text/html')); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) ); |
|
| 124 | + return $this->jsonld_using_post_id(array('id' => $post->ID,)); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - public function jsonld_using_get_page_by_path( $request ) { |
|
| 127 | + public function jsonld_using_get_page_by_path($request) { |
|
| 128 | 128 | |
| 129 | 129 | $post_name = $request['post_name']; |
| 130 | 130 | $post_type = $request['post_type']; |
@@ -138,13 +138,13 @@ discard block |
||
| 138 | 138 | AND post_type = %s |
| 139 | 139 | "; |
| 140 | 140 | |
| 141 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) ); |
|
| 141 | + $post_id = $wpdb->get_var($wpdb->prepare($sql, $post_name, $post_type)); |
|
| 142 | 142 | |
| 143 | - if ( is_null( $post_id ) ) { |
|
| 144 | - return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 143 | + if (is_null($post_id)) { |
|
| 144 | + return new WP_REST_Response(esc_html("$post_name of type $post_type not found."), 404, array('Content-Type' => 'text/html')); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 147 | + return $this->jsonld_using_post_id(array('id' => $post_id,)); |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | /** |
@@ -153,10 +153,10 @@ discard block |
||
| 153 | 153 | * @return WP_REST_Response |
| 154 | 154 | * @throws \Exception |
| 155 | 155 | */ |
| 156 | - public function jsonld_using_post_meta( $request ) { |
|
| 156 | + public function jsonld_using_post_meta($request) { |
|
| 157 | 157 | |
| 158 | 158 | $meta_key = $request['meta_key']; |
| 159 | - $meta_value = current( $request->get_query_params( 'meta_value' ) ); |
|
| 159 | + $meta_value = current($request->get_query_params('meta_value')); |
|
| 160 | 160 | |
| 161 | 161 | global $wpdb; |
| 162 | 162 | |
@@ -168,13 +168,13 @@ discard block |
||
| 168 | 168 | LIMIT 1 |
| 169 | 169 | "; |
| 170 | 170 | |
| 171 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) ); |
|
| 171 | + $post_id = $wpdb->get_var($wpdb->prepare($sql, $meta_key, $meta_value)); |
|
| 172 | 172 | |
| 173 | - if ( is_null( $post_id ) ) { |
|
| 174 | - return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 173 | + if (is_null($post_id)) { |
|
| 174 | + return new WP_REST_Response(esc_html("Post with meta key $meta_key and value $meta_value not found."), 404, array('Content-Type' => 'text/html')); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 177 | + return $this->jsonld_using_post_id(array('id' => $post_id,)); |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | } |