@@ -15,84 +15,84 @@ |
||
15 | 15 | |
16 | 16 | class Assertions { |
17 | 17 | |
18 | - /** |
|
19 | - * Asserts that the provided value is of the specified type. |
|
20 | - * |
|
21 | - * @param mixed $value The value to test. |
|
22 | - * @param string $type The expected type. |
|
23 | - * |
|
24 | - * @throws Exception when an error occurs. |
|
25 | - */ |
|
26 | - public static function assert_of_type( $value, $type ) { |
|
27 | - |
|
28 | - // Check for nulls. |
|
29 | - if ( null === $value ) { |
|
30 | - throw new Exception( 'Value can`t be null.' ); |
|
31 | - } |
|
32 | - |
|
33 | - // Check for type. |
|
34 | - if ( get_class( $value ) !== $type ) { |
|
35 | - throw new Exception( "Value must be a $type." ); |
|
36 | - } |
|
37 | - |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @throws Exception when the argument is not a string. |
|
42 | - */ |
|
43 | - public static function is_string( $arg, $message = 'Value must be a string' ) { |
|
44 | - if ( ! is_string( $arg ) ) { |
|
45 | - throw new Exception( $message ); |
|
46 | - } |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @throws Exception when actual doesn't match expected. |
|
51 | - */ |
|
52 | - public static function equals( $actual, $expected, $message = 'Values do not match' ) { |
|
53 | - if ( $actual !== $expected ) { |
|
54 | - throw new Exception( $message ); |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @throws Exception when the value doesn't match the pattern. |
|
60 | - */ |
|
61 | - public static function matches( $value, $pattern, $message = "Value doesn't match" ) { |
|
62 | - if ( 1 !== preg_match( $pattern, $value ) ) { |
|
63 | - throw new Exception( $message ); |
|
64 | - } |
|
65 | - |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @throws Exception when the value doesn't start with the provided scope. |
|
70 | - */ |
|
71 | - public static function starts_with( $value, $scope, $message = "Value doesn't start with provided scope" ) { |
|
72 | - if ( 0 !== strpos( $value, $scope ) ) { |
|
73 | - throw new Exception( $message ); |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @throws Exception when the value is not of the specified type. |
|
79 | - */ |
|
80 | - public static function is_a( $value, $type, $message = 'Value is not of the required type' ) { |
|
81 | - if ( ! is_a( $value, $type ) ) { |
|
82 | - throw new Exception( $message ); |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - public static function is_set( $value, $message = 'Value is not set' ) { |
|
87 | - if ( ! isset( $value ) ) { |
|
88 | - throw new Exception( $message ); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - public static function not_empty( $value, $message = "Value can't be empty" ) { |
|
93 | - if ( empty( $value ) ) { |
|
94 | - throw new Exception( $message ); |
|
95 | - } |
|
96 | - } |
|
18 | + /** |
|
19 | + * Asserts that the provided value is of the specified type. |
|
20 | + * |
|
21 | + * @param mixed $value The value to test. |
|
22 | + * @param string $type The expected type. |
|
23 | + * |
|
24 | + * @throws Exception when an error occurs. |
|
25 | + */ |
|
26 | + public static function assert_of_type( $value, $type ) { |
|
27 | + |
|
28 | + // Check for nulls. |
|
29 | + if ( null === $value ) { |
|
30 | + throw new Exception( 'Value can`t be null.' ); |
|
31 | + } |
|
32 | + |
|
33 | + // Check for type. |
|
34 | + if ( get_class( $value ) !== $type ) { |
|
35 | + throw new Exception( "Value must be a $type." ); |
|
36 | + } |
|
37 | + |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @throws Exception when the argument is not a string. |
|
42 | + */ |
|
43 | + public static function is_string( $arg, $message = 'Value must be a string' ) { |
|
44 | + if ( ! is_string( $arg ) ) { |
|
45 | + throw new Exception( $message ); |
|
46 | + } |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @throws Exception when actual doesn't match expected. |
|
51 | + */ |
|
52 | + public static function equals( $actual, $expected, $message = 'Values do not match' ) { |
|
53 | + if ( $actual !== $expected ) { |
|
54 | + throw new Exception( $message ); |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @throws Exception when the value doesn't match the pattern. |
|
60 | + */ |
|
61 | + public static function matches( $value, $pattern, $message = "Value doesn't match" ) { |
|
62 | + if ( 1 !== preg_match( $pattern, $value ) ) { |
|
63 | + throw new Exception( $message ); |
|
64 | + } |
|
65 | + |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @throws Exception when the value doesn't start with the provided scope. |
|
70 | + */ |
|
71 | + public static function starts_with( $value, $scope, $message = "Value doesn't start with provided scope" ) { |
|
72 | + if ( 0 !== strpos( $value, $scope ) ) { |
|
73 | + throw new Exception( $message ); |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @throws Exception when the value is not of the specified type. |
|
79 | + */ |
|
80 | + public static function is_a( $value, $type, $message = 'Value is not of the required type' ) { |
|
81 | + if ( ! is_a( $value, $type ) ) { |
|
82 | + throw new Exception( $message ); |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + public static function is_set( $value, $message = 'Value is not set' ) { |
|
87 | + if ( ! isset( $value ) ) { |
|
88 | + throw new Exception( $message ); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + public static function not_empty( $value, $message = "Value can't be empty" ) { |
|
93 | + if ( empty( $value ) ) { |
|
94 | + throw new Exception( $message ); |
|
95 | + } |
|
96 | + } |
|
97 | 97 | |
98 | 98 | } |
@@ -23,16 +23,16 @@ discard block |
||
23 | 23 | * |
24 | 24 | * @throws Exception when an error occurs. |
25 | 25 | */ |
26 | - public static function assert_of_type( $value, $type ) { |
|
26 | + public static function assert_of_type($value, $type) { |
|
27 | 27 | |
28 | 28 | // Check for nulls. |
29 | - if ( null === $value ) { |
|
30 | - throw new Exception( 'Value can`t be null.' ); |
|
29 | + if (null === $value) { |
|
30 | + throw new Exception('Value can`t be null.'); |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | // Check for type. |
34 | - if ( get_class( $value ) !== $type ) { |
|
35 | - throw new Exception( "Value must be a $type." ); |
|
34 | + if (get_class($value) !== $type) { |
|
35 | + throw new Exception("Value must be a $type."); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | } |
@@ -40,27 +40,27 @@ discard block |
||
40 | 40 | /** |
41 | 41 | * @throws Exception when the argument is not a string. |
42 | 42 | */ |
43 | - public static function is_string( $arg, $message = 'Value must be a string' ) { |
|
44 | - if ( ! is_string( $arg ) ) { |
|
45 | - throw new Exception( $message ); |
|
43 | + public static function is_string($arg, $message = 'Value must be a string') { |
|
44 | + if ( ! is_string($arg)) { |
|
45 | + throw new Exception($message); |
|
46 | 46 | } |
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
50 | 50 | * @throws Exception when actual doesn't match expected. |
51 | 51 | */ |
52 | - public static function equals( $actual, $expected, $message = 'Values do not match' ) { |
|
53 | - if ( $actual !== $expected ) { |
|
54 | - throw new Exception( $message ); |
|
52 | + public static function equals($actual, $expected, $message = 'Values do not match') { |
|
53 | + if ($actual !== $expected) { |
|
54 | + throw new Exception($message); |
|
55 | 55 | } |
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * @throws Exception when the value doesn't match the pattern. |
60 | 60 | */ |
61 | - public static function matches( $value, $pattern, $message = "Value doesn't match" ) { |
|
62 | - if ( 1 !== preg_match( $pattern, $value ) ) { |
|
63 | - throw new Exception( $message ); |
|
61 | + public static function matches($value, $pattern, $message = "Value doesn't match") { |
|
62 | + if (1 !== preg_match($pattern, $value)) { |
|
63 | + throw new Exception($message); |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | } |
@@ -68,30 +68,30 @@ discard block |
||
68 | 68 | /** |
69 | 69 | * @throws Exception when the value doesn't start with the provided scope. |
70 | 70 | */ |
71 | - public static function starts_with( $value, $scope, $message = "Value doesn't start with provided scope" ) { |
|
72 | - if ( 0 !== strpos( $value, $scope ) ) { |
|
73 | - throw new Exception( $message ); |
|
71 | + public static function starts_with($value, $scope, $message = "Value doesn't start with provided scope") { |
|
72 | + if (0 !== strpos($value, $scope)) { |
|
73 | + throw new Exception($message); |
|
74 | 74 | } |
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
78 | 78 | * @throws Exception when the value is not of the specified type. |
79 | 79 | */ |
80 | - public static function is_a( $value, $type, $message = 'Value is not of the required type' ) { |
|
81 | - if ( ! is_a( $value, $type ) ) { |
|
82 | - throw new Exception( $message ); |
|
80 | + public static function is_a($value, $type, $message = 'Value is not of the required type') { |
|
81 | + if ( ! is_a($value, $type)) { |
|
82 | + throw new Exception($message); |
|
83 | 83 | } |
84 | 84 | } |
85 | 85 | |
86 | - public static function is_set( $value, $message = 'Value is not set' ) { |
|
87 | - if ( ! isset( $value ) ) { |
|
88 | - throw new Exception( $message ); |
|
86 | + public static function is_set($value, $message = 'Value is not set') { |
|
87 | + if ( ! isset($value)) { |
|
88 | + throw new Exception($message); |
|
89 | 89 | } |
90 | 90 | } |
91 | 91 | |
92 | - public static function not_empty( $value, $message = "Value can't be empty" ) { |
|
93 | - if ( empty( $value ) ) { |
|
94 | - throw new Exception( $message ); |
|
92 | + public static function not_empty($value, $message = "Value can't be empty") { |
|
93 | + if (empty($value)) { |
|
94 | + throw new Exception($message); |
|
95 | 95 | } |
96 | 96 | } |
97 | 97 |
@@ -22,69 +22,69 @@ |
||
22 | 22 | */ |
23 | 23 | class Admin_Mappings_Page extends Wordlift_Admin_Page { |
24 | 24 | |
25 | - /** |
|
26 | - * Provides script and js global values used by react component. |
|
27 | - */ |
|
28 | - public static function provide_ui_dependencies() { |
|
29 | - // Create ui settings array to be used by js client. |
|
30 | - $mapping_settings = array(); |
|
31 | - $mapping_settings['rest_url'] = get_rest_url( |
|
32 | - null, |
|
33 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
34 | - ); |
|
35 | - $mapping_settings['wl_mapping_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
36 | - $mapping_settings['wl_edit_mapping_nonce'] = wp_create_nonce( 'wl-edit-mapping-nonce' ); |
|
37 | - wp_localize_script( 'wl-mappings-admin', 'wlMappingsConfig', $mapping_settings ); |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * {@inheritdoc} |
|
42 | - */ |
|
43 | - public function get_page_title() { |
|
44 | - |
|
45 | - return __( 'Mappings', 'wordlift' ); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * {@inheritdoc} |
|
50 | - */ |
|
51 | - public function get_menu_title() { |
|
52 | - |
|
53 | - return __( 'Mappings', 'wordlift' ); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * {@inheritdoc} |
|
58 | - */ |
|
59 | - public function get_menu_slug() { |
|
60 | - |
|
61 | - return 'wl_mappings_admin'; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * {@inheritdoc} |
|
66 | - */ |
|
67 | - public function get_partial_name() { |
|
68 | - |
|
69 | - return 'wordlift-admin-mappings-admin.php'; |
|
70 | - } |
|
71 | - |
|
72 | - public function enqueue_scripts() { |
|
73 | - |
|
74 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
75 | - 'wl-mappings-admin', |
|
76 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings', |
|
77 | - array( 'react', 'react-dom', 'wp-polyfill' ), |
|
78 | - true |
|
79 | - ); |
|
80 | - |
|
81 | - wp_enqueue_style( |
|
82 | - 'wl-mappings-admin', |
|
83 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings.css', |
|
84 | - array(), |
|
85 | - WORDLIFT_VERSION |
|
86 | - ); |
|
87 | - self::provide_ui_dependencies(); |
|
88 | - } |
|
25 | + /** |
|
26 | + * Provides script and js global values used by react component. |
|
27 | + */ |
|
28 | + public static function provide_ui_dependencies() { |
|
29 | + // Create ui settings array to be used by js client. |
|
30 | + $mapping_settings = array(); |
|
31 | + $mapping_settings['rest_url'] = get_rest_url( |
|
32 | + null, |
|
33 | + WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
34 | + ); |
|
35 | + $mapping_settings['wl_mapping_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
36 | + $mapping_settings['wl_edit_mapping_nonce'] = wp_create_nonce( 'wl-edit-mapping-nonce' ); |
|
37 | + wp_localize_script( 'wl-mappings-admin', 'wlMappingsConfig', $mapping_settings ); |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * {@inheritdoc} |
|
42 | + */ |
|
43 | + public function get_page_title() { |
|
44 | + |
|
45 | + return __( 'Mappings', 'wordlift' ); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * {@inheritdoc} |
|
50 | + */ |
|
51 | + public function get_menu_title() { |
|
52 | + |
|
53 | + return __( 'Mappings', 'wordlift' ); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * {@inheritdoc} |
|
58 | + */ |
|
59 | + public function get_menu_slug() { |
|
60 | + |
|
61 | + return 'wl_mappings_admin'; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * {@inheritdoc} |
|
66 | + */ |
|
67 | + public function get_partial_name() { |
|
68 | + |
|
69 | + return 'wordlift-admin-mappings-admin.php'; |
|
70 | + } |
|
71 | + |
|
72 | + public function enqueue_scripts() { |
|
73 | + |
|
74 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
75 | + 'wl-mappings-admin', |
|
76 | + plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings', |
|
77 | + array( 'react', 'react-dom', 'wp-polyfill' ), |
|
78 | + true |
|
79 | + ); |
|
80 | + |
|
81 | + wp_enqueue_style( |
|
82 | + 'wl-mappings-admin', |
|
83 | + plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings.css', |
|
84 | + array(), |
|
85 | + WORDLIFT_VERSION |
|
86 | + ); |
|
87 | + self::provide_ui_dependencies(); |
|
88 | + } |
|
89 | 89 | |
90 | 90 | } |
@@ -30,11 +30,11 @@ discard block |
||
30 | 30 | $mapping_settings = array(); |
31 | 31 | $mapping_settings['rest_url'] = get_rest_url( |
32 | 32 | null, |
33 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
33 | + WL_REST_ROUTE_DEFAULT_NAMESPACE.Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
34 | 34 | ); |
35 | - $mapping_settings['wl_mapping_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
36 | - $mapping_settings['wl_edit_mapping_nonce'] = wp_create_nonce( 'wl-edit-mapping-nonce' ); |
|
37 | - wp_localize_script( 'wl-mappings-admin', 'wlMappingsConfig', $mapping_settings ); |
|
35 | + $mapping_settings['wl_mapping_nonce'] = wp_create_nonce('wp_rest'); |
|
36 | + $mapping_settings['wl_edit_mapping_nonce'] = wp_create_nonce('wl-edit-mapping-nonce'); |
|
37 | + wp_localize_script('wl-mappings-admin', 'wlMappingsConfig', $mapping_settings); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public function get_page_title() { |
44 | 44 | |
45 | - return __( 'Mappings', 'wordlift' ); |
|
45 | + return __('Mappings', 'wordlift'); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | /** |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | */ |
51 | 51 | public function get_menu_title() { |
52 | 52 | |
53 | - return __( 'Mappings', 'wordlift' ); |
|
53 | + return __('Mappings', 'wordlift'); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
@@ -73,14 +73,14 @@ discard block |
||
73 | 73 | |
74 | 74 | Scripts_Helper::enqueue_based_on_wordpress_version( |
75 | 75 | 'wl-mappings-admin', |
76 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings', |
|
77 | - array( 'react', 'react-dom', 'wp-polyfill' ), |
|
76 | + plugin_dir_url(dirname(dirname(__DIR__))).'js/dist/mappings', |
|
77 | + array('react', 'react-dom', 'wp-polyfill'), |
|
78 | 78 | true |
79 | 79 | ); |
80 | 80 | |
81 | 81 | wp_enqueue_style( |
82 | 82 | 'wl-mappings-admin', |
83 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings.css', |
|
83 | + plugin_dir_url(dirname(dirname(__DIR__))).'js/dist/mappings.css', |
|
84 | 84 | array(), |
85 | 85 | WORDLIFT_VERSION |
86 | 86 | ); |
@@ -24,397 +24,397 @@ |
||
24 | 24 | */ |
25 | 25 | class Edit_Mappings_Page extends Wordlift_Admin_Page { |
26 | 26 | |
27 | - /** Instance to store the registry class. |
|
28 | - * |
|
29 | - * @var Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance} |
|
30 | - */ |
|
31 | - public $transform_function_registry; |
|
32 | - |
|
33 | - /** |
|
34 | - * Edit_Mappings_Page constructor. |
|
35 | - * |
|
36 | - * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance } |
|
37 | - */ |
|
38 | - public function __construct( $transform_function_registry ) { |
|
39 | - parent::__construct(); |
|
40 | - $this->transform_function_registry = $transform_function_registry; |
|
41 | - } |
|
42 | - |
|
43 | - public function render() { |
|
44 | - // Render all the settings when this method is called, because the partial page is loaded after |
|
45 | - // this method. |
|
46 | - // Load the UI dependencies. |
|
47 | - $edit_mapping_settings = $this->get_ui_settings_array(); |
|
48 | - // Supply the settings to js client. |
|
49 | - wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings ); |
|
50 | - |
|
51 | - parent::render(); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Load the text settings needed for the edit_mappings_page. |
|
56 | - * |
|
57 | - * @param array $edit_mapping_settings Key value pair of settings used by edit mappings page. |
|
58 | - * |
|
59 | - * @return array Adding text settings to the main settings array. |
|
60 | - */ |
|
61 | - private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) { |
|
62 | - $edit_mapping_settings['wl_add_mapping_text'] = __( 'Add Mapping', 'wordlift' ); |
|
63 | - $edit_mapping_settings['wl_edit_mapping_text'] = __( 'Edit Mapping', 'wordlift' ); |
|
64 | - $edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' ); |
|
65 | - $edit_mapping_settings['page'] = 'wl_edit_mapping'; |
|
66 | - |
|
67 | - return $edit_mapping_settings; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * The base class {@link Wordlift_Admin_Page} will add the admin page to the WordLift menu. |
|
72 | - * |
|
73 | - * We don't want this page to be in the menu though. Therefore we override the `parent_slug` so that WordPress won't |
|
74 | - * show it there. |
|
75 | - * |
|
76 | - * @return null return null to avoid this page to be displayed in WordLift's menu. |
|
77 | - */ |
|
78 | - protected function get_parent_slug() { |
|
79 | - return null; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * {@inheritdoc} |
|
84 | - */ |
|
85 | - public function get_page_title() { |
|
86 | - |
|
87 | - return __( 'Edit Mappings', 'wordlift' ); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * {@inheritdoc} |
|
92 | - */ |
|
93 | - public function get_menu_title() { |
|
94 | - |
|
95 | - return __( 'Edit Mappings', 'wordlift' ); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * {@inheritdoc} |
|
100 | - */ |
|
101 | - public function get_menu_slug() { |
|
102 | - |
|
103 | - return 'wl_edit_mapping'; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * {@inheritdoc} |
|
108 | - */ |
|
109 | - public function get_partial_name() { |
|
110 | - return 'wordlift-admin-mappings-edit.php'; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * {@inheritdoc} |
|
115 | - */ |
|
116 | - public function enqueue_scripts() { |
|
117 | - |
|
118 | - // Enqueue the script. |
|
119 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
120 | - 'wl-mappings-edit', |
|
121 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings-edit', |
|
122 | - array( 'react', 'react-dom', 'wp-polyfill' ), |
|
123 | - true |
|
124 | - ); |
|
125 | - |
|
126 | - // Enqueue the style. |
|
127 | - wp_enqueue_style( |
|
128 | - 'wl-mappings-edit', |
|
129 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings-edit.css', |
|
130 | - array(), |
|
131 | - WORDLIFT_VERSION |
|
132 | - ); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Returns field name options based on the chosen field type. |
|
137 | - * if string is returned a text field would be shown to user, if an array of options is returned |
|
138 | - * then the select box would be shown to user. |
|
139 | - * |
|
140 | - * @return array Array of the options. |
|
141 | - */ |
|
142 | - public static function get_all_field_name_options() { |
|
143 | - |
|
144 | - $options = array( |
|
145 | - array( |
|
146 | - 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD, |
|
147 | - 'value' => '', |
|
148 | - 'label' => __( 'Fixed Text', 'wordlift' ), |
|
149 | - ), |
|
150 | - // @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings. |
|
151 | - array( |
|
152 | - 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD, |
|
153 | - 'value' => '', |
|
154 | - 'label' => __( 'Custom Field', 'wordlift' ), |
|
155 | - ), |
|
156 | - ); |
|
157 | - |
|
158 | - /** |
|
159 | - * Allow 3rd parties to add field types. |
|
160 | - * |
|
161 | - * @param array An array of Field Types. |
|
162 | - * |
|
163 | - * @return array An array of Field Types. |
|
164 | - * |
|
165 | - * @since 3.25.0 |
|
166 | - */ |
|
167 | - return apply_filters( 'wl_mappings_field_types', $options ); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @return array An Array containing key value pairs of settings. |
|
172 | - * @since 3.25.0 |
|
173 | - * Load dependencies required for js client. |
|
174 | - */ |
|
175 | - public function get_ui_settings_array() { |
|
176 | - // Create ui settings array to be used by js client. |
|
177 | - $edit_mapping_settings = array(); |
|
178 | - $edit_mapping_settings = $this->load_rest_settings( $edit_mapping_settings ); |
|
179 | - $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings ); |
|
180 | - $edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options(); |
|
181 | - $edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings ); |
|
182 | - // Load logic field options. |
|
183 | - $edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings ); |
|
184 | - $edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings ); |
|
185 | - |
|
186 | - return $edit_mapping_settings; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Returns post type, post category, or any other post taxonomies |
|
191 | - * |
|
192 | - * @return array An array of select options |
|
193 | - */ |
|
194 | - private static function get_post_taxonomies_and_terms() { |
|
195 | - $taxonomy_options = array(); |
|
196 | - $term_options = array(); |
|
197 | - $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
198 | - |
|
199 | - foreach ( $taxonomies as $taxonomy ) { |
|
200 | - array_push( |
|
201 | - $taxonomy_options, |
|
202 | - array( |
|
203 | - 'label' => $taxonomy->label, |
|
204 | - 'value' => $taxonomy->name, |
|
205 | - 'api_source' => 'taxonomy', |
|
206 | - ) |
|
207 | - ); |
|
208 | - } |
|
209 | - // Post type is also included in the list of taxonomies, so get the post type and merge with options. |
|
210 | - $post_type_array = self::get_post_type_key_and_value(); |
|
211 | - $post_type_option = $post_type_array['post_type_option_name']; |
|
212 | - // Get also the list of post types from the post_type_array. |
|
213 | - $post_type_option_values = $post_type_array['post_type_option_values']; |
|
214 | - |
|
215 | - $post_taxonomy_array = self::get_post_taxonomy_key_and_value(); |
|
216 | - $post_taxonomy_option = $post_taxonomy_array['post_taxonomy_option_name']; |
|
217 | - |
|
218 | - // Merge the post type option and post types in the taxonomy options |
|
219 | - array_push( $taxonomy_options, $post_type_option, $post_taxonomy_option ); |
|
220 | - $term_options = array_merge( $term_options, $post_type_option_values ); |
|
221 | - |
|
222 | - return array( |
|
223 | - 'taxonomy_options' => $taxonomy_options, |
|
224 | - 'term_options' => $term_options, |
|
225 | - ); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Return post type option and post type option values. |
|
230 | - * |
|
231 | - * @return array Array of post_type_option and post_type_option_values. |
|
232 | - */ |
|
233 | - private static function get_post_type_key_and_value() { |
|
234 | - $post_type_option_name = array( |
|
235 | - 'label' => __( 'Post type', 'wordlift' ), |
|
236 | - 'value' => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE, |
|
237 | - // Left empty since post types are provided locally. |
|
238 | - 'api_source' => '', |
|
239 | - ); |
|
240 | - $post_type_option_values = array(); |
|
241 | - $post_types = get_post_types( |
|
242 | - array(), |
|
243 | - 'objects' |
|
244 | - ); |
|
245 | - |
|
246 | - foreach ( $post_types as $post_type ) { |
|
247 | - array_push( |
|
248 | - $post_type_option_values, |
|
249 | - array( |
|
250 | - 'label' => $post_type->label, |
|
251 | - 'value' => $post_type->name, |
|
252 | - 'parent_value' => 'post_type', |
|
253 | - ) |
|
254 | - ); |
|
255 | - } |
|
256 | - |
|
257 | - return array( |
|
258 | - 'post_type_option_name' => $post_type_option_name, |
|
259 | - 'post_type_option_values' => $post_type_option_values, |
|
260 | - ); |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Return post type option and post type option values. |
|
265 | - * |
|
266 | - * @return array Array of post_type_option and post_type_option_values. |
|
267 | - */ |
|
268 | - private static function get_post_taxonomy_key_and_value() { |
|
269 | - |
|
270 | - $post_taxonomy_option_name = array( |
|
271 | - 'label' => __( 'Post Taxonomy', 'wordlift' ), |
|
272 | - 'value' => Wordlift\Mappings\Validators\Post_Taxonomy_Term_Rule_Validator::POST_TAXONOMY, |
|
273 | - 'api_source' => 'post_taxonomy', |
|
274 | - ); |
|
275 | - $post_taxonomy_option_values = array(); |
|
276 | - |
|
277 | - return array( |
|
278 | - 'post_taxonomy_option_name' => $post_taxonomy_option_name, |
|
279 | - 'post_taxonomy_option_values' => $post_taxonomy_option_values, |
|
280 | - ); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * This function loads the equal to, not equal to operator to the edit mapping settings. |
|
285 | - * |
|
286 | - * @param array $edit_mapping_settings |
|
287 | - * |
|
288 | - * @return array Loads the logic field options to the $edit_mapping_settings. |
|
289 | - */ |
|
290 | - private function load_logic_field_options( array $edit_mapping_settings ) { |
|
291 | - $edit_mapping_settings['wl_logic_field_options'] = array( |
|
292 | - array( |
|
293 | - 'label' => __( 'is equal to', 'wordlift' ), |
|
294 | - 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO, |
|
295 | - ), |
|
296 | - array( |
|
297 | - 'label' => __( 'is not equal to', 'wordlift' ), |
|
298 | - 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO, |
|
299 | - ), |
|
300 | - ); |
|
301 | - |
|
302 | - return $edit_mapping_settings; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Validates the nonce posted by client and then assign the mapping id which should be edited. |
|
307 | - * |
|
308 | - * @param array $edit_mapping_settings |
|
309 | - * |
|
310 | - * @return array Edit mapping settings array with the mapping id if the nonce is valid. |
|
311 | - */ |
|
312 | - private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) { |
|
313 | - // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id |
|
314 | - if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] ) |
|
315 | - && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wl_edit_mapping_nonce'] ) ), 'wl-edit-mapping-nonce' ) ) { |
|
316 | - // We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST. |
|
317 | - $mapping_id = isset( $_REQUEST['wl_edit_mapping_id'] ) ? |
|
318 | - (int) filter_var( sanitize_text_field( wp_unslash( $_REQUEST['wl_edit_mapping_id'] ) ), FILTER_VALIDATE_INT ) : 0; |
|
319 | - $edit_mapping_settings['wl_edit_mapping_id'] = $mapping_id; |
|
320 | - } |
|
321 | - |
|
322 | - return $edit_mapping_settings; |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Load the rest settings required for the edit_mappings js client. |
|
327 | - * |
|
328 | - * @param array $edit_mapping_settings |
|
329 | - * |
|
330 | - * @return array |
|
331 | - */ |
|
332 | - private function load_rest_settings( array $edit_mapping_settings ) { |
|
333 | - $edit_mapping_settings['rest_url'] = get_rest_url( |
|
334 | - null, |
|
335 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
336 | - ); |
|
337 | - $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
338 | - $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings ); |
|
339 | - |
|
340 | - return $edit_mapping_settings; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Load the rule field options in to the settings. |
|
345 | - * |
|
346 | - * @param array $edit_mapping_settings |
|
347 | - * |
|
348 | - * @return array Return the settings. |
|
349 | - */ |
|
350 | - private function load_rule_field_options( array $edit_mapping_settings ) { |
|
351 | - // Load the rule field options. |
|
352 | - $rule_field_data = self::get_post_taxonomies_and_terms(); |
|
353 | - $edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options']; |
|
354 | - $edit_mapping_settings['wl_rule_field_two_options'] = $rule_field_data['term_options']; |
|
355 | - |
|
356 | - /** |
|
357 | - * Allow 3rd parties to add ui options. |
|
358 | - * |
|
359 | - * @param array Array of Rule field one options where each item is in format |
|
360 | - * |
|
361 | - * array ( 'label' => string, 'value' => string, 'api_source'=>string); |
|
362 | - * |
|
363 | - * Leave api_source empty string to ensure didnt fetch rule field two options |
|
364 | - * from api. |
|
365 | - * |
|
366 | - * @return array Array of Rule field one options |
|
367 | - * |
|
368 | - * @since 3.27.0 |
|
369 | - */ |
|
370 | - $edit_mapping_settings['wl_rule_field_one_options'] = apply_filters( |
|
371 | - 'wl_mappings_rule_field_one_options', |
|
372 | - $edit_mapping_settings['wl_rule_field_one_options'] |
|
373 | - ); |
|
374 | - |
|
375 | - /** |
|
376 | - * Allow 3rd parties to add rule field two options. |
|
377 | - * |
|
378 | - * @param array Array of Rule field two option where each item is in format |
|
379 | - * |
|
380 | - * array ( 'label' => string, 'value' => string, 'parent_value' => string ); |
|
381 | - * |
|
382 | - * where parent_value is the value of the parent option in the rule_field_one_option. |
|
383 | - * |
|
384 | - * @since 3.27.0 |
|
385 | - */ |
|
386 | - $edit_mapping_settings['wl_rule_field_two_options'] = apply_filters( |
|
387 | - 'wl_mappings_rule_field_two_options', |
|
388 | - $edit_mapping_settings['wl_rule_field_two_options'] |
|
389 | - ); |
|
390 | - |
|
391 | - return $edit_mapping_settings; |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Load field type and field name options to the settings array. |
|
396 | - * |
|
397 | - * @param array $edit_mapping_settings |
|
398 | - * |
|
399 | - * @return array |
|
400 | - */ |
|
401 | - private function load_field_type_and_name_options( array $edit_mapping_settings ) { |
|
402 | - $all_field_name_options = self::get_all_field_name_options(); |
|
403 | - $all_field_types_options = array_map( |
|
404 | - function ( $item ) { |
|
405 | - return array( |
|
406 | - 'label' => $item['label'], |
|
407 | - 'value' => $item['field_type'], |
|
408 | - ); |
|
409 | - }, |
|
410 | - $all_field_name_options |
|
411 | - ); |
|
412 | - |
|
413 | - $edit_mapping_settings['wl_field_type_options'] = $all_field_types_options; |
|
414 | - // Add wl_edit_field_name_options. |
|
415 | - $edit_mapping_settings['wl_field_name_options'] = $all_field_name_options; |
|
416 | - |
|
417 | - return $edit_mapping_settings; |
|
418 | - } |
|
27 | + /** Instance to store the registry class. |
|
28 | + * |
|
29 | + * @var Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance} |
|
30 | + */ |
|
31 | + public $transform_function_registry; |
|
32 | + |
|
33 | + /** |
|
34 | + * Edit_Mappings_Page constructor. |
|
35 | + * |
|
36 | + * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance } |
|
37 | + */ |
|
38 | + public function __construct( $transform_function_registry ) { |
|
39 | + parent::__construct(); |
|
40 | + $this->transform_function_registry = $transform_function_registry; |
|
41 | + } |
|
42 | + |
|
43 | + public function render() { |
|
44 | + // Render all the settings when this method is called, because the partial page is loaded after |
|
45 | + // this method. |
|
46 | + // Load the UI dependencies. |
|
47 | + $edit_mapping_settings = $this->get_ui_settings_array(); |
|
48 | + // Supply the settings to js client. |
|
49 | + wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings ); |
|
50 | + |
|
51 | + parent::render(); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Load the text settings needed for the edit_mappings_page. |
|
56 | + * |
|
57 | + * @param array $edit_mapping_settings Key value pair of settings used by edit mappings page. |
|
58 | + * |
|
59 | + * @return array Adding text settings to the main settings array. |
|
60 | + */ |
|
61 | + private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) { |
|
62 | + $edit_mapping_settings['wl_add_mapping_text'] = __( 'Add Mapping', 'wordlift' ); |
|
63 | + $edit_mapping_settings['wl_edit_mapping_text'] = __( 'Edit Mapping', 'wordlift' ); |
|
64 | + $edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' ); |
|
65 | + $edit_mapping_settings['page'] = 'wl_edit_mapping'; |
|
66 | + |
|
67 | + return $edit_mapping_settings; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * The base class {@link Wordlift_Admin_Page} will add the admin page to the WordLift menu. |
|
72 | + * |
|
73 | + * We don't want this page to be in the menu though. Therefore we override the `parent_slug` so that WordPress won't |
|
74 | + * show it there. |
|
75 | + * |
|
76 | + * @return null return null to avoid this page to be displayed in WordLift's menu. |
|
77 | + */ |
|
78 | + protected function get_parent_slug() { |
|
79 | + return null; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * {@inheritdoc} |
|
84 | + */ |
|
85 | + public function get_page_title() { |
|
86 | + |
|
87 | + return __( 'Edit Mappings', 'wordlift' ); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * {@inheritdoc} |
|
92 | + */ |
|
93 | + public function get_menu_title() { |
|
94 | + |
|
95 | + return __( 'Edit Mappings', 'wordlift' ); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * {@inheritdoc} |
|
100 | + */ |
|
101 | + public function get_menu_slug() { |
|
102 | + |
|
103 | + return 'wl_edit_mapping'; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * {@inheritdoc} |
|
108 | + */ |
|
109 | + public function get_partial_name() { |
|
110 | + return 'wordlift-admin-mappings-edit.php'; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * {@inheritdoc} |
|
115 | + */ |
|
116 | + public function enqueue_scripts() { |
|
117 | + |
|
118 | + // Enqueue the script. |
|
119 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
120 | + 'wl-mappings-edit', |
|
121 | + plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings-edit', |
|
122 | + array( 'react', 'react-dom', 'wp-polyfill' ), |
|
123 | + true |
|
124 | + ); |
|
125 | + |
|
126 | + // Enqueue the style. |
|
127 | + wp_enqueue_style( |
|
128 | + 'wl-mappings-edit', |
|
129 | + plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings-edit.css', |
|
130 | + array(), |
|
131 | + WORDLIFT_VERSION |
|
132 | + ); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Returns field name options based on the chosen field type. |
|
137 | + * if string is returned a text field would be shown to user, if an array of options is returned |
|
138 | + * then the select box would be shown to user. |
|
139 | + * |
|
140 | + * @return array Array of the options. |
|
141 | + */ |
|
142 | + public static function get_all_field_name_options() { |
|
143 | + |
|
144 | + $options = array( |
|
145 | + array( |
|
146 | + 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD, |
|
147 | + 'value' => '', |
|
148 | + 'label' => __( 'Fixed Text', 'wordlift' ), |
|
149 | + ), |
|
150 | + // @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings. |
|
151 | + array( |
|
152 | + 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD, |
|
153 | + 'value' => '', |
|
154 | + 'label' => __( 'Custom Field', 'wordlift' ), |
|
155 | + ), |
|
156 | + ); |
|
157 | + |
|
158 | + /** |
|
159 | + * Allow 3rd parties to add field types. |
|
160 | + * |
|
161 | + * @param array An array of Field Types. |
|
162 | + * |
|
163 | + * @return array An array of Field Types. |
|
164 | + * |
|
165 | + * @since 3.25.0 |
|
166 | + */ |
|
167 | + return apply_filters( 'wl_mappings_field_types', $options ); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @return array An Array containing key value pairs of settings. |
|
172 | + * @since 3.25.0 |
|
173 | + * Load dependencies required for js client. |
|
174 | + */ |
|
175 | + public function get_ui_settings_array() { |
|
176 | + // Create ui settings array to be used by js client. |
|
177 | + $edit_mapping_settings = array(); |
|
178 | + $edit_mapping_settings = $this->load_rest_settings( $edit_mapping_settings ); |
|
179 | + $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings ); |
|
180 | + $edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options(); |
|
181 | + $edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings ); |
|
182 | + // Load logic field options. |
|
183 | + $edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings ); |
|
184 | + $edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings ); |
|
185 | + |
|
186 | + return $edit_mapping_settings; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Returns post type, post category, or any other post taxonomies |
|
191 | + * |
|
192 | + * @return array An array of select options |
|
193 | + */ |
|
194 | + private static function get_post_taxonomies_and_terms() { |
|
195 | + $taxonomy_options = array(); |
|
196 | + $term_options = array(); |
|
197 | + $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
198 | + |
|
199 | + foreach ( $taxonomies as $taxonomy ) { |
|
200 | + array_push( |
|
201 | + $taxonomy_options, |
|
202 | + array( |
|
203 | + 'label' => $taxonomy->label, |
|
204 | + 'value' => $taxonomy->name, |
|
205 | + 'api_source' => 'taxonomy', |
|
206 | + ) |
|
207 | + ); |
|
208 | + } |
|
209 | + // Post type is also included in the list of taxonomies, so get the post type and merge with options. |
|
210 | + $post_type_array = self::get_post_type_key_and_value(); |
|
211 | + $post_type_option = $post_type_array['post_type_option_name']; |
|
212 | + // Get also the list of post types from the post_type_array. |
|
213 | + $post_type_option_values = $post_type_array['post_type_option_values']; |
|
214 | + |
|
215 | + $post_taxonomy_array = self::get_post_taxonomy_key_and_value(); |
|
216 | + $post_taxonomy_option = $post_taxonomy_array['post_taxonomy_option_name']; |
|
217 | + |
|
218 | + // Merge the post type option and post types in the taxonomy options |
|
219 | + array_push( $taxonomy_options, $post_type_option, $post_taxonomy_option ); |
|
220 | + $term_options = array_merge( $term_options, $post_type_option_values ); |
|
221 | + |
|
222 | + return array( |
|
223 | + 'taxonomy_options' => $taxonomy_options, |
|
224 | + 'term_options' => $term_options, |
|
225 | + ); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Return post type option and post type option values. |
|
230 | + * |
|
231 | + * @return array Array of post_type_option and post_type_option_values. |
|
232 | + */ |
|
233 | + private static function get_post_type_key_and_value() { |
|
234 | + $post_type_option_name = array( |
|
235 | + 'label' => __( 'Post type', 'wordlift' ), |
|
236 | + 'value' => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE, |
|
237 | + // Left empty since post types are provided locally. |
|
238 | + 'api_source' => '', |
|
239 | + ); |
|
240 | + $post_type_option_values = array(); |
|
241 | + $post_types = get_post_types( |
|
242 | + array(), |
|
243 | + 'objects' |
|
244 | + ); |
|
245 | + |
|
246 | + foreach ( $post_types as $post_type ) { |
|
247 | + array_push( |
|
248 | + $post_type_option_values, |
|
249 | + array( |
|
250 | + 'label' => $post_type->label, |
|
251 | + 'value' => $post_type->name, |
|
252 | + 'parent_value' => 'post_type', |
|
253 | + ) |
|
254 | + ); |
|
255 | + } |
|
256 | + |
|
257 | + return array( |
|
258 | + 'post_type_option_name' => $post_type_option_name, |
|
259 | + 'post_type_option_values' => $post_type_option_values, |
|
260 | + ); |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Return post type option and post type option values. |
|
265 | + * |
|
266 | + * @return array Array of post_type_option and post_type_option_values. |
|
267 | + */ |
|
268 | + private static function get_post_taxonomy_key_and_value() { |
|
269 | + |
|
270 | + $post_taxonomy_option_name = array( |
|
271 | + 'label' => __( 'Post Taxonomy', 'wordlift' ), |
|
272 | + 'value' => Wordlift\Mappings\Validators\Post_Taxonomy_Term_Rule_Validator::POST_TAXONOMY, |
|
273 | + 'api_source' => 'post_taxonomy', |
|
274 | + ); |
|
275 | + $post_taxonomy_option_values = array(); |
|
276 | + |
|
277 | + return array( |
|
278 | + 'post_taxonomy_option_name' => $post_taxonomy_option_name, |
|
279 | + 'post_taxonomy_option_values' => $post_taxonomy_option_values, |
|
280 | + ); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * This function loads the equal to, not equal to operator to the edit mapping settings. |
|
285 | + * |
|
286 | + * @param array $edit_mapping_settings |
|
287 | + * |
|
288 | + * @return array Loads the logic field options to the $edit_mapping_settings. |
|
289 | + */ |
|
290 | + private function load_logic_field_options( array $edit_mapping_settings ) { |
|
291 | + $edit_mapping_settings['wl_logic_field_options'] = array( |
|
292 | + array( |
|
293 | + 'label' => __( 'is equal to', 'wordlift' ), |
|
294 | + 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO, |
|
295 | + ), |
|
296 | + array( |
|
297 | + 'label' => __( 'is not equal to', 'wordlift' ), |
|
298 | + 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO, |
|
299 | + ), |
|
300 | + ); |
|
301 | + |
|
302 | + return $edit_mapping_settings; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Validates the nonce posted by client and then assign the mapping id which should be edited. |
|
307 | + * |
|
308 | + * @param array $edit_mapping_settings |
|
309 | + * |
|
310 | + * @return array Edit mapping settings array with the mapping id if the nonce is valid. |
|
311 | + */ |
|
312 | + private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) { |
|
313 | + // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id |
|
314 | + if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] ) |
|
315 | + && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wl_edit_mapping_nonce'] ) ), 'wl-edit-mapping-nonce' ) ) { |
|
316 | + // We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST. |
|
317 | + $mapping_id = isset( $_REQUEST['wl_edit_mapping_id'] ) ? |
|
318 | + (int) filter_var( sanitize_text_field( wp_unslash( $_REQUEST['wl_edit_mapping_id'] ) ), FILTER_VALIDATE_INT ) : 0; |
|
319 | + $edit_mapping_settings['wl_edit_mapping_id'] = $mapping_id; |
|
320 | + } |
|
321 | + |
|
322 | + return $edit_mapping_settings; |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Load the rest settings required for the edit_mappings js client. |
|
327 | + * |
|
328 | + * @param array $edit_mapping_settings |
|
329 | + * |
|
330 | + * @return array |
|
331 | + */ |
|
332 | + private function load_rest_settings( array $edit_mapping_settings ) { |
|
333 | + $edit_mapping_settings['rest_url'] = get_rest_url( |
|
334 | + null, |
|
335 | + WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
336 | + ); |
|
337 | + $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
338 | + $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings ); |
|
339 | + |
|
340 | + return $edit_mapping_settings; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Load the rule field options in to the settings. |
|
345 | + * |
|
346 | + * @param array $edit_mapping_settings |
|
347 | + * |
|
348 | + * @return array Return the settings. |
|
349 | + */ |
|
350 | + private function load_rule_field_options( array $edit_mapping_settings ) { |
|
351 | + // Load the rule field options. |
|
352 | + $rule_field_data = self::get_post_taxonomies_and_terms(); |
|
353 | + $edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options']; |
|
354 | + $edit_mapping_settings['wl_rule_field_two_options'] = $rule_field_data['term_options']; |
|
355 | + |
|
356 | + /** |
|
357 | + * Allow 3rd parties to add ui options. |
|
358 | + * |
|
359 | + * @param array Array of Rule field one options where each item is in format |
|
360 | + * |
|
361 | + * array ( 'label' => string, 'value' => string, 'api_source'=>string); |
|
362 | + * |
|
363 | + * Leave api_source empty string to ensure didnt fetch rule field two options |
|
364 | + * from api. |
|
365 | + * |
|
366 | + * @return array Array of Rule field one options |
|
367 | + * |
|
368 | + * @since 3.27.0 |
|
369 | + */ |
|
370 | + $edit_mapping_settings['wl_rule_field_one_options'] = apply_filters( |
|
371 | + 'wl_mappings_rule_field_one_options', |
|
372 | + $edit_mapping_settings['wl_rule_field_one_options'] |
|
373 | + ); |
|
374 | + |
|
375 | + /** |
|
376 | + * Allow 3rd parties to add rule field two options. |
|
377 | + * |
|
378 | + * @param array Array of Rule field two option where each item is in format |
|
379 | + * |
|
380 | + * array ( 'label' => string, 'value' => string, 'parent_value' => string ); |
|
381 | + * |
|
382 | + * where parent_value is the value of the parent option in the rule_field_one_option. |
|
383 | + * |
|
384 | + * @since 3.27.0 |
|
385 | + */ |
|
386 | + $edit_mapping_settings['wl_rule_field_two_options'] = apply_filters( |
|
387 | + 'wl_mappings_rule_field_two_options', |
|
388 | + $edit_mapping_settings['wl_rule_field_two_options'] |
|
389 | + ); |
|
390 | + |
|
391 | + return $edit_mapping_settings; |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Load field type and field name options to the settings array. |
|
396 | + * |
|
397 | + * @param array $edit_mapping_settings |
|
398 | + * |
|
399 | + * @return array |
|
400 | + */ |
|
401 | + private function load_field_type_and_name_options( array $edit_mapping_settings ) { |
|
402 | + $all_field_name_options = self::get_all_field_name_options(); |
|
403 | + $all_field_types_options = array_map( |
|
404 | + function ( $item ) { |
|
405 | + return array( |
|
406 | + 'label' => $item['label'], |
|
407 | + 'value' => $item['field_type'], |
|
408 | + ); |
|
409 | + }, |
|
410 | + $all_field_name_options |
|
411 | + ); |
|
412 | + |
|
413 | + $edit_mapping_settings['wl_field_type_options'] = $all_field_types_options; |
|
414 | + // Add wl_edit_field_name_options. |
|
415 | + $edit_mapping_settings['wl_field_name_options'] = $all_field_name_options; |
|
416 | + |
|
417 | + return $edit_mapping_settings; |
|
418 | + } |
|
419 | 419 | |
420 | 420 | } |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | * |
36 | 36 | * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance } |
37 | 37 | */ |
38 | - public function __construct( $transform_function_registry ) { |
|
38 | + public function __construct($transform_function_registry) { |
|
39 | 39 | parent::__construct(); |
40 | 40 | $this->transform_function_registry = $transform_function_registry; |
41 | 41 | } |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | // Load the UI dependencies. |
47 | 47 | $edit_mapping_settings = $this->get_ui_settings_array(); |
48 | 48 | // Supply the settings to js client. |
49 | - wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings ); |
|
49 | + wp_localize_script('wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings); |
|
50 | 50 | |
51 | 51 | parent::render(); |
52 | 52 | } |
@@ -58,10 +58,10 @@ discard block |
||
58 | 58 | * |
59 | 59 | * @return array Adding text settings to the main settings array. |
60 | 60 | */ |
61 | - private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) { |
|
62 | - $edit_mapping_settings['wl_add_mapping_text'] = __( 'Add Mapping', 'wordlift' ); |
|
63 | - $edit_mapping_settings['wl_edit_mapping_text'] = __( 'Edit Mapping', 'wordlift' ); |
|
64 | - $edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' ); |
|
61 | + private function load_text_settings_for_edit_mapping_page(array $edit_mapping_settings) { |
|
62 | + $edit_mapping_settings['wl_add_mapping_text'] = __('Add Mapping', 'wordlift'); |
|
63 | + $edit_mapping_settings['wl_edit_mapping_text'] = __('Edit Mapping', 'wordlift'); |
|
64 | + $edit_mapping_settings['wl_edit_mapping_no_item'] = __('Unable to find the mapping item', 'wordlift'); |
|
65 | 65 | $edit_mapping_settings['page'] = 'wl_edit_mapping'; |
66 | 66 | |
67 | 67 | return $edit_mapping_settings; |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function get_page_title() { |
86 | 86 | |
87 | - return __( 'Edit Mappings', 'wordlift' ); |
|
87 | + return __('Edit Mappings', 'wordlift'); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | /** |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | */ |
93 | 93 | public function get_menu_title() { |
94 | 94 | |
95 | - return __( 'Edit Mappings', 'wordlift' ); |
|
95 | + return __('Edit Mappings', 'wordlift'); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | /** |
@@ -118,15 +118,15 @@ discard block |
||
118 | 118 | // Enqueue the script. |
119 | 119 | Scripts_Helper::enqueue_based_on_wordpress_version( |
120 | 120 | 'wl-mappings-edit', |
121 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings-edit', |
|
122 | - array( 'react', 'react-dom', 'wp-polyfill' ), |
|
121 | + plugin_dir_url(dirname(dirname(__DIR__))).'js/dist/mappings-edit', |
|
122 | + array('react', 'react-dom', 'wp-polyfill'), |
|
123 | 123 | true |
124 | 124 | ); |
125 | 125 | |
126 | 126 | // Enqueue the style. |
127 | 127 | wp_enqueue_style( |
128 | 128 | 'wl-mappings-edit', |
129 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/mappings-edit.css', |
|
129 | + plugin_dir_url(dirname(dirname(__DIR__))).'js/dist/mappings-edit.css', |
|
130 | 130 | array(), |
131 | 131 | WORDLIFT_VERSION |
132 | 132 | ); |
@@ -145,13 +145,13 @@ discard block |
||
145 | 145 | array( |
146 | 146 | 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD, |
147 | 147 | 'value' => '', |
148 | - 'label' => __( 'Fixed Text', 'wordlift' ), |
|
148 | + 'label' => __('Fixed Text', 'wordlift'), |
|
149 | 149 | ), |
150 | 150 | // @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings. |
151 | 151 | array( |
152 | 152 | 'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD, |
153 | 153 | 'value' => '', |
154 | - 'label' => __( 'Custom Field', 'wordlift' ), |
|
154 | + 'label' => __('Custom Field', 'wordlift'), |
|
155 | 155 | ), |
156 | 156 | ); |
157 | 157 | |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | * |
165 | 165 | * @since 3.25.0 |
166 | 166 | */ |
167 | - return apply_filters( 'wl_mappings_field_types', $options ); |
|
167 | + return apply_filters('wl_mappings_field_types', $options); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -175,13 +175,13 @@ discard block |
||
175 | 175 | public function get_ui_settings_array() { |
176 | 176 | // Create ui settings array to be used by js client. |
177 | 177 | $edit_mapping_settings = array(); |
178 | - $edit_mapping_settings = $this->load_rest_settings( $edit_mapping_settings ); |
|
179 | - $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings ); |
|
178 | + $edit_mapping_settings = $this->load_rest_settings($edit_mapping_settings); |
|
179 | + $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page($edit_mapping_settings); |
|
180 | 180 | $edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options(); |
181 | - $edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings ); |
|
181 | + $edit_mapping_settings = $this->load_field_type_and_name_options($edit_mapping_settings); |
|
182 | 182 | // Load logic field options. |
183 | - $edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings ); |
|
184 | - $edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings ); |
|
183 | + $edit_mapping_settings = $this->load_logic_field_options($edit_mapping_settings); |
|
184 | + $edit_mapping_settings = $this->load_rule_field_options($edit_mapping_settings); |
|
185 | 185 | |
186 | 186 | return $edit_mapping_settings; |
187 | 187 | } |
@@ -194,9 +194,9 @@ discard block |
||
194 | 194 | private static function get_post_taxonomies_and_terms() { |
195 | 195 | $taxonomy_options = array(); |
196 | 196 | $term_options = array(); |
197 | - $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
197 | + $taxonomies = get_object_taxonomies('post', 'objects'); |
|
198 | 198 | |
199 | - foreach ( $taxonomies as $taxonomy ) { |
|
199 | + foreach ($taxonomies as $taxonomy) { |
|
200 | 200 | array_push( |
201 | 201 | $taxonomy_options, |
202 | 202 | array( |
@@ -216,8 +216,8 @@ discard block |
||
216 | 216 | $post_taxonomy_option = $post_taxonomy_array['post_taxonomy_option_name']; |
217 | 217 | |
218 | 218 | // Merge the post type option and post types in the taxonomy options |
219 | - array_push( $taxonomy_options, $post_type_option, $post_taxonomy_option ); |
|
220 | - $term_options = array_merge( $term_options, $post_type_option_values ); |
|
219 | + array_push($taxonomy_options, $post_type_option, $post_taxonomy_option); |
|
220 | + $term_options = array_merge($term_options, $post_type_option_values); |
|
221 | 221 | |
222 | 222 | return array( |
223 | 223 | 'taxonomy_options' => $taxonomy_options, |
@@ -231,8 +231,8 @@ discard block |
||
231 | 231 | * @return array Array of post_type_option and post_type_option_values. |
232 | 232 | */ |
233 | 233 | private static function get_post_type_key_and_value() { |
234 | - $post_type_option_name = array( |
|
235 | - 'label' => __( 'Post type', 'wordlift' ), |
|
234 | + $post_type_option_name = array( |
|
235 | + 'label' => __('Post type', 'wordlift'), |
|
236 | 236 | 'value' => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE, |
237 | 237 | // Left empty since post types are provided locally. |
238 | 238 | 'api_source' => '', |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | 'objects' |
244 | 244 | ); |
245 | 245 | |
246 | - foreach ( $post_types as $post_type ) { |
|
246 | + foreach ($post_types as $post_type) { |
|
247 | 247 | array_push( |
248 | 248 | $post_type_option_values, |
249 | 249 | array( |
@@ -267,8 +267,8 @@ discard block |
||
267 | 267 | */ |
268 | 268 | private static function get_post_taxonomy_key_and_value() { |
269 | 269 | |
270 | - $post_taxonomy_option_name = array( |
|
271 | - 'label' => __( 'Post Taxonomy', 'wordlift' ), |
|
270 | + $post_taxonomy_option_name = array( |
|
271 | + 'label' => __('Post Taxonomy', 'wordlift'), |
|
272 | 272 | 'value' => Wordlift\Mappings\Validators\Post_Taxonomy_Term_Rule_Validator::POST_TAXONOMY, |
273 | 273 | 'api_source' => 'post_taxonomy', |
274 | 274 | ); |
@@ -287,14 +287,14 @@ discard block |
||
287 | 287 | * |
288 | 288 | * @return array Loads the logic field options to the $edit_mapping_settings. |
289 | 289 | */ |
290 | - private function load_logic_field_options( array $edit_mapping_settings ) { |
|
290 | + private function load_logic_field_options(array $edit_mapping_settings) { |
|
291 | 291 | $edit_mapping_settings['wl_logic_field_options'] = array( |
292 | 292 | array( |
293 | - 'label' => __( 'is equal to', 'wordlift' ), |
|
293 | + 'label' => __('is equal to', 'wordlift'), |
|
294 | 294 | 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO, |
295 | 295 | ), |
296 | 296 | array( |
297 | - 'label' => __( 'is not equal to', 'wordlift' ), |
|
297 | + 'label' => __('is not equal to', 'wordlift'), |
|
298 | 298 | 'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO, |
299 | 299 | ), |
300 | 300 | ); |
@@ -309,13 +309,13 @@ discard block |
||
309 | 309 | * |
310 | 310 | * @return array Edit mapping settings array with the mapping id if the nonce is valid. |
311 | 311 | */ |
312 | - private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) { |
|
312 | + private function validate_nonce_and_assign_mapping_id(array $edit_mapping_settings) { |
|
313 | 313 | // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id |
314 | - if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] ) |
|
315 | - && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wl_edit_mapping_nonce'] ) ), 'wl-edit-mapping-nonce' ) ) { |
|
314 | + if (isset($_REQUEST['_wl_edit_mapping_nonce']) |
|
315 | + && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wl_edit_mapping_nonce'])), 'wl-edit-mapping-nonce')) { |
|
316 | 316 | // We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST. |
317 | - $mapping_id = isset( $_REQUEST['wl_edit_mapping_id'] ) ? |
|
318 | - (int) filter_var( sanitize_text_field( wp_unslash( $_REQUEST['wl_edit_mapping_id'] ) ), FILTER_VALIDATE_INT ) : 0; |
|
317 | + $mapping_id = isset($_REQUEST['wl_edit_mapping_id']) ? |
|
318 | + (int) filter_var(sanitize_text_field(wp_unslash($_REQUEST['wl_edit_mapping_id'])), FILTER_VALIDATE_INT) : 0; |
|
319 | 319 | $edit_mapping_settings['wl_edit_mapping_id'] = $mapping_id; |
320 | 320 | } |
321 | 321 | |
@@ -329,13 +329,13 @@ discard block |
||
329 | 329 | * |
330 | 330 | * @return array |
331 | 331 | */ |
332 | - private function load_rest_settings( array $edit_mapping_settings ) { |
|
333 | - $edit_mapping_settings['rest_url'] = get_rest_url( |
|
332 | + private function load_rest_settings(array $edit_mapping_settings) { |
|
333 | + $edit_mapping_settings['rest_url'] = get_rest_url( |
|
334 | 334 | null, |
335 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
335 | + WL_REST_ROUTE_DEFAULT_NAMESPACE.Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
336 | 336 | ); |
337 | - $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
338 | - $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings ); |
|
337 | + $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce('wp_rest'); |
|
338 | + $edit_mapping_settings = $this->validate_nonce_and_assign_mapping_id($edit_mapping_settings); |
|
339 | 339 | |
340 | 340 | return $edit_mapping_settings; |
341 | 341 | } |
@@ -347,7 +347,7 @@ discard block |
||
347 | 347 | * |
348 | 348 | * @return array Return the settings. |
349 | 349 | */ |
350 | - private function load_rule_field_options( array $edit_mapping_settings ) { |
|
350 | + private function load_rule_field_options(array $edit_mapping_settings) { |
|
351 | 351 | // Load the rule field options. |
352 | 352 | $rule_field_data = self::get_post_taxonomies_and_terms(); |
353 | 353 | $edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options']; |
@@ -398,10 +398,10 @@ discard block |
||
398 | 398 | * |
399 | 399 | * @return array |
400 | 400 | */ |
401 | - private function load_field_type_and_name_options( array $edit_mapping_settings ) { |
|
401 | + private function load_field_type_and_name_options(array $edit_mapping_settings) { |
|
402 | 402 | $all_field_name_options = self::get_all_field_name_options(); |
403 | 403 | $all_field_types_options = array_map( |
404 | - function ( $item ) { |
|
404 | + function($item) { |
|
405 | 405 | return array( |
406 | 406 | 'label' => $item['label'], |
407 | 407 | 'value' => $item['field_type'], |
@@ -17,70 +17,70 @@ |
||
17 | 17 | * @package Wordlift\Mappings\Validators |
18 | 18 | */ |
19 | 19 | class Post_Type_Rule_Validator implements Rule_Validator { |
20 | - /** |
|
21 | - * @since 3.25.0 |
|
22 | - * Enum for the post type rule validator. |
|
23 | - */ |
|
24 | - const POST_TYPE = 'post_type'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Post_Type_Rule_Validator constructor. |
|
28 | - * |
|
29 | - * When initializing the class hooks to `wl_mappings_rule_validators`. |
|
30 | - */ |
|
31 | - public function __construct() { |
|
32 | - |
|
33 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
34 | - |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
39 | - * |
|
40 | - * @param array $value An array with validators. |
|
41 | - * |
|
42 | - * @return array An array with validators plus ours. |
|
43 | - */ |
|
44 | - public function wl_mappings_rule_validators( $value ) { |
|
45 | - |
|
46 | - $value[ self::POST_TYPE ] = $this; |
|
47 | - |
|
48 | - return $value; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - public function get_label() { |
|
55 | - return __( 'Post Type', 'wordlift' ); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * {@inheritdoc} |
|
60 | - */ |
|
61 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
62 | - /** |
|
63 | - * If this is not done then this will validate for term |
|
64 | - * which causes a bug, so this rule validator would return false |
|
65 | - * when the current thing is not a post. |
|
66 | - */ |
|
67 | - if ( Jsonld_Converter::POST !== $type ) { |
|
68 | - return false; |
|
69 | - } |
|
70 | - // Get the post type and then check whether it matches or not according to the operator. |
|
71 | - $post_type = get_post_type( $identifier ); |
|
72 | - |
|
73 | - switch ( $operator ) { |
|
74 | - case Rule_Validator::IS_NOT_EQUAL_TO: |
|
75 | - return $post_type !== $operand_2; |
|
76 | - |
|
77 | - case Rule_Validator::IS_EQUAL_TO: |
|
78 | - return $post_type === $operand_2; |
|
79 | - |
|
80 | - default: |
|
81 | - } |
|
82 | - |
|
83 | - return false; |
|
84 | - } |
|
20 | + /** |
|
21 | + * @since 3.25.0 |
|
22 | + * Enum for the post type rule validator. |
|
23 | + */ |
|
24 | + const POST_TYPE = 'post_type'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Post_Type_Rule_Validator constructor. |
|
28 | + * |
|
29 | + * When initializing the class hooks to `wl_mappings_rule_validators`. |
|
30 | + */ |
|
31 | + public function __construct() { |
|
32 | + |
|
33 | + add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
34 | + |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
39 | + * |
|
40 | + * @param array $value An array with validators. |
|
41 | + * |
|
42 | + * @return array An array with validators plus ours. |
|
43 | + */ |
|
44 | + public function wl_mappings_rule_validators( $value ) { |
|
45 | + |
|
46 | + $value[ self::POST_TYPE ] = $this; |
|
47 | + |
|
48 | + return $value; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + public function get_label() { |
|
55 | + return __( 'Post Type', 'wordlift' ); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * {@inheritdoc} |
|
60 | + */ |
|
61 | + public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
62 | + /** |
|
63 | + * If this is not done then this will validate for term |
|
64 | + * which causes a bug, so this rule validator would return false |
|
65 | + * when the current thing is not a post. |
|
66 | + */ |
|
67 | + if ( Jsonld_Converter::POST !== $type ) { |
|
68 | + return false; |
|
69 | + } |
|
70 | + // Get the post type and then check whether it matches or not according to the operator. |
|
71 | + $post_type = get_post_type( $identifier ); |
|
72 | + |
|
73 | + switch ( $operator ) { |
|
74 | + case Rule_Validator::IS_NOT_EQUAL_TO: |
|
75 | + return $post_type !== $operand_2; |
|
76 | + |
|
77 | + case Rule_Validator::IS_EQUAL_TO: |
|
78 | + return $post_type === $operand_2; |
|
79 | + |
|
80 | + default: |
|
81 | + } |
|
82 | + |
|
83 | + return false; |
|
84 | + } |
|
85 | 85 | |
86 | 86 | } |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | */ |
31 | 31 | public function __construct() { |
32 | 32 | |
33 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
33 | + add_filter('wl_mappings_rule_validators', array($this, 'wl_mappings_rule_validators')); |
|
34 | 34 | |
35 | 35 | } |
36 | 36 | |
@@ -41,9 +41,9 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @return array An array with validators plus ours. |
43 | 43 | */ |
44 | - public function wl_mappings_rule_validators( $value ) { |
|
44 | + public function wl_mappings_rule_validators($value) { |
|
45 | 45 | |
46 | - $value[ self::POST_TYPE ] = $this; |
|
46 | + $value[self::POST_TYPE] = $this; |
|
47 | 47 | |
48 | 48 | return $value; |
49 | 49 | } |
@@ -52,25 +52,25 @@ discard block |
||
52 | 52 | * {@inheritdoc} |
53 | 53 | */ |
54 | 54 | public function get_label() { |
55 | - return __( 'Post Type', 'wordlift' ); |
|
55 | + return __('Post Type', 'wordlift'); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * {@inheritdoc} |
60 | 60 | */ |
61 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
61 | + public function is_valid($identifier, $operator, $operand_1, $operand_2, $type) { |
|
62 | 62 | /** |
63 | 63 | * If this is not done then this will validate for term |
64 | 64 | * which causes a bug, so this rule validator would return false |
65 | 65 | * when the current thing is not a post. |
66 | 66 | */ |
67 | - if ( Jsonld_Converter::POST !== $type ) { |
|
67 | + if (Jsonld_Converter::POST !== $type) { |
|
68 | 68 | return false; |
69 | 69 | } |
70 | 70 | // Get the post type and then check whether it matches or not according to the operator. |
71 | - $post_type = get_post_type( $identifier ); |
|
71 | + $post_type = get_post_type($identifier); |
|
72 | 72 | |
73 | - switch ( $operator ) { |
|
73 | + switch ($operator) { |
|
74 | 74 | case Rule_Validator::IS_NOT_EQUAL_TO: |
75 | 75 | return $post_type !== $operand_2; |
76 | 76 |
@@ -18,43 +18,43 @@ |
||
18 | 18 | */ |
19 | 19 | class Rule_Validators_Registry { |
20 | 20 | |
21 | - /** |
|
22 | - * An array of {@link Rule_Validator}s. |
|
23 | - * |
|
24 | - * @var array An array of {@link Rule_Validator}s. |
|
25 | - */ |
|
26 | - private $rule_validators; |
|
27 | - |
|
28 | - /** |
|
29 | - * Rule_Validators_Registry constructor. |
|
30 | - * |
|
31 | - * @param Rule_Validator $default The default rule validator. |
|
32 | - * |
|
33 | - * @throws Exception throws an exception if an invalid validator has been provided. |
|
34 | - */ |
|
35 | - public function __construct( $default ) { |
|
36 | - |
|
37 | - // Check that a valid validator has been provided. |
|
38 | - if ( ! ( $default instanceof Rule_Validator ) ) { |
|
39 | - throw new Exception( 'An invalid Rule_Validator was provided as default validator.' ); |
|
40 | - } |
|
41 | - |
|
42 | - // Allow 3rd parties to register other validators. |
|
43 | - $this->rule_validators = apply_filters( 'wl_mappings_rule_validators', array( '__default__' => $default ) ); |
|
44 | - |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Get a rule validator by its key. |
|
49 | - * |
|
50 | - * @param string $key A key uniquely identifying a validator. |
|
51 | - * |
|
52 | - * @return Rule_Validator A {@link Rule_Validator} instance or the default one when not found. |
|
53 | - */ |
|
54 | - public function get_rule_validator( $key ) { |
|
55 | - |
|
56 | - return isset( $this->rule_validators[ $key ] ) |
|
57 | - ? $this->rule_validators[ $key ] : $this->rule_validators['__default__']; |
|
58 | - } |
|
21 | + /** |
|
22 | + * An array of {@link Rule_Validator}s. |
|
23 | + * |
|
24 | + * @var array An array of {@link Rule_Validator}s. |
|
25 | + */ |
|
26 | + private $rule_validators; |
|
27 | + |
|
28 | + /** |
|
29 | + * Rule_Validators_Registry constructor. |
|
30 | + * |
|
31 | + * @param Rule_Validator $default The default rule validator. |
|
32 | + * |
|
33 | + * @throws Exception throws an exception if an invalid validator has been provided. |
|
34 | + */ |
|
35 | + public function __construct( $default ) { |
|
36 | + |
|
37 | + // Check that a valid validator has been provided. |
|
38 | + if ( ! ( $default instanceof Rule_Validator ) ) { |
|
39 | + throw new Exception( 'An invalid Rule_Validator was provided as default validator.' ); |
|
40 | + } |
|
41 | + |
|
42 | + // Allow 3rd parties to register other validators. |
|
43 | + $this->rule_validators = apply_filters( 'wl_mappings_rule_validators', array( '__default__' => $default ) ); |
|
44 | + |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Get a rule validator by its key. |
|
49 | + * |
|
50 | + * @param string $key A key uniquely identifying a validator. |
|
51 | + * |
|
52 | + * @return Rule_Validator A {@link Rule_Validator} instance or the default one when not found. |
|
53 | + */ |
|
54 | + public function get_rule_validator( $key ) { |
|
55 | + |
|
56 | + return isset( $this->rule_validators[ $key ] ) |
|
57 | + ? $this->rule_validators[ $key ] : $this->rule_validators['__default__']; |
|
58 | + } |
|
59 | 59 | |
60 | 60 | } |
@@ -32,15 +32,15 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @throws Exception throws an exception if an invalid validator has been provided. |
34 | 34 | */ |
35 | - public function __construct( $default ) { |
|
35 | + public function __construct($default) { |
|
36 | 36 | |
37 | 37 | // Check that a valid validator has been provided. |
38 | - if ( ! ( $default instanceof Rule_Validator ) ) { |
|
39 | - throw new Exception( 'An invalid Rule_Validator was provided as default validator.' ); |
|
38 | + if ( ! ($default instanceof Rule_Validator)) { |
|
39 | + throw new Exception('An invalid Rule_Validator was provided as default validator.'); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | // Allow 3rd parties to register other validators. |
43 | - $this->rule_validators = apply_filters( 'wl_mappings_rule_validators', array( '__default__' => $default ) ); |
|
43 | + $this->rule_validators = apply_filters('wl_mappings_rule_validators', array('__default__' => $default)); |
|
44 | 44 | |
45 | 45 | } |
46 | 46 | |
@@ -51,10 +51,10 @@ discard block |
||
51 | 51 | * |
52 | 52 | * @return Rule_Validator A {@link Rule_Validator} instance or the default one when not found. |
53 | 53 | */ |
54 | - public function get_rule_validator( $key ) { |
|
54 | + public function get_rule_validator($key) { |
|
55 | 55 | |
56 | - return isset( $this->rule_validators[ $key ] ) |
|
57 | - ? $this->rule_validators[ $key ] : $this->rule_validators['__default__']; |
|
56 | + return isset($this->rule_validators[$key]) |
|
57 | + ? $this->rule_validators[$key] : $this->rule_validators['__default__']; |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | } |
@@ -15,60 +15,60 @@ |
||
15 | 15 | * @package Wordlift\Mappings\Validators |
16 | 16 | */ |
17 | 17 | class Post_Taxonomy_Term_Rule_Validator implements Rule_Validator { |
18 | - /** |
|
19 | - * @since 3.25.0 |
|
20 | - * Enum for the post taxonomy type rule validator. |
|
21 | - */ |
|
22 | - const POST_TAXONOMY = 'post_taxonomy'; |
|
18 | + /** |
|
19 | + * @since 3.25.0 |
|
20 | + * Enum for the post taxonomy type rule validator. |
|
21 | + */ |
|
22 | + const POST_TAXONOMY = 'post_taxonomy'; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Post_Taxonomy_Term_Rule_Validator constructor. |
|
26 | - * |
|
27 | - * When initializing the class hooks to `wl_mappings_rule_validators`. |
|
28 | - */ |
|
29 | - public function __construct() { |
|
24 | + /** |
|
25 | + * Post_Taxonomy_Term_Rule_Validator constructor. |
|
26 | + * |
|
27 | + * When initializing the class hooks to `wl_mappings_rule_validators`. |
|
28 | + */ |
|
29 | + public function __construct() { |
|
30 | 30 | |
31 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
31 | + add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
32 | 32 | |
33 | - } |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
37 | - * |
|
38 | - * @param array $value An array with validators. |
|
39 | - * |
|
40 | - * @return array An array with validators plus ours. |
|
41 | - */ |
|
42 | - public function wl_mappings_rule_validators( $value ) { |
|
35 | + /** |
|
36 | + * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
37 | + * |
|
38 | + * @param array $value An array with validators. |
|
39 | + * |
|
40 | + * @return array An array with validators plus ours. |
|
41 | + */ |
|
42 | + public function wl_mappings_rule_validators( $value ) { |
|
43 | 43 | |
44 | - $value[ self::POST_TAXONOMY ] = $this; |
|
44 | + $value[ self::POST_TAXONOMY ] = $this; |
|
45 | 45 | |
46 | - return $value; |
|
47 | - } |
|
46 | + return $value; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * {@inheritdoc} |
|
51 | - */ |
|
52 | - public function get_label() { |
|
53 | - return __( 'Post Taxonomy', 'wordlift' ); |
|
54 | - } |
|
49 | + /** |
|
50 | + * {@inheritdoc} |
|
51 | + */ |
|
52 | + public function get_label() { |
|
53 | + return __( 'Post Taxonomy', 'wordlift' ); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * {@inheritdoc} |
|
58 | - */ |
|
59 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
60 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
61 | - $taxonomy = $operand_1; |
|
62 | - $term_slug = $operand_2; |
|
63 | - if ( get_post_type( $identifier ) !== 'post' ) { |
|
64 | - return false; |
|
65 | - } |
|
66 | - $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
67 | - if ( is_wp_error( $is_object_in_term ) ) { |
|
68 | - return false; |
|
69 | - } |
|
56 | + /** |
|
57 | + * {@inheritdoc} |
|
58 | + */ |
|
59 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
60 | + public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
61 | + $taxonomy = $operand_1; |
|
62 | + $term_slug = $operand_2; |
|
63 | + if ( get_post_type( $identifier ) !== 'post' ) { |
|
64 | + return false; |
|
65 | + } |
|
66 | + $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
67 | + if ( is_wp_error( $is_object_in_term ) ) { |
|
68 | + return false; |
|
69 | + } |
|
70 | 70 | |
71 | - return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
72 | - || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
73 | - } |
|
71 | + return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
72 | + || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
73 | + } |
|
74 | 74 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | */ |
29 | 29 | public function __construct() { |
30 | 30 | |
31 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
31 | + add_filter('wl_mappings_rule_validators', array($this, 'wl_mappings_rule_validators')); |
|
32 | 32 | |
33 | 33 | } |
34 | 34 | |
@@ -39,9 +39,9 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @return array An array with validators plus ours. |
41 | 41 | */ |
42 | - public function wl_mappings_rule_validators( $value ) { |
|
42 | + public function wl_mappings_rule_validators($value) { |
|
43 | 43 | |
44 | - $value[ self::POST_TAXONOMY ] = $this; |
|
44 | + $value[self::POST_TAXONOMY] = $this; |
|
45 | 45 | |
46 | 46 | return $value; |
47 | 47 | } |
@@ -50,25 +50,25 @@ discard block |
||
50 | 50 | * {@inheritdoc} |
51 | 51 | */ |
52 | 52 | public function get_label() { |
53 | - return __( 'Post Taxonomy', 'wordlift' ); |
|
53 | + return __('Post Taxonomy', 'wordlift'); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
57 | 57 | * {@inheritdoc} |
58 | 58 | */ |
59 | 59 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
60 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
60 | + public function is_valid($identifier, $operator, $operand_1, $operand_2, $type) { |
|
61 | 61 | $taxonomy = $operand_1; |
62 | 62 | $term_slug = $operand_2; |
63 | - if ( get_post_type( $identifier ) !== 'post' ) { |
|
63 | + if (get_post_type($identifier) !== 'post') { |
|
64 | 64 | return false; |
65 | 65 | } |
66 | - $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
67 | - if ( is_wp_error( $is_object_in_term ) ) { |
|
66 | + $is_object_in_term = is_object_in_term($identifier, $taxonomy, $term_slug); |
|
67 | + if (is_wp_error($is_object_in_term)) { |
|
68 | 68 | return false; |
69 | 69 | } |
70 | 70 | |
71 | - return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
72 | - || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
71 | + return ($is_object_in_term && self::IS_EQUAL_TO === $operator) |
|
72 | + || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator); |
|
73 | 73 | } |
74 | 74 | } |
@@ -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 $identifier The post id or term id. |
|
44 | - * @param array $rule_groups An array of rules' groups. |
|
45 | - * @param $type string Post or term |
|
46 | - * |
|
47 | - * @return bool Whether the post passes at least one rule group. |
|
48 | - */ |
|
49 | - public function is_valid( $identifier, $rule_groups, $type ) { |
|
40 | + /** |
|
41 | + * Check whether the specified post passes at least one group of rules. |
|
42 | + * |
|
43 | + * @param int $identifier The post id or term id. |
|
44 | + * @param array $rule_groups An array of rules' groups. |
|
45 | + * @param $type string Post or term |
|
46 | + * |
|
47 | + * @return bool Whether the post passes at least one rule group. |
|
48 | + */ |
|
49 | + public function is_valid( $identifier, $rule_groups, $type ) { |
|
50 | 50 | |
51 | - // Validate each group. Return true as soon as one group is validated (all rules). |
|
52 | - foreach ( (array) $rule_groups as $rule_group ) { |
|
53 | - foreach ( $rule_group['rules'] as $rule ) { |
|
54 | - $rule_field_one = $rule['rule_field_one']; |
|
55 | - $rule_logic_field = $rule['rule_logic_field']; |
|
56 | - $rule_field_two = $rule['rule_field_two']; |
|
51 | + // Validate each group. Return true as soon as one group is validated (all rules). |
|
52 | + foreach ( (array) $rule_groups as $rule_group ) { |
|
53 | + foreach ( $rule_group['rules'] as $rule ) { |
|
54 | + $rule_field_one = $rule['rule_field_one']; |
|
55 | + $rule_logic_field = $rule['rule_logic_field']; |
|
56 | + $rule_field_two = $rule['rule_field_two']; |
|
57 | 57 | |
58 | - $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
59 | - // Skip to the next Rule Group if a rule isn't valid. |
|
60 | - if ( ! $rule_validator->is_valid( $identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type ) ) { |
|
61 | - continue 2; |
|
62 | - } |
|
63 | - } |
|
58 | + $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
59 | + // Skip to the next Rule Group if a rule isn't valid. |
|
60 | + if ( ! $rule_validator->is_valid( $identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type ) ) { |
|
61 | + continue 2; |
|
62 | + } |
|
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 | } |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * |
32 | 32 | * @param Rule_Validators_Registry $rule_validators_registry |
33 | 33 | */ |
34 | - public function __construct( $rule_validators_registry ) { |
|
34 | + public function __construct($rule_validators_registry) { |
|
35 | 35 | |
36 | 36 | $this->rule_validators_registry = $rule_validators_registry; |
37 | 37 | |
@@ -46,18 +46,18 @@ discard block |
||
46 | 46 | * |
47 | 47 | * @return bool Whether the post passes at least one rule group. |
48 | 48 | */ |
49 | - public function is_valid( $identifier, $rule_groups, $type ) { |
|
49 | + public function is_valid($identifier, $rule_groups, $type) { |
|
50 | 50 | |
51 | 51 | // Validate each group. Return true as soon as one group is validated (all rules). |
52 | - foreach ( (array) $rule_groups as $rule_group ) { |
|
53 | - foreach ( $rule_group['rules'] as $rule ) { |
|
52 | + foreach ((array) $rule_groups as $rule_group) { |
|
53 | + foreach ($rule_group['rules'] as $rule) { |
|
54 | 54 | $rule_field_one = $rule['rule_field_one']; |
55 | 55 | $rule_logic_field = $rule['rule_logic_field']; |
56 | 56 | $rule_field_two = $rule['rule_field_two']; |
57 | 57 | |
58 | - $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
58 | + $rule_validator = $this->rule_validators_registry->get_rule_validator($rule_field_one); |
|
59 | 59 | // Skip to the next Rule Group if a rule isn't valid. |
60 | - if ( ! $rule_validator->is_valid( $identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type ) ) { |
|
60 | + if ( ! $rule_validator->is_valid($identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type)) { |
|
61 | 61 | continue 2; |
62 | 62 | } |
63 | 63 | } |
@@ -17,58 +17,58 @@ |
||
17 | 17 | */ |
18 | 18 | class Taxonomy_Term_Rule_Validator implements Rule_Validator { |
19 | 19 | |
20 | - const TAXONOMY = 'taxonomy'; |
|
20 | + const TAXONOMY = 'taxonomy'; |
|
21 | 21 | |
22 | - public function __construct() { |
|
23 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
24 | - } |
|
22 | + public function __construct() { |
|
23 | + add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
28 | - * |
|
29 | - * @param array $value An array with validators. |
|
30 | - * |
|
31 | - * @return array An array with validators plus ours. |
|
32 | - */ |
|
33 | - public function wl_mappings_rule_validators( $value ) { |
|
34 | - $value[ self::TAXONOMY ] = $this; |
|
26 | + /** |
|
27 | + * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
28 | + * |
|
29 | + * @param array $value An array with validators. |
|
30 | + * |
|
31 | + * @return array An array with validators plus ours. |
|
32 | + */ |
|
33 | + public function wl_mappings_rule_validators( $value ) { |
|
34 | + $value[ self::TAXONOMY ] = $this; |
|
35 | 35 | |
36 | - return $value; |
|
37 | - } |
|
36 | + return $value; |
|
37 | + } |
|
38 | 38 | |
39 | - public function get_label() { |
|
40 | - return __( 'TaxonomyTerm', 'wordlift' ); |
|
41 | - } |
|
39 | + public function get_label() { |
|
40 | + return __( 'TaxonomyTerm', 'wordlift' ); |
|
41 | + } |
|
42 | 42 | |
43 | - public function is_valid( $identifier, $operator, $operand_1, $taxonomy, $type ) { |
|
44 | - if ( Jsonld_Converter::TERM !== $type ) { |
|
45 | - return false; |
|
46 | - } |
|
47 | - $current_term = get_term( $identifier ); |
|
48 | - // If it is not a term page, then return false for two operators. |
|
49 | - if ( ! $current_term instanceof \WP_Term ) { |
|
50 | - return false; |
|
51 | - } |
|
52 | - $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
53 | - $terms = array_map( |
|
54 | - function ( $term ) { |
|
55 | - /** |
|
56 | - * @var $term \WP_Term |
|
57 | - */ |
|
58 | - return $term->term_id; |
|
59 | - }, |
|
60 | - $terms |
|
61 | - ); |
|
62 | - if ( Rule_Validator::IS_EQUAL_TO === $operator ) { |
|
63 | - // if we dont have term id, then skip the flow. |
|
64 | - // If we are in term page, then we need to check if the current |
|
65 | - // term belongs to the taxonomy |
|
66 | - return in_array( $current_term->term_id, $terms, true ); |
|
67 | - } |
|
43 | + public function is_valid( $identifier, $operator, $operand_1, $taxonomy, $type ) { |
|
44 | + if ( Jsonld_Converter::TERM !== $type ) { |
|
45 | + return false; |
|
46 | + } |
|
47 | + $current_term = get_term( $identifier ); |
|
48 | + // If it is not a term page, then return false for two operators. |
|
49 | + if ( ! $current_term instanceof \WP_Term ) { |
|
50 | + return false; |
|
51 | + } |
|
52 | + $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
53 | + $terms = array_map( |
|
54 | + function ( $term ) { |
|
55 | + /** |
|
56 | + * @var $term \WP_Term |
|
57 | + */ |
|
58 | + return $term->term_id; |
|
59 | + }, |
|
60 | + $terms |
|
61 | + ); |
|
62 | + if ( Rule_Validator::IS_EQUAL_TO === $operator ) { |
|
63 | + // if we dont have term id, then skip the flow. |
|
64 | + // If we are in term page, then we need to check if the current |
|
65 | + // term belongs to the taxonomy |
|
66 | + return in_array( $current_term->term_id, $terms, true ); |
|
67 | + } |
|
68 | 68 | |
69 | - if ( Rule_Validator::IS_NOT_EQUAL_TO === $operator ) { |
|
70 | - return ! in_array( $current_term->term_id, $terms, true ); |
|
71 | - } |
|
69 | + if ( Rule_Validator::IS_NOT_EQUAL_TO === $operator ) { |
|
70 | + return ! in_array( $current_term->term_id, $terms, true ); |
|
71 | + } |
|
72 | 72 | |
73 | - } |
|
73 | + } |
|
74 | 74 | } |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | const TAXONOMY = 'taxonomy'; |
21 | 21 | |
22 | 22 | public function __construct() { |
23 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
23 | + add_filter('wl_mappings_rule_validators', array($this, 'wl_mappings_rule_validators')); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
@@ -30,28 +30,28 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return array An array with validators plus ours. |
32 | 32 | */ |
33 | - public function wl_mappings_rule_validators( $value ) { |
|
34 | - $value[ self::TAXONOMY ] = $this; |
|
33 | + public function wl_mappings_rule_validators($value) { |
|
34 | + $value[self::TAXONOMY] = $this; |
|
35 | 35 | |
36 | 36 | return $value; |
37 | 37 | } |
38 | 38 | |
39 | 39 | public function get_label() { |
40 | - return __( 'TaxonomyTerm', 'wordlift' ); |
|
40 | + return __('TaxonomyTerm', 'wordlift'); |
|
41 | 41 | } |
42 | 42 | |
43 | - public function is_valid( $identifier, $operator, $operand_1, $taxonomy, $type ) { |
|
44 | - if ( Jsonld_Converter::TERM !== $type ) { |
|
43 | + public function is_valid($identifier, $operator, $operand_1, $taxonomy, $type) { |
|
44 | + if (Jsonld_Converter::TERM !== $type) { |
|
45 | 45 | return false; |
46 | 46 | } |
47 | - $current_term = get_term( $identifier ); |
|
47 | + $current_term = get_term($identifier); |
|
48 | 48 | // If it is not a term page, then return false for two operators. |
49 | - if ( ! $current_term instanceof \WP_Term ) { |
|
49 | + if ( ! $current_term instanceof \WP_Term) { |
|
50 | 50 | return false; |
51 | 51 | } |
52 | - $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
52 | + $terms = get_terms($taxonomy, array('get' => 'all')); |
|
53 | 53 | $terms = array_map( |
54 | - function ( $term ) { |
|
54 | + function($term) { |
|
55 | 55 | /** |
56 | 56 | * @var $term \WP_Term |
57 | 57 | */ |
@@ -59,15 +59,15 @@ discard block |
||
59 | 59 | }, |
60 | 60 | $terms |
61 | 61 | ); |
62 | - if ( Rule_Validator::IS_EQUAL_TO === $operator ) { |
|
62 | + if (Rule_Validator::IS_EQUAL_TO === $operator) { |
|
63 | 63 | // if we dont have term id, then skip the flow. |
64 | 64 | // If we are in term page, then we need to check if the current |
65 | 65 | // term belongs to the taxonomy |
66 | - return in_array( $current_term->term_id, $terms, true ); |
|
66 | + return in_array($current_term->term_id, $terms, true); |
|
67 | 67 | } |
68 | 68 | |
69 | - if ( Rule_Validator::IS_NOT_EQUAL_TO === $operator ) { |
|
70 | - return ! in_array( $current_term->term_id, $terms, true ); |
|
69 | + if (Rule_Validator::IS_NOT_EQUAL_TO === $operator) { |
|
70 | + return ! in_array($current_term->term_id, $terms, true); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | } |
@@ -16,29 +16,29 @@ |
||
16 | 16 | */ |
17 | 17 | class Taxonomy_Rule_Validator implements Rule_Validator { |
18 | 18 | |
19 | - /** |
|
20 | - * {@inheritdoc} |
|
21 | - */ |
|
22 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
23 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
24 | - |
|
25 | - $taxonomy = $operand_1; |
|
26 | - $term_slug = $operand_2; |
|
27 | - |
|
28 | - $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
29 | - |
|
30 | - if ( is_wp_error( $is_object_in_term ) ) { |
|
31 | - return false; |
|
32 | - } |
|
33 | - |
|
34 | - return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
35 | - || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * {@inheritdoc} |
|
40 | - */ |
|
41 | - public function get_label() { |
|
42 | - return __( 'Taxonomy', 'wordlift' ); |
|
43 | - } |
|
19 | + /** |
|
20 | + * {@inheritdoc} |
|
21 | + */ |
|
22 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
23 | + public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
24 | + |
|
25 | + $taxonomy = $operand_1; |
|
26 | + $term_slug = $operand_2; |
|
27 | + |
|
28 | + $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
29 | + |
|
30 | + if ( is_wp_error( $is_object_in_term ) ) { |
|
31 | + return false; |
|
32 | + } |
|
33 | + |
|
34 | + return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
35 | + || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * {@inheritdoc} |
|
40 | + */ |
|
41 | + public function get_label() { |
|
42 | + return __( 'Taxonomy', 'wordlift' ); |
|
43 | + } |
|
44 | 44 | } |
@@ -20,25 +20,25 @@ |
||
20 | 20 | * {@inheritdoc} |
21 | 21 | */ |
22 | 22 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
23 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
23 | + public function is_valid($identifier, $operator, $operand_1, $operand_2, $type) { |
|
24 | 24 | |
25 | 25 | $taxonomy = $operand_1; |
26 | 26 | $term_slug = $operand_2; |
27 | 27 | |
28 | - $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
28 | + $is_object_in_term = is_object_in_term($identifier, $taxonomy, $term_slug); |
|
29 | 29 | |
30 | - if ( is_wp_error( $is_object_in_term ) ) { |
|
30 | + if (is_wp_error($is_object_in_term)) { |
|
31 | 31 | return false; |
32 | 32 | } |
33 | 33 | |
34 | - return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
35 | - || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
34 | + return ($is_object_in_term && self::IS_EQUAL_TO === $operator) |
|
35 | + || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | /** |
39 | 39 | * {@inheritdoc} |
40 | 40 | */ |
41 | 41 | public function get_label() { |
42 | - return __( 'Taxonomy', 'wordlift' ); |
|
42 | + return __('Taxonomy', 'wordlift'); |
|
43 | 43 | } |
44 | 44 | } |