@@ -13,6 +13,6 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | interface Abstract_Data_Source { |
| 15 | 15 | |
| 16 | - public function get_data( $post_id, $property ); |
|
| 16 | + public function get_data( $post_id, $property ); |
|
| 17 | 17 | |
| 18 | 18 | } |
@@ -13,6 +13,6 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | interface Abstract_Data_Source { |
| 15 | 15 | |
| 16 | - public function get_data( $post_id, $property ); |
|
| 16 | + public function get_data($post_id, $property); |
|
| 17 | 17 | |
| 18 | 18 | } |
@@ -11,38 +11,38 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Data_Source_Factory { |
| 13 | 13 | |
| 14 | - private static $instance = null; |
|
| 15 | - |
|
| 16 | - private $data_sources = array(); |
|
| 17 | - |
|
| 18 | - public function __construct() { |
|
| 19 | - $this->data_sources = array( |
|
| 20 | - 'acf' => new Acf_Data_Source(), |
|
| 21 | - 'meta' => new Meta_Data_Source() |
|
| 22 | - ); |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public function get_data( $post_id, $property_data ) { |
|
| 26 | - switch ( $property_data['field_type'] ) { |
|
| 27 | - case Jsonld_Converter::FIELD_TYPE_ACF: |
|
| 28 | - return $this->data_sources['acf']->get_data( $post_id, $property_data ); |
|
| 29 | - case Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD: |
|
| 30 | - return $this->data_sources['meta']->get_data( $post_id, $property_data ); |
|
| 31 | - default: |
|
| 32 | - return $property_data['field_name']; |
|
| 33 | - } |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @return Data_Source_Factory |
|
| 39 | - */ |
|
| 40 | - public static function get_instance() { |
|
| 41 | - if ( self::$instance === null ) { |
|
| 42 | - self::$instance = new Data_Source_Factory(); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - return self::$instance; |
|
| 46 | - } |
|
| 14 | + private static $instance = null; |
|
| 15 | + |
|
| 16 | + private $data_sources = array(); |
|
| 17 | + |
|
| 18 | + public function __construct() { |
|
| 19 | + $this->data_sources = array( |
|
| 20 | + 'acf' => new Acf_Data_Source(), |
|
| 21 | + 'meta' => new Meta_Data_Source() |
|
| 22 | + ); |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public function get_data( $post_id, $property_data ) { |
|
| 26 | + switch ( $property_data['field_type'] ) { |
|
| 27 | + case Jsonld_Converter::FIELD_TYPE_ACF: |
|
| 28 | + return $this->data_sources['acf']->get_data( $post_id, $property_data ); |
|
| 29 | + case Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD: |
|
| 30 | + return $this->data_sources['meta']->get_data( $post_id, $property_data ); |
|
| 31 | + default: |
|
| 32 | + return $property_data['field_name']; |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @return Data_Source_Factory |
|
| 39 | + */ |
|
| 40 | + public static function get_instance() { |
|
| 41 | + if ( self::$instance === null ) { |
|
| 42 | + self::$instance = new Data_Source_Factory(); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + return self::$instance; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | } |
@@ -22,12 +22,12 @@ discard block |
||
| 22 | 22 | ); |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - public function get_data( $post_id, $property_data ) { |
|
| 26 | - switch ( $property_data['field_type'] ) { |
|
| 25 | + public function get_data($post_id, $property_data) { |
|
| 26 | + switch ($property_data['field_type']) { |
|
| 27 | 27 | case Jsonld_Converter::FIELD_TYPE_ACF: |
| 28 | - return $this->data_sources['acf']->get_data( $post_id, $property_data ); |
|
| 28 | + return $this->data_sources['acf']->get_data($post_id, $property_data); |
|
| 29 | 29 | case Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD: |
| 30 | - return $this->data_sources['meta']->get_data( $post_id, $property_data ); |
|
| 30 | + return $this->data_sources['meta']->get_data($post_id, $property_data); |
|
| 31 | 31 | default: |
| 32 | 32 | return $property_data['field_name']; |
| 33 | 33 | } |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * @return Data_Source_Factory |
| 39 | 39 | */ |
| 40 | 40 | public static function get_instance() { |
| 41 | - if ( self::$instance === null ) { |
|
| 41 | + if (self::$instance === null) { |
|
| 42 | 42 | self::$instance = new Data_Source_Factory(); |
| 43 | 43 | } |
| 44 | 44 | |
@@ -13,14 +13,14 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Meta_Data_Source implements Abstract_Data_Source { |
| 15 | 15 | |
| 16 | - public function get_data( $post_id, $property ) { |
|
| 16 | + public function get_data( $post_id, $property ) { |
|
| 17 | 17 | |
| 18 | - $value = $property['field_name']; |
|
| 18 | + $value = $property['field_name']; |
|
| 19 | 19 | |
| 20 | - if ( get_queried_object() instanceof \WP_Term ) { |
|
| 21 | - return array_map( 'wp_strip_all_tags', get_term_meta( get_queried_object_id(), $value ) ); |
|
| 22 | - } else { |
|
| 23 | - return array_map( 'wp_strip_all_tags', get_post_meta( $post_id, $value ) ); |
|
| 24 | - } |
|
| 25 | - } |
|
| 20 | + if ( get_queried_object() instanceof \WP_Term ) { |
|
| 21 | + return array_map( 'wp_strip_all_tags', get_term_meta( get_queried_object_id(), $value ) ); |
|
| 22 | + } else { |
|
| 23 | + return array_map( 'wp_strip_all_tags', get_post_meta( $post_id, $value ) ); |
|
| 24 | + } |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -13,14 +13,14 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Meta_Data_Source implements Abstract_Data_Source { |
| 15 | 15 | |
| 16 | - public function get_data( $post_id, $property ) { |
|
| 16 | + public function get_data($post_id, $property) { |
|
| 17 | 17 | |
| 18 | 18 | $value = $property['field_name']; |
| 19 | 19 | |
| 20 | - if ( get_queried_object() instanceof \WP_Term ) { |
|
| 21 | - return array_map( 'wp_strip_all_tags', get_term_meta( get_queried_object_id(), $value ) ); |
|
| 20 | + if (get_queried_object() instanceof \WP_Term) { |
|
| 21 | + return array_map('wp_strip_all_tags', get_term_meta(get_queried_object_id(), $value)); |
|
| 22 | 22 | } else { |
| 23 | - return array_map( 'wp_strip_all_tags', get_post_meta( $post_id, $value ) ); |
|
| 23 | + return array_map('wp_strip_all_tags', get_post_meta($post_id, $value)); |
|
| 24 | 24 | } |
| 25 | 25 | } |
| 26 | 26 | } |
@@ -8,58 +8,58 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Acf_Data_Source implements Abstract_Data_Source { |
| 10 | 10 | |
| 11 | - public function get_data( $post_id, $property_data ) { |
|
| 11 | + public function get_data( $post_id, $property_data ) { |
|
| 12 | 12 | |
| 13 | - if ( ! function_exists( 'get_field' ) || ! function_exists( 'get_field_object' ) ) { |
|
| 14 | - return array(); |
|
| 15 | - } |
|
| 13 | + if ( ! function_exists( 'get_field' ) || ! function_exists( 'get_field_object' ) ) { |
|
| 14 | + return array(); |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - return $this->get_data_for_acf_field( $property_data['field_name'], $post_id ); |
|
| 18 | - } |
|
| 17 | + return $this->get_data_for_acf_field( $property_data['field_name'], $post_id ); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Gets data from acf, format the data if it is a repeater field. |
|
| 22 | - * |
|
| 23 | - * @param $field_name |
|
| 24 | - * @param $post_id |
|
| 25 | - * |
|
| 26 | - * @return array|mixed |
|
| 27 | - */ |
|
| 28 | - private function get_data_for_acf_field( $field_name, $post_id ) { |
|
| 29 | - if ( get_queried_object() instanceof \WP_Term ) { |
|
| 30 | - // Data fetching method for term is different. |
|
| 31 | - $term = get_queried_object(); |
|
| 32 | - $field_data = get_field_object( $field_name, $term ); |
|
| 33 | - $data = get_field( $field_name, $term ); |
|
| 34 | - } else { |
|
| 35 | - $field_data = get_field_object( $field_name, $post_id ); |
|
| 36 | - $data = get_field( $field_name, $post_id ); |
|
| 37 | - } |
|
| 38 | - // only process if it is a repeater field, else return the data. |
|
| 39 | - if ( is_array( $field_data ) && array_key_exists( 'type', $field_data ) |
|
| 40 | - && $field_data['type'] === 'repeater' ) { |
|
| 41 | - /** |
|
| 42 | - * check if we have only one sub field, currently we only support one subfield, |
|
| 43 | - * so each repeater item should be checked if there is a single sub field. |
|
| 44 | - */ |
|
| 45 | - if ( is_array( $data ) && |
|
| 46 | - count( $data ) > 0 && |
|
| 47 | - count( array_keys( $data[0] ) ) === 1 ) { |
|
| 48 | - $repeater_formatted_data = array(); |
|
| 49 | - foreach ( $data as $item ) { |
|
| 50 | - $repeater_formatted_data = array_merge( $repeater_formatted_data, array_values( $item ) ); |
|
| 51 | - } |
|
| 52 | - // Remove non unique values. |
|
| 53 | - $repeater_formatted_data = array_unique( $repeater_formatted_data ); |
|
| 54 | - // Remove empty values |
|
| 55 | - $repeater_formatted_data = array_filter( $repeater_formatted_data, 'strlen' ); |
|
| 20 | + /** |
|
| 21 | + * Gets data from acf, format the data if it is a repeater field. |
|
| 22 | + * |
|
| 23 | + * @param $field_name |
|
| 24 | + * @param $post_id |
|
| 25 | + * |
|
| 26 | + * @return array|mixed |
|
| 27 | + */ |
|
| 28 | + private function get_data_for_acf_field( $field_name, $post_id ) { |
|
| 29 | + if ( get_queried_object() instanceof \WP_Term ) { |
|
| 30 | + // Data fetching method for term is different. |
|
| 31 | + $term = get_queried_object(); |
|
| 32 | + $field_data = get_field_object( $field_name, $term ); |
|
| 33 | + $data = get_field( $field_name, $term ); |
|
| 34 | + } else { |
|
| 35 | + $field_data = get_field_object( $field_name, $post_id ); |
|
| 36 | + $data = get_field( $field_name, $post_id ); |
|
| 37 | + } |
|
| 38 | + // only process if it is a repeater field, else return the data. |
|
| 39 | + if ( is_array( $field_data ) && array_key_exists( 'type', $field_data ) |
|
| 40 | + && $field_data['type'] === 'repeater' ) { |
|
| 41 | + /** |
|
| 42 | + * check if we have only one sub field, currently we only support one subfield, |
|
| 43 | + * so each repeater item should be checked if there is a single sub field. |
|
| 44 | + */ |
|
| 45 | + if ( is_array( $data ) && |
|
| 46 | + count( $data ) > 0 && |
|
| 47 | + count( array_keys( $data[0] ) ) === 1 ) { |
|
| 48 | + $repeater_formatted_data = array(); |
|
| 49 | + foreach ( $data as $item ) { |
|
| 50 | + $repeater_formatted_data = array_merge( $repeater_formatted_data, array_values( $item ) ); |
|
| 51 | + } |
|
| 52 | + // Remove non unique values. |
|
| 53 | + $repeater_formatted_data = array_unique( $repeater_formatted_data ); |
|
| 54 | + // Remove empty values |
|
| 55 | + $repeater_formatted_data = array_filter( $repeater_formatted_data, 'strlen' ); |
|
| 56 | 56 | |
| 57 | - // re-index all the values. |
|
| 58 | - return array_values( $repeater_formatted_data ); |
|
| 59 | - } |
|
| 60 | - } |
|
| 57 | + // re-index all the values. |
|
| 58 | + return array_values( $repeater_formatted_data ); |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - // Return normal acf data if it is not a repeater field. |
|
| 63 | - return $data; |
|
| 64 | - } |
|
| 62 | + // Return normal acf data if it is not a repeater field. |
|
| 63 | + return $data; |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -8,13 +8,13 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | class Acf_Data_Source implements Abstract_Data_Source { |
| 10 | 10 | |
| 11 | - public function get_data( $post_id, $property_data ) { |
|
| 11 | + public function get_data($post_id, $property_data) { |
|
| 12 | 12 | |
| 13 | - if ( ! function_exists( 'get_field' ) || ! function_exists( 'get_field_object' ) ) { |
|
| 13 | + if ( ! function_exists('get_field') || ! function_exists('get_field_object')) { |
|
| 14 | 14 | return array(); |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | - return $this->get_data_for_acf_field( $property_data['field_name'], $post_id ); |
|
| 17 | + return $this->get_data_for_acf_field($property_data['field_name'], $post_id); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
@@ -25,37 +25,37 @@ discard block |
||
| 25 | 25 | * |
| 26 | 26 | * @return array|mixed |
| 27 | 27 | */ |
| 28 | - private function get_data_for_acf_field( $field_name, $post_id ) { |
|
| 29 | - if ( get_queried_object() instanceof \WP_Term ) { |
|
| 28 | + private function get_data_for_acf_field($field_name, $post_id) { |
|
| 29 | + if (get_queried_object() instanceof \WP_Term) { |
|
| 30 | 30 | // Data fetching method for term is different. |
| 31 | 31 | $term = get_queried_object(); |
| 32 | - $field_data = get_field_object( $field_name, $term ); |
|
| 33 | - $data = get_field( $field_name, $term ); |
|
| 32 | + $field_data = get_field_object($field_name, $term); |
|
| 33 | + $data = get_field($field_name, $term); |
|
| 34 | 34 | } else { |
| 35 | - $field_data = get_field_object( $field_name, $post_id ); |
|
| 36 | - $data = get_field( $field_name, $post_id ); |
|
| 35 | + $field_data = get_field_object($field_name, $post_id); |
|
| 36 | + $data = get_field($field_name, $post_id); |
|
| 37 | 37 | } |
| 38 | 38 | // only process if it is a repeater field, else return the data. |
| 39 | - if ( is_array( $field_data ) && array_key_exists( 'type', $field_data ) |
|
| 40 | - && $field_data['type'] === 'repeater' ) { |
|
| 39 | + if (is_array($field_data) && array_key_exists('type', $field_data) |
|
| 40 | + && $field_data['type'] === 'repeater') { |
|
| 41 | 41 | /** |
| 42 | 42 | * check if we have only one sub field, currently we only support one subfield, |
| 43 | 43 | * so each repeater item should be checked if there is a single sub field. |
| 44 | 44 | */ |
| 45 | - if ( is_array( $data ) && |
|
| 46 | - count( $data ) > 0 && |
|
| 47 | - count( array_keys( $data[0] ) ) === 1 ) { |
|
| 45 | + if (is_array($data) && |
|
| 46 | + count($data) > 0 && |
|
| 47 | + count(array_keys($data[0])) === 1) { |
|
| 48 | 48 | $repeater_formatted_data = array(); |
| 49 | - foreach ( $data as $item ) { |
|
| 50 | - $repeater_formatted_data = array_merge( $repeater_formatted_data, array_values( $item ) ); |
|
| 49 | + foreach ($data as $item) { |
|
| 50 | + $repeater_formatted_data = array_merge($repeater_formatted_data, array_values($item)); |
|
| 51 | 51 | } |
| 52 | 52 | // Remove non unique values. |
| 53 | - $repeater_formatted_data = array_unique( $repeater_formatted_data ); |
|
| 53 | + $repeater_formatted_data = array_unique($repeater_formatted_data); |
|
| 54 | 54 | // Remove empty values |
| 55 | - $repeater_formatted_data = array_filter( $repeater_formatted_data, 'strlen' ); |
|
| 55 | + $repeater_formatted_data = array_filter($repeater_formatted_data, 'strlen'); |
|
| 56 | 56 | |
| 57 | 57 | // re-index all the values. |
| 58 | - return array_values( $repeater_formatted_data ); |
|
| 58 | + return array_values($repeater_formatted_data); |
|
| 59 | 59 | } |
| 60 | 60 | } |
| 61 | 61 | |
@@ -24,356 +24,356 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class Edit_Mappings_Page extends Wordlift_Admin_Page { |
| 26 | 26 | |
| 27 | - /** Instance to store the registry class. |
|
| 28 | - * @var Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance} |
|
| 29 | - */ |
|
| 30 | - public $transform_function_registry; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Edit_Mappings_Page constructor. |
|
| 34 | - * |
|
| 35 | - * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance } |
|
| 36 | - */ |
|
| 37 | - public function __construct( $transform_function_registry ) { |
|
| 38 | - parent::__construct(); |
|
| 39 | - $this->transform_function_registry = $transform_function_registry; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function render() { |
|
| 43 | - // Render all the settings when this method is called, because the partial page is loaded after |
|
| 44 | - // this method. |
|
| 45 | - // Load the UI dependencies. |
|
| 46 | - $edit_mapping_settings = $this->get_ui_settings_array(); |
|
| 47 | - // Supply the settings to js client. |
|
| 48 | - wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings ); |
|
| 49 | - |
|
| 50 | - parent::render(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Load the text settings needed for the edit_mappings_page. |
|
| 55 | - * @param array $edit_mapping_settings Key value pair of settings used by edit mappings page. |
|
| 56 | - * |
|
| 57 | - * @return array Adding text settings to the main settings array. |
|
| 58 | - */ |
|
| 59 | - private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) { |
|
| 60 | - $edit_mapping_settings['wl_add_mapping_text'] = __( 'Add Mapping', 'wordlift' ); |
|
| 61 | - $edit_mapping_settings['wl_edit_mapping_text'] = __( 'Edit Mapping', 'wordlift' ); |
|
| 62 | - $edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' ); |
|
| 63 | - $edit_mapping_settings['page'] = 'wl_edit_mapping'; |
|
| 64 | - return $edit_mapping_settings; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * The base class {@link Wordlift_Admin_Page} will add the admin page to the WordLift menu. |
|
| 69 | - * |
|
| 70 | - * We don't want this page to be in the menu though. Therefore we override the `parent_slug` so that WordPress won't |
|
| 71 | - * show it there. |
|
| 72 | - * |
|
| 73 | - * @return null return null to avoid this page to be displayed in WordLift's menu. |
|
| 74 | - */ |
|
| 75 | - protected function get_parent_slug() { |
|
| 76 | - return null; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * {@inheritdoc} |
|
| 81 | - */ |
|
| 82 | - public function get_page_title() { |
|
| 83 | - |
|
| 84 | - return __( 'Edit Mappings', 'wordlift' ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * {@inheritdoc} |
|
| 89 | - */ |
|
| 90 | - public function get_menu_title() { |
|
| 91 | - |
|
| 92 | - return __( 'Edit Mappings', 'wordlift' ); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * {@inheritdoc} |
|
| 97 | - */ |
|
| 98 | - public function get_menu_slug() { |
|
| 99 | - |
|
| 100 | - return 'wl_edit_mapping'; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * {@inheritdoc} |
|
| 105 | - */ |
|
| 106 | - public function get_partial_name() { |
|
| 107 | - return 'wordlift-admin-mappings-edit.php'; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * {@inheritdoc} |
|
| 112 | - */ |
|
| 113 | - public function enqueue_scripts() { |
|
| 114 | - |
|
| 115 | - // Enqueue the script. |
|
| 116 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 117 | - 'wl-mappings-edit', |
|
| 118 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit', |
|
| 119 | - array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 120 | - true |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - // Enqueue the style. |
|
| 124 | - wp_enqueue_style( |
|
| 125 | - 'wl-mappings-edit', |
|
| 126 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit.css', |
|
| 127 | - Wordlift::get_instance()->get_version() |
|
| 128 | - ); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Returns field name options based on the chosen field type. |
|
| 133 | - * if string is returned a text field would be shown to user, if an array of options is returned |
|
| 134 | - * then the select box would be shown to user. |
|
| 135 | - * |
|
| 136 | - * @return array Array of the options. |
|
| 137 | - */ |
|
| 138 | - public static function get_all_field_name_options() { |
|
| 139 | - |
|
| 140 | - $options = array( |
|
| 141 | - array( |
|
| 142 | - 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD, |
|
| 143 | - 'value' => '', |
|
| 144 | - 'label' => __( 'Fixed Text', 'wordlift' ), |
|
| 145 | - ), |
|
| 146 | - // @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings. |
|
| 147 | - array( |
|
| 148 | - 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD, |
|
| 149 | - 'value' => '', |
|
| 150 | - 'label' => __( 'Custom Field', 'wordlift' ), |
|
| 151 | - ), |
|
| 152 | - ); |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Allow 3rd parties to add field types. |
|
| 156 | - * |
|
| 157 | - * @param array An array of Field Types. |
|
| 158 | - * |
|
| 159 | - * @return array An array of Field Types. |
|
| 160 | - * |
|
| 161 | - * @since 3.25.0 |
|
| 162 | - */ |
|
| 163 | - return apply_filters( 'wl_mappings_field_types', $options ); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @since 3.25.0 |
|
| 168 | - * Load dependencies required for js client. |
|
| 169 | - * @return array An Array containing key value pairs of settings. |
|
| 170 | - */ |
|
| 171 | - public function get_ui_settings_array() { |
|
| 172 | - // Create ui settings array to be used by js client. |
|
| 173 | - $edit_mapping_settings = array(); |
|
| 174 | - $edit_mapping_settings = $this->load_rest_settings( $edit_mapping_settings ); |
|
| 175 | - $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings ); |
|
| 176 | - $edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options(); |
|
| 177 | - $edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings ); |
|
| 178 | - // Load logic field options. |
|
| 179 | - $edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings ); |
|
| 180 | - $edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings ); |
|
| 181 | - |
|
| 182 | - return $edit_mapping_settings; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Returns post type, post category, or any other post taxonomies |
|
| 187 | - * @return array An array of select options |
|
| 188 | - */ |
|
| 189 | - private static function get_post_taxonomies_and_terms() { |
|
| 190 | - $taxonomy_options = array(); |
|
| 191 | - $term_options = array(); |
|
| 192 | - $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
| 193 | - |
|
| 194 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 195 | - array_push( |
|
| 196 | - $taxonomy_options, |
|
| 197 | - array( |
|
| 198 | - 'label' => $taxonomy->label, |
|
| 199 | - 'value' => $taxonomy->name, |
|
| 200 | - 'api_source' => 'taxonomy' |
|
| 201 | - ) |
|
| 202 | - ); |
|
| 203 | - } |
|
| 204 | - // Post type is also included in the list of taxonomies, so get the post type and merge with options. |
|
| 205 | - $post_type_array = self::get_post_type_key_and_value(); |
|
| 206 | - $post_type_option = $post_type_array['post_type_option_name']; |
|
| 207 | - // Get also the list of post types from the post_type_array. |
|
| 208 | - $post_type_option_values = $post_type_array['post_type_option_values']; |
|
| 209 | - // Merge the post type option and post types in the taxonomy options |
|
| 210 | - array_push( $taxonomy_options, $post_type_option ); |
|
| 211 | - $term_options = array_merge( $term_options, $post_type_option_values ); |
|
| 212 | - return array( |
|
| 213 | - 'taxonomy_options' => $taxonomy_options, |
|
| 214 | - 'term_options' => $term_options |
|
| 215 | - ); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Return post type option and post type option values. |
|
| 220 | - * |
|
| 221 | - * @return array Array of post_type_option and post_type_option_values. |
|
| 222 | - */ |
|
| 223 | - private static function get_post_type_key_and_value() { |
|
| 224 | - $post_type_option_name = array( |
|
| 225 | - 'label' => __( 'Post type', 'wordlift' ), |
|
| 226 | - 'value' => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE, |
|
| 227 | - // Left empty since post types are provided locally. |
|
| 228 | - 'api_source' => '', |
|
| 229 | - ); |
|
| 230 | - $post_type_option_values = array(); |
|
| 231 | - $post_types = get_post_types( |
|
| 232 | - array(), |
|
| 233 | - 'objects' |
|
| 234 | - ); |
|
| 235 | - foreach ( $post_types as $post_type ) { |
|
| 236 | - array_push( |
|
| 237 | - $post_type_option_values, |
|
| 238 | - array( |
|
| 239 | - 'label' => $post_type->label, |
|
| 240 | - 'value' => $post_type->name, |
|
| 241 | - 'parent_value' => 'post_type', |
|
| 242 | - ) |
|
| 243 | - ); |
|
| 244 | - } |
|
| 245 | - return array( |
|
| 246 | - 'post_type_option_name' => $post_type_option_name, |
|
| 247 | - 'post_type_option_values' => $post_type_option_values |
|
| 248 | - ); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * This function loads the equal to, not equal to operator to the edit mapping settings. |
|
| 253 | - * |
|
| 254 | - * @param array $edit_mapping_settings |
|
| 255 | - * @return array Loads the logic field options to the $edit_mapping_settings. |
|
| 256 | - */ |
|
| 257 | - private function load_logic_field_options( array $edit_mapping_settings ) { |
|
| 258 | - $edit_mapping_settings['wl_logic_field_options'] = array( |
|
| 259 | - array( |
|
| 260 | - 'label' => __( 'is equal to', 'wordlift' ), |
|
| 261 | - 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO, |
|
| 262 | - ), |
|
| 263 | - array( |
|
| 264 | - 'label' => __( 'is not equal to', 'wordlift' ), |
|
| 265 | - 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO, |
|
| 266 | - ), |
|
| 267 | - ); |
|
| 268 | - |
|
| 269 | - return $edit_mapping_settings; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Validates the nonce posted by client and then assign the mapping id which should be edited. |
|
| 274 | - * |
|
| 275 | - * @param array $edit_mapping_settings |
|
| 276 | - * @return array Edit mapping settings array with the mapping id if the nonce is valid. |
|
| 277 | - */ |
|
| 278 | - private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) { |
|
| 27 | + /** Instance to store the registry class. |
|
| 28 | + * @var Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance} |
|
| 29 | + */ |
|
| 30 | + public $transform_function_registry; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Edit_Mappings_Page constructor. |
|
| 34 | + * |
|
| 35 | + * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance } |
|
| 36 | + */ |
|
| 37 | + public function __construct( $transform_function_registry ) { |
|
| 38 | + parent::__construct(); |
|
| 39 | + $this->transform_function_registry = $transform_function_registry; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function render() { |
|
| 43 | + // Render all the settings when this method is called, because the partial page is loaded after |
|
| 44 | + // this method. |
|
| 45 | + // Load the UI dependencies. |
|
| 46 | + $edit_mapping_settings = $this->get_ui_settings_array(); |
|
| 47 | + // Supply the settings to js client. |
|
| 48 | + wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings ); |
|
| 49 | + |
|
| 50 | + parent::render(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Load the text settings needed for the edit_mappings_page. |
|
| 55 | + * @param array $edit_mapping_settings Key value pair of settings used by edit mappings page. |
|
| 56 | + * |
|
| 57 | + * @return array Adding text settings to the main settings array. |
|
| 58 | + */ |
|
| 59 | + private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) { |
|
| 60 | + $edit_mapping_settings['wl_add_mapping_text'] = __( 'Add Mapping', 'wordlift' ); |
|
| 61 | + $edit_mapping_settings['wl_edit_mapping_text'] = __( 'Edit Mapping', 'wordlift' ); |
|
| 62 | + $edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' ); |
|
| 63 | + $edit_mapping_settings['page'] = 'wl_edit_mapping'; |
|
| 64 | + return $edit_mapping_settings; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * The base class {@link Wordlift_Admin_Page} will add the admin page to the WordLift menu. |
|
| 69 | + * |
|
| 70 | + * We don't want this page to be in the menu though. Therefore we override the `parent_slug` so that WordPress won't |
|
| 71 | + * show it there. |
|
| 72 | + * |
|
| 73 | + * @return null return null to avoid this page to be displayed in WordLift's menu. |
|
| 74 | + */ |
|
| 75 | + protected function get_parent_slug() { |
|
| 76 | + return null; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * {@inheritdoc} |
|
| 81 | + */ |
|
| 82 | + public function get_page_title() { |
|
| 83 | + |
|
| 84 | + return __( 'Edit Mappings', 'wordlift' ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * {@inheritdoc} |
|
| 89 | + */ |
|
| 90 | + public function get_menu_title() { |
|
| 91 | + |
|
| 92 | + return __( 'Edit Mappings', 'wordlift' ); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * {@inheritdoc} |
|
| 97 | + */ |
|
| 98 | + public function get_menu_slug() { |
|
| 99 | + |
|
| 100 | + return 'wl_edit_mapping'; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * {@inheritdoc} |
|
| 105 | + */ |
|
| 106 | + public function get_partial_name() { |
|
| 107 | + return 'wordlift-admin-mappings-edit.php'; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * {@inheritdoc} |
|
| 112 | + */ |
|
| 113 | + public function enqueue_scripts() { |
|
| 114 | + |
|
| 115 | + // Enqueue the script. |
|
| 116 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 117 | + 'wl-mappings-edit', |
|
| 118 | + plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit', |
|
| 119 | + array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 120 | + true |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + // Enqueue the style. |
|
| 124 | + wp_enqueue_style( |
|
| 125 | + 'wl-mappings-edit', |
|
| 126 | + plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit.css', |
|
| 127 | + Wordlift::get_instance()->get_version() |
|
| 128 | + ); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Returns field name options based on the chosen field type. |
|
| 133 | + * if string is returned a text field would be shown to user, if an array of options is returned |
|
| 134 | + * then the select box would be shown to user. |
|
| 135 | + * |
|
| 136 | + * @return array Array of the options. |
|
| 137 | + */ |
|
| 138 | + public static function get_all_field_name_options() { |
|
| 139 | + |
|
| 140 | + $options = array( |
|
| 141 | + array( |
|
| 142 | + 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD, |
|
| 143 | + 'value' => '', |
|
| 144 | + 'label' => __( 'Fixed Text', 'wordlift' ), |
|
| 145 | + ), |
|
| 146 | + // @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings. |
|
| 147 | + array( |
|
| 148 | + 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD, |
|
| 149 | + 'value' => '', |
|
| 150 | + 'label' => __( 'Custom Field', 'wordlift' ), |
|
| 151 | + ), |
|
| 152 | + ); |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Allow 3rd parties to add field types. |
|
| 156 | + * |
|
| 157 | + * @param array An array of Field Types. |
|
| 158 | + * |
|
| 159 | + * @return array An array of Field Types. |
|
| 160 | + * |
|
| 161 | + * @since 3.25.0 |
|
| 162 | + */ |
|
| 163 | + return apply_filters( 'wl_mappings_field_types', $options ); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @since 3.25.0 |
|
| 168 | + * Load dependencies required for js client. |
|
| 169 | + * @return array An Array containing key value pairs of settings. |
|
| 170 | + */ |
|
| 171 | + public function get_ui_settings_array() { |
|
| 172 | + // Create ui settings array to be used by js client. |
|
| 173 | + $edit_mapping_settings = array(); |
|
| 174 | + $edit_mapping_settings = $this->load_rest_settings( $edit_mapping_settings ); |
|
| 175 | + $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings ); |
|
| 176 | + $edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options(); |
|
| 177 | + $edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings ); |
|
| 178 | + // Load logic field options. |
|
| 179 | + $edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings ); |
|
| 180 | + $edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings ); |
|
| 181 | + |
|
| 182 | + return $edit_mapping_settings; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Returns post type, post category, or any other post taxonomies |
|
| 187 | + * @return array An array of select options |
|
| 188 | + */ |
|
| 189 | + private static function get_post_taxonomies_and_terms() { |
|
| 190 | + $taxonomy_options = array(); |
|
| 191 | + $term_options = array(); |
|
| 192 | + $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
| 193 | + |
|
| 194 | + foreach ( $taxonomies as $taxonomy ) { |
|
| 195 | + array_push( |
|
| 196 | + $taxonomy_options, |
|
| 197 | + array( |
|
| 198 | + 'label' => $taxonomy->label, |
|
| 199 | + 'value' => $taxonomy->name, |
|
| 200 | + 'api_source' => 'taxonomy' |
|
| 201 | + ) |
|
| 202 | + ); |
|
| 203 | + } |
|
| 204 | + // Post type is also included in the list of taxonomies, so get the post type and merge with options. |
|
| 205 | + $post_type_array = self::get_post_type_key_and_value(); |
|
| 206 | + $post_type_option = $post_type_array['post_type_option_name']; |
|
| 207 | + // Get also the list of post types from the post_type_array. |
|
| 208 | + $post_type_option_values = $post_type_array['post_type_option_values']; |
|
| 209 | + // Merge the post type option and post types in the taxonomy options |
|
| 210 | + array_push( $taxonomy_options, $post_type_option ); |
|
| 211 | + $term_options = array_merge( $term_options, $post_type_option_values ); |
|
| 212 | + return array( |
|
| 213 | + 'taxonomy_options' => $taxonomy_options, |
|
| 214 | + 'term_options' => $term_options |
|
| 215 | + ); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Return post type option and post type option values. |
|
| 220 | + * |
|
| 221 | + * @return array Array of post_type_option and post_type_option_values. |
|
| 222 | + */ |
|
| 223 | + private static function get_post_type_key_and_value() { |
|
| 224 | + $post_type_option_name = array( |
|
| 225 | + 'label' => __( 'Post type', 'wordlift' ), |
|
| 226 | + 'value' => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE, |
|
| 227 | + // Left empty since post types are provided locally. |
|
| 228 | + 'api_source' => '', |
|
| 229 | + ); |
|
| 230 | + $post_type_option_values = array(); |
|
| 231 | + $post_types = get_post_types( |
|
| 232 | + array(), |
|
| 233 | + 'objects' |
|
| 234 | + ); |
|
| 235 | + foreach ( $post_types as $post_type ) { |
|
| 236 | + array_push( |
|
| 237 | + $post_type_option_values, |
|
| 238 | + array( |
|
| 239 | + 'label' => $post_type->label, |
|
| 240 | + 'value' => $post_type->name, |
|
| 241 | + 'parent_value' => 'post_type', |
|
| 242 | + ) |
|
| 243 | + ); |
|
| 244 | + } |
|
| 245 | + return array( |
|
| 246 | + 'post_type_option_name' => $post_type_option_name, |
|
| 247 | + 'post_type_option_values' => $post_type_option_values |
|
| 248 | + ); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * This function loads the equal to, not equal to operator to the edit mapping settings. |
|
| 253 | + * |
|
| 254 | + * @param array $edit_mapping_settings |
|
| 255 | + * @return array Loads the logic field options to the $edit_mapping_settings. |
|
| 256 | + */ |
|
| 257 | + private function load_logic_field_options( array $edit_mapping_settings ) { |
|
| 258 | + $edit_mapping_settings['wl_logic_field_options'] = array( |
|
| 259 | + array( |
|
| 260 | + 'label' => __( 'is equal to', 'wordlift' ), |
|
| 261 | + 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO, |
|
| 262 | + ), |
|
| 263 | + array( |
|
| 264 | + 'label' => __( 'is not equal to', 'wordlift' ), |
|
| 265 | + 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO, |
|
| 266 | + ), |
|
| 267 | + ); |
|
| 268 | + |
|
| 269 | + return $edit_mapping_settings; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Validates the nonce posted by client and then assign the mapping id which should be edited. |
|
| 274 | + * |
|
| 275 | + * @param array $edit_mapping_settings |
|
| 276 | + * @return array Edit mapping settings array with the mapping id if the nonce is valid. |
|
| 277 | + */ |
|
| 278 | + private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) { |
|
| 279 | 279 | // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id |
| 280 | - if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] ) |
|
| 281 | - && wp_verify_nonce( $_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce' ) ) { |
|
| 282 | - // We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST. |
|
| 283 | - $edit_mapping_settings['wl_edit_mapping_id'] = (int) filter_var( $_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT ); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - return $edit_mapping_settings; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Load the rest settings required for the edit_mappings js client. |
|
| 291 | - * |
|
| 292 | - * @param array $edit_mapping_settings |
|
| 293 | - * @return array |
|
| 294 | - */ |
|
| 295 | - private function load_rest_settings( array $edit_mapping_settings ) { |
|
| 296 | - $edit_mapping_settings['rest_url'] = get_rest_url( |
|
| 297 | - null, |
|
| 298 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 299 | - ); |
|
| 300 | - $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
| 301 | - $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings ); |
|
| 302 | - |
|
| 303 | - return $edit_mapping_settings; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Load the rule field options in to the settings. |
|
| 308 | - * |
|
| 309 | - * @param array $edit_mapping_settings |
|
| 310 | - * |
|
| 311 | - * @return array Return the settings. |
|
| 312 | - */ |
|
| 313 | - private function load_rule_field_options( array $edit_mapping_settings ) { |
|
| 314 | - // Load the rule field options. |
|
| 315 | - $rule_field_data = self::get_post_taxonomies_and_terms(); |
|
| 316 | - $edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options']; |
|
| 317 | - $edit_mapping_settings['wl_rule_field_two_options'] = $rule_field_data['term_options']; |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Allow 3rd parties to add ui options. |
|
| 321 | - * |
|
| 322 | - * @param array Array of Rule field one options where each item is in format |
|
| 323 | - * |
|
| 324 | - * array ( 'label' => string, 'value' => string, 'api_source'=>string); |
|
| 325 | - * |
|
| 326 | - * Leave api_source empty string to ensure didnt fetch rule field two options |
|
| 327 | - * from api. |
|
| 328 | - * |
|
| 329 | - * @return array Array of Rule field one options |
|
| 330 | - * |
|
| 331 | - * @since 3.27.0 |
|
| 332 | - */ |
|
| 333 | - $edit_mapping_settings['wl_rule_field_one_options'] = apply_filters( |
|
| 334 | - 'wl_mappings_rule_field_one_options', |
|
| 335 | - $edit_mapping_settings['wl_rule_field_one_options'] |
|
| 336 | - ); |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Allow 3rd parties to add rule field two options. |
|
| 340 | - * |
|
| 341 | - * @param array Array of Rule field two option where each item is in format |
|
| 342 | - * |
|
| 343 | - * array ( 'label' => string, 'value' => string, 'parent_value' => string ); |
|
| 344 | - * |
|
| 345 | - * where parent_value is the value of the parent option in the rule_field_one_option. |
|
| 346 | - * |
|
| 347 | - * @since 3.27.0 |
|
| 348 | - */ |
|
| 349 | - $edit_mapping_settings['wl_rule_field_two_options'] = apply_filters( |
|
| 350 | - 'wl_mappings_rule_field_two_options', |
|
| 351 | - $edit_mapping_settings['wl_rule_field_two_options'] |
|
| 352 | - ); |
|
| 353 | - |
|
| 354 | - return $edit_mapping_settings; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Load field type and field name options to the settings array. |
|
| 359 | - * @param array $edit_mapping_settings |
|
| 360 | - * |
|
| 361 | - * @return array |
|
| 362 | - */ |
|
| 363 | - private function load_field_type_and_name_options( array $edit_mapping_settings ) { |
|
| 364 | - $all_field_name_options = self::get_all_field_name_options(); |
|
| 365 | - $all_field_types_options = array_map( function ( $item ) { |
|
| 366 | - return array( |
|
| 367 | - 'label' => $item['label'], |
|
| 368 | - 'value' => $item['field_type'], |
|
| 369 | - ); |
|
| 370 | - }, $all_field_name_options ); |
|
| 371 | - |
|
| 372 | - $edit_mapping_settings['wl_field_type_options'] = $all_field_types_options; |
|
| 373 | - // Add wl_edit_field_name_options. |
|
| 374 | - $edit_mapping_settings['wl_field_name_options'] = $all_field_name_options; |
|
| 375 | - |
|
| 376 | - return $edit_mapping_settings; |
|
| 377 | - } |
|
| 280 | + if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] ) |
|
| 281 | + && wp_verify_nonce( $_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce' ) ) { |
|
| 282 | + // We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST. |
|
| 283 | + $edit_mapping_settings['wl_edit_mapping_id'] = (int) filter_var( $_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT ); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + return $edit_mapping_settings; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Load the rest settings required for the edit_mappings js client. |
|
| 291 | + * |
|
| 292 | + * @param array $edit_mapping_settings |
|
| 293 | + * @return array |
|
| 294 | + */ |
|
| 295 | + private function load_rest_settings( array $edit_mapping_settings ) { |
|
| 296 | + $edit_mapping_settings['rest_url'] = get_rest_url( |
|
| 297 | + null, |
|
| 298 | + WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 299 | + ); |
|
| 300 | + $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
| 301 | + $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings ); |
|
| 302 | + |
|
| 303 | + return $edit_mapping_settings; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Load the rule field options in to the settings. |
|
| 308 | + * |
|
| 309 | + * @param array $edit_mapping_settings |
|
| 310 | + * |
|
| 311 | + * @return array Return the settings. |
|
| 312 | + */ |
|
| 313 | + private function load_rule_field_options( array $edit_mapping_settings ) { |
|
| 314 | + // Load the rule field options. |
|
| 315 | + $rule_field_data = self::get_post_taxonomies_and_terms(); |
|
| 316 | + $edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options']; |
|
| 317 | + $edit_mapping_settings['wl_rule_field_two_options'] = $rule_field_data['term_options']; |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Allow 3rd parties to add ui options. |
|
| 321 | + * |
|
| 322 | + * @param array Array of Rule field one options where each item is in format |
|
| 323 | + * |
|
| 324 | + * array ( 'label' => string, 'value' => string, 'api_source'=>string); |
|
| 325 | + * |
|
| 326 | + * Leave api_source empty string to ensure didnt fetch rule field two options |
|
| 327 | + * from api. |
|
| 328 | + * |
|
| 329 | + * @return array Array of Rule field one options |
|
| 330 | + * |
|
| 331 | + * @since 3.27.0 |
|
| 332 | + */ |
|
| 333 | + $edit_mapping_settings['wl_rule_field_one_options'] = apply_filters( |
|
| 334 | + 'wl_mappings_rule_field_one_options', |
|
| 335 | + $edit_mapping_settings['wl_rule_field_one_options'] |
|
| 336 | + ); |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Allow 3rd parties to add rule field two options. |
|
| 340 | + * |
|
| 341 | + * @param array Array of Rule field two option where each item is in format |
|
| 342 | + * |
|
| 343 | + * array ( 'label' => string, 'value' => string, 'parent_value' => string ); |
|
| 344 | + * |
|
| 345 | + * where parent_value is the value of the parent option in the rule_field_one_option. |
|
| 346 | + * |
|
| 347 | + * @since 3.27.0 |
|
| 348 | + */ |
|
| 349 | + $edit_mapping_settings['wl_rule_field_two_options'] = apply_filters( |
|
| 350 | + 'wl_mappings_rule_field_two_options', |
|
| 351 | + $edit_mapping_settings['wl_rule_field_two_options'] |
|
| 352 | + ); |
|
| 353 | + |
|
| 354 | + return $edit_mapping_settings; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Load field type and field name options to the settings array. |
|
| 359 | + * @param array $edit_mapping_settings |
|
| 360 | + * |
|
| 361 | + * @return array |
|
| 362 | + */ |
|
| 363 | + private function load_field_type_and_name_options( array $edit_mapping_settings ) { |
|
| 364 | + $all_field_name_options = self::get_all_field_name_options(); |
|
| 365 | + $all_field_types_options = array_map( function ( $item ) { |
|
| 366 | + return array( |
|
| 367 | + 'label' => $item['label'], |
|
| 368 | + 'value' => $item['field_type'], |
|
| 369 | + ); |
|
| 370 | + }, $all_field_name_options ); |
|
| 371 | + |
|
| 372 | + $edit_mapping_settings['wl_field_type_options'] = $all_field_types_options; |
|
| 373 | + // Add wl_edit_field_name_options. |
|
| 374 | + $edit_mapping_settings['wl_field_name_options'] = $all_field_name_options; |
|
| 375 | + |
|
| 376 | + return $edit_mapping_settings; |
|
| 377 | + } |
|
| 378 | 378 | |
| 379 | 379 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance } |
| 36 | 36 | */ |
| 37 | - public function __construct( $transform_function_registry ) { |
|
| 37 | + public function __construct($transform_function_registry) { |
|
| 38 | 38 | parent::__construct(); |
| 39 | 39 | $this->transform_function_registry = $transform_function_registry; |
| 40 | 40 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | // Load the UI dependencies. |
| 46 | 46 | $edit_mapping_settings = $this->get_ui_settings_array(); |
| 47 | 47 | // Supply the settings to js client. |
| 48 | - wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings ); |
|
| 48 | + wp_localize_script('wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings); |
|
| 49 | 49 | |
| 50 | 50 | parent::render(); |
| 51 | 51 | } |
@@ -56,10 +56,10 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @return array Adding text settings to the main settings array. |
| 58 | 58 | */ |
| 59 | - private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) { |
|
| 60 | - $edit_mapping_settings['wl_add_mapping_text'] = __( 'Add Mapping', 'wordlift' ); |
|
| 61 | - $edit_mapping_settings['wl_edit_mapping_text'] = __( 'Edit Mapping', 'wordlift' ); |
|
| 62 | - $edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' ); |
|
| 59 | + private function load_text_settings_for_edit_mapping_page(array $edit_mapping_settings) { |
|
| 60 | + $edit_mapping_settings['wl_add_mapping_text'] = __('Add Mapping', 'wordlift'); |
|
| 61 | + $edit_mapping_settings['wl_edit_mapping_text'] = __('Edit Mapping', 'wordlift'); |
|
| 62 | + $edit_mapping_settings['wl_edit_mapping_no_item'] = __('Unable to find the mapping item', 'wordlift'); |
|
| 63 | 63 | $edit_mapping_settings['page'] = 'wl_edit_mapping'; |
| 64 | 64 | return $edit_mapping_settings; |
| 65 | 65 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | public function get_page_title() { |
| 83 | 83 | |
| 84 | - return __( 'Edit Mappings', 'wordlift' ); |
|
| 84 | + return __('Edit Mappings', 'wordlift'); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | /** |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | */ |
| 90 | 90 | public function get_menu_title() { |
| 91 | 91 | |
| 92 | - return __( 'Edit Mappings', 'wordlift' ); |
|
| 92 | + return __('Edit Mappings', 'wordlift'); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
@@ -115,15 +115,15 @@ discard block |
||
| 115 | 115 | // Enqueue the script. |
| 116 | 116 | Scripts_Helper::enqueue_based_on_wordpress_version( |
| 117 | 117 | 'wl-mappings-edit', |
| 118 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit', |
|
| 119 | - array( 'react', 'react-dom', 'wp-polyfill' ), |
|
| 118 | + plugin_dir_url(dirname(dirname(dirname(__FILE__)))).'js/dist/mappings-edit', |
|
| 119 | + array('react', 'react-dom', 'wp-polyfill'), |
|
| 120 | 120 | true |
| 121 | 121 | ); |
| 122 | 122 | |
| 123 | 123 | // Enqueue the style. |
| 124 | 124 | wp_enqueue_style( |
| 125 | 125 | 'wl-mappings-edit', |
| 126 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit.css', |
|
| 126 | + plugin_dir_url(dirname(dirname(dirname(__FILE__)))).'js/dist/mappings-edit.css', |
|
| 127 | 127 | Wordlift::get_instance()->get_version() |
| 128 | 128 | ); |
| 129 | 129 | } |
@@ -141,13 +141,13 @@ discard block |
||
| 141 | 141 | array( |
| 142 | 142 | 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD, |
| 143 | 143 | 'value' => '', |
| 144 | - 'label' => __( 'Fixed Text', 'wordlift' ), |
|
| 144 | + 'label' => __('Fixed Text', 'wordlift'), |
|
| 145 | 145 | ), |
| 146 | 146 | // @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings. |
| 147 | 147 | array( |
| 148 | 148 | 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD, |
| 149 | 149 | 'value' => '', |
| 150 | - 'label' => __( 'Custom Field', 'wordlift' ), |
|
| 150 | + 'label' => __('Custom Field', 'wordlift'), |
|
| 151 | 151 | ), |
| 152 | 152 | ); |
| 153 | 153 | |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | * @since 3.25.0 |
| 162 | 162 | */ |
| 163 | - return apply_filters( 'wl_mappings_field_types', $options ); |
|
| 163 | + return apply_filters('wl_mappings_field_types', $options); |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | /** |
@@ -171,13 +171,13 @@ discard block |
||
| 171 | 171 | public function get_ui_settings_array() { |
| 172 | 172 | // Create ui settings array to be used by js client. |
| 173 | 173 | $edit_mapping_settings = array(); |
| 174 | - $edit_mapping_settings = $this->load_rest_settings( $edit_mapping_settings ); |
|
| 175 | - $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings ); |
|
| 174 | + $edit_mapping_settings = $this->load_rest_settings($edit_mapping_settings); |
|
| 175 | + $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page($edit_mapping_settings); |
|
| 176 | 176 | $edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options(); |
| 177 | - $edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings ); |
|
| 177 | + $edit_mapping_settings = $this->load_field_type_and_name_options($edit_mapping_settings); |
|
| 178 | 178 | // Load logic field options. |
| 179 | - $edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings ); |
|
| 180 | - $edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings ); |
|
| 179 | + $edit_mapping_settings = $this->load_logic_field_options($edit_mapping_settings); |
|
| 180 | + $edit_mapping_settings = $this->load_rule_field_options($edit_mapping_settings); |
|
| 181 | 181 | |
| 182 | 182 | return $edit_mapping_settings; |
| 183 | 183 | } |
@@ -189,9 +189,9 @@ discard block |
||
| 189 | 189 | private static function get_post_taxonomies_and_terms() { |
| 190 | 190 | $taxonomy_options = array(); |
| 191 | 191 | $term_options = array(); |
| 192 | - $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
| 192 | + $taxonomies = get_object_taxonomies('post', 'objects'); |
|
| 193 | 193 | |
| 194 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 194 | + foreach ($taxonomies as $taxonomy) { |
|
| 195 | 195 | array_push( |
| 196 | 196 | $taxonomy_options, |
| 197 | 197 | array( |
@@ -202,13 +202,13 @@ discard block |
||
| 202 | 202 | ); |
| 203 | 203 | } |
| 204 | 204 | // Post type is also included in the list of taxonomies, so get the post type and merge with options. |
| 205 | - $post_type_array = self::get_post_type_key_and_value(); |
|
| 205 | + $post_type_array = self::get_post_type_key_and_value(); |
|
| 206 | 206 | $post_type_option = $post_type_array['post_type_option_name']; |
| 207 | 207 | // Get also the list of post types from the post_type_array. |
| 208 | 208 | $post_type_option_values = $post_type_array['post_type_option_values']; |
| 209 | 209 | // Merge the post type option and post types in the taxonomy options |
| 210 | - array_push( $taxonomy_options, $post_type_option ); |
|
| 211 | - $term_options = array_merge( $term_options, $post_type_option_values ); |
|
| 210 | + array_push($taxonomy_options, $post_type_option); |
|
| 211 | + $term_options = array_merge($term_options, $post_type_option_values); |
|
| 212 | 212 | return array( |
| 213 | 213 | 'taxonomy_options' => $taxonomy_options, |
| 214 | 214 | 'term_options' => $term_options |
@@ -221,8 +221,8 @@ discard block |
||
| 221 | 221 | * @return array Array of post_type_option and post_type_option_values. |
| 222 | 222 | */ |
| 223 | 223 | private static function get_post_type_key_and_value() { |
| 224 | - $post_type_option_name = array( |
|
| 225 | - 'label' => __( 'Post type', 'wordlift' ), |
|
| 224 | + $post_type_option_name = array( |
|
| 225 | + 'label' => __('Post type', 'wordlift'), |
|
| 226 | 226 | 'value' => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE, |
| 227 | 227 | // Left empty since post types are provided locally. |
| 228 | 228 | 'api_source' => '', |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | array(), |
| 233 | 233 | 'objects' |
| 234 | 234 | ); |
| 235 | - foreach ( $post_types as $post_type ) { |
|
| 235 | + foreach ($post_types as $post_type) { |
|
| 236 | 236 | array_push( |
| 237 | 237 | $post_type_option_values, |
| 238 | 238 | array( |
@@ -254,14 +254,14 @@ discard block |
||
| 254 | 254 | * @param array $edit_mapping_settings |
| 255 | 255 | * @return array Loads the logic field options to the $edit_mapping_settings. |
| 256 | 256 | */ |
| 257 | - private function load_logic_field_options( array $edit_mapping_settings ) { |
|
| 257 | + private function load_logic_field_options(array $edit_mapping_settings) { |
|
| 258 | 258 | $edit_mapping_settings['wl_logic_field_options'] = array( |
| 259 | 259 | array( |
| 260 | - 'label' => __( 'is equal to', 'wordlift' ), |
|
| 260 | + 'label' => __('is equal to', 'wordlift'), |
|
| 261 | 261 | 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO, |
| 262 | 262 | ), |
| 263 | 263 | array( |
| 264 | - 'label' => __( 'is not equal to', 'wordlift' ), |
|
| 264 | + 'label' => __('is not equal to', 'wordlift'), |
|
| 265 | 265 | 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO, |
| 266 | 266 | ), |
| 267 | 267 | ); |
@@ -275,12 +275,12 @@ discard block |
||
| 275 | 275 | * @param array $edit_mapping_settings |
| 276 | 276 | * @return array Edit mapping settings array with the mapping id if the nonce is valid. |
| 277 | 277 | */ |
| 278 | - private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) { |
|
| 278 | + private function validate_nonce_and_assign_mapping_id(array $edit_mapping_settings) { |
|
| 279 | 279 | // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id |
| 280 | - if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] ) |
|
| 281 | - && wp_verify_nonce( $_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce' ) ) { |
|
| 280 | + if (isset($_REQUEST['_wl_edit_mapping_nonce']) |
|
| 281 | + && wp_verify_nonce($_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce')) { |
|
| 282 | 282 | // We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST. |
| 283 | - $edit_mapping_settings['wl_edit_mapping_id'] = (int) filter_var( $_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT ); |
|
| 283 | + $edit_mapping_settings['wl_edit_mapping_id'] = (int) filter_var($_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT); |
|
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | return $edit_mapping_settings; |
@@ -292,13 +292,13 @@ discard block |
||
| 292 | 292 | * @param array $edit_mapping_settings |
| 293 | 293 | * @return array |
| 294 | 294 | */ |
| 295 | - private function load_rest_settings( array $edit_mapping_settings ) { |
|
| 296 | - $edit_mapping_settings['rest_url'] = get_rest_url( |
|
| 295 | + private function load_rest_settings(array $edit_mapping_settings) { |
|
| 296 | + $edit_mapping_settings['rest_url'] = get_rest_url( |
|
| 297 | 297 | null, |
| 298 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 298 | + WL_REST_ROUTE_DEFAULT_NAMESPACE.Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 299 | 299 | ); |
| 300 | - $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
| 301 | - $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings ); |
|
| 300 | + $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce('wp_rest'); |
|
| 301 | + $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id($edit_mapping_settings); |
|
| 302 | 302 | |
| 303 | 303 | return $edit_mapping_settings; |
| 304 | 304 | } |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | * |
| 311 | 311 | * @return array Return the settings. |
| 312 | 312 | */ |
| 313 | - private function load_rule_field_options( array $edit_mapping_settings ) { |
|
| 313 | + private function load_rule_field_options(array $edit_mapping_settings) { |
|
| 314 | 314 | // Load the rule field options. |
| 315 | 315 | $rule_field_data = self::get_post_taxonomies_and_terms(); |
| 316 | 316 | $edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options']; |
@@ -360,14 +360,14 @@ discard block |
||
| 360 | 360 | * |
| 361 | 361 | * @return array |
| 362 | 362 | */ |
| 363 | - private function load_field_type_and_name_options( array $edit_mapping_settings ) { |
|
| 363 | + private function load_field_type_and_name_options(array $edit_mapping_settings) { |
|
| 364 | 364 | $all_field_name_options = self::get_all_field_name_options(); |
| 365 | - $all_field_types_options = array_map( function ( $item ) { |
|
| 365 | + $all_field_types_options = array_map(function($item) { |
|
| 366 | 366 | return array( |
| 367 | 367 | 'label' => $item['label'], |
| 368 | 368 | 'value' => $item['field_type'], |
| 369 | 369 | ); |
| 370 | - }, $all_field_name_options ); |
|
| 370 | + }, $all_field_name_options); |
|
| 371 | 371 | |
| 372 | 372 | $edit_mapping_settings['wl_field_type_options'] = $all_field_types_options; |
| 373 | 373 | // Add wl_edit_field_name_options. |
@@ -14,58 +14,58 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Taxonomy_Option { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * This value is used in rule field one and rule field two |
|
| 19 | - * options are linked to one by this value |
|
| 20 | - */ |
|
| 21 | - const PARENT_VALUE = 'taxonomy'; |
|
| 17 | + /** |
|
| 18 | + * This value is used in rule field one and rule field two |
|
| 19 | + * options are linked to one by this value |
|
| 20 | + */ |
|
| 21 | + const PARENT_VALUE = 'taxonomy'; |
|
| 22 | 22 | |
| 23 | - public function add_taxonomy_option() { |
|
| 24 | - $this->add_rule_field_one_options(); |
|
| 25 | - $this->add_rule_field_two_options(); |
|
| 26 | - } |
|
| 23 | + public function add_taxonomy_option() { |
|
| 24 | + $this->add_rule_field_one_options(); |
|
| 25 | + $this->add_rule_field_two_options(); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - private function add_rule_field_one_options() { |
|
| 28 | + private function add_rule_field_one_options() { |
|
| 29 | 29 | |
| 30 | - add_filter( 'wl_mappings_rule_field_one_options', function ( $rule_field_one_options ) { |
|
| 30 | + add_filter( 'wl_mappings_rule_field_one_options', function ( $rule_field_one_options ) { |
|
| 31 | 31 | |
| 32 | - $rule_field_one_options[] = array( |
|
| 33 | - 'label' => __( 'Taxonomy', 'wordlift' ), |
|
| 34 | - 'value' => Taxonomy_Option::PARENT_VALUE, |
|
| 35 | - // Left empty since these values are provided locally, not needed to be fetched from |
|
| 36 | - // api. |
|
| 37 | - 'api_source' => '' |
|
| 38 | - ); |
|
| 32 | + $rule_field_one_options[] = array( |
|
| 33 | + 'label' => __( 'Taxonomy', 'wordlift' ), |
|
| 34 | + 'value' => Taxonomy_Option::PARENT_VALUE, |
|
| 35 | + // Left empty since these values are provided locally, not needed to be fetched from |
|
| 36 | + // api. |
|
| 37 | + 'api_source' => '' |
|
| 38 | + ); |
|
| 39 | 39 | |
| 40 | - return $rule_field_one_options; |
|
| 40 | + return $rule_field_one_options; |
|
| 41 | 41 | |
| 42 | - } ); |
|
| 43 | - } |
|
| 42 | + } ); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - private function add_rule_field_two_options() { |
|
| 45 | + private function add_rule_field_two_options() { |
|
| 46 | 46 | |
| 47 | - add_filter( 'wl_mappings_rule_field_two_options', function ( $rule_field_two_options ) { |
|
| 47 | + add_filter( 'wl_mappings_rule_field_two_options', function ( $rule_field_two_options ) { |
|
| 48 | 48 | |
| 49 | - $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
| 49 | + $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
| 50 | 50 | |
| 51 | - $taxonomy_options = array(); |
|
| 51 | + $taxonomy_options = array(); |
|
| 52 | 52 | |
| 53 | - foreach ( $taxonomies as $item ) { |
|
| 54 | - /** |
|
| 55 | - * $item Taxonomy |
|
| 56 | - */ |
|
| 57 | - $taxonomy_options[] = array( |
|
| 58 | - 'label' => __( $item->label, 'wordlift' ), |
|
| 59 | - 'value' => $item->name, |
|
| 60 | - // The value of parent option on rule field one. |
|
| 61 | - 'parent_value' => Taxonomy_Option::PARENT_VALUE |
|
| 62 | - ); |
|
| 63 | - } |
|
| 53 | + foreach ( $taxonomies as $item ) { |
|
| 54 | + /** |
|
| 55 | + * $item Taxonomy |
|
| 56 | + */ |
|
| 57 | + $taxonomy_options[] = array( |
|
| 58 | + 'label' => __( $item->label, 'wordlift' ), |
|
| 59 | + 'value' => $item->name, |
|
| 60 | + // The value of parent option on rule field one. |
|
| 61 | + 'parent_value' => Taxonomy_Option::PARENT_VALUE |
|
| 62 | + ); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - return array_merge( $rule_field_two_options, $taxonomy_options ); |
|
| 65 | + return array_merge( $rule_field_two_options, $taxonomy_options ); |
|
| 66 | 66 | |
| 67 | - } ); |
|
| 67 | + } ); |
|
| 68 | 68 | |
| 69 | - } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | } |
@@ -27,10 +27,10 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | private function add_rule_field_one_options() { |
| 29 | 29 | |
| 30 | - add_filter( 'wl_mappings_rule_field_one_options', function ( $rule_field_one_options ) { |
|
| 30 | + add_filter('wl_mappings_rule_field_one_options', function($rule_field_one_options) { |
|
| 31 | 31 | |
| 32 | 32 | $rule_field_one_options[] = array( |
| 33 | - 'label' => __( 'Taxonomy', 'wordlift' ), |
|
| 33 | + 'label' => __('Taxonomy', 'wordlift'), |
|
| 34 | 34 | 'value' => Taxonomy_Option::PARENT_VALUE, |
| 35 | 35 | // Left empty since these values are provided locally, not needed to be fetched from |
| 36 | 36 | // api. |
@@ -44,25 +44,25 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | private function add_rule_field_two_options() { |
| 46 | 46 | |
| 47 | - add_filter( 'wl_mappings_rule_field_two_options', function ( $rule_field_two_options ) { |
|
| 47 | + add_filter('wl_mappings_rule_field_two_options', function($rule_field_two_options) { |
|
| 48 | 48 | |
| 49 | - $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
| 49 | + $taxonomies = get_object_taxonomies('post', 'objects'); |
|
| 50 | 50 | |
| 51 | 51 | $taxonomy_options = array(); |
| 52 | 52 | |
| 53 | - foreach ( $taxonomies as $item ) { |
|
| 53 | + foreach ($taxonomies as $item) { |
|
| 54 | 54 | /** |
| 55 | 55 | * $item Taxonomy |
| 56 | 56 | */ |
| 57 | 57 | $taxonomy_options[] = array( |
| 58 | - 'label' => __( $item->label, 'wordlift' ), |
|
| 58 | + 'label' => __($item->label, 'wordlift'), |
|
| 59 | 59 | 'value' => $item->name, |
| 60 | 60 | // The value of parent option on rule field one. |
| 61 | 61 | 'parent_value' => Taxonomy_Option::PARENT_VALUE |
| 62 | 62 | ); |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - return array_merge( $rule_field_two_options, $taxonomy_options ); |
|
| 65 | + return array_merge($rule_field_two_options, $taxonomy_options); |
|
| 66 | 66 | |
| 67 | 67 | } ); |
| 68 | 68 | |
@@ -15,56 +15,56 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Taxonomy_Term_Rule_Validator implements Rule_Validator { |
| 17 | 17 | |
| 18 | - const TAXONOMY = 'taxonomy'; |
|
| 18 | + const TAXONOMY = 'taxonomy'; |
|
| 19 | 19 | |
| 20 | - public function __construct() { |
|
| 21 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 22 | - } |
|
| 20 | + public function __construct() { |
|
| 21 | + add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
| 26 | - * |
|
| 27 | - * @param array $value An array with validators. |
|
| 28 | - * |
|
| 29 | - * @return array An array with validators plus ours. |
|
| 30 | - */ |
|
| 31 | - public function wl_mappings_rule_validators( $value ) { |
|
| 32 | - $value[ self::TAXONOMY ] = $this; |
|
| 24 | + /** |
|
| 25 | + * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
| 26 | + * |
|
| 27 | + * @param array $value An array with validators. |
|
| 28 | + * |
|
| 29 | + * @return array An array with validators plus ours. |
|
| 30 | + */ |
|
| 31 | + public function wl_mappings_rule_validators( $value ) { |
|
| 32 | + $value[ self::TAXONOMY ] = $this; |
|
| 33 | 33 | |
| 34 | - return $value; |
|
| 35 | - } |
|
| 34 | + return $value; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | |
| 38 | - public function get_label() { |
|
| 39 | - return __( 'TaxonomyTerm', 'wordlift' ); |
|
| 40 | - } |
|
| 38 | + public function get_label() { |
|
| 39 | + return __( 'TaxonomyTerm', 'wordlift' ); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function is_valid( $post_id, $operator, $operand_1, $taxonomy ) { |
|
| 43 | - /* |
|
| 42 | + public function is_valid( $post_id, $operator, $operand_1, $taxonomy ) { |
|
| 43 | + /* |
|
| 44 | 44 | * post_id should not be used since we validate this for term pages. |
| 45 | 45 | */ |
| 46 | - $current_term = get_queried_object(); |
|
| 47 | - // If it is not a term page, then return false for two operators. |
|
| 48 | - if ( ! $current_term instanceof \WP_Term ) { |
|
| 49 | - return false; |
|
| 50 | - } |
|
| 51 | - $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
| 52 | - $terms = array_map( function($term) { |
|
| 53 | - /** |
|
| 54 | - *@var $term \WP_Term |
|
| 55 | - */ |
|
| 56 | - return $term->term_id; |
|
| 57 | - }, $terms); |
|
| 58 | - if ( $operator === Rule_Validator::IS_EQUAL_TO ) { |
|
| 59 | - // if we dont have term id, then skip the flow. |
|
| 60 | - // If we are in term page, then we need to check if the current |
|
| 61 | - // term belongs to the taxonomy |
|
| 62 | - return in_array( $current_term->term_id, $terms ); |
|
| 63 | - } |
|
| 46 | + $current_term = get_queried_object(); |
|
| 47 | + // If it is not a term page, then return false for two operators. |
|
| 48 | + if ( ! $current_term instanceof \WP_Term ) { |
|
| 49 | + return false; |
|
| 50 | + } |
|
| 51 | + $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
| 52 | + $terms = array_map( function($term) { |
|
| 53 | + /** |
|
| 54 | + *@var $term \WP_Term |
|
| 55 | + */ |
|
| 56 | + return $term->term_id; |
|
| 57 | + }, $terms); |
|
| 58 | + if ( $operator === Rule_Validator::IS_EQUAL_TO ) { |
|
| 59 | + // if we dont have term id, then skip the flow. |
|
| 60 | + // If we are in term page, then we need to check if the current |
|
| 61 | + // term belongs to the taxonomy |
|
| 62 | + return in_array( $current_term->term_id, $terms ); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - if ( $operator === Rule_Validator::IS_NOT_EQUAL_TO ) { |
|
| 66 | - return ! in_array( $current_term->term_id, $terms ); |
|
| 67 | - } |
|
| 65 | + if ( $operator === Rule_Validator::IS_NOT_EQUAL_TO ) { |
|
| 66 | + return ! in_array( $current_term->term_id, $terms ); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - } |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | const TAXONOMY = 'taxonomy'; |
| 19 | 19 | |
| 20 | 20 | public function __construct() { |
| 21 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 21 | + add_filter('wl_mappings_rule_validators', array($this, 'wl_mappings_rule_validators')); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
@@ -28,42 +28,42 @@ discard block |
||
| 28 | 28 | * |
| 29 | 29 | * @return array An array with validators plus ours. |
| 30 | 30 | */ |
| 31 | - public function wl_mappings_rule_validators( $value ) { |
|
| 32 | - $value[ self::TAXONOMY ] = $this; |
|
| 31 | + public function wl_mappings_rule_validators($value) { |
|
| 32 | + $value[self::TAXONOMY] = $this; |
|
| 33 | 33 | |
| 34 | 34 | return $value; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | |
| 38 | 38 | public function get_label() { |
| 39 | - return __( 'TaxonomyTerm', 'wordlift' ); |
|
| 39 | + return __('TaxonomyTerm', 'wordlift'); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - public function is_valid( $post_id, $operator, $operand_1, $taxonomy ) { |
|
| 42 | + public function is_valid($post_id, $operator, $operand_1, $taxonomy) { |
|
| 43 | 43 | /* |
| 44 | 44 | * post_id should not be used since we validate this for term pages. |
| 45 | 45 | */ |
| 46 | 46 | $current_term = get_queried_object(); |
| 47 | 47 | // If it is not a term page, then return false for two operators. |
| 48 | - if ( ! $current_term instanceof \WP_Term ) { |
|
| 48 | + if ( ! $current_term instanceof \WP_Term) { |
|
| 49 | 49 | return false; |
| 50 | 50 | } |
| 51 | - $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
| 52 | - $terms = array_map( function($term) { |
|
| 51 | + $terms = get_terms($taxonomy, array('get' => 'all')); |
|
| 52 | + $terms = array_map(function($term) { |
|
| 53 | 53 | /** |
| 54 | 54 | *@var $term \WP_Term |
| 55 | 55 | */ |
| 56 | 56 | return $term->term_id; |
| 57 | 57 | }, $terms); |
| 58 | - if ( $operator === Rule_Validator::IS_EQUAL_TO ) { |
|
| 58 | + if ($operator === Rule_Validator::IS_EQUAL_TO) { |
|
| 59 | 59 | // if we dont have term id, then skip the flow. |
| 60 | 60 | // If we are in term page, then we need to check if the current |
| 61 | 61 | // term belongs to the taxonomy |
| 62 | - return in_array( $current_term->term_id, $terms ); |
|
| 62 | + return in_array($current_term->term_id, $terms); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - if ( $operator === Rule_Validator::IS_NOT_EQUAL_TO ) { |
|
| 66 | - return ! in_array( $current_term->term_id, $terms ); |
|
| 65 | + if ($operator === Rule_Validator::IS_NOT_EQUAL_TO) { |
|
| 66 | + return ! in_array($current_term->term_id, $terms); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | } |
@@ -19,54 +19,54 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Rule_Groups_Validator { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The {@link Rule_Validators_Registry} instance. |
|
| 24 | - * |
|
| 25 | - * @var Rule_Validators_Registry $rule_validators_registry The {@link Rule_Validators_Registry} instance. |
|
| 26 | - */ |
|
| 27 | - private $rule_validators_registry; |
|
| 22 | + /** |
|
| 23 | + * The {@link Rule_Validators_Registry} instance. |
|
| 24 | + * |
|
| 25 | + * @var Rule_Validators_Registry $rule_validators_registry The {@link Rule_Validators_Registry} instance. |
|
| 26 | + */ |
|
| 27 | + private $rule_validators_registry; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Rule_Groups_Validator constructor. |
|
| 31 | - * |
|
| 32 | - * @param Rule_Validators_Registry $rule_validators_registry |
|
| 33 | - */ |
|
| 34 | - public function __construct( $rule_validators_registry ) { |
|
| 29 | + /** |
|
| 30 | + * Rule_Groups_Validator constructor. |
|
| 31 | + * |
|
| 32 | + * @param Rule_Validators_Registry $rule_validators_registry |
|
| 33 | + */ |
|
| 34 | + public function __construct( $rule_validators_registry ) { |
|
| 35 | 35 | |
| 36 | - $this->rule_validators_registry = $rule_validators_registry; |
|
| 36 | + $this->rule_validators_registry = $rule_validators_registry; |
|
| 37 | 37 | |
| 38 | - } |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Check whether the specified post passes at least one group of rules. |
|
| 42 | - * |
|
| 43 | - * @param int $post_id The post id. |
|
| 44 | - * @param array $rule_groups An array of rules' groups. |
|
| 45 | - * |
|
| 46 | - * @return bool Whether the post passes at least one rule group. |
|
| 47 | - */ |
|
| 48 | - public function is_valid( $post_id, $rule_groups ) { |
|
| 40 | + /** |
|
| 41 | + * Check whether the specified post passes at least one group of rules. |
|
| 42 | + * |
|
| 43 | + * @param int $post_id The post id. |
|
| 44 | + * @param array $rule_groups An array of rules' groups. |
|
| 45 | + * |
|
| 46 | + * @return bool Whether the post passes at least one rule group. |
|
| 47 | + */ |
|
| 48 | + public function is_valid( $post_id, $rule_groups ) { |
|
| 49 | 49 | |
| 50 | - // Validate each group. Return true as soon as one group is validated (all rules). |
|
| 51 | - foreach ( (array) $rule_groups as $rule_group ) { |
|
| 52 | - foreach ( $rule_group['rules'] as $rule ) { |
|
| 53 | - $rule_field_one = $rule['rule_field_one']; |
|
| 54 | - $rule_logic_field = $rule['rule_logic_field']; |
|
| 55 | - $rule_field_two = $rule['rule_field_two']; |
|
| 50 | + // Validate each group. Return true as soon as one group is validated (all rules). |
|
| 51 | + foreach ( (array) $rule_groups as $rule_group ) { |
|
| 52 | + foreach ( $rule_group['rules'] as $rule ) { |
|
| 53 | + $rule_field_one = $rule['rule_field_one']; |
|
| 54 | + $rule_logic_field = $rule['rule_logic_field']; |
|
| 55 | + $rule_field_two = $rule['rule_field_two']; |
|
| 56 | 56 | |
| 57 | - $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
| 58 | - // Skip to the next Rule Group if a rule isn't valid. |
|
| 59 | - if ( ! $rule_validator->is_valid( $post_id, $rule_logic_field, $rule_field_one, $rule_field_two ) ) { |
|
| 60 | - continue 2; |
|
| 61 | - } |
|
| 57 | + $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
| 58 | + // Skip to the next Rule Group if a rule isn't valid. |
|
| 59 | + if ( ! $rule_validator->is_valid( $post_id, $rule_logic_field, $rule_field_one, $rule_field_two ) ) { |
|
| 60 | + continue 2; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - // If we got here it means that all the rules have been validated (or the rules' group has no rules). |
|
| 66 | - return true; |
|
| 67 | - } |
|
| 65 | + // If we got here it means that all the rules have been validated (or the rules' group has no rules). |
|
| 66 | + return true; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - return false; |
|
| 70 | - } |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | 72 | } |
@@ -18,193 +18,193 @@ |
||
| 18 | 18 | * @since 3.25.0 |
| 19 | 19 | */ |
| 20 | 20 | class Jsonld_Converter { |
| 21 | - /** |
|
| 22 | - * Enumerations for the field types. |
|
| 23 | - * Enumerations for the field types. |
|
| 24 | - */ |
|
| 25 | - const FIELD_TYPE_TEXT_FIELD = 'text'; |
|
| 26 | - const FIELD_TYPE_CUSTOM_FIELD = 'custom_field'; |
|
| 27 | - const FIELD_TYPE_ACF = 'acf'; |
|
| 28 | - /** |
|
| 29 | - * The {@link Mappings_Validator} instance to test. |
|
| 30 | - * |
|
| 31 | - * @since 3.25.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var Mappings_Validator $validator The {@link Mappings_Validator} instance. |
|
| 34 | - */ |
|
| 35 | - private $validator; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.25.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var Mappings_Transform_Functions_Registry $transform_functions_registry The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 43 | - */ |
|
| 44 | - private $transform_functions_registry; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Initialize all dependencies required. |
|
| 48 | - * |
|
| 49 | - * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
|
| 50 | - * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
|
| 51 | - */ |
|
| 52 | - public function __construct( $validator, $transform_functions_registry ) { |
|
| 53 | - |
|
| 54 | - $this->validator = $validator; |
|
| 55 | - $this->transform_functions_registry = $transform_functions_registry; |
|
| 56 | - |
|
| 57 | - // Hook to refactor the JSON-LD. |
|
| 58 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 59 | - add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 60 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Hook to `wl_post_jsonld_array` and `wl_entity_jsonld_array`. |
|
| 65 | - * |
|
| 66 | - * Receive the JSON-LD and the references in the array along with the post ID and transform them according to |
|
| 67 | - * the configuration. |
|
| 68 | - * |
|
| 69 | - * @param array $value { |
|
| 70 | - * The array containing the JSON-LD and the references. |
|
| 71 | - * |
|
| 72 | - * @type array $jsonld The JSON-LD array. |
|
| 73 | - * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter). |
|
| 74 | - * } |
|
| 75 | - * |
|
| 76 | - * @param int $post_id The post ID. |
|
| 77 | - * |
|
| 78 | - * @return array An array with the updated JSON-LD and references. |
|
| 79 | - */ |
|
| 80 | - public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 81 | - |
|
| 82 | - $jsonld = $value['jsonld']; |
|
| 83 | - $references = $value['references']; |
|
| 84 | - |
|
| 85 | - return array( |
|
| 86 | - 'jsonld' => $this->wl_post_jsonld( $jsonld, $post_id, $references ), |
|
| 87 | - 'references' => $references, |
|
| 88 | - ); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Returns JSON-LD data after applying transformation functions. |
|
| 93 | - * |
|
| 94 | - * @param array $jsonld The JSON-LD structure. |
|
| 95 | - * @param int $post_id The {@link WP_Post} id. |
|
| 96 | - * @param array $references An array of post references. |
|
| 97 | - * |
|
| 98 | - * @return array the new refactored array structure. |
|
| 99 | - * @since 3.25.0 |
|
| 100 | - */ |
|
| 101 | - private function wl_post_jsonld( $jsonld, $post_id, &$references ) { |
|
| 102 | - |
|
| 103 | - // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
|
| 104 | - // instance may be reused afterwards. |
|
| 105 | - |
|
| 106 | - $properties = $this->validator->validate( $post_id ); |
|
| 107 | - $nested_properties = array(); |
|
| 108 | - |
|
| 109 | - foreach ( $properties as $property ) { |
|
| 110 | - // If the property has the character '/' in the property name then it is a nested property. |
|
| 111 | - if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 112 | - $nested_properties[] = $property; |
|
| 113 | - continue; |
|
| 114 | - } |
|
| 115 | - $property_transformed_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 116 | - if ( false !== $property_transformed_data ) { |
|
| 117 | - $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $post_id, $references ); |
|
| 122 | - |
|
| 123 | - return $jsonld; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Get the property data by applying the transformation function |
|
| 128 | - * |
|
| 129 | - * @param $property |
|
| 130 | - * @param $jsonld |
|
| 131 | - * @param $post_id |
|
| 132 | - * @param $references |
|
| 133 | - * |
|
| 134 | - * @return array|bool|null |
|
| 135 | - */ |
|
| 136 | - public function get_property_data( $property, $jsonld, $post_id, &$references ) { |
|
| 137 | - $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 138 | - $data = Data_Source_Factory::get_instance()->get_data( $post_id, $property ); |
|
| 139 | - if ( null !== $transform_instance ) { |
|
| 140 | - $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 141 | - if ( null !== $transform_data ) { |
|
| 142 | - return $this->make_single( $transform_data ); |
|
| 143 | - } |
|
| 144 | - } else { |
|
| 145 | - return $this->make_single( $data ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - return false; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Process all the nested properties. |
|
| 153 | - * |
|
| 154 | - * @param $nested_properties array |
|
| 155 | - * @param $jsonld array |
|
| 156 | - * |
|
| 157 | - * @return array |
|
| 158 | - */ |
|
| 159 | - public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references ) { |
|
| 160 | - foreach ( $nested_properties as $property ) { |
|
| 161 | - $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 162 | - if ( false === $property_data ) { |
|
| 163 | - // No need to create nested levels. |
|
| 164 | - continue; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $keys = explode( '/', $property['property_name'] ); |
|
| 168 | - // end is the last level of the nested property. |
|
| 169 | - $end = array_pop( $keys ); |
|
| 170 | - $current_property_pointer = &$jsonld; |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Once we find all the nested levels from the property name |
|
| 174 | - * loop through it and create associative array if the levels |
|
| 175 | - * didnt exist. |
|
| 176 | - */ |
|
| 177 | - while ( count( $keys ) > 0 ) { |
|
| 178 | - $key = array_shift( $keys ); |
|
| 179 | - if ( $key === "" ) { |
|
| 180 | - continue; |
|
| 181 | - } |
|
| 182 | - if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 183 | - $current_property_pointer[ $key ] = array(); |
|
| 184 | - } |
|
| 185 | - // We are setting the pointer to the current key, so that at the end |
|
| 186 | - // we can add the data at last level. |
|
| 187 | - $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 188 | - } |
|
| 189 | - $current_property_pointer[ $end ] = $property_data; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - return $jsonld; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - private function make_single( $value ) { |
|
| 196 | - |
|
| 197 | - $values = (array) $value; |
|
| 198 | - |
|
| 199 | - if ( empty( $values ) ) { |
|
| 200 | - return false; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 204 | - return current( $values ); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $values; |
|
| 208 | - } |
|
| 21 | + /** |
|
| 22 | + * Enumerations for the field types. |
|
| 23 | + * Enumerations for the field types. |
|
| 24 | + */ |
|
| 25 | + const FIELD_TYPE_TEXT_FIELD = 'text'; |
|
| 26 | + const FIELD_TYPE_CUSTOM_FIELD = 'custom_field'; |
|
| 27 | + const FIELD_TYPE_ACF = 'acf'; |
|
| 28 | + /** |
|
| 29 | + * The {@link Mappings_Validator} instance to test. |
|
| 30 | + * |
|
| 31 | + * @since 3.25.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var Mappings_Validator $validator The {@link Mappings_Validator} instance. |
|
| 34 | + */ |
|
| 35 | + private $validator; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.25.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var Mappings_Transform_Functions_Registry $transform_functions_registry The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 43 | + */ |
|
| 44 | + private $transform_functions_registry; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Initialize all dependencies required. |
|
| 48 | + * |
|
| 49 | + * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
|
| 50 | + * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
|
| 51 | + */ |
|
| 52 | + public function __construct( $validator, $transform_functions_registry ) { |
|
| 53 | + |
|
| 54 | + $this->validator = $validator; |
|
| 55 | + $this->transform_functions_registry = $transform_functions_registry; |
|
| 56 | + |
|
| 57 | + // Hook to refactor the JSON-LD. |
|
| 58 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 59 | + add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 60 | + add_filter( 'wl_term_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Hook to `wl_post_jsonld_array` and `wl_entity_jsonld_array`. |
|
| 65 | + * |
|
| 66 | + * Receive the JSON-LD and the references in the array along with the post ID and transform them according to |
|
| 67 | + * the configuration. |
|
| 68 | + * |
|
| 69 | + * @param array $value { |
|
| 70 | + * The array containing the JSON-LD and the references. |
|
| 71 | + * |
|
| 72 | + * @type array $jsonld The JSON-LD array. |
|
| 73 | + * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter). |
|
| 74 | + * } |
|
| 75 | + * |
|
| 76 | + * @param int $post_id The post ID. |
|
| 77 | + * |
|
| 78 | + * @return array An array with the updated JSON-LD and references. |
|
| 79 | + */ |
|
| 80 | + public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 81 | + |
|
| 82 | + $jsonld = $value['jsonld']; |
|
| 83 | + $references = $value['references']; |
|
| 84 | + |
|
| 85 | + return array( |
|
| 86 | + 'jsonld' => $this->wl_post_jsonld( $jsonld, $post_id, $references ), |
|
| 87 | + 'references' => $references, |
|
| 88 | + ); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Returns JSON-LD data after applying transformation functions. |
|
| 93 | + * |
|
| 94 | + * @param array $jsonld The JSON-LD structure. |
|
| 95 | + * @param int $post_id The {@link WP_Post} id. |
|
| 96 | + * @param array $references An array of post references. |
|
| 97 | + * |
|
| 98 | + * @return array the new refactored array structure. |
|
| 99 | + * @since 3.25.0 |
|
| 100 | + */ |
|
| 101 | + private function wl_post_jsonld( $jsonld, $post_id, &$references ) { |
|
| 102 | + |
|
| 103 | + // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
|
| 104 | + // instance may be reused afterwards. |
|
| 105 | + |
|
| 106 | + $properties = $this->validator->validate( $post_id ); |
|
| 107 | + $nested_properties = array(); |
|
| 108 | + |
|
| 109 | + foreach ( $properties as $property ) { |
|
| 110 | + // If the property has the character '/' in the property name then it is a nested property. |
|
| 111 | + if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 112 | + $nested_properties[] = $property; |
|
| 113 | + continue; |
|
| 114 | + } |
|
| 115 | + $property_transformed_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 116 | + if ( false !== $property_transformed_data ) { |
|
| 117 | + $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $post_id, $references ); |
|
| 122 | + |
|
| 123 | + return $jsonld; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Get the property data by applying the transformation function |
|
| 128 | + * |
|
| 129 | + * @param $property |
|
| 130 | + * @param $jsonld |
|
| 131 | + * @param $post_id |
|
| 132 | + * @param $references |
|
| 133 | + * |
|
| 134 | + * @return array|bool|null |
|
| 135 | + */ |
|
| 136 | + public function get_property_data( $property, $jsonld, $post_id, &$references ) { |
|
| 137 | + $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 138 | + $data = Data_Source_Factory::get_instance()->get_data( $post_id, $property ); |
|
| 139 | + if ( null !== $transform_instance ) { |
|
| 140 | + $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 141 | + if ( null !== $transform_data ) { |
|
| 142 | + return $this->make_single( $transform_data ); |
|
| 143 | + } |
|
| 144 | + } else { |
|
| 145 | + return $this->make_single( $data ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + return false; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Process all the nested properties. |
|
| 153 | + * |
|
| 154 | + * @param $nested_properties array |
|
| 155 | + * @param $jsonld array |
|
| 156 | + * |
|
| 157 | + * @return array |
|
| 158 | + */ |
|
| 159 | + public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references ) { |
|
| 160 | + foreach ( $nested_properties as $property ) { |
|
| 161 | + $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 162 | + if ( false === $property_data ) { |
|
| 163 | + // No need to create nested levels. |
|
| 164 | + continue; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $keys = explode( '/', $property['property_name'] ); |
|
| 168 | + // end is the last level of the nested property. |
|
| 169 | + $end = array_pop( $keys ); |
|
| 170 | + $current_property_pointer = &$jsonld; |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Once we find all the nested levels from the property name |
|
| 174 | + * loop through it and create associative array if the levels |
|
| 175 | + * didnt exist. |
|
| 176 | + */ |
|
| 177 | + while ( count( $keys ) > 0 ) { |
|
| 178 | + $key = array_shift( $keys ); |
|
| 179 | + if ( $key === "" ) { |
|
| 180 | + continue; |
|
| 181 | + } |
|
| 182 | + if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 183 | + $current_property_pointer[ $key ] = array(); |
|
| 184 | + } |
|
| 185 | + // We are setting the pointer to the current key, so that at the end |
|
| 186 | + // we can add the data at last level. |
|
| 187 | + $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 188 | + } |
|
| 189 | + $current_property_pointer[ $end ] = $property_data; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + return $jsonld; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + private function make_single( $value ) { |
|
| 196 | + |
|
| 197 | + $values = (array) $value; |
|
| 198 | + |
|
| 199 | + if ( empty( $values ) ) { |
|
| 200 | + return false; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 204 | + return current( $values ); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $values; |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | 210 | } |
@@ -49,15 +49,15 @@ discard block |
||
| 49 | 49 | * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
| 50 | 50 | * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
| 51 | 51 | */ |
| 52 | - public function __construct( $validator, $transform_functions_registry ) { |
|
| 52 | + public function __construct($validator, $transform_functions_registry) { |
|
| 53 | 53 | |
| 54 | 54 | $this->validator = $validator; |
| 55 | 55 | $this->transform_functions_registry = $transform_functions_registry; |
| 56 | 56 | |
| 57 | 57 | // Hook to refactor the JSON-LD. |
| 58 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 59 | - add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 60 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 58 | + add_filter('wl_post_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 2); |
|
| 59 | + add_filter('wl_entity_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 3); |
|
| 60 | + add_filter('wl_term_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 3); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
@@ -77,13 +77,13 @@ discard block |
||
| 77 | 77 | * |
| 78 | 78 | * @return array An array with the updated JSON-LD and references. |
| 79 | 79 | */ |
| 80 | - public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 80 | + public function wl_post_jsonld_array($value, $post_id) { |
|
| 81 | 81 | |
| 82 | 82 | $jsonld = $value['jsonld']; |
| 83 | 83 | $references = $value['references']; |
| 84 | 84 | |
| 85 | 85 | return array( |
| 86 | - 'jsonld' => $this->wl_post_jsonld( $jsonld, $post_id, $references ), |
|
| 86 | + 'jsonld' => $this->wl_post_jsonld($jsonld, $post_id, $references), |
|
| 87 | 87 | 'references' => $references, |
| 88 | 88 | ); |
| 89 | 89 | } |
@@ -98,27 +98,27 @@ discard block |
||
| 98 | 98 | * @return array the new refactored array structure. |
| 99 | 99 | * @since 3.25.0 |
| 100 | 100 | */ |
| 101 | - private function wl_post_jsonld( $jsonld, $post_id, &$references ) { |
|
| 101 | + private function wl_post_jsonld($jsonld, $post_id, &$references) { |
|
| 102 | 102 | |
| 103 | 103 | // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
| 104 | 104 | // instance may be reused afterwards. |
| 105 | 105 | |
| 106 | - $properties = $this->validator->validate( $post_id ); |
|
| 106 | + $properties = $this->validator->validate($post_id); |
|
| 107 | 107 | $nested_properties = array(); |
| 108 | 108 | |
| 109 | - foreach ( $properties as $property ) { |
|
| 109 | + foreach ($properties as $property) { |
|
| 110 | 110 | // If the property has the character '/' in the property name then it is a nested property. |
| 111 | - if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 111 | + if (strpos($property['property_name'], '/') !== false) { |
|
| 112 | 112 | $nested_properties[] = $property; |
| 113 | 113 | continue; |
| 114 | 114 | } |
| 115 | - $property_transformed_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 116 | - if ( false !== $property_transformed_data ) { |
|
| 117 | - $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 115 | + $property_transformed_data = $this->get_property_data($property, $jsonld, $post_id, $references); |
|
| 116 | + if (false !== $property_transformed_data) { |
|
| 117 | + $jsonld[$property['property_name']] = $property_transformed_data; |
|
| 118 | 118 | } |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $post_id, $references ); |
|
| 121 | + $jsonld = $this->process_nested_properties($nested_properties, $jsonld, $post_id, $references); |
|
| 122 | 122 | |
| 123 | 123 | return $jsonld; |
| 124 | 124 | } |
@@ -133,16 +133,16 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * @return array|bool|null |
| 135 | 135 | */ |
| 136 | - public function get_property_data( $property, $jsonld, $post_id, &$references ) { |
|
| 137 | - $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 138 | - $data = Data_Source_Factory::get_instance()->get_data( $post_id, $property ); |
|
| 139 | - if ( null !== $transform_instance ) { |
|
| 140 | - $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 141 | - if ( null !== $transform_data ) { |
|
| 142 | - return $this->make_single( $transform_data ); |
|
| 136 | + public function get_property_data($property, $jsonld, $post_id, &$references) { |
|
| 137 | + $transform_instance = $this->transform_functions_registry->get_transform_function($property['transform_function']); |
|
| 138 | + $data = Data_Source_Factory::get_instance()->get_data($post_id, $property); |
|
| 139 | + if (null !== $transform_instance) { |
|
| 140 | + $transform_data = $transform_instance->transform_data($data, $jsonld, $references, $post_id); |
|
| 141 | + if (null !== $transform_data) { |
|
| 142 | + return $this->make_single($transform_data); |
|
| 143 | 143 | } |
| 144 | 144 | } else { |
| 145 | - return $this->make_single( $data ); |
|
| 145 | + return $this->make_single($data); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | return false; |
@@ -156,17 +156,17 @@ discard block |
||
| 156 | 156 | * |
| 157 | 157 | * @return array |
| 158 | 158 | */ |
| 159 | - public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references ) { |
|
| 160 | - foreach ( $nested_properties as $property ) { |
|
| 161 | - $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 162 | - if ( false === $property_data ) { |
|
| 159 | + public function process_nested_properties($nested_properties, $jsonld, $post_id, &$references) { |
|
| 160 | + foreach ($nested_properties as $property) { |
|
| 161 | + $property_data = $this->get_property_data($property, $jsonld, $post_id, $references); |
|
| 162 | + if (false === $property_data) { |
|
| 163 | 163 | // No need to create nested levels. |
| 164 | 164 | continue; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - $keys = explode( '/', $property['property_name'] ); |
|
| 167 | + $keys = explode('/', $property['property_name']); |
|
| 168 | 168 | // end is the last level of the nested property. |
| 169 | - $end = array_pop( $keys ); |
|
| 169 | + $end = array_pop($keys); |
|
| 170 | 170 | $current_property_pointer = &$jsonld; |
| 171 | 171 | |
| 172 | 172 | /** |
@@ -174,34 +174,34 @@ discard block |
||
| 174 | 174 | * loop through it and create associative array if the levels |
| 175 | 175 | * didnt exist. |
| 176 | 176 | */ |
| 177 | - while ( count( $keys ) > 0 ) { |
|
| 178 | - $key = array_shift( $keys ); |
|
| 179 | - if ( $key === "" ) { |
|
| 177 | + while (count($keys) > 0) { |
|
| 178 | + $key = array_shift($keys); |
|
| 179 | + if ($key === "") { |
|
| 180 | 180 | continue; |
| 181 | 181 | } |
| 182 | - if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 183 | - $current_property_pointer[ $key ] = array(); |
|
| 182 | + if ( ! array_key_exists($key, $current_property_pointer)) { |
|
| 183 | + $current_property_pointer[$key] = array(); |
|
| 184 | 184 | } |
| 185 | 185 | // We are setting the pointer to the current key, so that at the end |
| 186 | 186 | // we can add the data at last level. |
| 187 | - $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 187 | + $current_property_pointer = &$current_property_pointer[$key]; |
|
| 188 | 188 | } |
| 189 | - $current_property_pointer[ $end ] = $property_data; |
|
| 189 | + $current_property_pointer[$end] = $property_data; |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | return $jsonld; |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | - private function make_single( $value ) { |
|
| 195 | + private function make_single($value) { |
|
| 196 | 196 | |
| 197 | 197 | $values = (array) $value; |
| 198 | 198 | |
| 199 | - if ( empty( $values ) ) { |
|
| 199 | + if (empty($values)) { |
|
| 200 | 200 | return false; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | - if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 204 | - return current( $values ); |
|
| 203 | + if (1 === count($values) && 0 === key($values)) { |
|
| 204 | + return current($values); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | return $values; |