@@ -8,23 +8,23 @@ discard block |
||
8 | 8 | */ |
9 | 9 | class Top_Entities { |
10 | 10 | |
11 | - const CRON_ACTION = 'wl_admin_dashboard_top_entities'; |
|
11 | + const CRON_ACTION = 'wl_admin_dashboard_top_entities'; |
|
12 | 12 | |
13 | - /** |
|
14 | - * Option key where the top entities data is stored. |
|
15 | - */ |
|
16 | - const OPTION_KEY = 'wl_admin_dashboard_top_entities_option'; |
|
13 | + /** |
|
14 | + * Option key where the top entities data is stored. |
|
15 | + */ |
|
16 | + const OPTION_KEY = 'wl_admin_dashboard_top_entities_option'; |
|
17 | 17 | |
18 | - public function __construct() { |
|
19 | - add_action( self::CRON_ACTION, array( $this, 'save_top_entities' ) ); |
|
20 | - } |
|
18 | + public function __construct() { |
|
19 | + add_action( self::CRON_ACTION, array( $this, 'save_top_entities' ) ); |
|
20 | + } |
|
21 | 21 | |
22 | - public function save_top_entities() { |
|
22 | + public function save_top_entities() { |
|
23 | 23 | |
24 | - global $wpdb; |
|
24 | + global $wpdb; |
|
25 | 25 | |
26 | - $results = $wpdb->get_results( |
|
27 | - " |
|
26 | + $results = $wpdb->get_results( |
|
27 | + " |
|
28 | 28 | SELECT p_as_object.ID |
29 | 29 | , p_as_object.post_title |
30 | 30 | , COALESCE(SUM(CASE WHEN t_as_subject.slug IS NULL THEN 1 END), 0) entities |
@@ -49,20 +49,20 @@ discard block |
||
49 | 49 | ORDER BY total DESC |
50 | 50 | LIMIT 20 |
51 | 51 | " |
52 | - ); |
|
52 | + ); |
|
53 | 53 | |
54 | - update_option( self::OPTION_KEY, $results ); |
|
55 | - } |
|
54 | + update_option( self::OPTION_KEY, $results ); |
|
55 | + } |
|
56 | 56 | |
57 | - public static function activate() { |
|
58 | - if ( ! wp_next_scheduled( self::CRON_ACTION ) ) { |
|
59 | - wp_schedule_event( time(), 'daily', self::CRON_ACTION ); |
|
60 | - } |
|
61 | - } |
|
57 | + public static function activate() { |
|
58 | + if ( ! wp_next_scheduled( self::CRON_ACTION ) ) { |
|
59 | + wp_schedule_event( time(), 'daily', self::CRON_ACTION ); |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - public static function deactivate() { |
|
64 | - $timestamp = wp_next_scheduled( self::CRON_ACTION ); |
|
65 | - wp_unschedule_event( $timestamp, self::CRON_ACTION ); |
|
66 | - } |
|
63 | + public static function deactivate() { |
|
64 | + $timestamp = wp_next_scheduled( self::CRON_ACTION ); |
|
65 | + wp_unschedule_event( $timestamp, self::CRON_ACTION ); |
|
66 | + } |
|
67 | 67 | |
68 | 68 | } |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | const OPTION_KEY = 'wl_admin_dashboard_top_entities_option'; |
17 | 17 | |
18 | 18 | public function __construct() { |
19 | - add_action( self::CRON_ACTION, array( $this, 'save_top_entities' ) ); |
|
19 | + add_action(self::CRON_ACTION, array($this, 'save_top_entities')); |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | public function save_top_entities() { |
@@ -51,18 +51,18 @@ discard block |
||
51 | 51 | " |
52 | 52 | ); |
53 | 53 | |
54 | - update_option( self::OPTION_KEY, $results ); |
|
54 | + update_option(self::OPTION_KEY, $results); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | public static function activate() { |
58 | - if ( ! wp_next_scheduled( self::CRON_ACTION ) ) { |
|
59 | - wp_schedule_event( time(), 'daily', self::CRON_ACTION ); |
|
58 | + if ( ! wp_next_scheduled(self::CRON_ACTION)) { |
|
59 | + wp_schedule_event(time(), 'daily', self::CRON_ACTION); |
|
60 | 60 | } |
61 | 61 | } |
62 | 62 | |
63 | 63 | public static function deactivate() { |
64 | - $timestamp = wp_next_scheduled( self::CRON_ACTION ); |
|
65 | - wp_unschedule_event( $timestamp, self::CRON_ACTION ); |
|
64 | + $timestamp = wp_next_scheduled(self::CRON_ACTION); |
|
65 | + wp_unschedule_event($timestamp, self::CRON_ACTION); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | } |
@@ -8,55 +8,55 @@ |
||
8 | 8 | */ |
9 | 9 | class Admin_User_Option { |
10 | 10 | |
11 | - const WORDLIFT_ADMIN = 'wl_is_wordlift_admin'; |
|
12 | - |
|
13 | - public static function is_wordlift_admin() { |
|
14 | - return intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
15 | - } |
|
16 | - |
|
17 | - public function connect_hook() { |
|
18 | - add_action( 'wordlift_user_settings_page', array( $this, 'render_checkbox' ) ); |
|
19 | - add_action( 'edit_user_profile_update', array( $this, 'save_checkbox' ) ); |
|
20 | - add_action( 'personal_options_update', array( $this, 'save_checkbox' ) ); |
|
21 | - } |
|
22 | - |
|
23 | - public function save_checkbox() { |
|
24 | - |
|
25 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
26 | - return; |
|
27 | - } |
|
28 | - |
|
29 | - if ( ! isset( $_POST[ self::WORDLIFT_ADMIN ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
30 | - delete_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN ); |
|
31 | - |
|
32 | - return; |
|
33 | - } |
|
34 | - update_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, 1 ); |
|
35 | - } |
|
36 | - |
|
37 | - public function render_checkbox() { |
|
38 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
39 | - return; |
|
40 | - } |
|
41 | - $is_checked = intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
42 | - $allowed_html = array( |
|
43 | - 'tr' => array(), |
|
44 | - 'th' => array(), |
|
45 | - 'td' => array(), |
|
46 | - 'input' => array( |
|
47 | - 'type' => array(), |
|
48 | - 'name' => array(), |
|
49 | - 'checked' => array(), |
|
50 | - ), |
|
51 | - ); |
|
52 | - echo wp_kses( $this->get_checkbox( $is_checked ), $allowed_html ); |
|
53 | - } |
|
54 | - |
|
55 | - public static function get_checkbox( $is_checked ) { |
|
56 | - $checked = $is_checked ? "checked='checked'" : ''; |
|
57 | - $admin_text = __( 'Wordlift Admin', 'wordlift' ); |
|
58 | - |
|
59 | - return "<tr><th>$admin_text</th><td><input type='checkbox' name='wl_is_wordlift_admin' $checked></td></tr>"; |
|
60 | - } |
|
11 | + const WORDLIFT_ADMIN = 'wl_is_wordlift_admin'; |
|
12 | + |
|
13 | + public static function is_wordlift_admin() { |
|
14 | + return intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
15 | + } |
|
16 | + |
|
17 | + public function connect_hook() { |
|
18 | + add_action( 'wordlift_user_settings_page', array( $this, 'render_checkbox' ) ); |
|
19 | + add_action( 'edit_user_profile_update', array( $this, 'save_checkbox' ) ); |
|
20 | + add_action( 'personal_options_update', array( $this, 'save_checkbox' ) ); |
|
21 | + } |
|
22 | + |
|
23 | + public function save_checkbox() { |
|
24 | + |
|
25 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
26 | + return; |
|
27 | + } |
|
28 | + |
|
29 | + if ( ! isset( $_POST[ self::WORDLIFT_ADMIN ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
30 | + delete_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN ); |
|
31 | + |
|
32 | + return; |
|
33 | + } |
|
34 | + update_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, 1 ); |
|
35 | + } |
|
36 | + |
|
37 | + public function render_checkbox() { |
|
38 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
39 | + return; |
|
40 | + } |
|
41 | + $is_checked = intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
42 | + $allowed_html = array( |
|
43 | + 'tr' => array(), |
|
44 | + 'th' => array(), |
|
45 | + 'td' => array(), |
|
46 | + 'input' => array( |
|
47 | + 'type' => array(), |
|
48 | + 'name' => array(), |
|
49 | + 'checked' => array(), |
|
50 | + ), |
|
51 | + ); |
|
52 | + echo wp_kses( $this->get_checkbox( $is_checked ), $allowed_html ); |
|
53 | + } |
|
54 | + |
|
55 | + public static function get_checkbox( $is_checked ) { |
|
56 | + $checked = $is_checked ? "checked='checked'" : ''; |
|
57 | + $admin_text = __( 'Wordlift Admin', 'wordlift' ); |
|
58 | + |
|
59 | + return "<tr><th>$admin_text</th><td><input type='checkbox' name='wl_is_wordlift_admin' $checked></td></tr>"; |
|
60 | + } |
|
61 | 61 | |
62 | 62 | } |
@@ -11,34 +11,34 @@ discard block |
||
11 | 11 | const WORDLIFT_ADMIN = 'wl_is_wordlift_admin'; |
12 | 12 | |
13 | 13 | public static function is_wordlift_admin() { |
14 | - return intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
14 | + return intval(get_user_meta(get_current_user_id(), self::WORDLIFT_ADMIN, true)) === 1; |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | public function connect_hook() { |
18 | - add_action( 'wordlift_user_settings_page', array( $this, 'render_checkbox' ) ); |
|
19 | - add_action( 'edit_user_profile_update', array( $this, 'save_checkbox' ) ); |
|
20 | - add_action( 'personal_options_update', array( $this, 'save_checkbox' ) ); |
|
18 | + add_action('wordlift_user_settings_page', array($this, 'render_checkbox')); |
|
19 | + add_action('edit_user_profile_update', array($this, 'save_checkbox')); |
|
20 | + add_action('personal_options_update', array($this, 'save_checkbox')); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | public function save_checkbox() { |
24 | 24 | |
25 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
25 | + if ( ! current_user_can('manage_options')) { |
|
26 | 26 | return; |
27 | 27 | } |
28 | 28 | |
29 | - if ( ! isset( $_POST[ self::WORDLIFT_ADMIN ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
30 | - delete_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN ); |
|
29 | + if ( ! isset($_POST[self::WORDLIFT_ADMIN])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
30 | + delete_user_meta(get_current_user_id(), self::WORDLIFT_ADMIN); |
|
31 | 31 | |
32 | 32 | return; |
33 | 33 | } |
34 | - update_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, 1 ); |
|
34 | + update_user_meta(get_current_user_id(), self::WORDLIFT_ADMIN, 1); |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | public function render_checkbox() { |
38 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
38 | + if ( ! current_user_can('manage_options')) { |
|
39 | 39 | return; |
40 | 40 | } |
41 | - $is_checked = intval( get_user_meta( get_current_user_id(), self::WORDLIFT_ADMIN, true ) ) === 1; |
|
41 | + $is_checked = intval(get_user_meta(get_current_user_id(), self::WORDLIFT_ADMIN, true)) === 1; |
|
42 | 42 | $allowed_html = array( |
43 | 43 | 'tr' => array(), |
44 | 44 | 'th' => array(), |
@@ -49,12 +49,12 @@ discard block |
||
49 | 49 | 'checked' => array(), |
50 | 50 | ), |
51 | 51 | ); |
52 | - echo wp_kses( $this->get_checkbox( $is_checked ), $allowed_html ); |
|
52 | + echo wp_kses($this->get_checkbox($is_checked), $allowed_html); |
|
53 | 53 | } |
54 | 54 | |
55 | - public static function get_checkbox( $is_checked ) { |
|
55 | + public static function get_checkbox($is_checked) { |
|
56 | 56 | $checked = $is_checked ? "checked='checked'" : ''; |
57 | - $admin_text = __( 'Wordlift Admin', 'wordlift' ); |
|
57 | + $admin_text = __('Wordlift Admin', 'wordlift'); |
|
58 | 58 | |
59 | 59 | return "<tr><th>$admin_text</th><td><input type='checkbox' name='wl_is_wordlift_admin' $checked></td></tr>"; |
60 | 60 | } |
@@ -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 |
@@ -3,188 +3,188 @@ |
||
3 | 3 | namespace Wordlift\Configuration; |
4 | 4 | |
5 | 5 | class Config { |
6 | - /** |
|
7 | - * @var \Wordlift_Admin_Setup |
|
8 | - */ |
|
9 | - private $admin_setup; |
|
10 | - /** |
|
11 | - * @var \Wordlift_Key_Validation_Service |
|
12 | - */ |
|
13 | - private $key_validation_service; |
|
14 | - |
|
15 | - /** |
|
16 | - * Config constructor. |
|
17 | - * |
|
18 | - * @param $admin_setup \Wordlift_Admin_Setup |
|
19 | - * @param $key_validation_service \Wordlift_Key_Validation_Service |
|
20 | - */ |
|
21 | - public function __construct( $admin_setup, $key_validation_service ) { |
|
22 | - |
|
23 | - $this->admin_setup = $admin_setup; |
|
24 | - $this->key_validation_service = $key_validation_service; |
|
25 | - add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
26 | - |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Check if the key is valid and also not bound to any domain. |
|
31 | - * |
|
32 | - * @param $key string |
|
33 | - * |
|
34 | - * @return bool |
|
35 | - */ |
|
36 | - private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
37 | - $account_info = $this->key_validation_service->get_account_info( $key ); |
|
38 | - |
|
39 | - /** |
|
40 | - * we need to check if the key is not associated with any account |
|
41 | - * before setting it, we should check if the url is null. |
|
42 | - */ |
|
43 | - if ( is_wp_error( $account_info ) |
|
44 | - || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
45 | - return false; |
|
46 | - } |
|
47 | - |
|
48 | - $account_info_json = $account_info['body']; |
|
49 | - |
|
50 | - $account_info_data = json_decode( $account_info_json, true ); |
|
51 | - |
|
52 | - if ( ! $account_info_data ) { |
|
53 | - // Invalid json returned by api. |
|
54 | - return false; |
|
55 | - } |
|
56 | - |
|
57 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
58 | - |
|
59 | - if ( null === $account_info_data['url'] ) { |
|
60 | - return true; |
|
61 | - } |
|
62 | - |
|
63 | - // Check if the key belongs to same site. |
|
64 | - if ( untrailingslashit( $account_info_data['url'] ) !== $site_url ) { |
|
65 | - // key already associated with another account. |
|
66 | - return false; |
|
67 | - } |
|
68 | - |
|
69 | - // Return true if the key domain and site domain are the same. |
|
70 | - return true; |
|
71 | - } |
|
72 | - |
|
73 | - public function config() { |
|
74 | - |
|
75 | - // Perform validation check for all the parameters. |
|
76 | - $required_fields = array( |
|
77 | - 'diagnostic', |
|
78 | - 'vocabulary', |
|
79 | - // Don't ask for language from webapp. |
|
80 | - // 'language', |
|
81 | - 'country', |
|
82 | - 'publisherName', |
|
83 | - 'publisher', |
|
84 | - 'license', |
|
85 | - ); |
|
86 | - |
|
87 | - header( 'Access-Control-Allow-Origin: *' ); |
|
88 | - |
|
89 | - // validate all the fields before processing |
|
90 | - foreach ( $required_fields as $field ) { |
|
91 | - if ( ! array_key_exists( $field, $_POST ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
92 | - /* translators: %s: field name */ |
|
93 | - wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
94 | - |
|
95 | - return; |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - $key = isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
100 | - |
|
101 | - if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
102 | - wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
103 | - |
|
104 | - // exit if not valid. |
|
105 | - return; |
|
106 | - } |
|
107 | - |
|
108 | - // check if key is already configured, if yes then dont save settings. |
|
109 | - if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
110 | - wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
111 | - |
|
112 | - // key already configured |
|
113 | - return; |
|
114 | - } |
|
115 | - |
|
116 | - $this->admin_setup->save_configuration( $this->get_params() ); |
|
117 | - |
|
118 | - wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * |
|
123 | - * @return array |
|
124 | - */ |
|
125 | - private function get_params() { |
|
126 | - |
|
127 | - $attachment_id = $this->may_be_get_attachment_id(); |
|
128 | - |
|
129 | - $params = array( |
|
130 | - 'key' => isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
131 | - 'vocabulary' => isset( $_POST['vocabulary'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['vocabulary'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
132 | - 'wl-country-code' => isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['country'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
133 | - 'name' => isset( $_POST['publisherName'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisherName'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
134 | - 'user_type' => isset( $_POST['publisher'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisher'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
135 | - 'logo' => $attachment_id, |
|
136 | - ); |
|
137 | - |
|
138 | - $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
139 | - if ( $diagnostic ) { |
|
140 | - $params['share-diagnostic'] = 'on'; |
|
141 | - } |
|
142 | - |
|
143 | - return $params; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @return int | bool |
|
148 | - */ |
|
149 | - private function may_be_get_attachment_id() { |
|
150 | - |
|
151 | - // if image or image extension not posted then return false. |
|
152 | - if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
153 | - return false; |
|
154 | - } |
|
155 | - |
|
156 | - $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
157 | - $image_string = sanitize_text_field( wp_unslash( (string) $_POST['image'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
158 | - $image_ext = sanitize_text_field( wp_unslash( (string) $_POST['imageExtension'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
159 | - |
|
160 | - if ( ! in_array( $image_ext, $allowed_extensions, true ) ) { |
|
161 | - return false; |
|
162 | - } |
|
163 | - |
|
164 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
165 | - $image_decoded_string = base64_decode( $image_string ); |
|
166 | - |
|
167 | - $upload_dir = wp_upload_dir(); |
|
168 | - |
|
169 | - $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . '.' . $image_ext; |
|
170 | - |
|
171 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
172 | - file_put_contents( $file_path, $image_decoded_string ); |
|
173 | - |
|
174 | - $attachment_id = wp_insert_attachment( |
|
175 | - array( |
|
176 | - 'post_status' => 'inherit', |
|
177 | - 'post_mime_type' => "image/$image_ext", |
|
178 | - ), |
|
179 | - $file_path |
|
180 | - ); |
|
181 | - |
|
182 | - // Generate the metadata for the attachment, and update the database record. |
|
183 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
184 | - // Update the attachment metadata. |
|
185 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
6 | + /** |
|
7 | + * @var \Wordlift_Admin_Setup |
|
8 | + */ |
|
9 | + private $admin_setup; |
|
10 | + /** |
|
11 | + * @var \Wordlift_Key_Validation_Service |
|
12 | + */ |
|
13 | + private $key_validation_service; |
|
14 | + |
|
15 | + /** |
|
16 | + * Config constructor. |
|
17 | + * |
|
18 | + * @param $admin_setup \Wordlift_Admin_Setup |
|
19 | + * @param $key_validation_service \Wordlift_Key_Validation_Service |
|
20 | + */ |
|
21 | + public function __construct( $admin_setup, $key_validation_service ) { |
|
22 | + |
|
23 | + $this->admin_setup = $admin_setup; |
|
24 | + $this->key_validation_service = $key_validation_service; |
|
25 | + add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
26 | + |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Check if the key is valid and also not bound to any domain. |
|
31 | + * |
|
32 | + * @param $key string |
|
33 | + * |
|
34 | + * @return bool |
|
35 | + */ |
|
36 | + private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
37 | + $account_info = $this->key_validation_service->get_account_info( $key ); |
|
38 | + |
|
39 | + /** |
|
40 | + * we need to check if the key is not associated with any account |
|
41 | + * before setting it, we should check if the url is null. |
|
42 | + */ |
|
43 | + if ( is_wp_error( $account_info ) |
|
44 | + || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
45 | + return false; |
|
46 | + } |
|
47 | + |
|
48 | + $account_info_json = $account_info['body']; |
|
49 | + |
|
50 | + $account_info_data = json_decode( $account_info_json, true ); |
|
51 | + |
|
52 | + if ( ! $account_info_data ) { |
|
53 | + // Invalid json returned by api. |
|
54 | + return false; |
|
55 | + } |
|
56 | + |
|
57 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
58 | + |
|
59 | + if ( null === $account_info_data['url'] ) { |
|
60 | + return true; |
|
61 | + } |
|
62 | + |
|
63 | + // Check if the key belongs to same site. |
|
64 | + if ( untrailingslashit( $account_info_data['url'] ) !== $site_url ) { |
|
65 | + // key already associated with another account. |
|
66 | + return false; |
|
67 | + } |
|
68 | + |
|
69 | + // Return true if the key domain and site domain are the same. |
|
70 | + return true; |
|
71 | + } |
|
72 | + |
|
73 | + public function config() { |
|
74 | + |
|
75 | + // Perform validation check for all the parameters. |
|
76 | + $required_fields = array( |
|
77 | + 'diagnostic', |
|
78 | + 'vocabulary', |
|
79 | + // Don't ask for language from webapp. |
|
80 | + // 'language', |
|
81 | + 'country', |
|
82 | + 'publisherName', |
|
83 | + 'publisher', |
|
84 | + 'license', |
|
85 | + ); |
|
86 | + |
|
87 | + header( 'Access-Control-Allow-Origin: *' ); |
|
88 | + |
|
89 | + // validate all the fields before processing |
|
90 | + foreach ( $required_fields as $field ) { |
|
91 | + if ( ! array_key_exists( $field, $_POST ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
92 | + /* translators: %s: field name */ |
|
93 | + wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
94 | + |
|
95 | + return; |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + $key = isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
100 | + |
|
101 | + if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
102 | + wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
103 | + |
|
104 | + // exit if not valid. |
|
105 | + return; |
|
106 | + } |
|
107 | + |
|
108 | + // check if key is already configured, if yes then dont save settings. |
|
109 | + if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
110 | + wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
111 | + |
|
112 | + // key already configured |
|
113 | + return; |
|
114 | + } |
|
115 | + |
|
116 | + $this->admin_setup->save_configuration( $this->get_params() ); |
|
117 | + |
|
118 | + wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * |
|
123 | + * @return array |
|
124 | + */ |
|
125 | + private function get_params() { |
|
126 | + |
|
127 | + $attachment_id = $this->may_be_get_attachment_id(); |
|
128 | + |
|
129 | + $params = array( |
|
130 | + 'key' => isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
131 | + 'vocabulary' => isset( $_POST['vocabulary'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['vocabulary'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
132 | + 'wl-country-code' => isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['country'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
133 | + 'name' => isset( $_POST['publisherName'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisherName'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
134 | + 'user_type' => isset( $_POST['publisher'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisher'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
135 | + 'logo' => $attachment_id, |
|
136 | + ); |
|
137 | + |
|
138 | + $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
139 | + if ( $diagnostic ) { |
|
140 | + $params['share-diagnostic'] = 'on'; |
|
141 | + } |
|
142 | + |
|
143 | + return $params; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @return int | bool |
|
148 | + */ |
|
149 | + private function may_be_get_attachment_id() { |
|
150 | + |
|
151 | + // if image or image extension not posted then return false. |
|
152 | + if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
153 | + return false; |
|
154 | + } |
|
155 | + |
|
156 | + $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
157 | + $image_string = sanitize_text_field( wp_unslash( (string) $_POST['image'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
158 | + $image_ext = sanitize_text_field( wp_unslash( (string) $_POST['imageExtension'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
159 | + |
|
160 | + if ( ! in_array( $image_ext, $allowed_extensions, true ) ) { |
|
161 | + return false; |
|
162 | + } |
|
163 | + |
|
164 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
165 | + $image_decoded_string = base64_decode( $image_string ); |
|
166 | + |
|
167 | + $upload_dir = wp_upload_dir(); |
|
168 | + |
|
169 | + $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . '.' . $image_ext; |
|
170 | + |
|
171 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
172 | + file_put_contents( $file_path, $image_decoded_string ); |
|
173 | + |
|
174 | + $attachment_id = wp_insert_attachment( |
|
175 | + array( |
|
176 | + 'post_status' => 'inherit', |
|
177 | + 'post_mime_type' => "image/$image_ext", |
|
178 | + ), |
|
179 | + $file_path |
|
180 | + ); |
|
181 | + |
|
182 | + // Generate the metadata for the attachment, and update the database record. |
|
183 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
184 | + // Update the attachment metadata. |
|
185 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
186 | 186 | |
187 | - return $attachment_id; |
|
188 | - } |
|
187 | + return $attachment_id; |
|
188 | + } |
|
189 | 189 | |
190 | 190 | } |
@@ -18,11 +18,11 @@ discard block |
||
18 | 18 | * @param $admin_setup \Wordlift_Admin_Setup |
19 | 19 | * @param $key_validation_service \Wordlift_Key_Validation_Service |
20 | 20 | */ |
21 | - public function __construct( $admin_setup, $key_validation_service ) { |
|
21 | + public function __construct($admin_setup, $key_validation_service) { |
|
22 | 22 | |
23 | 23 | $this->admin_setup = $admin_setup; |
24 | 24 | $this->key_validation_service = $key_validation_service; |
25 | - add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
25 | + add_action('wp_ajax_nopriv_wl_config_plugin', array($this, 'config')); |
|
26 | 26 | |
27 | 27 | } |
28 | 28 | |
@@ -33,35 +33,35 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @return bool |
35 | 35 | */ |
36 | - private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
37 | - $account_info = $this->key_validation_service->get_account_info( $key ); |
|
36 | + private function is_key_valid_and_not_bound_to_any_domain($key) { |
|
37 | + $account_info = $this->key_validation_service->get_account_info($key); |
|
38 | 38 | |
39 | 39 | /** |
40 | 40 | * we need to check if the key is not associated with any account |
41 | 41 | * before setting it, we should check if the url is null. |
42 | 42 | */ |
43 | - if ( is_wp_error( $account_info ) |
|
44 | - || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
43 | + if (is_wp_error($account_info) |
|
44 | + || wp_remote_retrieve_response_code($account_info) !== 200) { |
|
45 | 45 | return false; |
46 | 46 | } |
47 | 47 | |
48 | 48 | $account_info_json = $account_info['body']; |
49 | 49 | |
50 | - $account_info_data = json_decode( $account_info_json, true ); |
|
50 | + $account_info_data = json_decode($account_info_json, true); |
|
51 | 51 | |
52 | - if ( ! $account_info_data ) { |
|
52 | + if ( ! $account_info_data) { |
|
53 | 53 | // Invalid json returned by api. |
54 | 54 | return false; |
55 | 55 | } |
56 | 56 | |
57 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
57 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit(get_option('home'))); |
|
58 | 58 | |
59 | - if ( null === $account_info_data['url'] ) { |
|
59 | + if (null === $account_info_data['url']) { |
|
60 | 60 | return true; |
61 | 61 | } |
62 | 62 | |
63 | 63 | // Check if the key belongs to same site. |
64 | - if ( untrailingslashit( $account_info_data['url'] ) !== $site_url ) { |
|
64 | + if (untrailingslashit($account_info_data['url']) !== $site_url) { |
|
65 | 65 | // key already associated with another account. |
66 | 66 | return false; |
67 | 67 | } |
@@ -84,38 +84,38 @@ discard block |
||
84 | 84 | 'license', |
85 | 85 | ); |
86 | 86 | |
87 | - header( 'Access-Control-Allow-Origin: *' ); |
|
87 | + header('Access-Control-Allow-Origin: *'); |
|
88 | 88 | |
89 | 89 | // validate all the fields before processing |
90 | - foreach ( $required_fields as $field ) { |
|
91 | - if ( ! array_key_exists( $field, $_POST ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
90 | + foreach ($required_fields as $field) { |
|
91 | + if ( ! array_key_exists($field, $_POST)) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
92 | 92 | /* translators: %s: field name */ |
93 | - wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
93 | + wp_send_json_error(sprintf(__('Field %s is required', 'wordlift'), $field), 422); |
|
94 | 94 | |
95 | 95 | return; |
96 | 96 | } |
97 | 97 | } |
98 | 98 | |
99 | - $key = isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
99 | + $key = isset($_POST['license']) ? sanitize_text_field(wp_unslash((string) $_POST['license'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
100 | 100 | |
101 | - if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
102 | - wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
101 | + if ( ! $this->is_key_valid_and_not_bound_to_any_domain($key)) { |
|
102 | + wp_send_json_error(__('Key is not valid or associated with other domain', 'wordlift'), 403); |
|
103 | 103 | |
104 | 104 | // exit if not valid. |
105 | 105 | return; |
106 | 106 | } |
107 | 107 | |
108 | 108 | // check if key is already configured, if yes then dont save settings. |
109 | - if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
110 | - wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
109 | + if (\Wordlift_Configuration_Service::get_instance()->get_key()) { |
|
110 | + wp_send_json_error(__('Key already configured.', 'wordlift'), 403); |
|
111 | 111 | |
112 | 112 | // key already configured |
113 | 113 | return; |
114 | 114 | } |
115 | 115 | |
116 | - $this->admin_setup->save_configuration( $this->get_params() ); |
|
116 | + $this->admin_setup->save_configuration($this->get_params()); |
|
117 | 117 | |
118 | - wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
118 | + wp_send_json_success(__('Configuration Saved', 'wordlift')); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | /** |
@@ -127,16 +127,16 @@ discard block |
||
127 | 127 | $attachment_id = $this->may_be_get_attachment_id(); |
128 | 128 | |
129 | 129 | $params = array( |
130 | - 'key' => isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
131 | - 'vocabulary' => isset( $_POST['vocabulary'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['vocabulary'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
132 | - 'wl-country-code' => isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['country'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
133 | - 'name' => isset( $_POST['publisherName'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisherName'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
134 | - 'user_type' => isset( $_POST['publisher'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisher'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
130 | + 'key' => isset($_POST['license']) ? sanitize_text_field(wp_unslash((string) $_POST['license'])) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
131 | + 'vocabulary' => isset($_POST['vocabulary']) ? sanitize_text_field(wp_unslash((string) $_POST['vocabulary'])) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
132 | + 'wl-country-code' => isset($_POST['country']) ? sanitize_text_field(wp_unslash((string) $_POST['country'])) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
133 | + 'name' => isset($_POST['publisherName']) ? sanitize_text_field(wp_unslash((string) $_POST['publisherName'])) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
134 | + 'user_type' => isset($_POST['publisher']) ? sanitize_text_field(wp_unslash((string) $_POST['publisher'])) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
135 | 135 | 'logo' => $attachment_id, |
136 | 136 | ); |
137 | 137 | |
138 | - $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
139 | - if ( $diagnostic ) { |
|
138 | + $diagnostic = isset($_POST['diagnostic']) ? (bool) $_POST['diagnostic'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
139 | + if ($diagnostic) { |
|
140 | 140 | $params['share-diagnostic'] = 'on'; |
141 | 141 | } |
142 | 142 | |
@@ -149,27 +149,27 @@ discard block |
||
149 | 149 | private function may_be_get_attachment_id() { |
150 | 150 | |
151 | 151 | // if image or image extension not posted then return false. |
152 | - if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
152 | + if ( ! isset($_POST['image']) || ! isset($_POST['imageExtension'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
153 | 153 | return false; |
154 | 154 | } |
155 | 155 | |
156 | - $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
157 | - $image_string = sanitize_text_field( wp_unslash( (string) $_POST['image'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
158 | - $image_ext = sanitize_text_field( wp_unslash( (string) $_POST['imageExtension'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
156 | + $allowed_extensions = array('png', 'jpeg', 'jpg'); |
|
157 | + $image_string = sanitize_text_field(wp_unslash((string) $_POST['image'])); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
158 | + $image_ext = sanitize_text_field(wp_unslash((string) $_POST['imageExtension'])); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
159 | 159 | |
160 | - if ( ! in_array( $image_ext, $allowed_extensions, true ) ) { |
|
160 | + if ( ! in_array($image_ext, $allowed_extensions, true)) { |
|
161 | 161 | return false; |
162 | 162 | } |
163 | 163 | |
164 | 164 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
165 | - $image_decoded_string = base64_decode( $image_string ); |
|
165 | + $image_decoded_string = base64_decode($image_string); |
|
166 | 166 | |
167 | 167 | $upload_dir = wp_upload_dir(); |
168 | 168 | |
169 | - $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . '.' . $image_ext; |
|
169 | + $file_path = $upload_dir['path'].DIRECTORY_SEPARATOR.md5($image_string).'.'.$image_ext; |
|
170 | 170 | |
171 | 171 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
172 | - file_put_contents( $file_path, $image_decoded_string ); |
|
172 | + file_put_contents($file_path, $image_decoded_string); |
|
173 | 173 | |
174 | 174 | $attachment_id = wp_insert_attachment( |
175 | 175 | array( |
@@ -180,9 +180,9 @@ discard block |
||
180 | 180 | ); |
181 | 181 | |
182 | 182 | // Generate the metadata for the attachment, and update the database record. |
183 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
183 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $file_path); |
|
184 | 184 | // Update the attachment metadata. |
185 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
185 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
186 | 186 | |
187 | 187 | return $attachment_id; |
188 | 188 | } |
@@ -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 | } |