@@ -14,54 +14,54 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Tinymce_Adapter { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The {@link Wordlift} plugin instance. |
|
| 19 | - * |
|
| 20 | - * @since 3.12.0 |
|
| 21 | - * @access private |
|
| 22 | - * @var \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 23 | - */ |
|
| 24 | - private $plugin; |
|
| 17 | + /** |
|
| 18 | + * The {@link Wordlift} plugin instance. |
|
| 19 | + * |
|
| 20 | + * @since 3.12.0 |
|
| 21 | + * @access private |
|
| 22 | + * @var \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 23 | + */ |
|
| 24 | + private $plugin; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Wordlift_Tinymce_Adapter constructor. |
|
| 28 | - * |
|
| 29 | - * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 30 | - */ |
|
| 31 | - public function __construct( $plugin ) { |
|
| 26 | + /** |
|
| 27 | + * Wordlift_Tinymce_Adapter constructor. |
|
| 28 | + * |
|
| 29 | + * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 30 | + */ |
|
| 31 | + public function __construct( $plugin ) { |
|
| 32 | 32 | |
| 33 | - $this->plugin = $plugin; |
|
| 33 | + $this->plugin = $plugin; |
|
| 34 | 34 | |
| 35 | - } |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Load the TinyMCE plugin. This method is called by the WP mce_external_plugins hook. |
|
| 39 | - * |
|
| 40 | - * @since 3.12.0 |
|
| 41 | - * |
|
| 42 | - * @param array $plugins The existing plugins array. |
|
| 43 | - * |
|
| 44 | - * @return array The modified plugins array. |
|
| 45 | - */ |
|
| 46 | - function mce_external_plugins( $plugins ) { |
|
| 37 | + /** |
|
| 38 | + * Load the TinyMCE plugin. This method is called by the WP mce_external_plugins hook. |
|
| 39 | + * |
|
| 40 | + * @since 3.12.0 |
|
| 41 | + * |
|
| 42 | + * @param array $plugins The existing plugins array. |
|
| 43 | + * |
|
| 44 | + * @return array The modified plugins array. |
|
| 45 | + */ |
|
| 46 | + function mce_external_plugins( $plugins ) { |
|
| 47 | 47 | |
| 48 | - // Get WordLift's version as a cache killer. |
|
| 49 | - $version = $this->plugin->get_version(); |
|
| 48 | + // Get WordLift's version as a cache killer. |
|
| 49 | + $version = $this->plugin->get_version(); |
|
| 50 | 50 | |
| 51 | - // User can edit? |
|
| 52 | - $can_edit = current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ); |
|
| 51 | + // User can edit? |
|
| 52 | + $can_edit = current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ); |
|
| 53 | 53 | |
| 54 | - // If user can't edit or rich editing isn't enabled, bail out. |
|
| 55 | - if ( ! $can_edit || ! get_user_option( 'rich_editing' ) ) { |
|
| 56 | - return $plugins; |
|
| 57 | - } |
|
| 54 | + // If user can't edit or rich editing isn't enabled, bail out. |
|
| 55 | + if ( ! $can_edit || ! get_user_option( 'rich_editing' ) ) { |
|
| 56 | + return $plugins; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - // Add our own JavaScript file to TinyMCE's extensions. |
|
| 60 | - $plugins['wordlift'] = plugin_dir_url( dirname( __FILE__ ) ) . 'js/wordlift-reloaded.js?ver=' . $version; |
|
| 61 | - $plugins['wl_shortcodes'] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift_shortcode_tinymce_plugin.js?ver=' . $version; |
|
| 62 | - $plugins['wl_tinymce'] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/tinymce.js?ver=' . $version; |
|
| 59 | + // Add our own JavaScript file to TinyMCE's extensions. |
|
| 60 | + $plugins['wordlift'] = plugin_dir_url( dirname( __FILE__ ) ) . 'js/wordlift-reloaded.js?ver=' . $version; |
|
| 61 | + $plugins['wl_shortcodes'] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift_shortcode_tinymce_plugin.js?ver=' . $version; |
|
| 62 | + $plugins['wl_tinymce'] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/tinymce.js?ver=' . $version; |
|
| 63 | 63 | |
| 64 | - return $plugins; |
|
| 65 | - } |
|
| 64 | + return $plugins; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | 67 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | * |
| 29 | 29 | * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
| 30 | 30 | */ |
| 31 | - public function __construct( $plugin ) { |
|
| 31 | + public function __construct($plugin) { |
|
| 32 | 32 | |
| 33 | 33 | $this->plugin = $plugin; |
| 34 | 34 | |
@@ -43,23 +43,23 @@ discard block |
||
| 43 | 43 | * |
| 44 | 44 | * @return array The modified plugins array. |
| 45 | 45 | */ |
| 46 | - function mce_external_plugins( $plugins ) { |
|
| 46 | + function mce_external_plugins($plugins) { |
|
| 47 | 47 | |
| 48 | 48 | // Get WordLift's version as a cache killer. |
| 49 | 49 | $version = $this->plugin->get_version(); |
| 50 | 50 | |
| 51 | 51 | // User can edit? |
| 52 | - $can_edit = current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ); |
|
| 52 | + $can_edit = current_user_can('edit_posts') || current_user_can('edit_pages'); |
|
| 53 | 53 | |
| 54 | 54 | // If user can't edit or rich editing isn't enabled, bail out. |
| 55 | - if ( ! $can_edit || ! get_user_option( 'rich_editing' ) ) { |
|
| 55 | + if ( ! $can_edit || ! get_user_option('rich_editing')) { |
|
| 56 | 56 | return $plugins; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | // Add our own JavaScript file to TinyMCE's extensions. |
| 60 | - $plugins['wordlift'] = plugin_dir_url( dirname( __FILE__ ) ) . 'js/wordlift-reloaded.js?ver=' . $version; |
|
| 61 | - $plugins['wl_shortcodes'] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift_shortcode_tinymce_plugin.js?ver=' . $version; |
|
| 62 | - $plugins['wl_tinymce'] = plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/tinymce.js?ver=' . $version; |
|
| 60 | + $plugins['wordlift'] = plugin_dir_url(dirname(__FILE__)).'js/wordlift-reloaded.js?ver='.$version; |
|
| 61 | + $plugins['wl_shortcodes'] = plugin_dir_url(dirname(__FILE__)).'admin/js/wordlift_shortcode_tinymce_plugin.js?ver='.$version; |
|
| 62 | + $plugins['wl_tinymce'] = plugin_dir_url(dirname(__FILE__)).'admin/js/1/tinymce.js?ver='.$version; |
|
| 63 | 63 | |
| 64 | 64 | return $plugins; |
| 65 | 65 | } |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | - exit; |
|
| 14 | + exit; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** |
@@ -20,79 +20,79 @@ discard block |
||
| 20 | 20 | * @since 3.15.0 |
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Autocomplete_Service { |
| 23 | - /** |
|
| 24 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.15.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $configuration_service; |
|
| 23 | + /** |
|
| 24 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.15.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $configuration_service; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * A {@link Wordlift_Log_Service} instance. |
|
| 34 | - * |
|
| 35 | - * @since 3.15.0 |
|
| 36 | - * @access private |
|
| 37 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 38 | - */ |
|
| 39 | - private $log; |
|
| 32 | + /** |
|
| 33 | + * A {@link Wordlift_Log_Service} instance. |
|
| 34 | + * |
|
| 35 | + * @since 3.15.0 |
|
| 36 | + * @access private |
|
| 37 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 38 | + */ |
|
| 39 | + private $log; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The {@link Class_Wordlift_Autocomplete_Service} instance. |
|
| 43 | - * |
|
| 44 | - * @since 3.15.0 |
|
| 45 | - * |
|
| 46 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 47 | - */ |
|
| 48 | - public function __construct( $configuration_service ) { |
|
| 49 | - $this->configuration_service = $configuration_service; |
|
| 50 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Autocomplete_Service' ); |
|
| 51 | - } |
|
| 41 | + /** |
|
| 42 | + * The {@link Class_Wordlift_Autocomplete_Service} instance. |
|
| 43 | + * |
|
| 44 | + * @since 3.15.0 |
|
| 45 | + * |
|
| 46 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 47 | + */ |
|
| 48 | + public function __construct( $configuration_service ) { |
|
| 49 | + $this->configuration_service = $configuration_service; |
|
| 50 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Autocomplete_Service' ); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Make request to external API and return the response. |
|
| 55 | - * |
|
| 56 | - * @since 3.15.0 |
|
| 57 | - * |
|
| 58 | - * @param string $query The search string. |
|
| 59 | - * |
|
| 60 | - * @return array $response The API response. |
|
| 61 | - */ |
|
| 62 | - public function make_request( $query ) { |
|
| 63 | - $url = $this->build_request_url( $query ); |
|
| 53 | + /** |
|
| 54 | + * Make request to external API and return the response. |
|
| 55 | + * |
|
| 56 | + * @since 3.15.0 |
|
| 57 | + * |
|
| 58 | + * @param string $query The search string. |
|
| 59 | + * |
|
| 60 | + * @return array $response The API response. |
|
| 61 | + */ |
|
| 62 | + public function make_request( $query ) { |
|
| 63 | + $url = $this->build_request_url( $query ); |
|
| 64 | 64 | |
| 65 | - // Make request. |
|
| 66 | - $response = wp_remote_get( $url ); |
|
| 65 | + // Make request. |
|
| 66 | + $response = wp_remote_get( $url ); |
|
| 67 | 67 | |
| 68 | - // Return the response. |
|
| 69 | - return $response; |
|
| 70 | - } |
|
| 68 | + // Return the response. |
|
| 69 | + return $response; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Build the autocomplete url. |
|
| 74 | - * |
|
| 75 | - * @since 3.15.0 |
|
| 76 | - * |
|
| 77 | - * @param string $query The search string. |
|
| 78 | - * |
|
| 79 | - * @return string Built url. |
|
| 80 | - */ |
|
| 81 | - private function build_request_url( $query ) { |
|
| 82 | - $args = array( |
|
| 83 | - 'key' => $this->configuration_service->get_key(), |
|
| 84 | - 'language' => $this->configuration_service->get_language_code(), |
|
| 85 | - 'query' => $query, |
|
| 86 | - 'limit' => 100, |
|
| 87 | - ); |
|
| 72 | + /** |
|
| 73 | + * Build the autocomplete url. |
|
| 74 | + * |
|
| 75 | + * @since 3.15.0 |
|
| 76 | + * |
|
| 77 | + * @param string $query The search string. |
|
| 78 | + * |
|
| 79 | + * @return string Built url. |
|
| 80 | + */ |
|
| 81 | + private function build_request_url( $query ) { |
|
| 82 | + $args = array( |
|
| 83 | + 'key' => $this->configuration_service->get_key(), |
|
| 84 | + 'language' => $this->configuration_service->get_language_code(), |
|
| 85 | + 'query' => $query, |
|
| 86 | + 'limit' => 100, |
|
| 87 | + ); |
|
| 88 | 88 | |
| 89 | - // Add args to URL. |
|
| 90 | - $request_url = add_query_arg( |
|
| 91 | - urlencode_deep( $args ), |
|
| 92 | - $this->configuration_service->get_autocomplete_url() |
|
| 93 | - ); |
|
| 89 | + // Add args to URL. |
|
| 90 | + $request_url = add_query_arg( |
|
| 91 | + urlencode_deep( $args ), |
|
| 92 | + $this->configuration_service->get_autocomplete_url() |
|
| 93 | + ); |
|
| 94 | 94 | |
| 95 | - // return the built url. |
|
| 96 | - return $request_url; |
|
| 97 | - } |
|
| 95 | + // return the built url. |
|
| 96 | + return $request_url; |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | * @since 3.15.0 |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 13 | +if ( ! defined('ABSPATH')) { |
|
| 14 | 14 | exit; |
| 15 | 15 | } |
| 16 | 16 | |
@@ -45,9 +45,9 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 47 | 47 | */ |
| 48 | - public function __construct( $configuration_service ) { |
|
| 48 | + public function __construct($configuration_service) { |
|
| 49 | 49 | $this->configuration_service = $configuration_service; |
| 50 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Autocomplete_Service' ); |
|
| 50 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Autocomplete_Service'); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -59,11 +59,11 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @return array $response The API response. |
| 61 | 61 | */ |
| 62 | - public function make_request( $query ) { |
|
| 63 | - $url = $this->build_request_url( $query ); |
|
| 62 | + public function make_request($query) { |
|
| 63 | + $url = $this->build_request_url($query); |
|
| 64 | 64 | |
| 65 | 65 | // Make request. |
| 66 | - $response = wp_remote_get( $url ); |
|
| 66 | + $response = wp_remote_get($url); |
|
| 67 | 67 | |
| 68 | 68 | // Return the response. |
| 69 | 69 | return $response; |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | * |
| 79 | 79 | * @return string Built url. |
| 80 | 80 | */ |
| 81 | - private function build_request_url( $query ) { |
|
| 81 | + private function build_request_url($query) { |
|
| 82 | 82 | $args = array( |
| 83 | 83 | 'key' => $this->configuration_service->get_key(), |
| 84 | 84 | 'language' => $this->configuration_service->get_language_code(), |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | // Add args to URL. |
| 90 | 90 | $request_url = add_query_arg( |
| 91 | - urlencode_deep( $args ), |
|
| 91 | + urlencode_deep($args), |
|
| 92 | 92 | $this->configuration_service->get_autocomplete_url() |
| 93 | 93 | ); |
| 94 | 94 | |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | - exit; |
|
| 14 | + exit; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** |
@@ -21,75 +21,75 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Autocomplete_Adapter { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The {@link Wordlift_Autocomplete_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.15.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Autocomplete_Service $configuration_service The {@link Wordlift_Autocomplete_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $autocomplete_service; |
|
| 24 | + /** |
|
| 25 | + * The {@link Wordlift_Autocomplete_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.15.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Autocomplete_Service $configuration_service The {@link Wordlift_Autocomplete_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $autocomplete_service; |
|
| 32 | 32 | |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Wordlift_Autocomplete_Adapter constructor. |
|
| 36 | - * |
|
| 37 | - * @since 3.14.2 |
|
| 38 | - * |
|
| 39 | - * @param \Wordlift_Autocomplete_Service $autocomplete_service The {@link Wordlift_Autocomplete_Service} instance. |
|
| 40 | - */ |
|
| 41 | - public function __construct( $autocomplete_service ) { |
|
| 42 | - $this->autocomplete_service = $autocomplete_service; |
|
| 43 | - } |
|
| 34 | + /** |
|
| 35 | + * Wordlift_Autocomplete_Adapter constructor. |
|
| 36 | + * |
|
| 37 | + * @since 3.14.2 |
|
| 38 | + * |
|
| 39 | + * @param \Wordlift_Autocomplete_Service $autocomplete_service The {@link Wordlift_Autocomplete_Service} instance. |
|
| 40 | + */ |
|
| 41 | + public function __construct( $autocomplete_service ) { |
|
| 42 | + $this->autocomplete_service = $autocomplete_service; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Handle the autocomplete ajax request. |
|
| 47 | - * |
|
| 48 | - * @since 3.15.0 |
|
| 49 | - */ |
|
| 50 | - public function wl_autocomplete() { |
|
| 51 | - if ( |
|
| 52 | - ! isset( $_REQUEST['_wpnonce'] ) || // Input var okay. |
|
| 53 | - ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'wordlift_autocomplete' ) // Input var okay. |
|
| 54 | - ) { |
|
| 55 | - wp_send_json_error( array( |
|
| 56 | - 'message' => __( 'Nonce field doesn\'t match.', 'wordlift' ), |
|
| 57 | - ) ); |
|
| 58 | - } |
|
| 45 | + /** |
|
| 46 | + * Handle the autocomplete ajax request. |
|
| 47 | + * |
|
| 48 | + * @since 3.15.0 |
|
| 49 | + */ |
|
| 50 | + public function wl_autocomplete() { |
|
| 51 | + if ( |
|
| 52 | + ! isset( $_REQUEST['_wpnonce'] ) || // Input var okay. |
|
| 53 | + ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'wordlift_autocomplete' ) // Input var okay. |
|
| 54 | + ) { |
|
| 55 | + wp_send_json_error( array( |
|
| 56 | + 'message' => __( 'Nonce field doesn\'t match.', 'wordlift' ), |
|
| 57 | + ) ); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - // Return error if the query param is empty. |
|
| 61 | - if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay. |
|
| 62 | - $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay. |
|
| 63 | - } else { |
|
| 64 | - wp_send_json_error( array( |
|
| 65 | - 'message' => __( 'The query param is empty.', 'wordlift' ), |
|
| 66 | - ) ); |
|
| 67 | - } |
|
| 60 | + // Return error if the query param is empty. |
|
| 61 | + if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay. |
|
| 62 | + $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay. |
|
| 63 | + } else { |
|
| 64 | + wp_send_json_error( array( |
|
| 65 | + 'message' => __( 'The query param is empty.', 'wordlift' ), |
|
| 66 | + ) ); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - // Make request. |
|
| 70 | - $response = $this->autocomplete_service->make_request( $query ); |
|
| 69 | + // Make request. |
|
| 70 | + $response = $this->autocomplete_service->make_request( $query ); |
|
| 71 | 71 | |
| 72 | - // Clear any buffer. |
|
| 73 | - ob_clean(); |
|
| 72 | + // Clear any buffer. |
|
| 73 | + ob_clean(); |
|
| 74 | 74 | |
| 75 | - // If the response is valid, then send the suggestions. |
|
| 76 | - if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) { |
|
| 77 | - // Echo the response. |
|
| 78 | - wp_send_json_success( json_decode( wp_remote_retrieve_body( $response ), true ) ); |
|
| 79 | - } else { |
|
| 80 | - // Default error message. |
|
| 81 | - $error_message = 'Something went wrong.'; |
|
| 75 | + // If the response is valid, then send the suggestions. |
|
| 76 | + if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) { |
|
| 77 | + // Echo the response. |
|
| 78 | + wp_send_json_success( json_decode( wp_remote_retrieve_body( $response ), true ) ); |
|
| 79 | + } else { |
|
| 80 | + // Default error message. |
|
| 81 | + $error_message = 'Something went wrong.'; |
|
| 82 | 82 | |
| 83 | - // Get the real error message if there is WP_Error. |
|
| 84 | - if ( is_wp_error( $response ) ) { |
|
| 85 | - $error_message = $response->get_error_message(); |
|
| 86 | - } |
|
| 83 | + // Get the real error message if there is WP_Error. |
|
| 84 | + if ( is_wp_error( $response ) ) { |
|
| 85 | + $error_message = $response->get_error_message(); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - // There is an error, so send error message. |
|
| 89 | - wp_send_json_error( array( |
|
| 90 | - /* translators: Placeholders: %s - the error message that will be returned. */ |
|
| 91 | - 'message' => sprintf( esc_html__( 'Error: %s', 'wordlift' ), $error_message ), |
|
| 92 | - ) ); |
|
| 93 | - } |
|
| 94 | - } |
|
| 88 | + // There is an error, so send error message. |
|
| 89 | + wp_send_json_error( array( |
|
| 90 | + /* translators: Placeholders: %s - the error message that will be returned. */ |
|
| 91 | + 'message' => sprintf( esc_html__( 'Error: %s', 'wordlift' ), $error_message ), |
|
| 92 | + ) ); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | 95 | } |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | * @since 3.15.0 |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 13 | +if ( ! defined('ABSPATH')) { |
|
| 14 | 14 | exit; |
| 15 | 15 | } |
| 16 | 16 | |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @param \Wordlift_Autocomplete_Service $autocomplete_service The {@link Wordlift_Autocomplete_Service} instance. |
| 40 | 40 | */ |
| 41 | - public function __construct( $autocomplete_service ) { |
|
| 41 | + public function __construct($autocomplete_service) { |
|
| 42 | 42 | $this->autocomplete_service = $autocomplete_service; |
| 43 | 43 | } |
| 44 | 44 | |
@@ -49,47 +49,47 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function wl_autocomplete() { |
| 51 | 51 | if ( |
| 52 | - ! isset( $_REQUEST['_wpnonce'] ) || // Input var okay. |
|
| 53 | - ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'wordlift_autocomplete' ) // Input var okay. |
|
| 52 | + ! isset($_REQUEST['_wpnonce']) || // Input var okay. |
|
| 53 | + ! wp_verify_nonce(sanitize_key($_REQUEST['_wpnonce']), 'wordlift_autocomplete') // Input var okay. |
|
| 54 | 54 | ) { |
| 55 | - wp_send_json_error( array( |
|
| 56 | - 'message' => __( 'Nonce field doesn\'t match.', 'wordlift' ), |
|
| 57 | - ) ); |
|
| 55 | + wp_send_json_error(array( |
|
| 56 | + 'message' => __('Nonce field doesn\'t match.', 'wordlift'), |
|
| 57 | + )); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | // Return error if the query param is empty. |
| 61 | - if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay. |
|
| 62 | - $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay. |
|
| 61 | + if ( ! empty($_REQUEST['query'])) { // Input var okay. |
|
| 62 | + $query = sanitize_text_field(wp_unslash($_REQUEST['query'])); // Input var okay. |
|
| 63 | 63 | } else { |
| 64 | - wp_send_json_error( array( |
|
| 65 | - 'message' => __( 'The query param is empty.', 'wordlift' ), |
|
| 66 | - ) ); |
|
| 64 | + wp_send_json_error(array( |
|
| 65 | + 'message' => __('The query param is empty.', 'wordlift'), |
|
| 66 | + )); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | // Make request. |
| 70 | - $response = $this->autocomplete_service->make_request( $query ); |
|
| 70 | + $response = $this->autocomplete_service->make_request($query); |
|
| 71 | 71 | |
| 72 | 72 | // Clear any buffer. |
| 73 | 73 | ob_clean(); |
| 74 | 74 | |
| 75 | 75 | // If the response is valid, then send the suggestions. |
| 76 | - if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) { |
|
| 76 | + if ( ! is_wp_error($response) && 200 === (int) $response['response']['code']) { |
|
| 77 | 77 | // Echo the response. |
| 78 | - wp_send_json_success( json_decode( wp_remote_retrieve_body( $response ), true ) ); |
|
| 78 | + wp_send_json_success(json_decode(wp_remote_retrieve_body($response), true)); |
|
| 79 | 79 | } else { |
| 80 | 80 | // Default error message. |
| 81 | 81 | $error_message = 'Something went wrong.'; |
| 82 | 82 | |
| 83 | 83 | // Get the real error message if there is WP_Error. |
| 84 | - if ( is_wp_error( $response ) ) { |
|
| 84 | + if (is_wp_error($response)) { |
|
| 85 | 85 | $error_message = $response->get_error_message(); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | // There is an error, so send error message. |
| 89 | - wp_send_json_error( array( |
|
| 89 | + wp_send_json_error(array( |
|
| 90 | 90 | /* translators: Placeholders: %s - the error message that will be returned. */ |
| 91 | - 'message' => sprintf( esc_html__( 'Error: %s', 'wordlift' ), $error_message ), |
|
| 92 | - ) ); |
|
| 91 | + 'message' => sprintf(esc_html__('Error: %s', 'wordlift'), $error_message), |
|
| 92 | + )); |
|
| 93 | 93 | } |
| 94 | 94 | } |
| 95 | 95 | } |
@@ -4,26 +4,26 @@ |
||
| 4 | 4 | */ |
| 5 | 5 | class Wordlift_Sanitizer { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * Only accept URIs |
|
| 9 | - * |
|
| 10 | - * @param string $value |
|
| 11 | - * |
|
| 12 | - * @return null |
|
| 13 | - */ |
|
| 14 | - public static function sanitize_url( $value ) { |
|
| 7 | + /** |
|
| 8 | + * Only accept URIs |
|
| 9 | + * |
|
| 10 | + * @param string $value |
|
| 11 | + * |
|
| 12 | + * @return null |
|
| 13 | + */ |
|
| 14 | + public static function sanitize_url( $value ) { |
|
| 15 | 15 | |
| 16 | - // Initially this function used also filter_var( $value, FILTER_VALIDATE_URL ) |
|
| 17 | - // but URLs with UTF-8 characters are not valid. We store those anyway in the DB as it's up to the browser |
|
| 18 | - // to do proper url encoding when requesting the URL. |
|
| 19 | - // |
|
| 20 | - // see also http://stackoverflow.com/questions/2137080/php-filter-var-filter-validate-url |
|
| 16 | + // Initially this function used also filter_var( $value, FILTER_VALIDATE_URL ) |
|
| 17 | + // but URLs with UTF-8 characters are not valid. We store those anyway in the DB as it's up to the browser |
|
| 18 | + // to do proper url encoding when requesting the URL. |
|
| 19 | + // |
|
| 20 | + // see also http://stackoverflow.com/questions/2137080/php-filter-var-filter-validate-url |
|
| 21 | 21 | |
| 22 | - if ( ! is_null( $value ) && $value !== '' ) { |
|
| 23 | - return $value; |
|
| 24 | - } |
|
| 22 | + if ( ! is_null( $value ) && $value !== '' ) { |
|
| 23 | + return $value; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - return null; |
|
| 27 | - } |
|
| 26 | + return null; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | 29 | } |
| 30 | 30 | \ No newline at end of file |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | * |
| 12 | 12 | * @return null |
| 13 | 13 | */ |
| 14 | - public static function sanitize_url( $value ) { |
|
| 14 | + public static function sanitize_url($value) { |
|
| 15 | 15 | |
| 16 | 16 | // Initially this function used also filter_var( $value, FILTER_VALIDATE_URL ) |
| 17 | 17 | // but URLs with UTF-8 characters are not valid. We store those anyway in the DB as it's up to the browser |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | // |
| 20 | 20 | // see also http://stackoverflow.com/questions/2137080/php-filter-var-filter-validate-url |
| 21 | 21 | |
| 22 | - if ( ! is_null( $value ) && $value !== '' ) { |
|
| 22 | + if ( ! is_null($value) && $value !== '') { |
|
| 23 | 23 | return $value; |
| 24 | 24 | } |
| 25 | 25 | |
@@ -18,321 +18,321 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Admin_Settings_Page extends Wordlift_Admin_Page { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * A singleton instance of the Notice service. |
|
| 23 | - * |
|
| 24 | - * @since 3.2.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Notice_Service $instance A singleton instance of the Notice service. |
|
| 27 | - */ |
|
| 28 | - private static $instance; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.11.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $entity_service; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.11.0 |
|
| 43 | - * @access private |
|
| 44 | - * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 45 | - */ |
|
| 46 | - private $configuration_service; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 50 | - * |
|
| 51 | - * @since 3.11.0 |
|
| 52 | - * @access private |
|
| 53 | - * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 54 | - */ |
|
| 55 | - private $input_element; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 59 | - * |
|
| 60 | - * @since 3.13.0 |
|
| 61 | - * @access protected |
|
| 62 | - * @var \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 63 | - */ |
|
| 64 | - private $radio_input_element; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 68 | - * |
|
| 69 | - * @since 3.11.0 |
|
| 70 | - * @access private |
|
| 71 | - * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 72 | - */ |
|
| 73 | - private $language_select_element; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 77 | - * |
|
| 78 | - * @since 3.11.0 |
|
| 79 | - * @access private |
|
| 80 | - * @var \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 81 | - */ |
|
| 82 | - private $publisher_element; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Create a {@link Wordlift_Admin_Settings_Page} instance. |
|
| 86 | - * |
|
| 87 | - * @since 3.11.0 |
|
| 88 | - * |
|
| 89 | - * @param \Wordlift_Configuration_Service $configuration_service |
|
| 90 | - * @param \Wordlift_Entity_Service $entity_service |
|
| 91 | - * @param \Wordlift_Admin_Input_Element $input_element |
|
| 92 | - * @param \Wordlift_Admin_Language_Select_Element $language_select_element |
|
| 93 | - * @param \Wordlift_Admin_Publisher_Element $publisher_element |
|
| 94 | - * @param \Wordlift_Admin_Radio_Input_Element $radio_input_element |
|
| 95 | - */ |
|
| 96 | - function __construct( $configuration_service, $entity_service, $input_element, $language_select_element, $publisher_element, $radio_input_element ) { |
|
| 97 | - |
|
| 98 | - $this->configuration_service = $configuration_service; |
|
| 99 | - $this->entity_service = $entity_service; |
|
| 100 | - |
|
| 101 | - // Set a reference to the UI elements. |
|
| 102 | - $this->input_element = $input_element; |
|
| 103 | - $this->radio_input_element = $radio_input_element; |
|
| 104 | - $this->language_select_element = $language_select_element; |
|
| 105 | - $this->publisher_element = $publisher_element; |
|
| 106 | - |
|
| 107 | - self::$instance = $this; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Get the singleton instance of the Notice service. |
|
| 112 | - * |
|
| 113 | - * @since 3.14.0 |
|
| 114 | - * @return \Wordlift_Admin_Settings_Page The singleton instance of the settings page service. |
|
| 115 | - */ |
|
| 116 | - public static function get_instance() { |
|
| 117 | - |
|
| 118 | - return self::$instance; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @inheritdoc |
|
| 123 | - */ |
|
| 124 | - function get_parent_slug() { |
|
| 125 | - |
|
| 126 | - return 'wl_admin_menu'; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @inheritdoc |
|
| 131 | - */ |
|
| 132 | - function get_capability() { |
|
| 133 | - |
|
| 134 | - return 'manage_options'; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @inheritdoc |
|
| 139 | - */ |
|
| 140 | - function get_page_title() { |
|
| 141 | - |
|
| 142 | - return 'WorldLift Settings'; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @inheritdoc |
|
| 147 | - */ |
|
| 148 | - function get_menu_title() { |
|
| 149 | - |
|
| 150 | - return 'Settings'; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @inheritdoc |
|
| 155 | - */ |
|
| 156 | - function get_menu_slug() { |
|
| 157 | - |
|
| 158 | - return 'wl_configuration_admin_menu'; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @inheritdoc |
|
| 163 | - */ |
|
| 164 | - function get_partial_name() { |
|
| 165 | - |
|
| 166 | - return 'wordlift-admin-settings-page.php'; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @inheritdoc |
|
| 171 | - */ |
|
| 172 | - public function enqueue_scripts() { |
|
| 173 | - |
|
| 174 | - // Enqueue the media scripts to be used for the publisher's logo selection. |
|
| 175 | - wp_enqueue_media(); |
|
| 176 | - |
|
| 177 | - // JavaScript required for the settings page. |
|
| 178 | - // @todo: try to move to the `wordlift-admin.bundle.js`. |
|
| 179 | - wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.js', array( 'wp-util' ) ); |
|
| 180 | - |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Configure all the configuration parameters. |
|
| 185 | - * |
|
| 186 | - * Called by the *admin_init* hook. |
|
| 187 | - * |
|
| 188 | - * @since 3.11.0 |
|
| 189 | - */ |
|
| 190 | - function admin_init() { |
|
| 191 | - |
|
| 192 | - // Register WordLift's general settings, providing our own sanitize callback |
|
| 193 | - // which will also check whether the user filled the WL Publisher form. |
|
| 194 | - register_setting( |
|
| 195 | - 'wl_general_settings', |
|
| 196 | - 'wl_general_settings', |
|
| 197 | - array( $this, 'sanitize_callback', ) |
|
| 198 | - ); |
|
| 199 | - |
|
| 200 | - // Add the general settings section. |
|
| 201 | - add_settings_section( |
|
| 202 | - 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
|
| 203 | - '', // Section header. |
|
| 204 | - '', // Callback used to render the description of the section. |
|
| 205 | - 'wl_general_settings' // Page on which to add this section of options. |
|
| 206 | - ); |
|
| 207 | - |
|
| 208 | - $key_args = array( |
|
| 209 | - 'id' => 'wl-key', |
|
| 210 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 211 | - 'value' => $this->configuration_service->get_key(), |
|
| 212 | - 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ), |
|
| 213 | - ); |
|
| 214 | - |
|
| 215 | - // Set the class for the key field based on the validity of the key. |
|
| 216 | - // Class should be "untouched" for an empty (virgin) value, "valid" |
|
| 217 | - // if the key is valid, or "invalid" otherwise. |
|
| 218 | - $validation_service = new Wordlift_Key_Validation_Service( $this->configuration_service ); |
|
| 219 | - |
|
| 220 | - if ( empty( $key_args['value'] ) ) { |
|
| 221 | - $key_args['css_class'] = 'untouched'; |
|
| 222 | - } elseif ( $validation_service->is_valid( $key_args['value'] ) ) { |
|
| 223 | - $key_args['css_class'] = 'valid'; |
|
| 224 | - } else { |
|
| 225 | - $key_args['css_class'] = 'invalid'; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - // Add the `key` field. |
|
| 229 | - add_settings_field( |
|
| 230 | - 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 231 | - _x( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 232 | - // The name of the function responsible for rendering the option interface. |
|
| 233 | - array( $this->input_element, 'render', ), |
|
| 234 | - 'wl_general_settings', // The page on which this option will be displayed. |
|
| 235 | - 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 236 | - $key_args // The array of arguments to pass to the callback. In this case, just a description. |
|
| 237 | - ); |
|
| 238 | - |
|
| 239 | - // Entity Base Path input. |
|
| 240 | - $entity_base_path_args = array( |
|
| 241 | - // The array of arguments to pass to the callback. In this case, just a description. |
|
| 242 | - 'id' => 'wl-entity-base-path', |
|
| 243 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 244 | - 'value' => $this->configuration_service->get_entity_base_path(), |
|
| 245 | - 'description' => sprintf( _x( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ), |
|
| 246 | - ); |
|
| 247 | - |
|
| 248 | - $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
|
| 249 | - |
|
| 250 | - // Add the `wl_entity_base_path` field. |
|
| 251 | - add_settings_field( |
|
| 252 | - 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 253 | - _x( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 254 | - // The name of the function responsible for rendering the option interface |
|
| 255 | - array( $this->input_element, 'render', ), |
|
| 256 | - 'wl_general_settings', // The page on which this option will be displayed |
|
| 257 | - 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 258 | - $entity_base_path_args |
|
| 259 | - ); |
|
| 260 | - |
|
| 261 | - // Add the `language_name` field. |
|
| 262 | - add_settings_field( |
|
| 263 | - 'wl-site-language', |
|
| 264 | - _x( 'Site Language', 'wordlift' ), |
|
| 265 | - array( $this->language_select_element, 'render' ), |
|
| 266 | - 'wl_general_settings', |
|
| 267 | - 'wl_general_settings_section', |
|
| 268 | - array( |
|
| 269 | - // The array of arguments to pass to the callback. In this case, just a description. |
|
| 270 | - 'id' => 'wl-site-language', |
|
| 271 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 272 | - 'value' => $this->configuration_service->get_language_code(), |
|
| 273 | - 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 274 | - ) |
|
| 275 | - ); |
|
| 276 | - |
|
| 277 | - // Add the `publisher` field. |
|
| 278 | - add_settings_field( |
|
| 279 | - 'wl-publisher-id', |
|
| 280 | - _x( 'Publisher', 'wordlift' ), |
|
| 281 | - array( $this->publisher_element, 'render' ), |
|
| 282 | - 'wl_general_settings', |
|
| 283 | - 'wl_general_settings_section', |
|
| 284 | - array( |
|
| 285 | - 'id' => 'wl-publisher-id', |
|
| 286 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 287 | - ) |
|
| 288 | - ); |
|
| 289 | - |
|
| 290 | - // Add the `link by default` field. |
|
| 291 | - add_settings_field( |
|
| 292 | - 'wl-link-by-default', |
|
| 293 | - _x( 'Link by Default', 'wordlift' ), |
|
| 294 | - array( $this->radio_input_element, 'render' ), |
|
| 295 | - 'wl_general_settings', |
|
| 296 | - 'wl_general_settings_section', |
|
| 297 | - array( |
|
| 298 | - 'id' => 'wl-link-by-default', |
|
| 299 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 300 | - 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
|
| 301 | - 'description' => _x( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 302 | - ) |
|
| 303 | - ); |
|
| 304 | - |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Sanitize the configuration settings to be stored. |
|
| 309 | - * |
|
| 310 | - * If a new entity is being created for the publisher, create it and set The |
|
| 311 | - * publisher setting. |
|
| 312 | - * |
|
| 313 | - * @since 3.11.0 |
|
| 314 | - * |
|
| 315 | - * @param array $input The configuration settings array. |
|
| 316 | - * |
|
| 317 | - * @return array The sanitized input array. |
|
| 318 | - */ |
|
| 319 | - function sanitize_callback( $input ) { |
|
| 320 | - |
|
| 321 | - // Check whether a publisher name has been set. |
|
| 322 | - if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { |
|
| 323 | - $name = $_POST['wl_publisher']['name']; |
|
| 324 | - $type = $_POST['wl_publisher']['type']; |
|
| 325 | - $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; |
|
| 326 | - |
|
| 327 | - // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
|
| 328 | - $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 329 | - |
|
| 330 | - // Create an entity for the publisher and assign it to the input |
|
| 331 | - // parameter which WordPress automatically saves into the settings. |
|
| 332 | - $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - return $input; |
|
| 336 | - } |
|
| 21 | + /** |
|
| 22 | + * A singleton instance of the Notice service. |
|
| 23 | + * |
|
| 24 | + * @since 3.2.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Notice_Service $instance A singleton instance of the Notice service. |
|
| 27 | + */ |
|
| 28 | + private static $instance; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.11.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $entity_service; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.11.0 |
|
| 43 | + * @access private |
|
| 44 | + * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 45 | + */ |
|
| 46 | + private $configuration_service; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 50 | + * |
|
| 51 | + * @since 3.11.0 |
|
| 52 | + * @access private |
|
| 53 | + * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 54 | + */ |
|
| 55 | + private $input_element; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 59 | + * |
|
| 60 | + * @since 3.13.0 |
|
| 61 | + * @access protected |
|
| 62 | + * @var \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 63 | + */ |
|
| 64 | + private $radio_input_element; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 68 | + * |
|
| 69 | + * @since 3.11.0 |
|
| 70 | + * @access private |
|
| 71 | + * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 72 | + */ |
|
| 73 | + private $language_select_element; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 77 | + * |
|
| 78 | + * @since 3.11.0 |
|
| 79 | + * @access private |
|
| 80 | + * @var \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 81 | + */ |
|
| 82 | + private $publisher_element; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Create a {@link Wordlift_Admin_Settings_Page} instance. |
|
| 86 | + * |
|
| 87 | + * @since 3.11.0 |
|
| 88 | + * |
|
| 89 | + * @param \Wordlift_Configuration_Service $configuration_service |
|
| 90 | + * @param \Wordlift_Entity_Service $entity_service |
|
| 91 | + * @param \Wordlift_Admin_Input_Element $input_element |
|
| 92 | + * @param \Wordlift_Admin_Language_Select_Element $language_select_element |
|
| 93 | + * @param \Wordlift_Admin_Publisher_Element $publisher_element |
|
| 94 | + * @param \Wordlift_Admin_Radio_Input_Element $radio_input_element |
|
| 95 | + */ |
|
| 96 | + function __construct( $configuration_service, $entity_service, $input_element, $language_select_element, $publisher_element, $radio_input_element ) { |
|
| 97 | + |
|
| 98 | + $this->configuration_service = $configuration_service; |
|
| 99 | + $this->entity_service = $entity_service; |
|
| 100 | + |
|
| 101 | + // Set a reference to the UI elements. |
|
| 102 | + $this->input_element = $input_element; |
|
| 103 | + $this->radio_input_element = $radio_input_element; |
|
| 104 | + $this->language_select_element = $language_select_element; |
|
| 105 | + $this->publisher_element = $publisher_element; |
|
| 106 | + |
|
| 107 | + self::$instance = $this; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Get the singleton instance of the Notice service. |
|
| 112 | + * |
|
| 113 | + * @since 3.14.0 |
|
| 114 | + * @return \Wordlift_Admin_Settings_Page The singleton instance of the settings page service. |
|
| 115 | + */ |
|
| 116 | + public static function get_instance() { |
|
| 117 | + |
|
| 118 | + return self::$instance; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @inheritdoc |
|
| 123 | + */ |
|
| 124 | + function get_parent_slug() { |
|
| 125 | + |
|
| 126 | + return 'wl_admin_menu'; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @inheritdoc |
|
| 131 | + */ |
|
| 132 | + function get_capability() { |
|
| 133 | + |
|
| 134 | + return 'manage_options'; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @inheritdoc |
|
| 139 | + */ |
|
| 140 | + function get_page_title() { |
|
| 141 | + |
|
| 142 | + return 'WorldLift Settings'; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @inheritdoc |
|
| 147 | + */ |
|
| 148 | + function get_menu_title() { |
|
| 149 | + |
|
| 150 | + return 'Settings'; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @inheritdoc |
|
| 155 | + */ |
|
| 156 | + function get_menu_slug() { |
|
| 157 | + |
|
| 158 | + return 'wl_configuration_admin_menu'; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @inheritdoc |
|
| 163 | + */ |
|
| 164 | + function get_partial_name() { |
|
| 165 | + |
|
| 166 | + return 'wordlift-admin-settings-page.php'; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @inheritdoc |
|
| 171 | + */ |
|
| 172 | + public function enqueue_scripts() { |
|
| 173 | + |
|
| 174 | + // Enqueue the media scripts to be used for the publisher's logo selection. |
|
| 175 | + wp_enqueue_media(); |
|
| 176 | + |
|
| 177 | + // JavaScript required for the settings page. |
|
| 178 | + // @todo: try to move to the `wordlift-admin.bundle.js`. |
|
| 179 | + wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.js', array( 'wp-util' ) ); |
|
| 180 | + |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Configure all the configuration parameters. |
|
| 185 | + * |
|
| 186 | + * Called by the *admin_init* hook. |
|
| 187 | + * |
|
| 188 | + * @since 3.11.0 |
|
| 189 | + */ |
|
| 190 | + function admin_init() { |
|
| 191 | + |
|
| 192 | + // Register WordLift's general settings, providing our own sanitize callback |
|
| 193 | + // which will also check whether the user filled the WL Publisher form. |
|
| 194 | + register_setting( |
|
| 195 | + 'wl_general_settings', |
|
| 196 | + 'wl_general_settings', |
|
| 197 | + array( $this, 'sanitize_callback', ) |
|
| 198 | + ); |
|
| 199 | + |
|
| 200 | + // Add the general settings section. |
|
| 201 | + add_settings_section( |
|
| 202 | + 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
|
| 203 | + '', // Section header. |
|
| 204 | + '', // Callback used to render the description of the section. |
|
| 205 | + 'wl_general_settings' // Page on which to add this section of options. |
|
| 206 | + ); |
|
| 207 | + |
|
| 208 | + $key_args = array( |
|
| 209 | + 'id' => 'wl-key', |
|
| 210 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 211 | + 'value' => $this->configuration_service->get_key(), |
|
| 212 | + 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ), |
|
| 213 | + ); |
|
| 214 | + |
|
| 215 | + // Set the class for the key field based on the validity of the key. |
|
| 216 | + // Class should be "untouched" for an empty (virgin) value, "valid" |
|
| 217 | + // if the key is valid, or "invalid" otherwise. |
|
| 218 | + $validation_service = new Wordlift_Key_Validation_Service( $this->configuration_service ); |
|
| 219 | + |
|
| 220 | + if ( empty( $key_args['value'] ) ) { |
|
| 221 | + $key_args['css_class'] = 'untouched'; |
|
| 222 | + } elseif ( $validation_service->is_valid( $key_args['value'] ) ) { |
|
| 223 | + $key_args['css_class'] = 'valid'; |
|
| 224 | + } else { |
|
| 225 | + $key_args['css_class'] = 'invalid'; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + // Add the `key` field. |
|
| 229 | + add_settings_field( |
|
| 230 | + 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 231 | + _x( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 232 | + // The name of the function responsible for rendering the option interface. |
|
| 233 | + array( $this->input_element, 'render', ), |
|
| 234 | + 'wl_general_settings', // The page on which this option will be displayed. |
|
| 235 | + 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 236 | + $key_args // The array of arguments to pass to the callback. In this case, just a description. |
|
| 237 | + ); |
|
| 238 | + |
|
| 239 | + // Entity Base Path input. |
|
| 240 | + $entity_base_path_args = array( |
|
| 241 | + // The array of arguments to pass to the callback. In this case, just a description. |
|
| 242 | + 'id' => 'wl-entity-base-path', |
|
| 243 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 244 | + 'value' => $this->configuration_service->get_entity_base_path(), |
|
| 245 | + 'description' => sprintf( _x( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ), |
|
| 246 | + ); |
|
| 247 | + |
|
| 248 | + $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
|
| 249 | + |
|
| 250 | + // Add the `wl_entity_base_path` field. |
|
| 251 | + add_settings_field( |
|
| 252 | + 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 253 | + _x( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 254 | + // The name of the function responsible for rendering the option interface |
|
| 255 | + array( $this->input_element, 'render', ), |
|
| 256 | + 'wl_general_settings', // The page on which this option will be displayed |
|
| 257 | + 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 258 | + $entity_base_path_args |
|
| 259 | + ); |
|
| 260 | + |
|
| 261 | + // Add the `language_name` field. |
|
| 262 | + add_settings_field( |
|
| 263 | + 'wl-site-language', |
|
| 264 | + _x( 'Site Language', 'wordlift' ), |
|
| 265 | + array( $this->language_select_element, 'render' ), |
|
| 266 | + 'wl_general_settings', |
|
| 267 | + 'wl_general_settings_section', |
|
| 268 | + array( |
|
| 269 | + // The array of arguments to pass to the callback. In this case, just a description. |
|
| 270 | + 'id' => 'wl-site-language', |
|
| 271 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 272 | + 'value' => $this->configuration_service->get_language_code(), |
|
| 273 | + 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 274 | + ) |
|
| 275 | + ); |
|
| 276 | + |
|
| 277 | + // Add the `publisher` field. |
|
| 278 | + add_settings_field( |
|
| 279 | + 'wl-publisher-id', |
|
| 280 | + _x( 'Publisher', 'wordlift' ), |
|
| 281 | + array( $this->publisher_element, 'render' ), |
|
| 282 | + 'wl_general_settings', |
|
| 283 | + 'wl_general_settings_section', |
|
| 284 | + array( |
|
| 285 | + 'id' => 'wl-publisher-id', |
|
| 286 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 287 | + ) |
|
| 288 | + ); |
|
| 289 | + |
|
| 290 | + // Add the `link by default` field. |
|
| 291 | + add_settings_field( |
|
| 292 | + 'wl-link-by-default', |
|
| 293 | + _x( 'Link by Default', 'wordlift' ), |
|
| 294 | + array( $this->radio_input_element, 'render' ), |
|
| 295 | + 'wl_general_settings', |
|
| 296 | + 'wl_general_settings_section', |
|
| 297 | + array( |
|
| 298 | + 'id' => 'wl-link-by-default', |
|
| 299 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 300 | + 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
|
| 301 | + 'description' => _x( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 302 | + ) |
|
| 303 | + ); |
|
| 304 | + |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Sanitize the configuration settings to be stored. |
|
| 309 | + * |
|
| 310 | + * If a new entity is being created for the publisher, create it and set The |
|
| 311 | + * publisher setting. |
|
| 312 | + * |
|
| 313 | + * @since 3.11.0 |
|
| 314 | + * |
|
| 315 | + * @param array $input The configuration settings array. |
|
| 316 | + * |
|
| 317 | + * @return array The sanitized input array. |
|
| 318 | + */ |
|
| 319 | + function sanitize_callback( $input ) { |
|
| 320 | + |
|
| 321 | + // Check whether a publisher name has been set. |
|
| 322 | + if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { |
|
| 323 | + $name = $_POST['wl_publisher']['name']; |
|
| 324 | + $type = $_POST['wl_publisher']['type']; |
|
| 325 | + $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; |
|
| 326 | + |
|
| 327 | + // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
|
| 328 | + $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 329 | + |
|
| 330 | + // Create an entity for the publisher and assign it to the input |
|
| 331 | + // parameter which WordPress automatically saves into the settings. |
|
| 332 | + $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + return $input; |
|
| 336 | + } |
|
| 337 | 337 | |
| 338 | 338 | } |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | * @param \Wordlift_Admin_Publisher_Element $publisher_element |
| 94 | 94 | * @param \Wordlift_Admin_Radio_Input_Element $radio_input_element |
| 95 | 95 | */ |
| 96 | - function __construct( $configuration_service, $entity_service, $input_element, $language_select_element, $publisher_element, $radio_input_element ) { |
|
| 96 | + function __construct($configuration_service, $entity_service, $input_element, $language_select_element, $publisher_element, $radio_input_element) { |
|
| 97 | 97 | |
| 98 | 98 | $this->configuration_service = $configuration_service; |
| 99 | 99 | $this->entity_service = $entity_service; |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | |
| 177 | 177 | // JavaScript required for the settings page. |
| 178 | 178 | // @todo: try to move to the `wordlift-admin.bundle.js`. |
| 179 | - wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.js', array( 'wp-util' ) ); |
|
| 179 | + wp_enqueue_script('wordlift-admin-settings-page', plugin_dir_url(dirname(__FILE__)).'admin/js/1/settings.js', array('wp-util')); |
|
| 180 | 180 | |
| 181 | 181 | } |
| 182 | 182 | |
@@ -194,32 +194,32 @@ discard block |
||
| 194 | 194 | register_setting( |
| 195 | 195 | 'wl_general_settings', |
| 196 | 196 | 'wl_general_settings', |
| 197 | - array( $this, 'sanitize_callback', ) |
|
| 197 | + array($this, 'sanitize_callback',) |
|
| 198 | 198 | ); |
| 199 | 199 | |
| 200 | 200 | // Add the general settings section. |
| 201 | 201 | add_settings_section( |
| 202 | 202 | 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
| 203 | - '', // Section header. |
|
| 204 | - '', // Callback used to render the description of the section. |
|
| 203 | + '', // Section header. |
|
| 204 | + '', // Callback used to render the description of the section. |
|
| 205 | 205 | 'wl_general_settings' // Page on which to add this section of options. |
| 206 | 206 | ); |
| 207 | 207 | |
| 208 | 208 | $key_args = array( |
| 209 | 209 | 'id' => 'wl-key', |
| 210 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 210 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::KEY.']', |
|
| 211 | 211 | 'value' => $this->configuration_service->get_key(), |
| 212 | - 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ), |
|
| 212 | + 'description' => __('Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift'), |
|
| 213 | 213 | ); |
| 214 | 214 | |
| 215 | 215 | // Set the class for the key field based on the validity of the key. |
| 216 | 216 | // Class should be "untouched" for an empty (virgin) value, "valid" |
| 217 | 217 | // if the key is valid, or "invalid" otherwise. |
| 218 | - $validation_service = new Wordlift_Key_Validation_Service( $this->configuration_service ); |
|
| 218 | + $validation_service = new Wordlift_Key_Validation_Service($this->configuration_service); |
|
| 219 | 219 | |
| 220 | - if ( empty( $key_args['value'] ) ) { |
|
| 220 | + if (empty($key_args['value'])) { |
|
| 221 | 221 | $key_args['css_class'] = 'untouched'; |
| 222 | - } elseif ( $validation_service->is_valid( $key_args['value'] ) ) { |
|
| 222 | + } elseif ($validation_service->is_valid($key_args['value'])) { |
|
| 223 | 223 | $key_args['css_class'] = 'valid'; |
| 224 | 224 | } else { |
| 225 | 225 | $key_args['css_class'] = 'invalid'; |
@@ -227,12 +227,12 @@ discard block |
||
| 227 | 227 | |
| 228 | 228 | // Add the `key` field. |
| 229 | 229 | add_settings_field( |
| 230 | - 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 231 | - _x( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 230 | + 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 231 | + _x('WordLift Key', 'wordlift'), // The label to the left of the option interface element. |
|
| 232 | 232 | // The name of the function responsible for rendering the option interface. |
| 233 | - array( $this->input_element, 'render', ), |
|
| 234 | - 'wl_general_settings', // The page on which this option will be displayed. |
|
| 235 | - 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 233 | + array($this->input_element, 'render',), |
|
| 234 | + 'wl_general_settings', // The page on which this option will be displayed. |
|
| 235 | + 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 236 | 236 | $key_args // The array of arguments to pass to the callback. In this case, just a description. |
| 237 | 237 | ); |
| 238 | 238 | |
@@ -240,65 +240,65 @@ discard block |
||
| 240 | 240 | $entity_base_path_args = array( |
| 241 | 241 | // The array of arguments to pass to the callback. In this case, just a description. |
| 242 | 242 | 'id' => 'wl-entity-base-path', |
| 243 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 243 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY.']', |
|
| 244 | 244 | 'value' => $this->configuration_service->get_entity_base_path(), |
| 245 | - 'description' => sprintf( _x( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ), |
|
| 245 | + 'description' => sprintf(_x('All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift'), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary'), |
|
| 246 | 246 | ); |
| 247 | 247 | |
| 248 | 248 | $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
| 249 | 249 | |
| 250 | 250 | // Add the `wl_entity_base_path` field. |
| 251 | 251 | add_settings_field( |
| 252 | - 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 253 | - _x( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 252 | + 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 253 | + _x('Entity Base Path', 'wordlift'), // The label to the left of the option interface element |
|
| 254 | 254 | // The name of the function responsible for rendering the option interface |
| 255 | - array( $this->input_element, 'render', ), |
|
| 256 | - 'wl_general_settings', // The page on which this option will be displayed |
|
| 257 | - 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 255 | + array($this->input_element, 'render',), |
|
| 256 | + 'wl_general_settings', // The page on which this option will be displayed |
|
| 257 | + 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 258 | 258 | $entity_base_path_args |
| 259 | 259 | ); |
| 260 | 260 | |
| 261 | 261 | // Add the `language_name` field. |
| 262 | 262 | add_settings_field( |
| 263 | 263 | 'wl-site-language', |
| 264 | - _x( 'Site Language', 'wordlift' ), |
|
| 265 | - array( $this->language_select_element, 'render' ), |
|
| 264 | + _x('Site Language', 'wordlift'), |
|
| 265 | + array($this->language_select_element, 'render'), |
|
| 266 | 266 | 'wl_general_settings', |
| 267 | 267 | 'wl_general_settings_section', |
| 268 | 268 | array( |
| 269 | 269 | // The array of arguments to pass to the callback. In this case, just a description. |
| 270 | 270 | 'id' => 'wl-site-language', |
| 271 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 271 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::LANGUAGE.']', |
|
| 272 | 272 | 'value' => $this->configuration_service->get_language_code(), |
| 273 | - 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 273 | + 'description' => __('Each WordLift Key can be used only in one language. Pick yours.', 'wordlift'), |
|
| 274 | 274 | ) |
| 275 | 275 | ); |
| 276 | 276 | |
| 277 | 277 | // Add the `publisher` field. |
| 278 | 278 | add_settings_field( |
| 279 | 279 | 'wl-publisher-id', |
| 280 | - _x( 'Publisher', 'wordlift' ), |
|
| 281 | - array( $this->publisher_element, 'render' ), |
|
| 280 | + _x('Publisher', 'wordlift'), |
|
| 281 | + array($this->publisher_element, 'render'), |
|
| 282 | 282 | 'wl_general_settings', |
| 283 | 283 | 'wl_general_settings_section', |
| 284 | 284 | array( |
| 285 | 285 | 'id' => 'wl-publisher-id', |
| 286 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 286 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::PUBLISHER_ID.']', |
|
| 287 | 287 | ) |
| 288 | 288 | ); |
| 289 | 289 | |
| 290 | 290 | // Add the `link by default` field. |
| 291 | 291 | add_settings_field( |
| 292 | 292 | 'wl-link-by-default', |
| 293 | - _x( 'Link by Default', 'wordlift' ), |
|
| 294 | - array( $this->radio_input_element, 'render' ), |
|
| 293 | + _x('Link by Default', 'wordlift'), |
|
| 294 | + array($this->radio_input_element, 'render'), |
|
| 295 | 295 | 'wl_general_settings', |
| 296 | 296 | 'wl_general_settings_section', |
| 297 | 297 | array( |
| 298 | 298 | 'id' => 'wl-link-by-default', |
| 299 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 299 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::LINK_BY_DEFAULT.']', |
|
| 300 | 300 | 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
| 301 | - 'description' => _x( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 301 | + 'description' => _x('Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift'), |
|
| 302 | 302 | ) |
| 303 | 303 | ); |
| 304 | 304 | |
@@ -316,20 +316,20 @@ discard block |
||
| 316 | 316 | * |
| 317 | 317 | * @return array The sanitized input array. |
| 318 | 318 | */ |
| 319 | - function sanitize_callback( $input ) { |
|
| 319 | + function sanitize_callback($input) { |
|
| 320 | 320 | |
| 321 | 321 | // Check whether a publisher name has been set. |
| 322 | - if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { |
|
| 322 | + if (isset($_POST['wl_publisher']) && ! empty($_POST['wl_publisher']['name'])) { |
|
| 323 | 323 | $name = $_POST['wl_publisher']['name']; |
| 324 | 324 | $type = $_POST['wl_publisher']['type']; |
| 325 | 325 | $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; |
| 326 | 326 | |
| 327 | 327 | // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
| 328 | - $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 328 | + $type_uri = sprintf('http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person'); |
|
| 329 | 329 | |
| 330 | 330 | // Create an entity for the publisher and assign it to the input |
| 331 | 331 | // parameter which WordPress automatically saves into the settings. |
| 332 | - $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 332 | + $input['publisher_id'] = $this->entity_service->create($name, $type_uri, $thumbnail_id, 'publish'); |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | return $input; |
@@ -22,163 +22,163 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class Wordlift_Admin { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The ID of this plugin. |
|
| 27 | - * |
|
| 28 | - * @since 1.0.0 |
|
| 29 | - * @access private |
|
| 30 | - * @var string $plugin_name The ID of this plugin. |
|
| 31 | - */ |
|
| 32 | - private $plugin_name; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The version of this plugin. |
|
| 36 | - * |
|
| 37 | - * @since 1.0.0 |
|
| 38 | - * @access private |
|
| 39 | - * @var string $version The current version of this plugin. |
|
| 40 | - */ |
|
| 41 | - private $version; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 45 | - * |
|
| 46 | - * @since 3.14.0 |
|
| 47 | - * @access private |
|
| 48 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 49 | - */ |
|
| 50 | - private $configuration_service; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The {@link Wordlift_User_Service} instance. |
|
| 54 | - * |
|
| 55 | - * @since 3.14.0 |
|
| 56 | - * @access private |
|
| 57 | - * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 58 | - */ |
|
| 59 | - private $user_service; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Initialize the class and set its properties. |
|
| 63 | - * |
|
| 64 | - * @since 1.0.0 |
|
| 65 | - * |
|
| 66 | - * @param string $plugin_name The name of this plugin. |
|
| 67 | - * @param string $version The version of this plugin. |
|
| 68 | - * @param \Wordlift_Configuration_Service $configuration_service The configuration service. |
|
| 69 | - * @param \Wordlift_Notice_Service $notice_service The notice service. |
|
| 70 | - * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 71 | - */ |
|
| 72 | - public function __construct( $plugin_name, $version, $configuration_service, $notice_service, $user_service ) { |
|
| 73 | - |
|
| 74 | - $this->plugin_name = $plugin_name; |
|
| 75 | - $this->version = $version; |
|
| 76 | - |
|
| 77 | - $this->configuration_service = $configuration_service; |
|
| 78 | - $this->user_service = $user_service; |
|
| 79 | - |
|
| 80 | - $dataset_uri = $configuration_service->get_dataset_uri(); |
|
| 81 | - $key = $configuration_service->get_key(); |
|
| 82 | - |
|
| 83 | - if ( empty( $dataset_uri ) ) { |
|
| 84 | - $settings_page = Wordlift_Admin_Settings_Page::get_instance(); |
|
| 85 | - if ( empty( $key ) ) { |
|
| 86 | - $error = sprintf( esc_html__( "WordLift's key isn't set, please open the %s to set WordLift's key.", 'wordlift' ), '<a href="' . $settings_page->get_url() . '">' . esc_html__( 'settings page', 'wordlift' ) . '</a>' ); |
|
| 87 | - } else { |
|
| 88 | - $error = sprintf( esc_html__( "WordLift's dataset URI is not configured: please open the %s to set WordLift's key again.", 'wordlift' ), '<a href="' . $settings_page->get_url() . '">' . esc_html__( 'settings page', 'wordlift' ) . '</a>' ); |
|
| 89 | - } |
|
| 90 | - $notice_service->add_error( $error ); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Register the stylesheets for the admin area. |
|
| 97 | - * |
|
| 98 | - * @since 1.0.0 |
|
| 99 | - */ |
|
| 100 | - public function enqueue_styles() { |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * This function is provided for demonstration purposes only. |
|
| 104 | - * |
|
| 105 | - * An instance of this class should be passed to the run() function |
|
| 106 | - * defined in Wordlift_Loader as all of the hooks are defined |
|
| 107 | - * in that particular class. |
|
| 108 | - * |
|
| 109 | - * The Wordlift_Loader will then create the relationship |
|
| 110 | - * between the defined hooks and the functions defined in this |
|
| 111 | - * class. |
|
| 112 | - */ |
|
| 113 | - |
|
| 114 | - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' ); |
|
| 115 | - |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Register the JavaScript for the admin area. |
|
| 120 | - * |
|
| 121 | - * @since 1.0.0 |
|
| 122 | - */ |
|
| 123 | - public function enqueue_scripts() { |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * This function is provided for demonstration purposes only. |
|
| 127 | - * |
|
| 128 | - * An instance of this class should be passed to the run() function |
|
| 129 | - * defined in Wordlift_Loader as all of the hooks are defined |
|
| 130 | - * in that particular class. |
|
| 131 | - * |
|
| 132 | - * The Wordlift_Loader will then create the relationship |
|
| 133 | - * between the defined hooks and the functions defined in this |
|
| 134 | - * class. |
|
| 135 | - */ |
|
| 136 | - |
|
| 137 | - // Enqueue the admin scripts. |
|
| 138 | - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/1/admin.js', array( |
|
| 139 | - 'jquery', |
|
| 140 | - 'underscore', |
|
| 141 | - 'backbone', |
|
| 142 | - ), $this->version, false ); |
|
| 143 | - |
|
| 144 | - // Set the basic params. |
|
| 145 | - $params = array( |
|
| 146 | - // @todo scripts in admin should use wp.post. |
|
| 147 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 148 | - // @todo remove specific actions from settings. |
|
| 149 | - 'action' => 'entity_by_title', |
|
| 150 | - 'datasetUri' => $this->configuration_service->get_dataset_uri(), |
|
| 151 | - 'language' => $this->configuration_service->get_language_code(), |
|
| 152 | - 'link_by_default' => $this->configuration_service->is_link_by_default(), |
|
| 153 | - // Whether the current user is allowed to create new entities. |
|
| 154 | - // |
|
| 155 | - // @see https://github.com/insideout10/wordlift-plugin/issues/561 |
|
| 156 | - 'can_create_entities' => current_user_can( 'edit_wordlift_entities' ) ? 'yes' : 'no', |
|
| 157 | - 'l10n' => array( |
|
| 158 | - 'You already published an entity with the same name' => __( 'You already published an entity with the same name: ', 'wordlift' ), |
|
| 159 | - 'logo_selection_title' => __( 'WordLift Choose Logo', 'wordlift' ), |
|
| 160 | - 'logo_selection_button' => array( 'text' => __( 'Choose Logo', 'wordlift' ) ), |
|
| 161 | - ), |
|
| 162 | - 'wl_autocomplete_nonce' => wp_create_nonce( 'wordlift_autocomplete' ), |
|
| 163 | - ); |
|
| 164 | - |
|
| 165 | - // Set post-related values if there's a current post. |
|
| 166 | - if ( null !== $post = $entity_being_edited = get_post() ) { |
|
| 167 | - |
|
| 168 | - $params['post_id'] = $entity_being_edited->ID; |
|
| 169 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 170 | - $params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_service->is_entity( $post->ID ) && is_numeric( get_the_ID() ); |
|
| 171 | - // We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it |
|
| 172 | - // from the results, since we don't want the current entity to be discovered by the analysis. |
|
| 173 | - // |
|
| 174 | - // See https://github.com/insideout10/wordlift-plugin/issues/345 |
|
| 175 | - $params['itemId'] = $entity_service->get_uri( $entity_being_edited->ID ); |
|
| 176 | - |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Finally output the params as `wlSettings` for JavaScript code. |
|
| 180 | - wp_localize_script( $this->plugin_name, 'wlSettings', $params ); |
|
| 181 | - |
|
| 182 | - } |
|
| 25 | + /** |
|
| 26 | + * The ID of this plugin. |
|
| 27 | + * |
|
| 28 | + * @since 1.0.0 |
|
| 29 | + * @access private |
|
| 30 | + * @var string $plugin_name The ID of this plugin. |
|
| 31 | + */ |
|
| 32 | + private $plugin_name; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The version of this plugin. |
|
| 36 | + * |
|
| 37 | + * @since 1.0.0 |
|
| 38 | + * @access private |
|
| 39 | + * @var string $version The current version of this plugin. |
|
| 40 | + */ |
|
| 41 | + private $version; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 45 | + * |
|
| 46 | + * @since 3.14.0 |
|
| 47 | + * @access private |
|
| 48 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 49 | + */ |
|
| 50 | + private $configuration_service; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The {@link Wordlift_User_Service} instance. |
|
| 54 | + * |
|
| 55 | + * @since 3.14.0 |
|
| 56 | + * @access private |
|
| 57 | + * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 58 | + */ |
|
| 59 | + private $user_service; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Initialize the class and set its properties. |
|
| 63 | + * |
|
| 64 | + * @since 1.0.0 |
|
| 65 | + * |
|
| 66 | + * @param string $plugin_name The name of this plugin. |
|
| 67 | + * @param string $version The version of this plugin. |
|
| 68 | + * @param \Wordlift_Configuration_Service $configuration_service The configuration service. |
|
| 69 | + * @param \Wordlift_Notice_Service $notice_service The notice service. |
|
| 70 | + * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 71 | + */ |
|
| 72 | + public function __construct( $plugin_name, $version, $configuration_service, $notice_service, $user_service ) { |
|
| 73 | + |
|
| 74 | + $this->plugin_name = $plugin_name; |
|
| 75 | + $this->version = $version; |
|
| 76 | + |
|
| 77 | + $this->configuration_service = $configuration_service; |
|
| 78 | + $this->user_service = $user_service; |
|
| 79 | + |
|
| 80 | + $dataset_uri = $configuration_service->get_dataset_uri(); |
|
| 81 | + $key = $configuration_service->get_key(); |
|
| 82 | + |
|
| 83 | + if ( empty( $dataset_uri ) ) { |
|
| 84 | + $settings_page = Wordlift_Admin_Settings_Page::get_instance(); |
|
| 85 | + if ( empty( $key ) ) { |
|
| 86 | + $error = sprintf( esc_html__( "WordLift's key isn't set, please open the %s to set WordLift's key.", 'wordlift' ), '<a href="' . $settings_page->get_url() . '">' . esc_html__( 'settings page', 'wordlift' ) . '</a>' ); |
|
| 87 | + } else { |
|
| 88 | + $error = sprintf( esc_html__( "WordLift's dataset URI is not configured: please open the %s to set WordLift's key again.", 'wordlift' ), '<a href="' . $settings_page->get_url() . '">' . esc_html__( 'settings page', 'wordlift' ) . '</a>' ); |
|
| 89 | + } |
|
| 90 | + $notice_service->add_error( $error ); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Register the stylesheets for the admin area. |
|
| 97 | + * |
|
| 98 | + * @since 1.0.0 |
|
| 99 | + */ |
|
| 100 | + public function enqueue_styles() { |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * This function is provided for demonstration purposes only. |
|
| 104 | + * |
|
| 105 | + * An instance of this class should be passed to the run() function |
|
| 106 | + * defined in Wordlift_Loader as all of the hooks are defined |
|
| 107 | + * in that particular class. |
|
| 108 | + * |
|
| 109 | + * The Wordlift_Loader will then create the relationship |
|
| 110 | + * between the defined hooks and the functions defined in this |
|
| 111 | + * class. |
|
| 112 | + */ |
|
| 113 | + |
|
| 114 | + wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' ); |
|
| 115 | + |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Register the JavaScript for the admin area. |
|
| 120 | + * |
|
| 121 | + * @since 1.0.0 |
|
| 122 | + */ |
|
| 123 | + public function enqueue_scripts() { |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * This function is provided for demonstration purposes only. |
|
| 127 | + * |
|
| 128 | + * An instance of this class should be passed to the run() function |
|
| 129 | + * defined in Wordlift_Loader as all of the hooks are defined |
|
| 130 | + * in that particular class. |
|
| 131 | + * |
|
| 132 | + * The Wordlift_Loader will then create the relationship |
|
| 133 | + * between the defined hooks and the functions defined in this |
|
| 134 | + * class. |
|
| 135 | + */ |
|
| 136 | + |
|
| 137 | + // Enqueue the admin scripts. |
|
| 138 | + wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/1/admin.js', array( |
|
| 139 | + 'jquery', |
|
| 140 | + 'underscore', |
|
| 141 | + 'backbone', |
|
| 142 | + ), $this->version, false ); |
|
| 143 | + |
|
| 144 | + // Set the basic params. |
|
| 145 | + $params = array( |
|
| 146 | + // @todo scripts in admin should use wp.post. |
|
| 147 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 148 | + // @todo remove specific actions from settings. |
|
| 149 | + 'action' => 'entity_by_title', |
|
| 150 | + 'datasetUri' => $this->configuration_service->get_dataset_uri(), |
|
| 151 | + 'language' => $this->configuration_service->get_language_code(), |
|
| 152 | + 'link_by_default' => $this->configuration_service->is_link_by_default(), |
|
| 153 | + // Whether the current user is allowed to create new entities. |
|
| 154 | + // |
|
| 155 | + // @see https://github.com/insideout10/wordlift-plugin/issues/561 |
|
| 156 | + 'can_create_entities' => current_user_can( 'edit_wordlift_entities' ) ? 'yes' : 'no', |
|
| 157 | + 'l10n' => array( |
|
| 158 | + 'You already published an entity with the same name' => __( 'You already published an entity with the same name: ', 'wordlift' ), |
|
| 159 | + 'logo_selection_title' => __( 'WordLift Choose Logo', 'wordlift' ), |
|
| 160 | + 'logo_selection_button' => array( 'text' => __( 'Choose Logo', 'wordlift' ) ), |
|
| 161 | + ), |
|
| 162 | + 'wl_autocomplete_nonce' => wp_create_nonce( 'wordlift_autocomplete' ), |
|
| 163 | + ); |
|
| 164 | + |
|
| 165 | + // Set post-related values if there's a current post. |
|
| 166 | + if ( null !== $post = $entity_being_edited = get_post() ) { |
|
| 167 | + |
|
| 168 | + $params['post_id'] = $entity_being_edited->ID; |
|
| 169 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 170 | + $params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_service->is_entity( $post->ID ) && is_numeric( get_the_ID() ); |
|
| 171 | + // We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it |
|
| 172 | + // from the results, since we don't want the current entity to be discovered by the analysis. |
|
| 173 | + // |
|
| 174 | + // See https://github.com/insideout10/wordlift-plugin/issues/345 |
|
| 175 | + $params['itemId'] = $entity_service->get_uri( $entity_being_edited->ID ); |
|
| 176 | + |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // Finally output the params as `wlSettings` for JavaScript code. |
|
| 180 | + wp_localize_script( $this->plugin_name, 'wlSettings', $params ); |
|
| 181 | + |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | 184 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * @param \Wordlift_Notice_Service $notice_service The notice service. |
| 70 | 70 | * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
| 71 | 71 | */ |
| 72 | - public function __construct( $plugin_name, $version, $configuration_service, $notice_service, $user_service ) { |
|
| 72 | + public function __construct($plugin_name, $version, $configuration_service, $notice_service, $user_service) { |
|
| 73 | 73 | |
| 74 | 74 | $this->plugin_name = $plugin_name; |
| 75 | 75 | $this->version = $version; |
@@ -80,14 +80,14 @@ discard block |
||
| 80 | 80 | $dataset_uri = $configuration_service->get_dataset_uri(); |
| 81 | 81 | $key = $configuration_service->get_key(); |
| 82 | 82 | |
| 83 | - if ( empty( $dataset_uri ) ) { |
|
| 83 | + if (empty($dataset_uri)) { |
|
| 84 | 84 | $settings_page = Wordlift_Admin_Settings_Page::get_instance(); |
| 85 | - if ( empty( $key ) ) { |
|
| 86 | - $error = sprintf( esc_html__( "WordLift's key isn't set, please open the %s to set WordLift's key.", 'wordlift' ), '<a href="' . $settings_page->get_url() . '">' . esc_html__( 'settings page', 'wordlift' ) . '</a>' ); |
|
| 85 | + if (empty($key)) { |
|
| 86 | + $error = sprintf(esc_html__("WordLift's key isn't set, please open the %s to set WordLift's key.", 'wordlift'), '<a href="'.$settings_page->get_url().'">'.esc_html__('settings page', 'wordlift').'</a>'); |
|
| 87 | 87 | } else { |
| 88 | - $error = sprintf( esc_html__( "WordLift's dataset URI is not configured: please open the %s to set WordLift's key again.", 'wordlift' ), '<a href="' . $settings_page->get_url() . '">' . esc_html__( 'settings page', 'wordlift' ) . '</a>' ); |
|
| 88 | + $error = sprintf(esc_html__("WordLift's dataset URI is not configured: please open the %s to set WordLift's key again.", 'wordlift'), '<a href="'.$settings_page->get_url().'">'.esc_html__('settings page', 'wordlift').'</a>'); |
|
| 89 | 89 | } |
| 90 | - $notice_service->add_error( $error ); |
|
| 90 | + $notice_service->add_error($error); |
|
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | * class. |
| 112 | 112 | */ |
| 113 | 113 | |
| 114 | - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' ); |
|
| 114 | + wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__).'css/wordlift-admin.css', array(), $this->version, 'all'); |
|
| 115 | 115 | |
| 116 | 116 | } |
| 117 | 117 | |
@@ -135,16 +135,16 @@ discard block |
||
| 135 | 135 | */ |
| 136 | 136 | |
| 137 | 137 | // Enqueue the admin scripts. |
| 138 | - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/1/admin.js', array( |
|
| 138 | + wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__).'js/1/admin.js', array( |
|
| 139 | 139 | 'jquery', |
| 140 | 140 | 'underscore', |
| 141 | 141 | 'backbone', |
| 142 | - ), $this->version, false ); |
|
| 142 | + ), $this->version, false); |
|
| 143 | 143 | |
| 144 | 144 | // Set the basic params. |
| 145 | 145 | $params = array( |
| 146 | 146 | // @todo scripts in admin should use wp.post. |
| 147 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 147 | + 'ajax_url' => admin_url('admin-ajax.php'), |
|
| 148 | 148 | // @todo remove specific actions from settings. |
| 149 | 149 | 'action' => 'entity_by_title', |
| 150 | 150 | 'datasetUri' => $this->configuration_service->get_dataset_uri(), |
@@ -153,31 +153,31 @@ discard block |
||
| 153 | 153 | // Whether the current user is allowed to create new entities. |
| 154 | 154 | // |
| 155 | 155 | // @see https://github.com/insideout10/wordlift-plugin/issues/561 |
| 156 | - 'can_create_entities' => current_user_can( 'edit_wordlift_entities' ) ? 'yes' : 'no', |
|
| 156 | + 'can_create_entities' => current_user_can('edit_wordlift_entities') ? 'yes' : 'no', |
|
| 157 | 157 | 'l10n' => array( |
| 158 | - 'You already published an entity with the same name' => __( 'You already published an entity with the same name: ', 'wordlift' ), |
|
| 159 | - 'logo_selection_title' => __( 'WordLift Choose Logo', 'wordlift' ), |
|
| 160 | - 'logo_selection_button' => array( 'text' => __( 'Choose Logo', 'wordlift' ) ), |
|
| 158 | + 'You already published an entity with the same name' => __('You already published an entity with the same name: ', 'wordlift'), |
|
| 159 | + 'logo_selection_title' => __('WordLift Choose Logo', 'wordlift'), |
|
| 160 | + 'logo_selection_button' => array('text' => __('Choose Logo', 'wordlift')), |
|
| 161 | 161 | ), |
| 162 | - 'wl_autocomplete_nonce' => wp_create_nonce( 'wordlift_autocomplete' ), |
|
| 162 | + 'wl_autocomplete_nonce' => wp_create_nonce('wordlift_autocomplete'), |
|
| 163 | 163 | ); |
| 164 | 164 | |
| 165 | 165 | // Set post-related values if there's a current post. |
| 166 | - if ( null !== $post = $entity_being_edited = get_post() ) { |
|
| 166 | + if (null !== $post = $entity_being_edited = get_post()) { |
|
| 167 | 167 | |
| 168 | 168 | $params['post_id'] = $entity_being_edited->ID; |
| 169 | 169 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 170 | - $params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_service->is_entity( $post->ID ) && is_numeric( get_the_ID() ); |
|
| 170 | + $params['entityBeingEdited'] = isset($entity_being_edited->post_type) && $entity_service->is_entity($post->ID) && is_numeric(get_the_ID()); |
|
| 171 | 171 | // We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it |
| 172 | 172 | // from the results, since we don't want the current entity to be discovered by the analysis. |
| 173 | 173 | // |
| 174 | 174 | // See https://github.com/insideout10/wordlift-plugin/issues/345 |
| 175 | - $params['itemId'] = $entity_service->get_uri( $entity_being_edited->ID ); |
|
| 175 | + $params['itemId'] = $entity_service->get_uri($entity_being_edited->ID); |
|
| 176 | 176 | |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // Finally output the params as `wlSettings` for JavaScript code. |
| 180 | - wp_localize_script( $this->plugin_name, 'wlSettings', $params ); |
|
| 180 | + wp_localize_script($this->plugin_name, 'wlSettings', $params); |
|
| 181 | 181 | |
| 182 | 182 | } |
| 183 | 183 | |
@@ -19,360 +19,360 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class WL_Metabox_Field { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * A {@link Wordlift_Log_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @since 3.15.0 |
|
| 26 | - * @access protected |
|
| 27 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 28 | - */ |
|
| 29 | - protected $log; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The meta name for this field's value. |
|
| 33 | - * |
|
| 34 | - * @var string $meta_name The meta name for this field's value. |
|
| 35 | - */ |
|
| 36 | - public $meta_name; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The custom field settings. |
|
| 40 | - * |
|
| 41 | - * @var null|array $raw_custom_field The custom field settings. |
|
| 42 | - */ |
|
| 43 | - public $raw_custom_field; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The schema.org predicate. |
|
| 47 | - * |
|
| 48 | - * @var string $predicate The schema.org predicate. |
|
| 49 | - */ |
|
| 50 | - public $predicate; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The field's label. |
|
| 54 | - * |
|
| 55 | - * @var string $label The field's label. |
|
| 56 | - */ |
|
| 57 | - public $label; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The WordLift data type. |
|
| 61 | - * |
|
| 62 | - * @var string $expected_wl_type The WordLift data type. |
|
| 63 | - */ |
|
| 64 | - public $expected_wl_type; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The RDF data type. |
|
| 68 | - * |
|
| 69 | - * @var string $expected_uri_type The RDF data type. |
|
| 70 | - */ |
|
| 71 | - public $expected_uri_type; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The cardinality. |
|
| 75 | - * |
|
| 76 | - * @var int $cardinality The cardinality. |
|
| 77 | - */ |
|
| 78 | - public $cardinality; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * The current value. |
|
| 82 | - * |
|
| 83 | - * @var array $data The current value. |
|
| 84 | - */ |
|
| 85 | - public $data; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Create a {@link WL_Metabox_Field} instance. |
|
| 89 | - * |
|
| 90 | - * @param array $args An array of parameters. |
|
| 91 | - */ |
|
| 92 | - public function __construct( $args ) { |
|
| 93 | - |
|
| 94 | - $this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' ); |
|
| 95 | - |
|
| 96 | - if ( empty( $args ) ) { |
|
| 97 | - return; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // Save a copy of the custom field's params. |
|
| 101 | - $this->raw_custom_field = reset( $args ); |
|
| 102 | - |
|
| 103 | - // Extract meta name (post_meta key for the DB). |
|
| 104 | - $this->meta_name = key( $args ); |
|
| 105 | - |
|
| 106 | - // Extract linked data predicate. |
|
| 107 | - if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 108 | - $this->predicate = $this->raw_custom_field['predicate']; |
|
| 109 | - } else { |
|
| 110 | - return; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - // Extract human readable label. |
|
| 114 | - $exploded_predicate = explode( '/', $this->predicate ); |
|
| 115 | - |
|
| 116 | - // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
|
| 117 | - $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 118 | - |
|
| 119 | - // Extract field constraints (numerosity, expected type). |
|
| 120 | - // Default constaints: accept one string.. |
|
| 121 | - if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 122 | - $this->expected_wl_type = $this->raw_custom_field['type']; |
|
| 123 | - } else { |
|
| 124 | - $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $this->cardinality = 1; |
|
| 128 | - if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 129 | - |
|
| 130 | - $constraints = $this->raw_custom_field['constraints']; |
|
| 131 | - |
|
| 132 | - // Extract cardinality. |
|
| 133 | - if ( isset( $constraints['cardinality'] ) ) { |
|
| 134 | - $this->cardinality = $constraints['cardinality']; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
|
| 138 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) { |
|
| 139 | - $this->expected_uri_type = is_array( $constraints['uri_type'] ) |
|
| 140 | - ? $constraints['uri_type'] |
|
| 141 | - : array( $constraints['uri_type'] ); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Return nonce HTML. |
|
| 149 | - * |
|
| 150 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 151 | - */ |
|
| 152 | - public function html_nonce() { |
|
| 153 | - |
|
| 154 | - return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Verify nonce. |
|
| 159 | - * |
|
| 160 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 161 | - * |
|
| 162 | - * @return boolean Nonce verification. |
|
| 163 | - */ |
|
| 164 | - public function verify_nonce() { |
|
| 165 | - |
|
| 166 | - $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 167 | - $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 168 | - |
|
| 169 | - if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 170 | - return false; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Verify that the nonce is valid. |
|
| 174 | - return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Load data from DB and store the resulting array in $this->data. |
|
| 179 | - * |
|
| 180 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 181 | - */ |
|
| 182 | - public function get_data() { |
|
| 183 | - |
|
| 184 | - $data = get_post_meta( get_the_ID(), $this->meta_name ); |
|
| 185 | - |
|
| 186 | - // Values are always contained in an array (makes it easier to manage cardinality). |
|
| 187 | - if ( ! is_array( $data ) ) { |
|
| 188 | - $data = array( $data ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - $this->data = $data; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Sanitizes data before saving to DB. Default sanitization trashes empty |
|
| 196 | - * values. |
|
| 197 | - * |
|
| 198 | - * Stores the sanitized values into $this->data so they can be later processed. |
|
| 199 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 200 | - * |
|
| 201 | - * @param array $values Array of values to be sanitized and then stored into |
|
| 202 | - * $this->data. |
|
| 203 | - */ |
|
| 204 | - public function sanitize_data( $values ) { |
|
| 205 | - |
|
| 206 | - $sanitized_data = array(); |
|
| 207 | - |
|
| 208 | - if ( ! is_array( $values ) ) { |
|
| 209 | - $values = array( $values ); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - foreach ( $values as $value ) { |
|
| 213 | - $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 214 | - if ( ! is_null( $sanitized_value ) ) { |
|
| 215 | - $sanitized_data[] = $sanitized_value; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $this->data = $sanitized_data; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Sanitize a single value. Called from $this->sanitize_data. Default |
|
| 224 | - * sanitization excludes empty values. |
|
| 225 | - * |
|
| 226 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 227 | - * |
|
| 228 | - * @param string $value The value to sanitize. |
|
| 229 | - * |
|
| 230 | - * @return mixed Returns sanitized value, or null. |
|
| 231 | - */ |
|
| 232 | - public function sanitize_data_filter( $value ) { |
|
| 233 | - |
|
| 234 | - // TODO: all fields should provide their own sanitize which shouldn't |
|
| 235 | - // be part of a UI class. |
|
| 236 | - |
|
| 237 | - // If the field provides its own validation, use it. |
|
| 238 | - if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 239 | - return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 243 | - return $value; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return null; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Save data to DB. |
|
| 251 | - * |
|
| 252 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 253 | - * |
|
| 254 | - * @param array $values Array of values to be sanitized and then stored into $this->data. |
|
| 255 | - */ |
|
| 256 | - public function save_data( $values ) { |
|
| 257 | - |
|
| 258 | - // Will sanitize data and store them in $field->data. |
|
| 259 | - $this->sanitize_data( $values ); |
|
| 260 | - |
|
| 261 | - $entity_id = get_the_ID(); |
|
| 262 | - |
|
| 263 | - // Take away old values. |
|
| 264 | - delete_post_meta( $entity_id, $this->meta_name ); |
|
| 265 | - |
|
| 266 | - // insert new values, respecting cardinality. |
|
| 267 | - $single = ( 1 === $this->cardinality ); |
|
| 268 | - foreach ( $this->data as $value ) { |
|
| 269 | - add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Returns the HTML tag that will contain the Field. By default the we |
|
| 275 | - * return a <div> with data- attributes on cardinality and expected types. |
|
| 276 | - * |
|
| 277 | - * It is useful to provide data- attributes for the JS scripts. |
|
| 278 | - * |
|
| 279 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 280 | - */ |
|
| 281 | - public function html_wrapper_open() { |
|
| 282 | - |
|
| 283 | - return "<div class='wl-field' data-cardinality='$this->cardinality'>"; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Returns Field HTML (nonce included). |
|
| 288 | - * |
|
| 289 | - * Overwrite this method (or methods called from this method) in a child |
|
| 290 | - * class to obtain custom behaviour. |
|
| 291 | - */ |
|
| 292 | - public function html() { |
|
| 293 | - |
|
| 294 | - // Open main <div> for the Field. |
|
| 295 | - $html = $this->html_wrapper_open(); |
|
| 296 | - |
|
| 297 | - // Label. |
|
| 298 | - $html .= "<h3>$this->label</h3>"; |
|
| 299 | - |
|
| 300 | - // print nonce. |
|
| 301 | - $html .= $this->html_nonce(); |
|
| 302 | - |
|
| 303 | - // print data loaded from DB. |
|
| 304 | - $count = 0; |
|
| 305 | - if ( $this->data ) { |
|
| 306 | - foreach ( $this->data as $value ) { |
|
| 307 | - if ( $count < $this->cardinality ) { |
|
| 308 | - $html .= $this->html_input( $value ); |
|
| 309 | - } |
|
| 310 | - $count ++; |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - // Print the empty <input> to add new values. |
|
| 315 | - if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 316 | - $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 317 | - $count ++; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - // If cardinality allows it, print button to add new values. |
|
| 321 | - $html .= $this->get_add_button_html( $count ); |
|
| 322 | - |
|
| 323 | - // Close the HTML wrapper. |
|
| 324 | - $html .= $this->html_wrapper_close(); |
|
| 325 | - |
|
| 326 | - return $html; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Get the add button html. |
|
| 331 | - * |
|
| 332 | - * This function is protected, allowing extending class to further customize |
|
| 333 | - * the add button html code. |
|
| 334 | - * |
|
| 335 | - * @since 3.15.0 |
|
| 336 | - * |
|
| 337 | - * @param int $count The current number of values. |
|
| 338 | - * |
|
| 339 | - * @return string The add button html code. |
|
| 340 | - */ |
|
| 341 | - protected function get_add_button_html( $count ) { |
|
| 342 | - |
|
| 343 | - // If cardinality allows it, print button to add new values. |
|
| 344 | - if ( $count < $this->cardinality ) { |
|
| 345 | - return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>'; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - // Return an empty string. |
|
| 349 | - return ''; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Return a single <input> tag for the Field. |
|
| 354 | - * |
|
| 355 | - * @param mixed $value Input value. |
|
| 356 | - * |
|
| 357 | - * @return string The html code fragment. |
|
| 358 | - */ |
|
| 359 | - public function html_input( $value ) { |
|
| 360 | - $html = <<<EOF |
|
| 22 | + /** |
|
| 23 | + * A {@link Wordlift_Log_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @since 3.15.0 |
|
| 26 | + * @access protected |
|
| 27 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 28 | + */ |
|
| 29 | + protected $log; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The meta name for this field's value. |
|
| 33 | + * |
|
| 34 | + * @var string $meta_name The meta name for this field's value. |
|
| 35 | + */ |
|
| 36 | + public $meta_name; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The custom field settings. |
|
| 40 | + * |
|
| 41 | + * @var null|array $raw_custom_field The custom field settings. |
|
| 42 | + */ |
|
| 43 | + public $raw_custom_field; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The schema.org predicate. |
|
| 47 | + * |
|
| 48 | + * @var string $predicate The schema.org predicate. |
|
| 49 | + */ |
|
| 50 | + public $predicate; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The field's label. |
|
| 54 | + * |
|
| 55 | + * @var string $label The field's label. |
|
| 56 | + */ |
|
| 57 | + public $label; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The WordLift data type. |
|
| 61 | + * |
|
| 62 | + * @var string $expected_wl_type The WordLift data type. |
|
| 63 | + */ |
|
| 64 | + public $expected_wl_type; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The RDF data type. |
|
| 68 | + * |
|
| 69 | + * @var string $expected_uri_type The RDF data type. |
|
| 70 | + */ |
|
| 71 | + public $expected_uri_type; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The cardinality. |
|
| 75 | + * |
|
| 76 | + * @var int $cardinality The cardinality. |
|
| 77 | + */ |
|
| 78 | + public $cardinality; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * The current value. |
|
| 82 | + * |
|
| 83 | + * @var array $data The current value. |
|
| 84 | + */ |
|
| 85 | + public $data; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Create a {@link WL_Metabox_Field} instance. |
|
| 89 | + * |
|
| 90 | + * @param array $args An array of parameters. |
|
| 91 | + */ |
|
| 92 | + public function __construct( $args ) { |
|
| 93 | + |
|
| 94 | + $this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' ); |
|
| 95 | + |
|
| 96 | + if ( empty( $args ) ) { |
|
| 97 | + return; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // Save a copy of the custom field's params. |
|
| 101 | + $this->raw_custom_field = reset( $args ); |
|
| 102 | + |
|
| 103 | + // Extract meta name (post_meta key for the DB). |
|
| 104 | + $this->meta_name = key( $args ); |
|
| 105 | + |
|
| 106 | + // Extract linked data predicate. |
|
| 107 | + if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 108 | + $this->predicate = $this->raw_custom_field['predicate']; |
|
| 109 | + } else { |
|
| 110 | + return; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + // Extract human readable label. |
|
| 114 | + $exploded_predicate = explode( '/', $this->predicate ); |
|
| 115 | + |
|
| 116 | + // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
|
| 117 | + $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 118 | + |
|
| 119 | + // Extract field constraints (numerosity, expected type). |
|
| 120 | + // Default constaints: accept one string.. |
|
| 121 | + if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 122 | + $this->expected_wl_type = $this->raw_custom_field['type']; |
|
| 123 | + } else { |
|
| 124 | + $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $this->cardinality = 1; |
|
| 128 | + if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 129 | + |
|
| 130 | + $constraints = $this->raw_custom_field['constraints']; |
|
| 131 | + |
|
| 132 | + // Extract cardinality. |
|
| 133 | + if ( isset( $constraints['cardinality'] ) ) { |
|
| 134 | + $this->cardinality = $constraints['cardinality']; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
|
| 138 | + if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) { |
|
| 139 | + $this->expected_uri_type = is_array( $constraints['uri_type'] ) |
|
| 140 | + ? $constraints['uri_type'] |
|
| 141 | + : array( $constraints['uri_type'] ); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Return nonce HTML. |
|
| 149 | + * |
|
| 150 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 151 | + */ |
|
| 152 | + public function html_nonce() { |
|
| 153 | + |
|
| 154 | + return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Verify nonce. |
|
| 159 | + * |
|
| 160 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 161 | + * |
|
| 162 | + * @return boolean Nonce verification. |
|
| 163 | + */ |
|
| 164 | + public function verify_nonce() { |
|
| 165 | + |
|
| 166 | + $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 167 | + $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 168 | + |
|
| 169 | + if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 170 | + return false; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // Verify that the nonce is valid. |
|
| 174 | + return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Load data from DB and store the resulting array in $this->data. |
|
| 179 | + * |
|
| 180 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 181 | + */ |
|
| 182 | + public function get_data() { |
|
| 183 | + |
|
| 184 | + $data = get_post_meta( get_the_ID(), $this->meta_name ); |
|
| 185 | + |
|
| 186 | + // Values are always contained in an array (makes it easier to manage cardinality). |
|
| 187 | + if ( ! is_array( $data ) ) { |
|
| 188 | + $data = array( $data ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + $this->data = $data; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Sanitizes data before saving to DB. Default sanitization trashes empty |
|
| 196 | + * values. |
|
| 197 | + * |
|
| 198 | + * Stores the sanitized values into $this->data so they can be later processed. |
|
| 199 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 200 | + * |
|
| 201 | + * @param array $values Array of values to be sanitized and then stored into |
|
| 202 | + * $this->data. |
|
| 203 | + */ |
|
| 204 | + public function sanitize_data( $values ) { |
|
| 205 | + |
|
| 206 | + $sanitized_data = array(); |
|
| 207 | + |
|
| 208 | + if ( ! is_array( $values ) ) { |
|
| 209 | + $values = array( $values ); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + foreach ( $values as $value ) { |
|
| 213 | + $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 214 | + if ( ! is_null( $sanitized_value ) ) { |
|
| 215 | + $sanitized_data[] = $sanitized_value; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $this->data = $sanitized_data; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Sanitize a single value. Called from $this->sanitize_data. Default |
|
| 224 | + * sanitization excludes empty values. |
|
| 225 | + * |
|
| 226 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 227 | + * |
|
| 228 | + * @param string $value The value to sanitize. |
|
| 229 | + * |
|
| 230 | + * @return mixed Returns sanitized value, or null. |
|
| 231 | + */ |
|
| 232 | + public function sanitize_data_filter( $value ) { |
|
| 233 | + |
|
| 234 | + // TODO: all fields should provide their own sanitize which shouldn't |
|
| 235 | + // be part of a UI class. |
|
| 236 | + |
|
| 237 | + // If the field provides its own validation, use it. |
|
| 238 | + if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 239 | + return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 243 | + return $value; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return null; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Save data to DB. |
|
| 251 | + * |
|
| 252 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 253 | + * |
|
| 254 | + * @param array $values Array of values to be sanitized and then stored into $this->data. |
|
| 255 | + */ |
|
| 256 | + public function save_data( $values ) { |
|
| 257 | + |
|
| 258 | + // Will sanitize data and store them in $field->data. |
|
| 259 | + $this->sanitize_data( $values ); |
|
| 260 | + |
|
| 261 | + $entity_id = get_the_ID(); |
|
| 262 | + |
|
| 263 | + // Take away old values. |
|
| 264 | + delete_post_meta( $entity_id, $this->meta_name ); |
|
| 265 | + |
|
| 266 | + // insert new values, respecting cardinality. |
|
| 267 | + $single = ( 1 === $this->cardinality ); |
|
| 268 | + foreach ( $this->data as $value ) { |
|
| 269 | + add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Returns the HTML tag that will contain the Field. By default the we |
|
| 275 | + * return a <div> with data- attributes on cardinality and expected types. |
|
| 276 | + * |
|
| 277 | + * It is useful to provide data- attributes for the JS scripts. |
|
| 278 | + * |
|
| 279 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 280 | + */ |
|
| 281 | + public function html_wrapper_open() { |
|
| 282 | + |
|
| 283 | + return "<div class='wl-field' data-cardinality='$this->cardinality'>"; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Returns Field HTML (nonce included). |
|
| 288 | + * |
|
| 289 | + * Overwrite this method (or methods called from this method) in a child |
|
| 290 | + * class to obtain custom behaviour. |
|
| 291 | + */ |
|
| 292 | + public function html() { |
|
| 293 | + |
|
| 294 | + // Open main <div> for the Field. |
|
| 295 | + $html = $this->html_wrapper_open(); |
|
| 296 | + |
|
| 297 | + // Label. |
|
| 298 | + $html .= "<h3>$this->label</h3>"; |
|
| 299 | + |
|
| 300 | + // print nonce. |
|
| 301 | + $html .= $this->html_nonce(); |
|
| 302 | + |
|
| 303 | + // print data loaded from DB. |
|
| 304 | + $count = 0; |
|
| 305 | + if ( $this->data ) { |
|
| 306 | + foreach ( $this->data as $value ) { |
|
| 307 | + if ( $count < $this->cardinality ) { |
|
| 308 | + $html .= $this->html_input( $value ); |
|
| 309 | + } |
|
| 310 | + $count ++; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + // Print the empty <input> to add new values. |
|
| 315 | + if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 316 | + $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 317 | + $count ++; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + // If cardinality allows it, print button to add new values. |
|
| 321 | + $html .= $this->get_add_button_html( $count ); |
|
| 322 | + |
|
| 323 | + // Close the HTML wrapper. |
|
| 324 | + $html .= $this->html_wrapper_close(); |
|
| 325 | + |
|
| 326 | + return $html; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Get the add button html. |
|
| 331 | + * |
|
| 332 | + * This function is protected, allowing extending class to further customize |
|
| 333 | + * the add button html code. |
|
| 334 | + * |
|
| 335 | + * @since 3.15.0 |
|
| 336 | + * |
|
| 337 | + * @param int $count The current number of values. |
|
| 338 | + * |
|
| 339 | + * @return string The add button html code. |
|
| 340 | + */ |
|
| 341 | + protected function get_add_button_html( $count ) { |
|
| 342 | + |
|
| 343 | + // If cardinality allows it, print button to add new values. |
|
| 344 | + if ( $count < $this->cardinality ) { |
|
| 345 | + return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>'; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + // Return an empty string. |
|
| 349 | + return ''; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Return a single <input> tag for the Field. |
|
| 354 | + * |
|
| 355 | + * @param mixed $value Input value. |
|
| 356 | + * |
|
| 357 | + * @return string The html code fragment. |
|
| 358 | + */ |
|
| 359 | + public function html_input( $value ) { |
|
| 360 | + $html = <<<EOF |
|
| 361 | 361 | <div class="wl-input-wrapper"> |
| 362 | 362 | <input type="text" id="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" style="width:88%" /> |
| 363 | 363 | <button class="button wl-remove-input wl-button" type="button">Remove</button> |
| 364 | 364 | </div> |
| 365 | 365 | EOF; |
| 366 | 366 | |
| 367 | - return $html; |
|
| 368 | - } |
|
| 367 | + return $html; |
|
| 368 | + } |
|
| 369 | 369 | |
| 370 | - /** |
|
| 371 | - * Returns closing for the wrapper HTML tag. |
|
| 372 | - */ |
|
| 373 | - public function html_wrapper_close() { |
|
| 370 | + /** |
|
| 371 | + * Returns closing for the wrapper HTML tag. |
|
| 372 | + */ |
|
| 373 | + public function html_wrapper_close() { |
|
| 374 | 374 | |
| 375 | - return '</div><hr>'; |
|
| 376 | - } |
|
| 375 | + return '</div><hr>'; |
|
| 376 | + } |
|
| 377 | 377 | |
| 378 | 378 | } |
@@ -89,56 +89,56 @@ discard block |
||
| 89 | 89 | * |
| 90 | 90 | * @param array $args An array of parameters. |
| 91 | 91 | */ |
| 92 | - public function __construct( $args ) { |
|
| 92 | + public function __construct($args) { |
|
| 93 | 93 | |
| 94 | - $this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' ); |
|
| 94 | + $this->log = Wordlift_Log_Service::get_logger('WL_Metabox_Field'); |
|
| 95 | 95 | |
| 96 | - if ( empty( $args ) ) { |
|
| 96 | + if (empty($args)) { |
|
| 97 | 97 | return; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // Save a copy of the custom field's params. |
| 101 | - $this->raw_custom_field = reset( $args ); |
|
| 101 | + $this->raw_custom_field = reset($args); |
|
| 102 | 102 | |
| 103 | 103 | // Extract meta name (post_meta key for the DB). |
| 104 | - $this->meta_name = key( $args ); |
|
| 104 | + $this->meta_name = key($args); |
|
| 105 | 105 | |
| 106 | 106 | // Extract linked data predicate. |
| 107 | - if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 107 | + if (isset($this->raw_custom_field['predicate'])) { |
|
| 108 | 108 | $this->predicate = $this->raw_custom_field['predicate']; |
| 109 | 109 | } else { |
| 110 | 110 | return; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | // Extract human readable label. |
| 114 | - $exploded_predicate = explode( '/', $this->predicate ); |
|
| 114 | + $exploded_predicate = explode('/', $this->predicate); |
|
| 115 | 115 | |
| 116 | 116 | // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
| 117 | - $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 117 | + $this->label = isset($this->raw_custom_field['metabox']['label']) ? $this->raw_custom_field['metabox']['label'] : end($exploded_predicate); |
|
| 118 | 118 | |
| 119 | 119 | // Extract field constraints (numerosity, expected type). |
| 120 | 120 | // Default constaints: accept one string.. |
| 121 | - if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 121 | + if (isset($this->raw_custom_field['type'])) { |
|
| 122 | 122 | $this->expected_wl_type = $this->raw_custom_field['type']; |
| 123 | 123 | } else { |
| 124 | 124 | $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | $this->cardinality = 1; |
| 128 | - if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 128 | + if (isset($this->raw_custom_field['constraints'])) { |
|
| 129 | 129 | |
| 130 | 130 | $constraints = $this->raw_custom_field['constraints']; |
| 131 | 131 | |
| 132 | 132 | // Extract cardinality. |
| 133 | - if ( isset( $constraints['cardinality'] ) ) { |
|
| 133 | + if (isset($constraints['cardinality'])) { |
|
| 134 | 134 | $this->cardinality = $constraints['cardinality']; |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
| 138 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) { |
|
| 139 | - $this->expected_uri_type = is_array( $constraints['uri_type'] ) |
|
| 138 | + if (Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset($constraints['uri_type'])) { |
|
| 139 | + $this->expected_uri_type = is_array($constraints['uri_type']) |
|
| 140 | 140 | ? $constraints['uri_type'] |
| 141 | - : array( $constraints['uri_type'] ); |
|
| 141 | + : array($constraints['uri_type']); |
|
| 142 | 142 | } |
| 143 | 143 | } |
| 144 | 144 | |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | public function html_nonce() { |
| 153 | 153 | |
| 154 | - return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false ); |
|
| 154 | + return wp_nonce_field('wordlift_'.$this->meta_name.'_entity_box', 'wordlift_'.$this->meta_name.'_entity_box_nonce', true, false); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -163,15 +163,15 @@ discard block |
||
| 163 | 163 | */ |
| 164 | 164 | public function verify_nonce() { |
| 165 | 165 | |
| 166 | - $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 167 | - $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 166 | + $nonce_name = 'wordlift_'.$this->meta_name.'_entity_box_nonce'; |
|
| 167 | + $nonce_verify = 'wordlift_'.$this->meta_name.'_entity_box'; |
|
| 168 | 168 | |
| 169 | - if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 169 | + if ( ! isset($_POST[$nonce_name])) { |
|
| 170 | 170 | return false; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // Verify that the nonce is valid. |
| 174 | - return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 174 | + return wp_verify_nonce($_POST[$nonce_name], $nonce_verify); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | /** |
@@ -181,11 +181,11 @@ discard block |
||
| 181 | 181 | */ |
| 182 | 182 | public function get_data() { |
| 183 | 183 | |
| 184 | - $data = get_post_meta( get_the_ID(), $this->meta_name ); |
|
| 184 | + $data = get_post_meta(get_the_ID(), $this->meta_name); |
|
| 185 | 185 | |
| 186 | 186 | // Values are always contained in an array (makes it easier to manage cardinality). |
| 187 | - if ( ! is_array( $data ) ) { |
|
| 188 | - $data = array( $data ); |
|
| 187 | + if ( ! is_array($data)) { |
|
| 188 | + $data = array($data); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | $this->data = $data; |
@@ -201,17 +201,17 @@ discard block |
||
| 201 | 201 | * @param array $values Array of values to be sanitized and then stored into |
| 202 | 202 | * $this->data. |
| 203 | 203 | */ |
| 204 | - public function sanitize_data( $values ) { |
|
| 204 | + public function sanitize_data($values) { |
|
| 205 | 205 | |
| 206 | 206 | $sanitized_data = array(); |
| 207 | 207 | |
| 208 | - if ( ! is_array( $values ) ) { |
|
| 209 | - $values = array( $values ); |
|
| 208 | + if ( ! is_array($values)) { |
|
| 209 | + $values = array($values); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | - foreach ( $values as $value ) { |
|
| 213 | - $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 214 | - if ( ! is_null( $sanitized_value ) ) { |
|
| 212 | + foreach ($values as $value) { |
|
| 213 | + $sanitized_value = $this->sanitize_data_filter($value); |
|
| 214 | + if ( ! is_null($sanitized_value)) { |
|
| 215 | 215 | $sanitized_data[] = $sanitized_value; |
| 216 | 216 | } |
| 217 | 217 | } |
@@ -229,17 +229,17 @@ discard block |
||
| 229 | 229 | * |
| 230 | 230 | * @return mixed Returns sanitized value, or null. |
| 231 | 231 | */ |
| 232 | - public function sanitize_data_filter( $value ) { |
|
| 232 | + public function sanitize_data_filter($value) { |
|
| 233 | 233 | |
| 234 | 234 | // TODO: all fields should provide their own sanitize which shouldn't |
| 235 | 235 | // be part of a UI class. |
| 236 | 236 | |
| 237 | 237 | // If the field provides its own validation, use it. |
| 238 | - if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 239 | - return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 238 | + if (isset($this->raw_custom_field['sanitize'])) { |
|
| 239 | + return call_user_func($this->raw_custom_field['sanitize'], $value); |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | - if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 242 | + if ( ! is_null($value) && '' !== $value) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 243 | 243 | return $value; |
| 244 | 244 | } |
| 245 | 245 | |
@@ -253,20 +253,20 @@ discard block |
||
| 253 | 253 | * |
| 254 | 254 | * @param array $values Array of values to be sanitized and then stored into $this->data. |
| 255 | 255 | */ |
| 256 | - public function save_data( $values ) { |
|
| 256 | + public function save_data($values) { |
|
| 257 | 257 | |
| 258 | 258 | // Will sanitize data and store them in $field->data. |
| 259 | - $this->sanitize_data( $values ); |
|
| 259 | + $this->sanitize_data($values); |
|
| 260 | 260 | |
| 261 | 261 | $entity_id = get_the_ID(); |
| 262 | 262 | |
| 263 | 263 | // Take away old values. |
| 264 | - delete_post_meta( $entity_id, $this->meta_name ); |
|
| 264 | + delete_post_meta($entity_id, $this->meta_name); |
|
| 265 | 265 | |
| 266 | 266 | // insert new values, respecting cardinality. |
| 267 | - $single = ( 1 === $this->cardinality ); |
|
| 268 | - foreach ( $this->data as $value ) { |
|
| 269 | - add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 267 | + $single = (1 === $this->cardinality); |
|
| 268 | + foreach ($this->data as $value) { |
|
| 269 | + add_post_meta($entity_id, $this->meta_name, $value, $single); |
|
| 270 | 270 | } |
| 271 | 271 | } |
| 272 | 272 | |
@@ -302,23 +302,23 @@ discard block |
||
| 302 | 302 | |
| 303 | 303 | // print data loaded from DB. |
| 304 | 304 | $count = 0; |
| 305 | - if ( $this->data ) { |
|
| 306 | - foreach ( $this->data as $value ) { |
|
| 307 | - if ( $count < $this->cardinality ) { |
|
| 308 | - $html .= $this->html_input( $value ); |
|
| 305 | + if ($this->data) { |
|
| 306 | + foreach ($this->data as $value) { |
|
| 307 | + if ($count < $this->cardinality) { |
|
| 308 | + $html .= $this->html_input($value); |
|
| 309 | 309 | } |
| 310 | - $count ++; |
|
| 310 | + $count++; |
|
| 311 | 311 | } |
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | // Print the empty <input> to add new values. |
| 315 | - if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 316 | - $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 317 | - $count ++; |
|
| 315 | + if (0 === $count) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 316 | + $html .= $this->html_input(''); // Will print an empty <input>. |
|
| 317 | + $count++; |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | // If cardinality allows it, print button to add new values. |
| 321 | - $html .= $this->get_add_button_html( $count ); |
|
| 321 | + $html .= $this->get_add_button_html($count); |
|
| 322 | 322 | |
| 323 | 323 | // Close the HTML wrapper. |
| 324 | 324 | $html .= $this->html_wrapper_close(); |
@@ -338,11 +338,11 @@ discard block |
||
| 338 | 338 | * |
| 339 | 339 | * @return string The add button html code. |
| 340 | 340 | */ |
| 341 | - protected function get_add_button_html( $count ) { |
|
| 341 | + protected function get_add_button_html($count) { |
|
| 342 | 342 | |
| 343 | 343 | // If cardinality allows it, print button to add new values. |
| 344 | - if ( $count < $this->cardinality ) { |
|
| 345 | - return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>'; |
|
| 344 | + if ($count < $this->cardinality) { |
|
| 345 | + return '<button class="button wl-add-input wl-button" type="button">'.esc_html__('Add').'</button>'; |
|
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | // Return an empty string. |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | * |
| 357 | 357 | * @return string The html code fragment. |
| 358 | 358 | */ |
| 359 | - public function html_input( $value ) { |
|
| 359 | + public function html_input($value) { |
|
| 360 | 360 | $html = <<<EOF |
| 361 | 361 | <div class="wl-input-wrapper"> |
| 362 | 362 | <input type="text" id="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" style="width:88%" /> |
@@ -7,14 +7,14 @@ discard block |
||
| 7 | 7 | * @subpackage Wordlift/admin/WL_Metabox |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -require_once( 'class-wl-metabox-field.php' ); |
|
| 11 | -require_once( 'WL_Metabox_Field_date.php' ); |
|
| 12 | -require_once( 'class-wl-metabox-field-uri.php' ); |
|
| 13 | -require_once( 'WL_Metabox_Field_coordinates.php' ); |
|
| 14 | -require_once( 'class-wl-metabox-field-sameas.php' ); |
|
| 15 | -require_once( 'WL_Metabox_Field_address.php' ); |
|
| 16 | -require_once( 'class-wordlift-metabox-field-duration.php' ); |
|
| 17 | -require_once( 'class-wordlift-metabox-field-multiline.php' ); |
|
| 10 | +require_once('class-wl-metabox-field.php'); |
|
| 11 | +require_once('WL_Metabox_Field_date.php'); |
|
| 12 | +require_once('class-wl-metabox-field-uri.php'); |
|
| 13 | +require_once('WL_Metabox_Field_coordinates.php'); |
|
| 14 | +require_once('class-wl-metabox-field-sameas.php'); |
|
| 15 | +require_once('WL_Metabox_Field_address.php'); |
|
| 16 | +require_once('class-wordlift-metabox-field-duration.php'); |
|
| 17 | +require_once('class-wordlift-metabox-field-multiline.php'); |
|
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * Define the {@link WL_Metabox} class. |
@@ -42,11 +42,11 @@ discard block |
||
| 42 | 42 | public function __construct() { |
| 43 | 43 | |
| 44 | 44 | // Add hooks to print metaboxes and save submitted data. |
| 45 | - add_action( 'add_meta_boxes', array( &$this, 'add_main_metabox' ) ); |
|
| 46 | - add_action( 'wl_linked_data_save_post', array( |
|
| 45 | + add_action('add_meta_boxes', array(&$this, 'add_main_metabox')); |
|
| 46 | + add_action('wl_linked_data_save_post', array( |
|
| 47 | 47 | &$this, |
| 48 | 48 | 'save_form_data', |
| 49 | - ) ); |
|
| 49 | + )); |
|
| 50 | 50 | |
| 51 | 51 | // Enqueue js and css. |
| 52 | 52 | $this->enqueue_scripts_and_styles(); |
@@ -60,27 +60,27 @@ discard block |
||
| 60 | 60 | public function add_main_metabox() { |
| 61 | 61 | |
| 62 | 62 | // Build the fields we need to print. |
| 63 | - $this->instantiate_fields( get_the_ID() ); |
|
| 63 | + $this->instantiate_fields(get_the_ID()); |
|
| 64 | 64 | |
| 65 | 65 | // Bailout if there are no actual fields, we do not need a metabox in that case. |
| 66 | - if ( empty( $this->fields ) ) { |
|
| 66 | + if (empty($this->fields)) { |
|
| 67 | 67 | return; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | // Add main metabox (will print also the inner fields). |
| 71 | - $id = uniqid( 'wl-metabox-' ); |
|
| 72 | - $title = get_the_title() . ' ' . __( 'properties', 'wordlift' ); |
|
| 71 | + $id = uniqid('wl-metabox-'); |
|
| 72 | + $title = get_the_title().' '.__('properties', 'wordlift'); |
|
| 73 | 73 | |
| 74 | 74 | // WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit. |
| 75 | - foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) { |
|
| 76 | - add_meta_box( $id, $title, array( |
|
| 75 | + foreach (Wordlift_Entity_Service::valid_entity_post_types() as $screen) { |
|
| 76 | + add_meta_box($id, $title, array( |
|
| 77 | 77 | $this, |
| 78 | 78 | 'html', |
| 79 | - ), $screen, 'normal', 'high' ); |
|
| 79 | + ), $screen, 'normal', 'high'); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | // Add filter to change the metabox CSS class. |
| 83 | - add_filter( "postbox_classes_entity_$id", 'wl_admin_metaboxes_add_css_class' ); |
|
| 83 | + add_filter("postbox_classes_entity_$id", 'wl_admin_metaboxes_add_css_class'); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
@@ -90,10 +90,10 @@ discard block |
||
| 90 | 90 | * |
| 91 | 91 | * @param WP_Post $post The post. |
| 92 | 92 | */ |
| 93 | - public function html( $post ) { |
|
| 93 | + public function html($post) { |
|
| 94 | 94 | |
| 95 | 95 | // Loop over the fields. |
| 96 | - foreach ( $this->fields as $field ) { |
|
| 96 | + foreach ($this->fields as $field) { |
|
| 97 | 97 | |
| 98 | 98 | // load data from DB (values will be available in $field->data). |
| 99 | 99 | $field->get_data(); |
@@ -114,16 +114,16 @@ discard block |
||
| 114 | 114 | * |
| 115 | 115 | * @param int $post_id The post id. |
| 116 | 116 | */ |
| 117 | - public function instantiate_fields( $post_id ) { |
|
| 117 | + public function instantiate_fields($post_id) { |
|
| 118 | 118 | |
| 119 | 119 | // This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering. |
| 120 | - if ( isset( $this->fields ) ) { |
|
| 120 | + if (isset($this->fields)) { |
|
| 121 | 121 | return; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - $entity_type = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 124 | + $entity_type = wl_entity_taxonomy_get_custom_fields($post_id); |
|
| 125 | 125 | |
| 126 | - if ( isset( $entity_type ) ) { |
|
| 126 | + if (isset($entity_type)) { |
|
| 127 | 127 | |
| 128 | 128 | /* |
| 129 | 129 | * Might not have any relevant meta box field, for example for articles, |
@@ -138,31 +138,31 @@ discard block |
||
| 138 | 138 | * - simple: accept values for one property |
| 139 | 139 | * - grouped: accept values for more properties, or for one property that needs a specific metabox. |
| 140 | 140 | */ |
| 141 | - $metaboxes = $this->group_properties_by_input_field( $entity_type ); |
|
| 141 | + $metaboxes = $this->group_properties_by_input_field($entity_type); |
|
| 142 | 142 | $simple_metaboxes = $metaboxes[0]; |
| 143 | 143 | $grouped_metaboxes = $metaboxes[1]; |
| 144 | 144 | |
| 145 | 145 | // Loop over simple entity properties. |
| 146 | - foreach ( $simple_metaboxes as $key => $property ) { |
|
| 146 | + foreach ($simple_metaboxes as $key => $property) { |
|
| 147 | 147 | |
| 148 | 148 | // Info passed to the metabox. |
| 149 | 149 | $info = array(); |
| 150 | - $info[ $key ] = $property; |
|
| 150 | + $info[$key] = $property; |
|
| 151 | 151 | |
| 152 | 152 | // Build the requested field as WL_Metabox_Field_ object. |
| 153 | - $this->add_field( $info ); |
|
| 153 | + $this->add_field($info); |
|
| 154 | 154 | |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | // Loop over grouped properties. |
| 158 | - foreach ( $grouped_metaboxes as $key => $property ) { |
|
| 158 | + foreach ($grouped_metaboxes as $key => $property) { |
|
| 159 | 159 | |
| 160 | 160 | // Info passed to the metabox. |
| 161 | 161 | $info = array(); |
| 162 | - $info[ $key ] = $property; |
|
| 162 | + $info[$key] = $property; |
|
| 163 | 163 | |
| 164 | 164 | // Build the requested field group as WL_Metabox_Field_ object. |
| 165 | - $this->add_field( $info, true ); |
|
| 165 | + $this->add_field($info, true); |
|
| 166 | 166 | |
| 167 | 167 | } |
| 168 | 168 | } |
@@ -176,34 +176,34 @@ discard block |
||
| 176 | 176 | * |
| 177 | 177 | * @return array |
| 178 | 178 | */ |
| 179 | - public function group_properties_by_input_field( $custom_fields ) { |
|
| 179 | + public function group_properties_by_input_field($custom_fields) { |
|
| 180 | 180 | |
| 181 | 181 | $simple_properties = array(); |
| 182 | 182 | $grouped_properties = array(); |
| 183 | 183 | |
| 184 | 184 | // Loop over possible entity properties. |
| 185 | - foreach ( $custom_fields as $key => $property ) { |
|
| 185 | + foreach ($custom_fields as $key => $property) { |
|
| 186 | 186 | |
| 187 | 187 | // Check presence of predicate and type. |
| 188 | - if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) { |
|
| 188 | + if (isset($property['predicate']) && isset($property['type'])) { |
|
| 189 | 189 | |
| 190 | 190 | // Check if input_field is defined. |
| 191 | - if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) { |
|
| 191 | + if (isset($property['input_field']) && '' !== $property['input_field']) { |
|
| 192 | 192 | |
| 193 | 193 | $grouped_key = $property['input_field']; |
| 194 | 194 | |
| 195 | 195 | // Update list of grouped properties. |
| 196 | - $grouped_properties[ $grouped_key ][ $key ] = $property; |
|
| 196 | + $grouped_properties[$grouped_key][$key] = $property; |
|
| 197 | 197 | |
| 198 | 198 | } else { |
| 199 | 199 | |
| 200 | 200 | // input_field not defined, add simple metabox. |
| 201 | - $simple_properties[ $key ] = $property; |
|
| 201 | + $simple_properties[$key] = $property; |
|
| 202 | 202 | } |
| 203 | 203 | } |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - return array( $simple_properties, $grouped_properties ); |
|
| 206 | + return array($simple_properties, $grouped_properties); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
@@ -213,29 +213,29 @@ discard block |
||
| 213 | 213 | * @param array $args The field's information. |
| 214 | 214 | * @param bool $grouped Flag to distinguish between simple and grouped fields. |
| 215 | 215 | */ |
| 216 | - public function add_field( $args, $grouped = false ) { |
|
| 216 | + public function add_field($args, $grouped = false) { |
|
| 217 | 217 | |
| 218 | - if ( $grouped ) { |
|
| 218 | + if ($grouped) { |
|
| 219 | 219 | |
| 220 | 220 | // Special fields (sameas, coordinates, etc.). |
| 221 | 221 | // |
| 222 | 222 | // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
| 223 | - $field_class = 'WL_Metabox_Field_' . key( $args ); |
|
| 223 | + $field_class = 'WL_Metabox_Field_'.key($args); |
|
| 224 | 224 | |
| 225 | 225 | } else { |
| 226 | 226 | |
| 227 | 227 | // Simple fields (string, uri, boolean, etc.). |
| 228 | 228 | // |
| 229 | 229 | // Which field? We want to use the class that is specific for the field. |
| 230 | - $meta = key( $args ); |
|
| 231 | - $this_meta = $args[ $meta ]; |
|
| 230 | + $meta = key($args); |
|
| 231 | + $this_meta = $args[$meta]; |
|
| 232 | 232 | |
| 233 | 233 | // If the field declares what metabox it wants, use that one. |
| 234 | - if ( isset( $this_meta['metabox']['class'] ) ) { |
|
| 234 | + if (isset($this_meta['metabox']['class'])) { |
|
| 235 | 235 | |
| 236 | 236 | $field_class = $this_meta['metabox']['class']; |
| 237 | 237 | |
| 238 | - } elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) { |
|
| 238 | + } elseif ( ! isset($this_meta['type']) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type']) { |
|
| 239 | 239 | |
| 240 | 240 | // TODO: all fields should explicitly declare the required WL_Metabox. |
| 241 | 241 | // When they will remove this. |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | // When they will remove this. |
| 250 | 250 | // |
| 251 | 251 | // Build Field with a custom class (e.g. WL_Metabox_Field_date). |
| 252 | - $field_class = 'WL_Metabox_Field_' . $this_meta['type']; |
|
| 252 | + $field_class = 'WL_Metabox_Field_'.$this_meta['type']; |
|
| 253 | 253 | |
| 254 | 254 | } |
| 255 | 255 | |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | // End if(). |
| 258 | 258 | |
| 259 | 259 | // Call apropriate constructor (e.g. WL_Metabox_Field_... ). |
| 260 | - $this->fields[] = new $field_class( $args ); |
|
| 260 | + $this->fields[] = new $field_class($args); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | /** |
@@ -267,40 +267,40 @@ discard block |
||
| 267 | 267 | * |
| 268 | 268 | * @param int $entity_id The entity's {@link WP_Post}'s id. |
| 269 | 269 | */ |
| 270 | - public function save_form_data( $entity_id ) { |
|
| 270 | + public function save_form_data($entity_id) { |
|
| 271 | 271 | |
| 272 | 272 | // Build Field objects. |
| 273 | - $this->instantiate_fields( $entity_id ); |
|
| 273 | + $this->instantiate_fields($entity_id); |
|
| 274 | 274 | |
| 275 | 275 | // Check if WL metabox form was posted. |
| 276 | - if ( ! isset( $_POST['wl_metaboxes'] ) ) { |
|
| 276 | + if ( ! isset($_POST['wl_metaboxes'])) { |
|
| 277 | 277 | return; |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - foreach ( $this->fields as $field ) { |
|
| 280 | + foreach ($this->fields as $field) { |
|
| 281 | 281 | |
| 282 | 282 | // Verify nonce. |
| 283 | 283 | $valid_nonce = $field->verify_nonce(); |
| 284 | - if ( $valid_nonce ) { |
|
| 284 | + if ($valid_nonce) { |
|
| 285 | 285 | |
| 286 | 286 | $posted_data = $_POST['wl_metaboxes']; |
| 287 | 287 | $field_name = $field->meta_name; |
| 288 | 288 | |
| 289 | 289 | // Each Filed only deals with its values. |
| 290 | - if ( isset( $posted_data[ $field_name ] ) ) { |
|
| 290 | + if (isset($posted_data[$field_name])) { |
|
| 291 | 291 | |
| 292 | - $values = $posted_data[ $field_name ]; |
|
| 293 | - if ( ! is_array( $values ) ) { |
|
| 294 | - $values = array( $values ); |
|
| 292 | + $values = $posted_data[$field_name]; |
|
| 293 | + if ( ! is_array($values)) { |
|
| 294 | + $values = array($values); |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | // Save data permanently |
| 298 | - $field->save_data( $values ); |
|
| 298 | + $field->save_data($values); |
|
| 299 | 299 | } |
| 300 | 300 | } |
| 301 | 301 | } |
| 302 | 302 | |
| 303 | - Wordlift_Linked_Data_Service::get_instance()->push( $entity_id ); |
|
| 303 | + Wordlift_Linked_Data_Service::get_instance()->push($entity_id); |
|
| 304 | 304 | |
| 305 | 305 | } |
| 306 | 306 | |
@@ -312,20 +312,20 @@ discard block |
||
| 312 | 312 | public function enqueue_scripts_and_styles() { |
| 313 | 313 | |
| 314 | 314 | // Use the minified version if PW_DEBUG isn't set. |
| 315 | - $min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : ''; |
|
| 315 | + $min = ! defined('WP_DEBUG') || ! WP_DEBUG ? '.min' : ''; |
|
| 316 | 316 | |
| 317 | 317 | // Load the jquery-ui-timepicker-addon library. |
| 318 | - wp_enqueue_style( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.css", array(), '3.0.6' ); |
|
| 319 | - wp_enqueue_script( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true ); |
|
| 318 | + wp_enqueue_style('wl-flatpickr', dirname(plugin_dir_url(__FILE__))."/js/flatpickr/flatpickr$min.css", array(), '3.0.6'); |
|
| 319 | + wp_enqueue_script('wl-flatpickr', dirname(plugin_dir_url(__FILE__))."/js/flatpickr/flatpickr$min.js", array('jquery'), '3.0.6', true); |
|
| 320 | 320 | |
| 321 | 321 | // Leaflet. |
| 322 | - wp_enqueue_style( 'leaflet', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/bower_components/leaflet/dist/leaflet.css' ); |
|
| 323 | - wp_enqueue_script( 'leaflet', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/bower_components/leaflet/dist/leaflet.js', __FILE__ ); |
|
| 322 | + wp_enqueue_style('leaflet', dirname(dirname(plugin_dir_url(__FILE__))).'/bower_components/leaflet/dist/leaflet.css'); |
|
| 323 | + wp_enqueue_script('leaflet', dirname(dirname(plugin_dir_url(__FILE__))).'/bower_components/leaflet/dist/leaflet.js', __FILE__); |
|
| 324 | 324 | |
| 325 | 325 | // Add AJAX autocomplete to facilitate metabox editing. |
| 326 | - wp_enqueue_script( 'wl-entity-metabox-utility', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wl_entity_metabox_utilities.js' ); |
|
| 327 | - wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array( |
|
| 328 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 326 | + wp_enqueue_script('wl-entity-metabox-utility', dirname(plugin_dir_url(__FILE__)).'/js/wl_entity_metabox_utilities.js'); |
|
| 327 | + wp_localize_script('wl-entity-metabox-utility', 'wlEntityMetaboxParams', array( |
|
| 328 | + 'ajax_url' => admin_url('admin-ajax.php'), |
|
| 329 | 329 | 'action' => 'entity_by_title', |
| 330 | 330 | ) |
| 331 | 331 | ); |
@@ -16,43 +16,43 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class WL_Metabox_Field_sameas extends WL_Metabox_Field { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @inheritdoc |
|
| 21 | - */ |
|
| 22 | - public function __construct( $args ) { |
|
| 23 | - parent::__construct( $args['sameas'] ); |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @inheritdoc |
|
| 28 | - */ |
|
| 29 | - public function save_data( $values ) { |
|
| 30 | - // The autocomplete select may send JSON arrays in input values. |
|
| 31 | - $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 32 | - return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 33 | - }, array() ); |
|
| 34 | - |
|
| 35 | - parent::save_data( $merged ); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @inheritdoc |
|
| 40 | - */ |
|
| 41 | - public function sanitize_data_filter( $value ) { |
|
| 42 | - |
|
| 43 | - // Call our sanitizer helper. |
|
| 44 | - return Wordlift_Sanitizer::sanitize_url( $value ); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @inheritdoc |
|
| 49 | - */ |
|
| 50 | - protected function get_add_button_html( $count ) { |
|
| 51 | - |
|
| 52 | - // Return an element where the new Autocomplete Select will attach to. |
|
| 53 | - return '<p>' |
|
| 54 | - . esc_html__( 'Type a URL or any text to find entities from the vocabulary and the cloud:', 'wordlift' ) |
|
| 55 | - . '</p><div id="wl-metabox-field-sameas"></div>'; |
|
| 56 | - } |
|
| 19 | + /** |
|
| 20 | + * @inheritdoc |
|
| 21 | + */ |
|
| 22 | + public function __construct( $args ) { |
|
| 23 | + parent::__construct( $args['sameas'] ); |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @inheritdoc |
|
| 28 | + */ |
|
| 29 | + public function save_data( $values ) { |
|
| 30 | + // The autocomplete select may send JSON arrays in input values. |
|
| 31 | + $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 32 | + return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 33 | + }, array() ); |
|
| 34 | + |
|
| 35 | + parent::save_data( $merged ); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @inheritdoc |
|
| 40 | + */ |
|
| 41 | + public function sanitize_data_filter( $value ) { |
|
| 42 | + |
|
| 43 | + // Call our sanitizer helper. |
|
| 44 | + return Wordlift_Sanitizer::sanitize_url( $value ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @inheritdoc |
|
| 49 | + */ |
|
| 50 | + protected function get_add_button_html( $count ) { |
|
| 51 | + |
|
| 52 | + // Return an element where the new Autocomplete Select will attach to. |
|
| 53 | + return '<p>' |
|
| 54 | + . esc_html__( 'Type a URL or any text to find entities from the vocabulary and the cloud:', 'wordlift' ) |
|
| 55 | + . '</p><div id="wl-metabox-field-sameas"></div>'; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
@@ -19,39 +19,39 @@ |
||
| 19 | 19 | /** |
| 20 | 20 | * @inheritdoc |
| 21 | 21 | */ |
| 22 | - public function __construct( $args ) { |
|
| 23 | - parent::__construct( $args['sameas'] ); |
|
| 22 | + public function __construct($args) { |
|
| 23 | + parent::__construct($args['sameas']); |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * @inheritdoc |
| 28 | 28 | */ |
| 29 | - public function save_data( $values ) { |
|
| 29 | + public function save_data($values) { |
|
| 30 | 30 | // The autocomplete select may send JSON arrays in input values. |
| 31 | - $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 32 | - return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 33 | - }, array() ); |
|
| 31 | + $merged = array_reduce((array) $values, function($carry, $item) { |
|
| 32 | + return array_merge($carry, mb_split("\x{2063}", wp_unslash($item))); |
|
| 33 | + }, array()); |
|
| 34 | 34 | |
| 35 | - parent::save_data( $merged ); |
|
| 35 | + parent::save_data($merged); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | 39 | * @inheritdoc |
| 40 | 40 | */ |
| 41 | - public function sanitize_data_filter( $value ) { |
|
| 41 | + public function sanitize_data_filter($value) { |
|
| 42 | 42 | |
| 43 | 43 | // Call our sanitizer helper. |
| 44 | - return Wordlift_Sanitizer::sanitize_url( $value ); |
|
| 44 | + return Wordlift_Sanitizer::sanitize_url($value); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
| 48 | 48 | * @inheritdoc |
| 49 | 49 | */ |
| 50 | - protected function get_add_button_html( $count ) { |
|
| 50 | + protected function get_add_button_html($count) { |
|
| 51 | 51 | |
| 52 | 52 | // Return an element where the new Autocomplete Select will attach to. |
| 53 | 53 | return '<p>' |
| 54 | - . esc_html__( 'Type a URL or any text to find entities from the vocabulary and the cloud:', 'wordlift' ) |
|
| 54 | + . esc_html__('Type a URL or any text to find entities from the vocabulary and the cloud:', 'wordlift') |
|
| 55 | 55 | . '</p><div id="wl-metabox-field-sameas"></div>'; |
| 56 | 56 | } |
| 57 | 57 | |