@@ -20,48 +20,48 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_entity_get_by_title( $title, $autocomplete = false, $include_alias = true ) { |
| 22 | 22 | |
| 23 | - global $wpdb; |
|
| 24 | - |
|
| 25 | - // Search by substring |
|
| 26 | - if ( $autocomplete ) { |
|
| 27 | - $title = '%' . $title . '%'; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - // The title is a LIKE query. |
|
| 31 | - $query = "SELECT DISTINCT p.ID AS id, p.post_title AS title, t.name AS schema_type_name, t.slug AS type_slug" |
|
| 32 | - . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t" |
|
| 33 | - . " WHERE p.post_title LIKE %s" |
|
| 34 | - . " AND t.term_id = tt.term_id" |
|
| 35 | - . " AND tt.taxonomy = %s" |
|
| 36 | - . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 37 | - . " AND tr.object_id = p.ID" |
|
| 38 | - // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 39 | - . " AND p.post_status != 'trash'"; |
|
| 40 | - |
|
| 41 | - if ( $include_alias ) { |
|
| 42 | - |
|
| 43 | - $query .= " UNION" |
|
| 44 | - . " SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug" |
|
| 45 | - . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t, $wpdb->postmeta m" |
|
| 46 | - . " WHERE m.meta_key = %s AND m.meta_value LIKE %s" |
|
| 47 | - . " AND m.post_id = p.ID" |
|
| 48 | - . " AND t.term_id = tt.term_id" |
|
| 49 | - . " AND tt.taxonomy = %s" |
|
| 50 | - . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 51 | - . " AND tr.object_id = p.ID" |
|
| 52 | - // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 53 | - . " AND p.post_status != 'trash'"; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 57 | - $query, |
|
| 58 | - $title, |
|
| 59 | - Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 60 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 61 | - Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
|
| 62 | - $title, |
|
| 63 | - Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME |
|
| 64 | - ) ); |
|
| 23 | + global $wpdb; |
|
| 24 | + |
|
| 25 | + // Search by substring |
|
| 26 | + if ( $autocomplete ) { |
|
| 27 | + $title = '%' . $title . '%'; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + // The title is a LIKE query. |
|
| 31 | + $query = "SELECT DISTINCT p.ID AS id, p.post_title AS title, t.name AS schema_type_name, t.slug AS type_slug" |
|
| 32 | + . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t" |
|
| 33 | + . " WHERE p.post_title LIKE %s" |
|
| 34 | + . " AND t.term_id = tt.term_id" |
|
| 35 | + . " AND tt.taxonomy = %s" |
|
| 36 | + . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 37 | + . " AND tr.object_id = p.ID" |
|
| 38 | + // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 39 | + . " AND p.post_status != 'trash'"; |
|
| 40 | + |
|
| 41 | + if ( $include_alias ) { |
|
| 42 | + |
|
| 43 | + $query .= " UNION" |
|
| 44 | + . " SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug" |
|
| 45 | + . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t, $wpdb->postmeta m" |
|
| 46 | + . " WHERE m.meta_key = %s AND m.meta_value LIKE %s" |
|
| 47 | + . " AND m.post_id = p.ID" |
|
| 48 | + . " AND t.term_id = tt.term_id" |
|
| 49 | + . " AND tt.taxonomy = %s" |
|
| 50 | + . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 51 | + . " AND tr.object_id = p.ID" |
|
| 52 | + // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 53 | + . " AND p.post_status != 'trash'"; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + return $wpdb->get_results( $wpdb->prepare( |
|
| 57 | + $query, |
|
| 58 | + $title, |
|
| 59 | + Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 60 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 61 | + Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
|
| 62 | + $title, |
|
| 63 | + Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME |
|
| 64 | + ) ); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
@@ -71,41 +71,41 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | function wl_entity_ajax_get_by_title() { |
| 73 | 73 | |
| 74 | - // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 75 | - // |
|
| 76 | - // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 77 | - // Get the title to search. |
|
| 78 | - if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { |
|
| 79 | - @ob_clean(); |
|
| 80 | - wp_send_json_error( 'The title parameter is required.' ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 84 | - // |
|
| 85 | - // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 86 | - $title = $_POST['title'] ?: $_GET['title']; |
|
| 87 | - |
|
| 88 | - // Are we searching for a specific title or for a containing title? |
|
| 89 | - $autocomplete = isset( $_GET['autocomplete'] ); |
|
| 90 | - |
|
| 91 | - // Are we searching also for the aliases? |
|
| 92 | - $include_alias = isset( $_GET['alias'] ); |
|
| 93 | - |
|
| 94 | - // Get the edit link. |
|
| 95 | - $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 96 | - $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 97 | - |
|
| 98 | - // Prepare the response with the edit link. |
|
| 99 | - $response = array( |
|
| 100 | - 'edit_link' => $edit_link, |
|
| 101 | - 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 102 | - ); |
|
| 103 | - |
|
| 104 | - // Clean any buffer. |
|
| 105 | - ob_clean(); |
|
| 106 | - |
|
| 107 | - // Send the success response. |
|
| 108 | - wp_send_json_success( $response ); |
|
| 74 | + // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 75 | + // |
|
| 76 | + // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 77 | + // Get the title to search. |
|
| 78 | + if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { |
|
| 79 | + @ob_clean(); |
|
| 80 | + wp_send_json_error( 'The title parameter is required.' ); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 84 | + // |
|
| 85 | + // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 86 | + $title = $_POST['title'] ?: $_GET['title']; |
|
| 87 | + |
|
| 88 | + // Are we searching for a specific title or for a containing title? |
|
| 89 | + $autocomplete = isset( $_GET['autocomplete'] ); |
|
| 90 | + |
|
| 91 | + // Are we searching also for the aliases? |
|
| 92 | + $include_alias = isset( $_GET['alias'] ); |
|
| 93 | + |
|
| 94 | + // Get the edit link. |
|
| 95 | + $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 96 | + $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 97 | + |
|
| 98 | + // Prepare the response with the edit link. |
|
| 99 | + $response = array( |
|
| 100 | + 'edit_link' => $edit_link, |
|
| 101 | + 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 102 | + ); |
|
| 103 | + |
|
| 104 | + // Clean any buffer. |
|
| 105 | + ob_clean(); |
|
| 106 | + |
|
| 107 | + // Send the success response. |
|
| 108 | + wp_send_json_success( $response ); |
|
| 109 | 109 | |
| 110 | 110 | } |
| 111 | 111 | |
@@ -18,13 +18,13 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @return array An array of WP_Post instances. |
| 20 | 20 | */ |
| 21 | -function wl_entity_get_by_title( $title, $autocomplete = false, $include_alias = true ) { |
|
| 21 | +function wl_entity_get_by_title($title, $autocomplete = false, $include_alias = true) { |
|
| 22 | 22 | |
| 23 | 23 | global $wpdb; |
| 24 | 24 | |
| 25 | 25 | // Search by substring |
| 26 | - if ( $autocomplete ) { |
|
| 27 | - $title = '%' . $title . '%'; |
|
| 26 | + if ($autocomplete) { |
|
| 27 | + $title = '%'.$title.'%'; |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | // The title is a LIKE query. |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
| 39 | 39 | . " AND p.post_status != 'trash'"; |
| 40 | 40 | |
| 41 | - if ( $include_alias ) { |
|
| 41 | + if ($include_alias) { |
|
| 42 | 42 | |
| 43 | 43 | $query .= " UNION" |
| 44 | 44 | . " SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug" |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | . " AND p.post_status != 'trash'"; |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 56 | + return $wpdb->get_results($wpdb->prepare( |
|
| 57 | 57 | $query, |
| 58 | 58 | $title, |
| 59 | 59 | Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
| 62 | 62 | $title, |
| 63 | 63 | Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME |
| 64 | - ) ); |
|
| 64 | + )); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
@@ -75,9 +75,9 @@ discard block |
||
| 75 | 75 | // |
| 76 | 76 | // See https://github.com/insideout10/wordlift-plugin/issues/438. |
| 77 | 77 | // Get the title to search. |
| 78 | - if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { |
|
| 78 | + if (empty($_POST['title']) && empty($_GET['title'])) { |
|
| 79 | 79 | @ob_clean(); |
| 80 | - wp_send_json_error( 'The title parameter is required.' ); |
|
| 80 | + wp_send_json_error('The title parameter is required.'); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
@@ -86,27 +86,27 @@ discard block |
||
| 86 | 86 | $title = $_POST['title'] ?: $_GET['title']; |
| 87 | 87 | |
| 88 | 88 | // Are we searching for a specific title or for a containing title? |
| 89 | - $autocomplete = isset( $_GET['autocomplete'] ); |
|
| 89 | + $autocomplete = isset($_GET['autocomplete']); |
|
| 90 | 90 | |
| 91 | 91 | // Are we searching also for the aliases? |
| 92 | - $include_alias = isset( $_GET['alias'] ); |
|
| 92 | + $include_alias = isset($_GET['alias']); |
|
| 93 | 93 | |
| 94 | 94 | // Get the edit link. |
| 95 | - $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 96 | - $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 95 | + $post_type_object = get_post_type_object(Wordlift_Entity_Service::TYPE_NAME); |
|
| 96 | + $edit_link = $post_type_object->_edit_link.'&action=edit'; |
|
| 97 | 97 | |
| 98 | 98 | // Prepare the response with the edit link. |
| 99 | 99 | $response = array( |
| 100 | 100 | 'edit_link' => $edit_link, |
| 101 | - 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 101 | + 'results' => wl_entity_get_by_title($title, $autocomplete, $include_alias), |
|
| 102 | 102 | ); |
| 103 | 103 | |
| 104 | 104 | // Clean any buffer. |
| 105 | 105 | ob_clean(); |
| 106 | 106 | |
| 107 | 107 | // Send the success response. |
| 108 | - wp_send_json_success( $response ); |
|
| 108 | + wp_send_json_success($response); |
|
| 109 | 109 | |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | -add_action( 'wp_ajax_entity_by_title', 'wl_entity_ajax_get_by_title' ); |
|
| 112 | +add_action('wp_ajax_entity_by_title', 'wl_entity_ajax_get_by_title'); |
|
@@ -14,73 +14,73 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | function wl_shortcode_faceted_search( $atts ) { |
| 16 | 16 | |
| 17 | - // Extract attributes and set default values. |
|
| 18 | - $shortcode_atts = shortcode_atts( array( |
|
| 19 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 20 | - 'show_facets' => true, |
|
| 21 | - 'with_carousel' => true, |
|
| 22 | - 'squared_thumbs' => false, |
|
| 23 | - 'limit' => 20, |
|
| 24 | - |
|
| 25 | - ), $atts ); |
|
| 26 | - |
|
| 27 | - foreach ( |
|
| 28 | - array( |
|
| 29 | - 'show_facets', |
|
| 30 | - 'with_carousel', |
|
| 31 | - 'squared_thumbs', |
|
| 32 | - ) as $att |
|
| 33 | - ) { |
|
| 34 | - |
|
| 35 | - // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode. |
|
| 36 | - $shortcode_atts[ $att ] = filter_var( |
|
| 37 | - $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 38 | - ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - // If the current post is not an entity and has no related entities |
|
| 42 | - // than the shortcode cannot be rendered |
|
| 43 | - // TODO Add an alert visibile only for connected admin users. |
|
| 44 | - $current_post = get_post(); |
|
| 45 | - |
|
| 46 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 47 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 48 | - $current_post->ID : |
|
| 49 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 50 | - |
|
| 51 | - if ( 0 === count( $entity_ids ) ) { |
|
| 52 | - return ''; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $div_id = 'wordlift-faceted-entity-search-widget'; |
|
| 56 | - |
|
| 57 | - wp_enqueue_style( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-faceted-entity-search-widget.min.css' ); |
|
| 58 | - wp_enqueue_script( 'angularjs', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.min.js' ); |
|
| 59 | - wp_enqueue_script( 'angularjs-touch', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-touch.min.js' ); |
|
| 60 | - |
|
| 61 | - wp_enqueue_script( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-faceted-entity-search-widget.min.js' ); |
|
| 62 | - |
|
| 63 | - wp_localize_script( |
|
| 64 | - 'wordlift-faceted-search', |
|
| 65 | - 'wl_faceted_search_params', array( |
|
| 66 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 67 | - 'action' => 'wl_faceted_search', |
|
| 68 | - 'post_id' => $current_post->ID, |
|
| 69 | - 'entity_ids' => $entity_ids, |
|
| 70 | - 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 71 | - 'div_id' => $div_id, |
|
| 72 | - 'defaultThumbnailPath' => WL_DEFAULT_THUMBNAIL_PATH, |
|
| 73 | - 'attrs' => $shortcode_atts, |
|
| 74 | - 'l10n' => array( |
|
| 75 | - 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 76 | - 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 77 | - 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 78 | - 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 79 | - ), |
|
| 80 | - ) |
|
| 81 | - ); |
|
| 82 | - |
|
| 83 | - return '<div id="' . $div_id . '" style="width:100%"></div>'; |
|
| 17 | + // Extract attributes and set default values. |
|
| 18 | + $shortcode_atts = shortcode_atts( array( |
|
| 19 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 20 | + 'show_facets' => true, |
|
| 21 | + 'with_carousel' => true, |
|
| 22 | + 'squared_thumbs' => false, |
|
| 23 | + 'limit' => 20, |
|
| 24 | + |
|
| 25 | + ), $atts ); |
|
| 26 | + |
|
| 27 | + foreach ( |
|
| 28 | + array( |
|
| 29 | + 'show_facets', |
|
| 30 | + 'with_carousel', |
|
| 31 | + 'squared_thumbs', |
|
| 32 | + ) as $att |
|
| 33 | + ) { |
|
| 34 | + |
|
| 35 | + // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode. |
|
| 36 | + $shortcode_atts[ $att ] = filter_var( |
|
| 37 | + $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 38 | + ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + // If the current post is not an entity and has no related entities |
|
| 42 | + // than the shortcode cannot be rendered |
|
| 43 | + // TODO Add an alert visibile only for connected admin users. |
|
| 44 | + $current_post = get_post(); |
|
| 45 | + |
|
| 46 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 47 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 48 | + $current_post->ID : |
|
| 49 | + wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 50 | + |
|
| 51 | + if ( 0 === count( $entity_ids ) ) { |
|
| 52 | + return ''; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $div_id = 'wordlift-faceted-entity-search-widget'; |
|
| 56 | + |
|
| 57 | + wp_enqueue_style( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-faceted-entity-search-widget.min.css' ); |
|
| 58 | + wp_enqueue_script( 'angularjs', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.min.js' ); |
|
| 59 | + wp_enqueue_script( 'angularjs-touch', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-touch.min.js' ); |
|
| 60 | + |
|
| 61 | + wp_enqueue_script( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-faceted-entity-search-widget.min.js' ); |
|
| 62 | + |
|
| 63 | + wp_localize_script( |
|
| 64 | + 'wordlift-faceted-search', |
|
| 65 | + 'wl_faceted_search_params', array( |
|
| 66 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 67 | + 'action' => 'wl_faceted_search', |
|
| 68 | + 'post_id' => $current_post->ID, |
|
| 69 | + 'entity_ids' => $entity_ids, |
|
| 70 | + 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 71 | + 'div_id' => $div_id, |
|
| 72 | + 'defaultThumbnailPath' => WL_DEFAULT_THUMBNAIL_PATH, |
|
| 73 | + 'attrs' => $shortcode_atts, |
|
| 74 | + 'l10n' => array( |
|
| 75 | + 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 76 | + 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 77 | + 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 78 | + 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 79 | + ), |
|
| 80 | + ) |
|
| 81 | + ); |
|
| 82 | + |
|
| 83 | + return '<div id="' . $div_id . '" style="width:100%"></div>'; |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | add_shortcode( 'wl_faceted_search', 'wl_shortcode_faceted_search' ); |
@@ -91,106 +91,106 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | function wl_shortcode_faceted_search_ajax( $http_raw_data = null ) { |
| 93 | 93 | |
| 94 | - // Post ID must be defined. |
|
| 95 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 96 | - wp_die( 'No post_id given' ); |
|
| 94 | + // Post ID must be defined. |
|
| 95 | + if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 96 | + wp_die( 'No post_id given' ); |
|
| 97 | 97 | |
| 98 | - return; |
|
| 99 | - } |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - // Extract filtering conditions. |
|
| 102 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 103 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 101 | + // Extract filtering conditions. |
|
| 102 | + $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 103 | + $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 104 | 104 | |
| 105 | - $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 106 | - $current_post = get_post( $current_post_id ); |
|
| 105 | + $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 106 | + $current_post = get_post( $current_post_id ); |
|
| 107 | 107 | |
| 108 | - // Post ID has to match an existing item. |
|
| 109 | - if ( null === $current_post ) { |
|
| 110 | - wp_die( 'No valid post_id given' ); |
|
| 108 | + // Post ID has to match an existing item. |
|
| 109 | + if ( null === $current_post ) { |
|
| 110 | + wp_die( 'No valid post_id given' ); |
|
| 111 | 111 | |
| 112 | - return; |
|
| 113 | - } |
|
| 112 | + return; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - // If the current post is an entity, |
|
| 116 | - // the current post is used as main entity. |
|
| 117 | - // Otherwise, current post related entities are used. |
|
| 118 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 119 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 120 | - array( $current_post->ID ) : |
|
| 121 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 115 | + // If the current post is an entity, |
|
| 116 | + // the current post is used as main entity. |
|
| 117 | + // Otherwise, current post related entities are used. |
|
| 118 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 119 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 120 | + array( $current_post->ID ) : |
|
| 121 | + wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 122 | 122 | |
| 123 | - // If there are no entities we cannot render the widget. |
|
| 124 | - if ( 0 === count( $entity_ids ) ) { |
|
| 125 | - wp_die( 'No entities available' ); |
|
| 123 | + // If there are no entities we cannot render the widget. |
|
| 124 | + if ( 0 === count( $entity_ids ) ) { |
|
| 125 | + wp_die( 'No entities available' ); |
|
| 126 | 126 | |
| 127 | - return; |
|
| 128 | - } |
|
| 127 | + return; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - // Retrieve requested type |
|
| 131 | - $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 130 | + // Retrieve requested type |
|
| 131 | + $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 132 | 132 | |
| 133 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 133 | + $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 134 | 134 | |
| 135 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 136 | - $entity_ids, |
|
| 137 | - '*', |
|
| 138 | - null, |
|
| 139 | - 'publish', |
|
| 140 | - array( $current_post_id ), |
|
| 141 | - $limit |
|
| 142 | - ); |
|
| 135 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 136 | + $entity_ids, |
|
| 137 | + '*', |
|
| 138 | + null, |
|
| 139 | + 'publish', |
|
| 140 | + array( $current_post_id ), |
|
| 141 | + $limit |
|
| 142 | + ); |
|
| 143 | 143 | |
| 144 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 145 | - return $p->ID; |
|
| 146 | - }, $referencing_posts ); |
|
| 147 | - $results = array(); |
|
| 144 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 145 | + return $p->ID; |
|
| 146 | + }, $referencing_posts ); |
|
| 147 | + $results = array(); |
|
| 148 | 148 | |
| 149 | - if ( 'posts' === $required_type ) { |
|
| 149 | + if ( 'posts' === $required_type ) { |
|
| 150 | 150 | |
| 151 | - // Required filtered posts. |
|
| 152 | - wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 151 | + // Required filtered posts. |
|
| 152 | + wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 153 | 153 | |
| 154 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 155 | - $referencing_posts : |
|
| 156 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 157 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 158 | - '*', |
|
| 159 | - null, |
|
| 160 | - null, |
|
| 161 | - array(), |
|
| 162 | - null, |
|
| 163 | - $referencing_post_ids |
|
| 164 | - ); |
|
| 154 | + $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 155 | + $referencing_posts : |
|
| 156 | + Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 157 | + wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 158 | + '*', |
|
| 159 | + null, |
|
| 160 | + null, |
|
| 161 | + array(), |
|
| 162 | + null, |
|
| 163 | + $referencing_post_ids |
|
| 164 | + ); |
|
| 165 | 165 | |
| 166 | - if ( $filtered_posts ) { |
|
| 167 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 166 | + if ( $filtered_posts ) { |
|
| 167 | + foreach ( $filtered_posts as $post_obj ) { |
|
| 168 | 168 | |
| 169 | - $thumbnail = wp_get_attachment_url( get_post_thumbnail_id( $post_obj->ID, 'thumbnail' ) ); |
|
| 170 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 171 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 172 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 169 | + $thumbnail = wp_get_attachment_url( get_post_thumbnail_id( $post_obj->ID, 'thumbnail' ) ); |
|
| 170 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 171 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 172 | + $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 173 | 173 | |
| 174 | - $results[] = $post_obj; |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - } else { |
|
| 174 | + $results[] = $post_obj; |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + } else { |
|
| 178 | 178 | |
| 179 | - global $wpdb; |
|
| 179 | + global $wpdb; |
|
| 180 | 180 | |
| 181 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 181 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 182 | 182 | |
| 183 | - // Retrieve Wordlift relation instances table name. |
|
| 184 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 183 | + // Retrieve Wordlift relation instances table name. |
|
| 184 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 185 | 185 | |
| 186 | - /* |
|
| 186 | + /* |
|
| 187 | 187 | * Make sure we have some referenced post, otherwise the IN parts of |
| 188 | 188 | * the SQL will produce an SQL error. |
| 189 | 189 | */ |
| 190 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 191 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 190 | + if ( ! empty( $referencing_post_ids ) ) { |
|
| 191 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 192 | 192 | |
| 193 | - $query = " |
|
| 193 | + $query = " |
|
| 194 | 194 | SELECT |
| 195 | 195 | object_id AS ID, |
| 196 | 196 | count( object_id ) AS counter |
@@ -202,29 +202,29 @@ discard block |
||
| 202 | 202 | LIMIT $limit; |
| 203 | 203 | "; |
| 204 | 204 | |
| 205 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 205 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 206 | 206 | |
| 207 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 207 | + $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 208 | 208 | |
| 209 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 209 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 210 | 210 | |
| 211 | - foreach ( $entities as $obj ) { |
|
| 211 | + foreach ( $entities as $obj ) { |
|
| 212 | 212 | |
| 213 | - $entity = get_post( $obj->ID ); |
|
| 214 | - // Ensure only valid and published entities are returned. |
|
| 215 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 213 | + $entity = get_post( $obj->ID ); |
|
| 214 | + // Ensure only valid and published entities are returned. |
|
| 215 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 216 | 216 | |
| 217 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 218 | - $serialized_entity['counter'] = $obj->counter; |
|
| 219 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
| 217 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
| 218 | + $serialized_entity['counter'] = $obj->counter; |
|
| 219 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
| 220 | 220 | |
| 221 | - $results[] = $serialized_entity; |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - } |
|
| 221 | + $results[] = $serialized_entity; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - wl_core_send_json( $results ); |
|
| 227 | + wl_core_send_json( $results ); |
|
| 228 | 228 | |
| 229 | 229 | } |
| 230 | 230 | |
@@ -12,17 +12,17 @@ discard block |
||
| 12 | 12 | * |
| 13 | 13 | * @param array $atts Shortcode attributes. |
| 14 | 14 | */ |
| 15 | -function wl_shortcode_faceted_search( $atts ) { |
|
| 15 | +function wl_shortcode_faceted_search($atts) { |
|
| 16 | 16 | |
| 17 | 17 | // Extract attributes and set default values. |
| 18 | - $shortcode_atts = shortcode_atts( array( |
|
| 19 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 18 | + $shortcode_atts = shortcode_atts(array( |
|
| 19 | + 'title' => __('Related articles', 'wordlift'), |
|
| 20 | 20 | 'show_facets' => true, |
| 21 | 21 | 'with_carousel' => true, |
| 22 | 22 | 'squared_thumbs' => false, |
| 23 | 23 | 'limit' => 20, |
| 24 | 24 | |
| 25 | - ), $atts ); |
|
| 25 | + ), $atts); |
|
| 26 | 26 | |
| 27 | 27 | foreach ( |
| 28 | 28 | array( |
@@ -33,8 +33,8 @@ discard block |
||
| 33 | 33 | ) { |
| 34 | 34 | |
| 35 | 35 | // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode. |
| 36 | - $shortcode_atts[ $att ] = filter_var( |
|
| 37 | - $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 36 | + $shortcode_atts[$att] = filter_var( |
|
| 37 | + $shortcode_atts[$att], FILTER_VALIDATE_BOOLEAN |
|
| 38 | 38 | ); |
| 39 | 39 | } |
| 40 | 40 | |
@@ -44,70 +44,69 @@ discard block |
||
| 44 | 44 | $current_post = get_post(); |
| 45 | 45 | |
| 46 | 46 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 47 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 48 | - $current_post->ID : |
|
| 49 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 47 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 48 | + $current_post->ID : wl_core_get_related_entity_ids($current_post->ID); |
|
| 50 | 49 | |
| 51 | - if ( 0 === count( $entity_ids ) ) { |
|
| 50 | + if (0 === count($entity_ids)) { |
|
| 52 | 51 | return ''; |
| 53 | 52 | } |
| 54 | 53 | |
| 55 | 54 | $div_id = 'wordlift-faceted-entity-search-widget'; |
| 56 | 55 | |
| 57 | - wp_enqueue_style( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-faceted-entity-search-widget.min.css' ); |
|
| 58 | - wp_enqueue_script( 'angularjs', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.min.js' ); |
|
| 59 | - wp_enqueue_script( 'angularjs-touch', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-touch.min.js' ); |
|
| 56 | + wp_enqueue_style('wordlift-faceted-search', dirname(plugin_dir_url(__FILE__)).'/css/wordlift-faceted-entity-search-widget.min.css'); |
|
| 57 | + wp_enqueue_script('angularjs', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.min.js'); |
|
| 58 | + wp_enqueue_script('angularjs-touch', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-touch.min.js'); |
|
| 60 | 59 | |
| 61 | - wp_enqueue_script( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-faceted-entity-search-widget.min.js' ); |
|
| 60 | + wp_enqueue_script('wordlift-faceted-search', dirname(plugin_dir_url(__FILE__)).'/js/wordlift-faceted-entity-search-widget.min.js'); |
|
| 62 | 61 | |
| 63 | 62 | wp_localize_script( |
| 64 | 63 | 'wordlift-faceted-search', |
| 65 | 64 | 'wl_faceted_search_params', array( |
| 66 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 65 | + 'ajax_url' => admin_url('admin-ajax.php'), |
|
| 67 | 66 | 'action' => 'wl_faceted_search', |
| 68 | 67 | 'post_id' => $current_post->ID, |
| 69 | 68 | 'entity_ids' => $entity_ids, |
| 70 | - 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 69 | + 'limit' => apply_filters('wl_faceted_search_limit', $shortcode_atts['limit']), |
|
| 71 | 70 | 'div_id' => $div_id, |
| 72 | 71 | 'defaultThumbnailPath' => WL_DEFAULT_THUMBNAIL_PATH, |
| 73 | 72 | 'attrs' => $shortcode_atts, |
| 74 | 73 | 'l10n' => array( |
| 75 | - 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 76 | - 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 77 | - 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 78 | - 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 74 | + 'what' => _x('What', 'Faceted Search Widget', 'wordlift'), |
|
| 75 | + 'who' => _x('Who', 'Faceted Search Widget', 'wordlift'), |
|
| 76 | + 'where' => _x('Where', 'Faceted Search Widget', 'wordlift'), |
|
| 77 | + 'when' => _x('When', 'Faceted Search Widget', 'wordlift'), |
|
| 79 | 78 | ), |
| 80 | 79 | ) |
| 81 | 80 | ); |
| 82 | 81 | |
| 83 | - return '<div id="' . $div_id . '" style="width:100%"></div>'; |
|
| 82 | + return '<div id="'.$div_id.'" style="width:100%"></div>'; |
|
| 84 | 83 | } |
| 85 | 84 | |
| 86 | -add_shortcode( 'wl_faceted_search', 'wl_shortcode_faceted_search' ); |
|
| 85 | +add_shortcode('wl_faceted_search', 'wl_shortcode_faceted_search'); |
|
| 87 | 86 | |
| 88 | 87 | |
| 89 | 88 | /** |
| 90 | 89 | * Ajax call for the faceted search widget |
| 91 | 90 | */ |
| 92 | -function wl_shortcode_faceted_search_ajax( $http_raw_data = null ) { |
|
| 91 | +function wl_shortcode_faceted_search_ajax($http_raw_data = null) { |
|
| 93 | 92 | |
| 94 | 93 | // Post ID must be defined. |
| 95 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 96 | - wp_die( 'No post_id given' ); |
|
| 94 | + if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
| 95 | + wp_die('No post_id given'); |
|
| 97 | 96 | |
| 98 | 97 | return; |
| 99 | 98 | } |
| 100 | 99 | |
| 101 | 100 | // Extract filtering conditions. |
| 102 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 103 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 101 | + $filtering_entity_uris = (null == $http_raw_data) ? file_get_contents('php://input') : $http_raw_data; |
|
| 102 | + $filtering_entity_uris = json_decode($filtering_entity_uris); |
|
| 104 | 103 | |
| 105 | 104 | $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
| 106 | - $current_post = get_post( $current_post_id ); |
|
| 105 | + $current_post = get_post($current_post_id); |
|
| 107 | 106 | |
| 108 | 107 | // Post ID has to match an existing item. |
| 109 | - if ( null === $current_post ) { |
|
| 110 | - wp_die( 'No valid post_id given' ); |
|
| 108 | + if (null === $current_post) { |
|
| 109 | + wp_die('No valid post_id given'); |
|
| 111 | 110 | |
| 112 | 111 | return; |
| 113 | 112 | } |
@@ -116,45 +115,43 @@ discard block |
||
| 116 | 115 | // the current post is used as main entity. |
| 117 | 116 | // Otherwise, current post related entities are used. |
| 118 | 117 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 119 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 120 | - array( $current_post->ID ) : |
|
| 121 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 118 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 119 | + array($current_post->ID) : wl_core_get_related_entity_ids($current_post->ID); |
|
| 122 | 120 | |
| 123 | 121 | // If there are no entities we cannot render the widget. |
| 124 | - if ( 0 === count( $entity_ids ) ) { |
|
| 125 | - wp_die( 'No entities available' ); |
|
| 122 | + if (0 === count($entity_ids)) { |
|
| 123 | + wp_die('No entities available'); |
|
| 126 | 124 | |
| 127 | 125 | return; |
| 128 | 126 | } |
| 129 | 127 | |
| 130 | 128 | // Retrieve requested type |
| 131 | - $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 129 | + $required_type = (isset($_GET['type'])) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 132 | 130 | |
| 133 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 131 | + $limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 134 | 132 | |
| 135 | 133 | $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
| 136 | 134 | $entity_ids, |
| 137 | 135 | '*', |
| 138 | 136 | null, |
| 139 | 137 | 'publish', |
| 140 | - array( $current_post_id ), |
|
| 138 | + array($current_post_id), |
|
| 141 | 139 | $limit |
| 142 | 140 | ); |
| 143 | 141 | |
| 144 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 142 | + $referencing_post_ids = array_map(function($p) { |
|
| 145 | 143 | return $p->ID; |
| 146 | - }, $referencing_posts ); |
|
| 144 | + }, $referencing_posts); |
|
| 147 | 145 | $results = array(); |
| 148 | 146 | |
| 149 | - if ( 'posts' === $required_type ) { |
|
| 147 | + if ('posts' === $required_type) { |
|
| 150 | 148 | |
| 151 | 149 | // Required filtered posts. |
| 152 | - wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 150 | + wl_write_log("Going to find related posts for the current post [ post ID :: $current_post_id ]"); |
|
| 153 | 151 | |
| 154 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 155 | - $referencing_posts : |
|
| 156 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 157 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 152 | + $filtered_posts = (empty($filtering_entity_uris)) ? |
|
| 153 | + $referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 154 | + wl_get_entity_post_ids_by_uris($filtering_entity_uris), |
|
| 158 | 155 | '*', |
| 159 | 156 | null, |
| 160 | 157 | null, |
@@ -163,13 +160,13 @@ discard block |
||
| 163 | 160 | $referencing_post_ids |
| 164 | 161 | ); |
| 165 | 162 | |
| 166 | - if ( $filtered_posts ) { |
|
| 167 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 163 | + if ($filtered_posts) { |
|
| 164 | + foreach ($filtered_posts as $post_obj) { |
|
| 168 | 165 | |
| 169 | - $thumbnail = wp_get_attachment_url( get_post_thumbnail_id( $post_obj->ID, 'thumbnail' ) ); |
|
| 170 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 166 | + $thumbnail = wp_get_attachment_url(get_post_thumbnail_id($post_obj->ID, 'thumbnail')); |
|
| 167 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 171 | 168 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 172 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 169 | + $post_obj->permalink = get_post_permalink($post_obj->ID); |
|
| 173 | 170 | |
| 174 | 171 | $results[] = $post_obj; |
| 175 | 172 | } |
@@ -178,7 +175,7 @@ discard block |
||
| 178 | 175 | |
| 179 | 176 | global $wpdb; |
| 180 | 177 | |
| 181 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 178 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ]"); |
|
| 182 | 179 | |
| 183 | 180 | // Retrieve Wordlift relation instances table name. |
| 184 | 181 | $table_name = wl_core_get_relation_instances_table_name(); |
@@ -187,8 +184,8 @@ discard block |
||
| 187 | 184 | * Make sure we have some referenced post, otherwise the IN parts of |
| 188 | 185 | * the SQL will produce an SQL error. |
| 189 | 186 | */ |
| 190 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 191 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 187 | + if ( ! empty($referencing_post_ids)) { |
|
| 188 | + $subject_ids = implode(',', $referencing_post_ids); |
|
| 192 | 189 | |
| 193 | 190 | $query = " |
| 194 | 191 | SELECT |
@@ -202,19 +199,19 @@ discard block |
||
| 202 | 199 | LIMIT $limit; |
| 203 | 200 | "; |
| 204 | 201 | |
| 205 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 202 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
| 206 | 203 | |
| 207 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 204 | + $entities = $wpdb->get_results($query, OBJECT); // No cache ok. |
|
| 208 | 205 | |
| 209 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 206 | + wl_write_log('Entities found '.count($entities)); |
|
| 210 | 207 | |
| 211 | - foreach ( $entities as $obj ) { |
|
| 208 | + foreach ($entities as $obj) { |
|
| 212 | 209 | |
| 213 | - $entity = get_post( $obj->ID ); |
|
| 210 | + $entity = get_post($obj->ID); |
|
| 214 | 211 | // Ensure only valid and published entities are returned. |
| 215 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 212 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
| 216 | 213 | |
| 217 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 214 | + $serialized_entity = wl_serialize_entity($entity); |
|
| 218 | 215 | $serialized_entity['counter'] = $obj->counter; |
| 219 | 216 | $serialized_entity['createdAt'] = $entity->post_date; |
| 220 | 217 | |
@@ -224,9 +221,9 @@ discard block |
||
| 224 | 221 | } |
| 225 | 222 | } |
| 226 | 223 | |
| 227 | - wl_core_send_json( $results ); |
|
| 224 | + wl_core_send_json($results); |
|
| 228 | 225 | |
| 229 | 226 | } |
| 230 | 227 | |
| 231 | -add_action( 'wp_ajax_wl_faceted_search', 'wl_shortcode_faceted_search_ajax' ); |
|
| 232 | -add_action( 'wp_ajax_nopriv_wl_faceted_search', 'wl_shortcode_faceted_search_ajax' ); |
|
| 228 | +add_action('wp_ajax_wl_faceted_search', 'wl_shortcode_faceted_search_ajax'); |
|
| 229 | +add_action('wp_ajax_nopriv_wl_faceted_search', 'wl_shortcode_faceted_search_ajax'); |
|
@@ -18,322 +18,322 @@ |
||
| 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 | - wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.css' ); |
|
| 181 | - |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Configure all the configuration parameters. |
|
| 186 | - * |
|
| 187 | - * Called by the *admin_init* hook. |
|
| 188 | - * |
|
| 189 | - * @since 3.11.0 |
|
| 190 | - */ |
|
| 191 | - function admin_init() { |
|
| 192 | - |
|
| 193 | - // Register WordLift's general settings, providing our own sanitize callback |
|
| 194 | - // which will also check whether the user filled the WL Publisher form. |
|
| 195 | - register_setting( |
|
| 196 | - 'wl_general_settings', |
|
| 197 | - 'wl_general_settings', |
|
| 198 | - array( $this, 'sanitize_callback', ) |
|
| 199 | - ); |
|
| 200 | - |
|
| 201 | - // Add the general settings section. |
|
| 202 | - add_settings_section( |
|
| 203 | - 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
|
| 204 | - '', // Section header. |
|
| 205 | - '', // Callback used to render the description of the section. |
|
| 206 | - 'wl_general_settings' // Page on which to add this section of options. |
|
| 207 | - ); |
|
| 208 | - |
|
| 209 | - $key_args = array( |
|
| 210 | - 'id' => 'wl-key', |
|
| 211 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 212 | - 'value' => $this->configuration_service->get_key(), |
|
| 213 | - 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ), |
|
| 214 | - ); |
|
| 215 | - |
|
| 216 | - // Set the class for the key field based on the validity of the key. |
|
| 217 | - // Class should be "untouched" for an empty (virgin) value, "valid" |
|
| 218 | - // if the key is valid, or "invalid" otherwise. |
|
| 219 | - $validation_service = new Wordlift_Key_Validation_Service( $this->configuration_service ); |
|
| 220 | - |
|
| 221 | - if ( empty( $key_args['value'] ) ) { |
|
| 222 | - $key_args['css_class'] = 'untouched'; |
|
| 223 | - } elseif ( $validation_service->is_valid( $key_args['value'] ) ) { |
|
| 224 | - $key_args['css_class'] = 'valid'; |
|
| 225 | - } else { |
|
| 226 | - $key_args['css_class'] = 'invalid'; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - // Add the `key` field. |
|
| 230 | - add_settings_field( |
|
| 231 | - 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 232 | - _x( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 233 | - // The name of the function responsible for rendering the option interface. |
|
| 234 | - array( $this->input_element, 'render', ), |
|
| 235 | - 'wl_general_settings', // The page on which this option will be displayed. |
|
| 236 | - 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 237 | - $key_args // The array of arguments to pass to the callback. In this case, just a description. |
|
| 238 | - ); |
|
| 239 | - |
|
| 240 | - // Entity Base Path input. |
|
| 241 | - $entity_base_path_args = array( |
|
| 242 | - // The array of arguments to pass to the callback. In this case, just a description. |
|
| 243 | - 'id' => 'wl-entity-base-path', |
|
| 244 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 245 | - 'value' => $this->configuration_service->get_entity_base_path(), |
|
| 246 | - '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' ), |
|
| 247 | - ); |
|
| 248 | - |
|
| 249 | - $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
|
| 250 | - |
|
| 251 | - // Add the `wl_entity_base_path` field. |
|
| 252 | - add_settings_field( |
|
| 253 | - 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 254 | - _x( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 255 | - // The name of the function responsible for rendering the option interface |
|
| 256 | - array( $this->input_element, 'render', ), |
|
| 257 | - 'wl_general_settings', // The page on which this option will be displayed |
|
| 258 | - 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 259 | - $entity_base_path_args |
|
| 260 | - ); |
|
| 261 | - |
|
| 262 | - // Add the `language_name` field. |
|
| 263 | - add_settings_field( |
|
| 264 | - 'wl-site-language', |
|
| 265 | - _x( 'Site Language', 'wordlift' ), |
|
| 266 | - array( $this->language_select_element, 'render' ), |
|
| 267 | - 'wl_general_settings', |
|
| 268 | - 'wl_general_settings_section', |
|
| 269 | - array( |
|
| 270 | - // The array of arguments to pass to the callback. In this case, just a description. |
|
| 271 | - 'id' => 'wl-site-language', |
|
| 272 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 273 | - 'value' => $this->configuration_service->get_language_code(), |
|
| 274 | - 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 275 | - ) |
|
| 276 | - ); |
|
| 277 | - |
|
| 278 | - // Add the `publisher` field. |
|
| 279 | - add_settings_field( |
|
| 280 | - 'wl-publisher-id', |
|
| 281 | - _x( 'Publisher', 'wordlift' ), |
|
| 282 | - array( $this->publisher_element, 'render' ), |
|
| 283 | - 'wl_general_settings', |
|
| 284 | - 'wl_general_settings_section', |
|
| 285 | - array( |
|
| 286 | - 'id' => 'wl-publisher-id', |
|
| 287 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 288 | - ) |
|
| 289 | - ); |
|
| 290 | - |
|
| 291 | - // Add the `link by default` field. |
|
| 292 | - add_settings_field( |
|
| 293 | - 'wl-link-by-default', |
|
| 294 | - _x( 'Link by Default', 'wordlift' ), |
|
| 295 | - array( $this->radio_input_element, 'render' ), |
|
| 296 | - 'wl_general_settings', |
|
| 297 | - 'wl_general_settings_section', |
|
| 298 | - array( |
|
| 299 | - 'id' => 'wl-link-by-default', |
|
| 300 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 301 | - 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
|
| 302 | - 'description' => _x( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 303 | - ) |
|
| 304 | - ); |
|
| 305 | - |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Sanitize the configuration settings to be stored. |
|
| 310 | - * |
|
| 311 | - * If a new entity is being created for the publisher, create it and set The |
|
| 312 | - * publisher setting. |
|
| 313 | - * |
|
| 314 | - * @since 3.11.0 |
|
| 315 | - * |
|
| 316 | - * @param array $input The configuration settings array. |
|
| 317 | - * |
|
| 318 | - * @return array The sanitized input array. |
|
| 319 | - */ |
|
| 320 | - function sanitize_callback( $input ) { |
|
| 321 | - |
|
| 322 | - // Check whether a publisher name has been set. |
|
| 323 | - if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { |
|
| 324 | - $name = $_POST['wl_publisher']['name']; |
|
| 325 | - $type = $_POST['wl_publisher']['type']; |
|
| 326 | - $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; |
|
| 327 | - |
|
| 328 | - // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
|
| 329 | - $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 330 | - |
|
| 331 | - // Create an entity for the publisher and assign it to the input |
|
| 332 | - // parameter which WordPress automatically saves into the settings. |
|
| 333 | - $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - return $input; |
|
| 337 | - } |
|
| 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 | + wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.css' ); |
|
| 181 | + |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Configure all the configuration parameters. |
|
| 186 | + * |
|
| 187 | + * Called by the *admin_init* hook. |
|
| 188 | + * |
|
| 189 | + * @since 3.11.0 |
|
| 190 | + */ |
|
| 191 | + function admin_init() { |
|
| 192 | + |
|
| 193 | + // Register WordLift's general settings, providing our own sanitize callback |
|
| 194 | + // which will also check whether the user filled the WL Publisher form. |
|
| 195 | + register_setting( |
|
| 196 | + 'wl_general_settings', |
|
| 197 | + 'wl_general_settings', |
|
| 198 | + array( $this, 'sanitize_callback', ) |
|
| 199 | + ); |
|
| 200 | + |
|
| 201 | + // Add the general settings section. |
|
| 202 | + add_settings_section( |
|
| 203 | + 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
|
| 204 | + '', // Section header. |
|
| 205 | + '', // Callback used to render the description of the section. |
|
| 206 | + 'wl_general_settings' // Page on which to add this section of options. |
|
| 207 | + ); |
|
| 208 | + |
|
| 209 | + $key_args = array( |
|
| 210 | + 'id' => 'wl-key', |
|
| 211 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 212 | + 'value' => $this->configuration_service->get_key(), |
|
| 213 | + 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ), |
|
| 214 | + ); |
|
| 215 | + |
|
| 216 | + // Set the class for the key field based on the validity of the key. |
|
| 217 | + // Class should be "untouched" for an empty (virgin) value, "valid" |
|
| 218 | + // if the key is valid, or "invalid" otherwise. |
|
| 219 | + $validation_service = new Wordlift_Key_Validation_Service( $this->configuration_service ); |
|
| 220 | + |
|
| 221 | + if ( empty( $key_args['value'] ) ) { |
|
| 222 | + $key_args['css_class'] = 'untouched'; |
|
| 223 | + } elseif ( $validation_service->is_valid( $key_args['value'] ) ) { |
|
| 224 | + $key_args['css_class'] = 'valid'; |
|
| 225 | + } else { |
|
| 226 | + $key_args['css_class'] = 'invalid'; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + // Add the `key` field. |
|
| 230 | + add_settings_field( |
|
| 231 | + 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 232 | + _x( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 233 | + // The name of the function responsible for rendering the option interface. |
|
| 234 | + array( $this->input_element, 'render', ), |
|
| 235 | + 'wl_general_settings', // The page on which this option will be displayed. |
|
| 236 | + 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 237 | + $key_args // The array of arguments to pass to the callback. In this case, just a description. |
|
| 238 | + ); |
|
| 239 | + |
|
| 240 | + // Entity Base Path input. |
|
| 241 | + $entity_base_path_args = array( |
|
| 242 | + // The array of arguments to pass to the callback. In this case, just a description. |
|
| 243 | + 'id' => 'wl-entity-base-path', |
|
| 244 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 245 | + 'value' => $this->configuration_service->get_entity_base_path(), |
|
| 246 | + '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' ), |
|
| 247 | + ); |
|
| 248 | + |
|
| 249 | + $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
|
| 250 | + |
|
| 251 | + // Add the `wl_entity_base_path` field. |
|
| 252 | + add_settings_field( |
|
| 253 | + 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 254 | + _x( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 255 | + // The name of the function responsible for rendering the option interface |
|
| 256 | + array( $this->input_element, 'render', ), |
|
| 257 | + 'wl_general_settings', // The page on which this option will be displayed |
|
| 258 | + 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 259 | + $entity_base_path_args |
|
| 260 | + ); |
|
| 261 | + |
|
| 262 | + // Add the `language_name` field. |
|
| 263 | + add_settings_field( |
|
| 264 | + 'wl-site-language', |
|
| 265 | + _x( 'Site Language', 'wordlift' ), |
|
| 266 | + array( $this->language_select_element, 'render' ), |
|
| 267 | + 'wl_general_settings', |
|
| 268 | + 'wl_general_settings_section', |
|
| 269 | + array( |
|
| 270 | + // The array of arguments to pass to the callback. In this case, just a description. |
|
| 271 | + 'id' => 'wl-site-language', |
|
| 272 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 273 | + 'value' => $this->configuration_service->get_language_code(), |
|
| 274 | + 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 275 | + ) |
|
| 276 | + ); |
|
| 277 | + |
|
| 278 | + // Add the `publisher` field. |
|
| 279 | + add_settings_field( |
|
| 280 | + 'wl-publisher-id', |
|
| 281 | + _x( 'Publisher', 'wordlift' ), |
|
| 282 | + array( $this->publisher_element, 'render' ), |
|
| 283 | + 'wl_general_settings', |
|
| 284 | + 'wl_general_settings_section', |
|
| 285 | + array( |
|
| 286 | + 'id' => 'wl-publisher-id', |
|
| 287 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 288 | + ) |
|
| 289 | + ); |
|
| 290 | + |
|
| 291 | + // Add the `link by default` field. |
|
| 292 | + add_settings_field( |
|
| 293 | + 'wl-link-by-default', |
|
| 294 | + _x( 'Link by Default', 'wordlift' ), |
|
| 295 | + array( $this->radio_input_element, 'render' ), |
|
| 296 | + 'wl_general_settings', |
|
| 297 | + 'wl_general_settings_section', |
|
| 298 | + array( |
|
| 299 | + 'id' => 'wl-link-by-default', |
|
| 300 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 301 | + 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
|
| 302 | + 'description' => _x( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 303 | + ) |
|
| 304 | + ); |
|
| 305 | + |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Sanitize the configuration settings to be stored. |
|
| 310 | + * |
|
| 311 | + * If a new entity is being created for the publisher, create it and set The |
|
| 312 | + * publisher setting. |
|
| 313 | + * |
|
| 314 | + * @since 3.11.0 |
|
| 315 | + * |
|
| 316 | + * @param array $input The configuration settings array. |
|
| 317 | + * |
|
| 318 | + * @return array The sanitized input array. |
|
| 319 | + */ |
|
| 320 | + function sanitize_callback( $input ) { |
|
| 321 | + |
|
| 322 | + // Check whether a publisher name has been set. |
|
| 323 | + if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { |
|
| 324 | + $name = $_POST['wl_publisher']['name']; |
|
| 325 | + $type = $_POST['wl_publisher']['type']; |
|
| 326 | + $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; |
|
| 327 | + |
|
| 328 | + // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
|
| 329 | + $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 330 | + |
|
| 331 | + // Create an entity for the publisher and assign it to the input |
|
| 332 | + // parameter which WordPress automatically saves into the settings. |
|
| 333 | + $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + return $input; |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | 339 | } |
@@ -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,8 +176,8 @@ 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' ) ); |
|
| 180 | - wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.css' ); |
|
| 179 | + wp_enqueue_script('wordlift-admin-settings-page', plugin_dir_url(dirname(__FILE__)).'admin/js/1/settings.js', array('wp-util')); |
|
| 180 | + wp_enqueue_style('wordlift-admin-settings-page', plugin_dir_url(dirname(__FILE__)).'admin/js/1/settings.css'); |
|
| 181 | 181 | |
| 182 | 182 | } |
| 183 | 183 | |
@@ -195,32 +195,32 @@ discard block |
||
| 195 | 195 | register_setting( |
| 196 | 196 | 'wl_general_settings', |
| 197 | 197 | 'wl_general_settings', |
| 198 | - array( $this, 'sanitize_callback', ) |
|
| 198 | + array($this, 'sanitize_callback',) |
|
| 199 | 199 | ); |
| 200 | 200 | |
| 201 | 201 | // Add the general settings section. |
| 202 | 202 | add_settings_section( |
| 203 | 203 | 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
| 204 | - '', // Section header. |
|
| 205 | - '', // Callback used to render the description of the section. |
|
| 204 | + '', // Section header. |
|
| 205 | + '', // Callback used to render the description of the section. |
|
| 206 | 206 | 'wl_general_settings' // Page on which to add this section of options. |
| 207 | 207 | ); |
| 208 | 208 | |
| 209 | 209 | $key_args = array( |
| 210 | 210 | 'id' => 'wl-key', |
| 211 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 211 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::KEY.']', |
|
| 212 | 212 | 'value' => $this->configuration_service->get_key(), |
| 213 | - 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ), |
|
| 213 | + 'description' => __('Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift'), |
|
| 214 | 214 | ); |
| 215 | 215 | |
| 216 | 216 | // Set the class for the key field based on the validity of the key. |
| 217 | 217 | // Class should be "untouched" for an empty (virgin) value, "valid" |
| 218 | 218 | // if the key is valid, or "invalid" otherwise. |
| 219 | - $validation_service = new Wordlift_Key_Validation_Service( $this->configuration_service ); |
|
| 219 | + $validation_service = new Wordlift_Key_Validation_Service($this->configuration_service); |
|
| 220 | 220 | |
| 221 | - if ( empty( $key_args['value'] ) ) { |
|
| 221 | + if (empty($key_args['value'])) { |
|
| 222 | 222 | $key_args['css_class'] = 'untouched'; |
| 223 | - } elseif ( $validation_service->is_valid( $key_args['value'] ) ) { |
|
| 223 | + } elseif ($validation_service->is_valid($key_args['value'])) { |
|
| 224 | 224 | $key_args['css_class'] = 'valid'; |
| 225 | 225 | } else { |
| 226 | 226 | $key_args['css_class'] = 'invalid'; |
@@ -228,12 +228,12 @@ discard block |
||
| 228 | 228 | |
| 229 | 229 | // Add the `key` field. |
| 230 | 230 | add_settings_field( |
| 231 | - 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 232 | - _x( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 231 | + 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 232 | + _x('WordLift Key', 'wordlift'), // The label to the left of the option interface element. |
|
| 233 | 233 | // The name of the function responsible for rendering the option interface. |
| 234 | - array( $this->input_element, 'render', ), |
|
| 235 | - 'wl_general_settings', // The page on which this option will be displayed. |
|
| 236 | - 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 234 | + array($this->input_element, 'render',), |
|
| 235 | + 'wl_general_settings', // The page on which this option will be displayed. |
|
| 236 | + 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 237 | 237 | $key_args // The array of arguments to pass to the callback. In this case, just a description. |
| 238 | 238 | ); |
| 239 | 239 | |
@@ -241,65 +241,65 @@ discard block |
||
| 241 | 241 | $entity_base_path_args = array( |
| 242 | 242 | // The array of arguments to pass to the callback. In this case, just a description. |
| 243 | 243 | 'id' => 'wl-entity-base-path', |
| 244 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 244 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY.']', |
|
| 245 | 245 | 'value' => $this->configuration_service->get_entity_base_path(), |
| 246 | - '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 | + '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'), |
|
| 247 | 247 | ); |
| 248 | 248 | |
| 249 | 249 | $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
| 250 | 250 | |
| 251 | 251 | // Add the `wl_entity_base_path` field. |
| 252 | 252 | add_settings_field( |
| 253 | - 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 254 | - _x( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 253 | + 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 254 | + _x('Entity Base Path', 'wordlift'), // The label to the left of the option interface element |
|
| 255 | 255 | // The name of the function responsible for rendering the option interface |
| 256 | - array( $this->input_element, 'render', ), |
|
| 257 | - 'wl_general_settings', // The page on which this option will be displayed |
|
| 258 | - 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 256 | + array($this->input_element, 'render',), |
|
| 257 | + 'wl_general_settings', // The page on which this option will be displayed |
|
| 258 | + 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 259 | 259 | $entity_base_path_args |
| 260 | 260 | ); |
| 261 | 261 | |
| 262 | 262 | // Add the `language_name` field. |
| 263 | 263 | add_settings_field( |
| 264 | 264 | 'wl-site-language', |
| 265 | - _x( 'Site Language', 'wordlift' ), |
|
| 266 | - array( $this->language_select_element, 'render' ), |
|
| 265 | + _x('Site Language', 'wordlift'), |
|
| 266 | + array($this->language_select_element, 'render'), |
|
| 267 | 267 | 'wl_general_settings', |
| 268 | 268 | 'wl_general_settings_section', |
| 269 | 269 | array( |
| 270 | 270 | // The array of arguments to pass to the callback. In this case, just a description. |
| 271 | 271 | 'id' => 'wl-site-language', |
| 272 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 272 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::LANGUAGE.']', |
|
| 273 | 273 | 'value' => $this->configuration_service->get_language_code(), |
| 274 | - 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 274 | + 'description' => __('Each WordLift Key can be used only in one language. Pick yours.', 'wordlift'), |
|
| 275 | 275 | ) |
| 276 | 276 | ); |
| 277 | 277 | |
| 278 | 278 | // Add the `publisher` field. |
| 279 | 279 | add_settings_field( |
| 280 | 280 | 'wl-publisher-id', |
| 281 | - _x( 'Publisher', 'wordlift' ), |
|
| 282 | - array( $this->publisher_element, 'render' ), |
|
| 281 | + _x('Publisher', 'wordlift'), |
|
| 282 | + array($this->publisher_element, 'render'), |
|
| 283 | 283 | 'wl_general_settings', |
| 284 | 284 | 'wl_general_settings_section', |
| 285 | 285 | array( |
| 286 | 286 | 'id' => 'wl-publisher-id', |
| 287 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 287 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::PUBLISHER_ID.']', |
|
| 288 | 288 | ) |
| 289 | 289 | ); |
| 290 | 290 | |
| 291 | 291 | // Add the `link by default` field. |
| 292 | 292 | add_settings_field( |
| 293 | 293 | 'wl-link-by-default', |
| 294 | - _x( 'Link by Default', 'wordlift' ), |
|
| 295 | - array( $this->radio_input_element, 'render' ), |
|
| 294 | + _x('Link by Default', 'wordlift'), |
|
| 295 | + array($this->radio_input_element, 'render'), |
|
| 296 | 296 | 'wl_general_settings', |
| 297 | 297 | 'wl_general_settings_section', |
| 298 | 298 | array( |
| 299 | 299 | 'id' => 'wl-link-by-default', |
| 300 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 300 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::LINK_BY_DEFAULT.']', |
|
| 301 | 301 | 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
| 302 | - 'description' => _x( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 302 | + 'description' => _x('Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift'), |
|
| 303 | 303 | ) |
| 304 | 304 | ); |
| 305 | 305 | |
@@ -317,20 +317,20 @@ discard block |
||
| 317 | 317 | * |
| 318 | 318 | * @return array The sanitized input array. |
| 319 | 319 | */ |
| 320 | - function sanitize_callback( $input ) { |
|
| 320 | + function sanitize_callback($input) { |
|
| 321 | 321 | |
| 322 | 322 | // Check whether a publisher name has been set. |
| 323 | - if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { |
|
| 323 | + if (isset($_POST['wl_publisher']) && ! empty($_POST['wl_publisher']['name'])) { |
|
| 324 | 324 | $name = $_POST['wl_publisher']['name']; |
| 325 | 325 | $type = $_POST['wl_publisher']['type']; |
| 326 | 326 | $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; |
| 327 | 327 | |
| 328 | 328 | // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
| 329 | - $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 329 | + $type_uri = sprintf('http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person'); |
|
| 330 | 330 | |
| 331 | 331 | // Create an entity for the publisher and assign it to the input |
| 332 | 332 | // parameter which WordPress automatically saves into the settings. |
| 333 | - $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 333 | + $input['publisher_id'] = $this->entity_service->create($name, $type_uri, $thumbnail_id, 'publish'); |
|
| 334 | 334 | } |
| 335 | 335 | |
| 336 | 336 | return $input; |
@@ -22,166 +22,166 @@ |
||
| 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 | - 'Type at least 3 characters to search...' => _x( 'Type at least 3 characters to search...', 'Autocomplete Select', 'wordlift' ), |
|
| 162 | - 'No results found for your search.' => _x( 'No results found: try changing or removing some words.', 'Autocomplete Select', 'wordlift' ), |
|
| 163 | - 'Please wait while we look for entities in the linked data cloud...' => _x( 'Please wait while we look for entities in the linked data cloud...', 'Autocomplete Select', 'wordlift' ), |
|
| 164 | - ), |
|
| 165 | - 'wl_autocomplete_nonce' => wp_create_nonce( 'wordlift_autocomplete' ), |
|
| 166 | - ); |
|
| 167 | - |
|
| 168 | - // Set post-related values if there's a current post. |
|
| 169 | - if ( null !== $post = $entity_being_edited = get_post() ) { |
|
| 170 | - |
|
| 171 | - $params['post_id'] = $entity_being_edited->ID; |
|
| 172 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 173 | - $params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_service->is_entity( $post->ID ) && is_numeric( get_the_ID() ); |
|
| 174 | - // We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it |
|
| 175 | - // from the results, since we don't want the current entity to be discovered by the analysis. |
|
| 176 | - // |
|
| 177 | - // See https://github.com/insideout10/wordlift-plugin/issues/345 |
|
| 178 | - $params['itemId'] = $entity_service->get_uri( $entity_being_edited->ID ); |
|
| 179 | - |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - // Finally output the params as `wlSettings` for JavaScript code. |
|
| 183 | - wp_localize_script( $this->plugin_name, 'wlSettings', $params ); |
|
| 184 | - |
|
| 185 | - } |
|
| 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 | + 'Type at least 3 characters to search...' => _x( 'Type at least 3 characters to search...', 'Autocomplete Select', 'wordlift' ), |
|
| 162 | + 'No results found for your search.' => _x( 'No results found: try changing or removing some words.', 'Autocomplete Select', 'wordlift' ), |
|
| 163 | + 'Please wait while we look for entities in the linked data cloud...' => _x( 'Please wait while we look for entities in the linked data cloud...', 'Autocomplete Select', 'wordlift' ), |
|
| 164 | + ), |
|
| 165 | + 'wl_autocomplete_nonce' => wp_create_nonce( 'wordlift_autocomplete' ), |
|
| 166 | + ); |
|
| 167 | + |
|
| 168 | + // Set post-related values if there's a current post. |
|
| 169 | + if ( null !== $post = $entity_being_edited = get_post() ) { |
|
| 170 | + |
|
| 171 | + $params['post_id'] = $entity_being_edited->ID; |
|
| 172 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 173 | + $params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_service->is_entity( $post->ID ) && is_numeric( get_the_ID() ); |
|
| 174 | + // We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it |
|
| 175 | + // from the results, since we don't want the current entity to be discovered by the analysis. |
|
| 176 | + // |
|
| 177 | + // See https://github.com/insideout10/wordlift-plugin/issues/345 |
|
| 178 | + $params['itemId'] = $entity_service->get_uri( $entity_being_edited->ID ); |
|
| 179 | + |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + // Finally output the params as `wlSettings` for JavaScript code. |
|
| 183 | + wp_localize_script( $this->plugin_name, 'wlSettings', $params ); |
|
| 184 | + |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | 187 | } |
@@ -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,34 +153,34 @@ 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' ) ), |
|
| 161 | - 'Type at least 3 characters to search...' => _x( 'Type at least 3 characters to search...', 'Autocomplete Select', 'wordlift' ), |
|
| 162 | - 'No results found for your search.' => _x( 'No results found: try changing or removing some words.', 'Autocomplete Select', 'wordlift' ), |
|
| 163 | - 'Please wait while we look for entities in the linked data cloud...' => _x( 'Please wait while we look for entities in the linked data cloud...', 'Autocomplete Select', '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 | + 'Type at least 3 characters to search...' => _x('Type at least 3 characters to search...', 'Autocomplete Select', 'wordlift'), |
|
| 162 | + 'No results found for your search.' => _x('No results found: try changing or removing some words.', 'Autocomplete Select', 'wordlift'), |
|
| 163 | + 'Please wait while we look for entities in the linked data cloud...' => _x('Please wait while we look for entities in the linked data cloud...', 'Autocomplete Select', 'wordlift'), |
|
| 164 | 164 | ), |
| 165 | - 'wl_autocomplete_nonce' => wp_create_nonce( 'wordlift_autocomplete' ), |
|
| 165 | + 'wl_autocomplete_nonce' => wp_create_nonce('wordlift_autocomplete'), |
|
| 166 | 166 | ); |
| 167 | 167 | |
| 168 | 168 | // Set post-related values if there's a current post. |
| 169 | - if ( null !== $post = $entity_being_edited = get_post() ) { |
|
| 169 | + if (null !== $post = $entity_being_edited = get_post()) { |
|
| 170 | 170 | |
| 171 | 171 | $params['post_id'] = $entity_being_edited->ID; |
| 172 | 172 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 173 | - $params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_service->is_entity( $post->ID ) && is_numeric( get_the_ID() ); |
|
| 173 | + $params['entityBeingEdited'] = isset($entity_being_edited->post_type) && $entity_service->is_entity($post->ID) && is_numeric(get_the_ID()); |
|
| 174 | 174 | // We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it |
| 175 | 175 | // from the results, since we don't want the current entity to be discovered by the analysis. |
| 176 | 176 | // |
| 177 | 177 | // See https://github.com/insideout10/wordlift-plugin/issues/345 |
| 178 | - $params['itemId'] = $entity_service->get_uri( $entity_being_edited->ID ); |
|
| 178 | + $params['itemId'] = $entity_service->get_uri($entity_being_edited->ID); |
|
| 179 | 179 | |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | // Finally output the params as `wlSettings` for JavaScript code. |
| 183 | - wp_localize_script( $this->plugin_name, 'wlSettings', $params ); |
|
| 183 | + wp_localize_script($this->plugin_name, 'wlSettings', $params); |
|
| 184 | 184 | |
| 185 | 185 | } |
| 186 | 186 | |
@@ -19,400 +19,400 @@ |
||
| 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 | - * The HTML fragment includes the following parts: |
|
| 293 | - * * html wrapper open. |
|
| 294 | - * * heading. |
|
| 295 | - * * nonce. |
|
| 296 | - * * stored values. |
|
| 297 | - * * an empty input when there are no stored values. |
|
| 298 | - * * an add button to add more values. |
|
| 299 | - * * html wrapper close. |
|
| 300 | - */ |
|
| 301 | - public function html() { |
|
| 302 | - |
|
| 303 | - // Open main <div> for the Field. |
|
| 304 | - $html = $this->html_wrapper_open(); |
|
| 305 | - |
|
| 306 | - // Label. |
|
| 307 | - $html .= $this->get_heading_html(); |
|
| 308 | - |
|
| 309 | - // print nonce. |
|
| 310 | - $html .= $this->html_nonce(); |
|
| 311 | - |
|
| 312 | - // print data loaded from DB. |
|
| 313 | - $count = 0; |
|
| 314 | - $html .= $this->get_stored_values_html( $count ); |
|
| 315 | - |
|
| 316 | - // Print the empty <input> to add new values. |
|
| 317 | - if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 318 | - $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 319 | - $count ++; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - // If cardinality allows it, print button to add new values. |
|
| 323 | - $html .= $this->get_add_button_html( $count ); |
|
| 324 | - |
|
| 325 | - // Close the HTML wrapper. |
|
| 326 | - $html .= $this->html_wrapper_close(); |
|
| 327 | - |
|
| 328 | - return $html; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Print the heading with the label for the metabox. |
|
| 333 | - * |
|
| 334 | - * @since 3.15.0 |
|
| 335 | - * @return string The heading html fragment. |
|
| 336 | - */ |
|
| 337 | - protected function get_heading_html() { |
|
| 338 | - |
|
| 339 | - return "<h3>$this->label</h3>"; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Print the stored values. |
|
| 344 | - * |
|
| 345 | - * @since 3.15.0 |
|
| 346 | - * |
|
| 347 | - * @param int $count An output value: the number of printed values. |
|
| 348 | - * |
|
| 349 | - * @return string The html fragment. |
|
| 350 | - */ |
|
| 351 | - protected function get_stored_values_html( &$count ) { |
|
| 352 | - |
|
| 353 | - $html = ''; |
|
| 354 | - |
|
| 355 | - // print data loaded from DB. |
|
| 356 | - $count = 0; |
|
| 357 | - if ( $this->data ) { |
|
| 358 | - foreach ( $this->data as $value ) { |
|
| 359 | - if ( $count < $this->cardinality ) { |
|
| 360 | - $html .= $this->html_input( $value ); |
|
| 361 | - } |
|
| 362 | - $count ++; |
|
| 363 | - } |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - return $html; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Get the add button html. |
|
| 371 | - * |
|
| 372 | - * This function is protected, allowing extending class to further customize |
|
| 373 | - * the add button html code. |
|
| 374 | - * |
|
| 375 | - * @since 3.15.0 |
|
| 376 | - * |
|
| 377 | - * @param int $count The current number of values. |
|
| 378 | - * |
|
| 379 | - * @return string The add button html code. |
|
| 380 | - */ |
|
| 381 | - protected function get_add_button_html( $count ) { |
|
| 382 | - |
|
| 383 | - // If cardinality allows it, print button to add new values. |
|
| 384 | - if ( $count < $this->cardinality ) { |
|
| 385 | - return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>'; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - // Return an empty string. |
|
| 389 | - return ''; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Return a single <input> tag for the Field. |
|
| 394 | - * |
|
| 395 | - * @param mixed $value Input value. |
|
| 396 | - * |
|
| 397 | - * @return string The html code fragment. |
|
| 398 | - */ |
|
| 399 | - public function html_input( $value ) { |
|
| 400 | - $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 | + * The HTML fragment includes the following parts: |
|
| 293 | + * * html wrapper open. |
|
| 294 | + * * heading. |
|
| 295 | + * * nonce. |
|
| 296 | + * * stored values. |
|
| 297 | + * * an empty input when there are no stored values. |
|
| 298 | + * * an add button to add more values. |
|
| 299 | + * * html wrapper close. |
|
| 300 | + */ |
|
| 301 | + public function html() { |
|
| 302 | + |
|
| 303 | + // Open main <div> for the Field. |
|
| 304 | + $html = $this->html_wrapper_open(); |
|
| 305 | + |
|
| 306 | + // Label. |
|
| 307 | + $html .= $this->get_heading_html(); |
|
| 308 | + |
|
| 309 | + // print nonce. |
|
| 310 | + $html .= $this->html_nonce(); |
|
| 311 | + |
|
| 312 | + // print data loaded from DB. |
|
| 313 | + $count = 0; |
|
| 314 | + $html .= $this->get_stored_values_html( $count ); |
|
| 315 | + |
|
| 316 | + // Print the empty <input> to add new values. |
|
| 317 | + if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 318 | + $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 319 | + $count ++; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + // If cardinality allows it, print button to add new values. |
|
| 323 | + $html .= $this->get_add_button_html( $count ); |
|
| 324 | + |
|
| 325 | + // Close the HTML wrapper. |
|
| 326 | + $html .= $this->html_wrapper_close(); |
|
| 327 | + |
|
| 328 | + return $html; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Print the heading with the label for the metabox. |
|
| 333 | + * |
|
| 334 | + * @since 3.15.0 |
|
| 335 | + * @return string The heading html fragment. |
|
| 336 | + */ |
|
| 337 | + protected function get_heading_html() { |
|
| 338 | + |
|
| 339 | + return "<h3>$this->label</h3>"; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Print the stored values. |
|
| 344 | + * |
|
| 345 | + * @since 3.15.0 |
|
| 346 | + * |
|
| 347 | + * @param int $count An output value: the number of printed values. |
|
| 348 | + * |
|
| 349 | + * @return string The html fragment. |
|
| 350 | + */ |
|
| 351 | + protected function get_stored_values_html( &$count ) { |
|
| 352 | + |
|
| 353 | + $html = ''; |
|
| 354 | + |
|
| 355 | + // print data loaded from DB. |
|
| 356 | + $count = 0; |
|
| 357 | + if ( $this->data ) { |
|
| 358 | + foreach ( $this->data as $value ) { |
|
| 359 | + if ( $count < $this->cardinality ) { |
|
| 360 | + $html .= $this->html_input( $value ); |
|
| 361 | + } |
|
| 362 | + $count ++; |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + return $html; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Get the add button html. |
|
| 371 | + * |
|
| 372 | + * This function is protected, allowing extending class to further customize |
|
| 373 | + * the add button html code. |
|
| 374 | + * |
|
| 375 | + * @since 3.15.0 |
|
| 376 | + * |
|
| 377 | + * @param int $count The current number of values. |
|
| 378 | + * |
|
| 379 | + * @return string The add button html code. |
|
| 380 | + */ |
|
| 381 | + protected function get_add_button_html( $count ) { |
|
| 382 | + |
|
| 383 | + // If cardinality allows it, print button to add new values. |
|
| 384 | + if ( $count < $this->cardinality ) { |
|
| 385 | + return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>'; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + // Return an empty string. |
|
| 389 | + return ''; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Return a single <input> tag for the Field. |
|
| 394 | + * |
|
| 395 | + * @param mixed $value Input value. |
|
| 396 | + * |
|
| 397 | + * @return string The html code fragment. |
|
| 398 | + */ |
|
| 399 | + public function html_input( $value ) { |
|
| 400 | + $html = <<<EOF |
|
| 401 | 401 | <div class="wl-input-wrapper"> |
| 402 | 402 | <input type="text" id="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" style="width:88%" /> |
| 403 | 403 | <button class="button wl-remove-input wl-button" type="button">Remove</button> |
| 404 | 404 | </div> |
| 405 | 405 | EOF; |
| 406 | 406 | |
| 407 | - return $html; |
|
| 408 | - } |
|
| 407 | + return $html; |
|
| 408 | + } |
|
| 409 | 409 | |
| 410 | - /** |
|
| 411 | - * Returns closing for the wrapper HTML tag. |
|
| 412 | - */ |
|
| 413 | - public function html_wrapper_close() { |
|
| 410 | + /** |
|
| 411 | + * Returns closing for the wrapper HTML tag. |
|
| 412 | + */ |
|
| 413 | + public function html_wrapper_close() { |
|
| 414 | 414 | |
| 415 | - return '</div><hr>'; |
|
| 416 | - } |
|
| 415 | + return '</div><hr>'; |
|
| 416 | + } |
|
| 417 | 417 | |
| 418 | 418 | } |
@@ -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 | |
@@ -311,16 +311,16 @@ discard block |
||
| 311 | 311 | |
| 312 | 312 | // print data loaded from DB. |
| 313 | 313 | $count = 0; |
| 314 | - $html .= $this->get_stored_values_html( $count ); |
|
| 314 | + $html .= $this->get_stored_values_html($count); |
|
| 315 | 315 | |
| 316 | 316 | // Print the empty <input> to add new values. |
| 317 | - if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 318 | - $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 319 | - $count ++; |
|
| 317 | + if (0 === $count) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 318 | + $html .= $this->html_input(''); // Will print an empty <input>. |
|
| 319 | + $count++; |
|
| 320 | 320 | } |
| 321 | 321 | |
| 322 | 322 | // If cardinality allows it, print button to add new values. |
| 323 | - $html .= $this->get_add_button_html( $count ); |
|
| 323 | + $html .= $this->get_add_button_html($count); |
|
| 324 | 324 | |
| 325 | 325 | // Close the HTML wrapper. |
| 326 | 326 | $html .= $this->html_wrapper_close(); |
@@ -348,18 +348,18 @@ discard block |
||
| 348 | 348 | * |
| 349 | 349 | * @return string The html fragment. |
| 350 | 350 | */ |
| 351 | - protected function get_stored_values_html( &$count ) { |
|
| 351 | + protected function get_stored_values_html(&$count) { |
|
| 352 | 352 | |
| 353 | 353 | $html = ''; |
| 354 | 354 | |
| 355 | 355 | // print data loaded from DB. |
| 356 | 356 | $count = 0; |
| 357 | - if ( $this->data ) { |
|
| 358 | - foreach ( $this->data as $value ) { |
|
| 359 | - if ( $count < $this->cardinality ) { |
|
| 360 | - $html .= $this->html_input( $value ); |
|
| 357 | + if ($this->data) { |
|
| 358 | + foreach ($this->data as $value) { |
|
| 359 | + if ($count < $this->cardinality) { |
|
| 360 | + $html .= $this->html_input($value); |
|
| 361 | 361 | } |
| 362 | - $count ++; |
|
| 362 | + $count++; |
|
| 363 | 363 | } |
| 364 | 364 | } |
| 365 | 365 | |
@@ -378,11 +378,11 @@ discard block |
||
| 378 | 378 | * |
| 379 | 379 | * @return string The add button html code. |
| 380 | 380 | */ |
| 381 | - protected function get_add_button_html( $count ) { |
|
| 381 | + protected function get_add_button_html($count) { |
|
| 382 | 382 | |
| 383 | 383 | // If cardinality allows it, print button to add new values. |
| 384 | - if ( $count < $this->cardinality ) { |
|
| 385 | - return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>'; |
|
| 384 | + if ($count < $this->cardinality) { |
|
| 385 | + return '<button class="button wl-add-input wl-button" type="button">'.esc_html__('Add').'</button>'; |
|
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | // Return an empty string. |
@@ -396,7 +396,7 @@ discard block |
||
| 396 | 396 | * |
| 397 | 397 | * @return string The html code fragment. |
| 398 | 398 | */ |
| 399 | - public function html_input( $value ) { |
|
| 399 | + public function html_input($value) { |
|
| 400 | 400 | $html = <<<EOF |
| 401 | 401 | <div class="wl-input-wrapper"> |
| 402 | 402 | <input type="text" id="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" style="width:88%" /> |
@@ -16,126 +16,126 @@ |
||
| 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 | - mb_regex_encoding( 'UTF-8' ); |
|
| 32 | - $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 33 | - return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 34 | - }, array() ); |
|
| 35 | - |
|
| 36 | - parent::save_data( $merged ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @inheritdoc |
|
| 41 | - */ |
|
| 42 | - public function sanitize_data_filter( $value ) { |
|
| 43 | - |
|
| 44 | - // Call our sanitizer helper. |
|
| 45 | - return Wordlift_Sanitizer::sanitize_url( $value ); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @inheritdoc |
|
| 50 | - */ |
|
| 51 | - protected function get_heading_html() { |
|
| 52 | - |
|
| 53 | - // Add the select html fragment after the heading. |
|
| 54 | - return parent::get_heading_html() |
|
| 55 | - . $this->get_select_html(); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Get the select html fragment. |
|
| 60 | - * |
|
| 61 | - * @since 3.15.0 |
|
| 62 | - * @return string The html fragment. |
|
| 63 | - */ |
|
| 64 | - private function get_select_html() { |
|
| 65 | - // Return an element where the new Autocomplete Select will attach to. |
|
| 66 | - return '<p>' |
|
| 67 | - . esc_html__( 'Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift' ) |
|
| 68 | - . '<div id="wl-metabox-field-sameas"></div></p>'; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @inheritdoc |
|
| 73 | - */ |
|
| 74 | - protected function get_add_button_html( $count ) { |
|
| 75 | - |
|
| 76 | - $placeholder = esc_attr_x( 'Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift' ); |
|
| 77 | - |
|
| 78 | - return |
|
| 79 | - "<label for='$this->meta_name'>" |
|
| 80 | - . esc_html__( 'If you already know the URL of the entity that you would like to link, add it in the field below.', 'wordlift' ) |
|
| 81 | - . '</label>' |
|
| 82 | - . '<div class="wl-input-wrapper">' |
|
| 83 | - . "<input type='text' id='$this->meta_name' name='wl_metaboxes[$this->meta_name][]' placeholder='$placeholder' style='width:88%' />" |
|
| 84 | - . '<button class="button wl-remove-input wl-button" type="button">Remove</button>' |
|
| 85 | - . '</div>' |
|
| 86 | - . parent::get_add_button_html( $count ); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @inheritdoc |
|
| 91 | - */ |
|
| 92 | - protected function get_stored_values_html( &$count ) { |
|
| 93 | - |
|
| 94 | - return '<p>' |
|
| 95 | - . parent::get_stored_values_html( $count ) |
|
| 96 | - . '</p>'; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @inheritdoc |
|
| 101 | - */ |
|
| 102 | - public function html() { |
|
| 103 | - |
|
| 104 | - // Open main <div> for the Field. |
|
| 105 | - $html = $this->html_wrapper_open(); |
|
| 106 | - |
|
| 107 | - // Label. |
|
| 108 | - $html .= $this->get_heading_html(); |
|
| 109 | - |
|
| 110 | - // print nonce. |
|
| 111 | - $html .= $this->html_nonce(); |
|
| 112 | - |
|
| 113 | - // print data loaded from DB. |
|
| 114 | - $count = 0; |
|
| 115 | - |
|
| 116 | - // If cardinality allows it, print button to add new values. |
|
| 117 | - $html .= $this->get_add_button_html( $count ); |
|
| 118 | - |
|
| 119 | - $html .= $this->get_stored_values_html( $count ); |
|
| 120 | - |
|
| 121 | - // Close the HTML wrapper. |
|
| 122 | - $html .= $this->html_wrapper_close(); |
|
| 123 | - |
|
| 124 | - return $html; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @inheritdoc |
|
| 129 | - */ |
|
| 130 | - public function html_input( $value ) { |
|
| 131 | - $html = <<<EOF |
|
| 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 | + mb_regex_encoding( 'UTF-8' ); |
|
| 32 | + $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 33 | + return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 34 | + }, array() ); |
|
| 35 | + |
|
| 36 | + parent::save_data( $merged ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @inheritdoc |
|
| 41 | + */ |
|
| 42 | + public function sanitize_data_filter( $value ) { |
|
| 43 | + |
|
| 44 | + // Call our sanitizer helper. |
|
| 45 | + return Wordlift_Sanitizer::sanitize_url( $value ); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @inheritdoc |
|
| 50 | + */ |
|
| 51 | + protected function get_heading_html() { |
|
| 52 | + |
|
| 53 | + // Add the select html fragment after the heading. |
|
| 54 | + return parent::get_heading_html() |
|
| 55 | + . $this->get_select_html(); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Get the select html fragment. |
|
| 60 | + * |
|
| 61 | + * @since 3.15.0 |
|
| 62 | + * @return string The html fragment. |
|
| 63 | + */ |
|
| 64 | + private function get_select_html() { |
|
| 65 | + // Return an element where the new Autocomplete Select will attach to. |
|
| 66 | + return '<p>' |
|
| 67 | + . esc_html__( 'Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift' ) |
|
| 68 | + . '<div id="wl-metabox-field-sameas"></div></p>'; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @inheritdoc |
|
| 73 | + */ |
|
| 74 | + protected function get_add_button_html( $count ) { |
|
| 75 | + |
|
| 76 | + $placeholder = esc_attr_x( 'Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift' ); |
|
| 77 | + |
|
| 78 | + return |
|
| 79 | + "<label for='$this->meta_name'>" |
|
| 80 | + . esc_html__( 'If you already know the URL of the entity that you would like to link, add it in the field below.', 'wordlift' ) |
|
| 81 | + . '</label>' |
|
| 82 | + . '<div class="wl-input-wrapper">' |
|
| 83 | + . "<input type='text' id='$this->meta_name' name='wl_metaboxes[$this->meta_name][]' placeholder='$placeholder' style='width:88%' />" |
|
| 84 | + . '<button class="button wl-remove-input wl-button" type="button">Remove</button>' |
|
| 85 | + . '</div>' |
|
| 86 | + . parent::get_add_button_html( $count ); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @inheritdoc |
|
| 91 | + */ |
|
| 92 | + protected function get_stored_values_html( &$count ) { |
|
| 93 | + |
|
| 94 | + return '<p>' |
|
| 95 | + . parent::get_stored_values_html( $count ) |
|
| 96 | + . '</p>'; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @inheritdoc |
|
| 101 | + */ |
|
| 102 | + public function html() { |
|
| 103 | + |
|
| 104 | + // Open main <div> for the Field. |
|
| 105 | + $html = $this->html_wrapper_open(); |
|
| 106 | + |
|
| 107 | + // Label. |
|
| 108 | + $html .= $this->get_heading_html(); |
|
| 109 | + |
|
| 110 | + // print nonce. |
|
| 111 | + $html .= $this->html_nonce(); |
|
| 112 | + |
|
| 113 | + // print data loaded from DB. |
|
| 114 | + $count = 0; |
|
| 115 | + |
|
| 116 | + // If cardinality allows it, print button to add new values. |
|
| 117 | + $html .= $this->get_add_button_html( $count ); |
|
| 118 | + |
|
| 119 | + $html .= $this->get_stored_values_html( $count ); |
|
| 120 | + |
|
| 121 | + // Close the HTML wrapper. |
|
| 122 | + $html .= $this->html_wrapper_close(); |
|
| 123 | + |
|
| 124 | + return $html; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @inheritdoc |
|
| 129 | + */ |
|
| 130 | + public function html_input( $value ) { |
|
| 131 | + $html = <<<EOF |
|
| 132 | 132 | <div class="wl-input-wrapper"> |
| 133 | 133 | <input type="text" readonly="readonly" id="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" style="width:88%" /> |
| 134 | 134 | <button class="button wl-remove-input wl-button" type="button">Remove</button> |
| 135 | 135 | </div> |
| 136 | 136 | EOF; |
| 137 | 137 | |
| 138 | - return $html; |
|
| 139 | - } |
|
| 138 | + return $html; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | 141 | } |
@@ -19,30 +19,30 @@ discard block |
||
| 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 | - mb_regex_encoding( 'UTF-8' ); |
|
| 32 | - $merged = array_reduce( (array) $values, function ( $carry, $item ) { |
|
| 33 | - return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 34 | - }, array() ); |
|
| 31 | + mb_regex_encoding('UTF-8'); |
|
| 32 | + $merged = array_reduce((array) $values, function($carry, $item) { |
|
| 33 | + return array_merge($carry, mb_split("\x{2063}", wp_unslash($item))); |
|
| 34 | + }, array()); |
|
| 35 | 35 | |
| 36 | - parent::save_data( $merged ); |
|
| 36 | + parent::save_data($merged); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * @inheritdoc |
| 41 | 41 | */ |
| 42 | - public function sanitize_data_filter( $value ) { |
|
| 42 | + public function sanitize_data_filter($value) { |
|
| 43 | 43 | |
| 44 | 44 | // Call our sanitizer helper. |
| 45 | - return Wordlift_Sanitizer::sanitize_url( $value ); |
|
| 45 | + return Wordlift_Sanitizer::sanitize_url($value); |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** |
@@ -64,35 +64,35 @@ discard block |
||
| 64 | 64 | private function get_select_html() { |
| 65 | 65 | // Return an element where the new Autocomplete Select will attach to. |
| 66 | 66 | return '<p>' |
| 67 | - . esc_html__( 'Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift' ) |
|
| 67 | + . esc_html__('Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift') |
|
| 68 | 68 | . '<div id="wl-metabox-field-sameas"></div></p>'; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /** |
| 72 | 72 | * @inheritdoc |
| 73 | 73 | */ |
| 74 | - protected function get_add_button_html( $count ) { |
|
| 74 | + protected function get_add_button_html($count) { |
|
| 75 | 75 | |
| 76 | - $placeholder = esc_attr_x( 'Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift' ); |
|
| 76 | + $placeholder = esc_attr_x('Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift'); |
|
| 77 | 77 | |
| 78 | 78 | return |
| 79 | 79 | "<label for='$this->meta_name'>" |
| 80 | - . esc_html__( 'If you already know the URL of the entity that you would like to link, add it in the field below.', 'wordlift' ) |
|
| 80 | + . esc_html__('If you already know the URL of the entity that you would like to link, add it in the field below.', 'wordlift') |
|
| 81 | 81 | . '</label>' |
| 82 | 82 | . '<div class="wl-input-wrapper">' |
| 83 | 83 | . "<input type='text' id='$this->meta_name' name='wl_metaboxes[$this->meta_name][]' placeholder='$placeholder' style='width:88%' />" |
| 84 | 84 | . '<button class="button wl-remove-input wl-button" type="button">Remove</button>' |
| 85 | 85 | . '</div>' |
| 86 | - . parent::get_add_button_html( $count ); |
|
| 86 | + . parent::get_add_button_html($count); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
| 90 | 90 | * @inheritdoc |
| 91 | 91 | */ |
| 92 | - protected function get_stored_values_html( &$count ) { |
|
| 92 | + protected function get_stored_values_html(&$count) { |
|
| 93 | 93 | |
| 94 | 94 | return '<p>' |
| 95 | - . parent::get_stored_values_html( $count ) |
|
| 95 | + . parent::get_stored_values_html($count) |
|
| 96 | 96 | . '</p>'; |
| 97 | 97 | } |
| 98 | 98 | |
@@ -114,9 +114,9 @@ discard block |
||
| 114 | 114 | $count = 0; |
| 115 | 115 | |
| 116 | 116 | // If cardinality allows it, print button to add new values. |
| 117 | - $html .= $this->get_add_button_html( $count ); |
|
| 117 | + $html .= $this->get_add_button_html($count); |
|
| 118 | 118 | |
| 119 | - $html .= $this->get_stored_values_html( $count ); |
|
| 119 | + $html .= $this->get_stored_values_html($count); |
|
| 120 | 120 | |
| 121 | 121 | // Close the HTML wrapper. |
| 122 | 122 | $html .= $this->html_wrapper_close(); |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | /** |
| 128 | 128 | * @inheritdoc |
| 129 | 129 | */ |
| 130 | - public function html_input( $value ) { |
|
| 130 | + public function html_input($value) { |
|
| 131 | 131 | $html = <<<EOF |
| 132 | 132 | <div class="wl-input-wrapper"> |
| 133 | 133 | <input type="text" readonly="readonly" id="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" style="width:88%" /> |
@@ -19,113 +19,113 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Admin_Author_Element implements Wordlift_Admin_Element { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The {@link Wordlift_Publisher_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @since 3.14.0 |
|
| 26 | - * @access private |
|
| 27 | - * @var \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 28 | - */ |
|
| 29 | - private $publisher_service; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * A {@link Wordlift_Admin_Select2_Element} instance. |
|
| 33 | - * |
|
| 34 | - * @since 3.14.0 |
|
| 35 | - * @access private |
|
| 36 | - * @var \Wordlift_Admin_Select2_Element $select_element A {@link Wordlift_Admin_Select2_Element} instance. |
|
| 37 | - */ |
|
| 38 | - private $select_element; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Create a {@link Wordlift_Admin_Person_Element} instance. |
|
| 42 | - * |
|
| 43 | - * @since 3.14.0 |
|
| 44 | - * |
|
| 45 | - * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 46 | - * @param \Wordlift_Admin_Select2_Element $select_element The {@link Wordlift_Admin_Select_Element} instance. |
|
| 47 | - */ |
|
| 48 | - function __construct( $publisher_service, $select_element ) { |
|
| 49 | - |
|
| 50 | - $this->publisher_service = $publisher_service; |
|
| 51 | - |
|
| 52 | - // Child elements. |
|
| 53 | - $this->select_element = $select_element; |
|
| 54 | - |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @inheritdoc |
|
| 59 | - */ |
|
| 60 | - public function render( $args ) { |
|
| 61 | - |
|
| 62 | - // Parse the arguments and merge with default values. |
|
| 63 | - $params = wp_parse_args( $args, array( |
|
| 64 | - 'id' => uniqid( 'wl-input-' ), |
|
| 65 | - 'name' => uniqid( 'wl-input-' ), |
|
| 66 | - 'current_entity' => 0, |
|
| 67 | - ) ); |
|
| 68 | - |
|
| 69 | - $current_entity_id = $params['current_entity']; |
|
| 70 | - $data = $this->publisher_service->query(); |
|
| 71 | - |
|
| 72 | - // Set a default to show when no entity is associated and a way to unassign. |
|
| 73 | - array_unshift( $data, array( |
|
| 74 | - 'id' => '0', |
|
| 75 | - 'text' => __( '<em>(none)</em>', 'wordlift' ), |
|
| 76 | - 'type' => '', |
|
| 77 | - 'thumbnail_url' => plugin_dir_url( dirname( __FILE__ ) ) . 'images/pixel.png', |
|
| 78 | - ) ); |
|
| 79 | - |
|
| 80 | - // Finally do the render, passing along also the current selected entity |
|
| 81 | - // id and the options data. |
|
| 82 | - return $this->do_render( $params, $current_entity_id, $data ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Render the `select` using the provided parameters. |
|
| 87 | - * |
|
| 88 | - * @since 3.14.0 |
|
| 89 | - * |
|
| 90 | - * @param array $params The array of parameters from the `render` function. |
|
| 91 | - * @param int $current_post_id The currently selected {@link WP_Post} `id`. |
|
| 92 | - * @param array $data An array of Select2 options. |
|
| 93 | - * |
|
| 94 | - * @return \Wordlift_Admin_Author_Element $this Return this element. |
|
| 95 | - */ |
|
| 96 | - protected function do_render( $params, $current_post_id, $data ) { |
|
| 97 | - |
|
| 98 | - // Queue the script which will initialize the select and style it. |
|
| 99 | - wp_enqueue_script( 'wl-author-element', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/author.js', array( 'wordlift-select2' ) ); |
|
| 100 | - wp_enqueue_style( 'wl-author-element', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/author.css' ); |
|
| 101 | - |
|
| 102 | - // Prepare the URLs for entities which don't have logos. |
|
| 103 | - $person_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/person.png'; |
|
| 104 | - $organization_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/organization.png'; |
|
| 105 | - |
|
| 106 | - // Get the current post. |
|
| 107 | - $current_post = $current_post_id ? get_post( $current_post_id ) : null; |
|
| 108 | - |
|
| 109 | - // Finally render the Select. |
|
| 110 | - $this->select_element->render( array( |
|
| 111 | - // Id. |
|
| 112 | - 'id' => $params['id'], |
|
| 113 | - // Name. |
|
| 114 | - 'name' => $params['name'], |
|
| 115 | - // The selected id. |
|
| 116 | - 'value' => $current_post_id, |
|
| 117 | - // The selected item (must be in the options for Select2 to display it). |
|
| 118 | - 'options' => $current_post ? array( $current_post->ID => $current_post->post_title ) : array(), |
|
| 119 | - // The list of available options. |
|
| 120 | - 'data' => $data, |
|
| 121 | - // The HTML template for each option. |
|
| 122 | - 'template-result' => "<div class='wl-select2-result'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'> </span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>", |
|
| 123 | - // The HTML template for the selected option. |
|
| 124 | - 'template-selection' => "<div class='wl-select2-selection'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'> </span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>", |
|
| 125 | - ) ); |
|
| 126 | - |
|
| 127 | - // Finally return the element instance. |
|
| 128 | - return $this; |
|
| 129 | - } |
|
| 22 | + /** |
|
| 23 | + * The {@link Wordlift_Publisher_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @since 3.14.0 |
|
| 26 | + * @access private |
|
| 27 | + * @var \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 28 | + */ |
|
| 29 | + private $publisher_service; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * A {@link Wordlift_Admin_Select2_Element} instance. |
|
| 33 | + * |
|
| 34 | + * @since 3.14.0 |
|
| 35 | + * @access private |
|
| 36 | + * @var \Wordlift_Admin_Select2_Element $select_element A {@link Wordlift_Admin_Select2_Element} instance. |
|
| 37 | + */ |
|
| 38 | + private $select_element; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Create a {@link Wordlift_Admin_Person_Element} instance. |
|
| 42 | + * |
|
| 43 | + * @since 3.14.0 |
|
| 44 | + * |
|
| 45 | + * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 46 | + * @param \Wordlift_Admin_Select2_Element $select_element The {@link Wordlift_Admin_Select_Element} instance. |
|
| 47 | + */ |
|
| 48 | + function __construct( $publisher_service, $select_element ) { |
|
| 49 | + |
|
| 50 | + $this->publisher_service = $publisher_service; |
|
| 51 | + |
|
| 52 | + // Child elements. |
|
| 53 | + $this->select_element = $select_element; |
|
| 54 | + |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @inheritdoc |
|
| 59 | + */ |
|
| 60 | + public function render( $args ) { |
|
| 61 | + |
|
| 62 | + // Parse the arguments and merge with default values. |
|
| 63 | + $params = wp_parse_args( $args, array( |
|
| 64 | + 'id' => uniqid( 'wl-input-' ), |
|
| 65 | + 'name' => uniqid( 'wl-input-' ), |
|
| 66 | + 'current_entity' => 0, |
|
| 67 | + ) ); |
|
| 68 | + |
|
| 69 | + $current_entity_id = $params['current_entity']; |
|
| 70 | + $data = $this->publisher_service->query(); |
|
| 71 | + |
|
| 72 | + // Set a default to show when no entity is associated and a way to unassign. |
|
| 73 | + array_unshift( $data, array( |
|
| 74 | + 'id' => '0', |
|
| 75 | + 'text' => __( '<em>(none)</em>', 'wordlift' ), |
|
| 76 | + 'type' => '', |
|
| 77 | + 'thumbnail_url' => plugin_dir_url( dirname( __FILE__ ) ) . 'images/pixel.png', |
|
| 78 | + ) ); |
|
| 79 | + |
|
| 80 | + // Finally do the render, passing along also the current selected entity |
|
| 81 | + // id and the options data. |
|
| 82 | + return $this->do_render( $params, $current_entity_id, $data ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Render the `select` using the provided parameters. |
|
| 87 | + * |
|
| 88 | + * @since 3.14.0 |
|
| 89 | + * |
|
| 90 | + * @param array $params The array of parameters from the `render` function. |
|
| 91 | + * @param int $current_post_id The currently selected {@link WP_Post} `id`. |
|
| 92 | + * @param array $data An array of Select2 options. |
|
| 93 | + * |
|
| 94 | + * @return \Wordlift_Admin_Author_Element $this Return this element. |
|
| 95 | + */ |
|
| 96 | + protected function do_render( $params, $current_post_id, $data ) { |
|
| 97 | + |
|
| 98 | + // Queue the script which will initialize the select and style it. |
|
| 99 | + wp_enqueue_script( 'wl-author-element', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/author.js', array( 'wordlift-select2' ) ); |
|
| 100 | + wp_enqueue_style( 'wl-author-element', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/author.css' ); |
|
| 101 | + |
|
| 102 | + // Prepare the URLs for entities which don't have logos. |
|
| 103 | + $person_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/person.png'; |
|
| 104 | + $organization_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/organization.png'; |
|
| 105 | + |
|
| 106 | + // Get the current post. |
|
| 107 | + $current_post = $current_post_id ? get_post( $current_post_id ) : null; |
|
| 108 | + |
|
| 109 | + // Finally render the Select. |
|
| 110 | + $this->select_element->render( array( |
|
| 111 | + // Id. |
|
| 112 | + 'id' => $params['id'], |
|
| 113 | + // Name. |
|
| 114 | + 'name' => $params['name'], |
|
| 115 | + // The selected id. |
|
| 116 | + 'value' => $current_post_id, |
|
| 117 | + // The selected item (must be in the options for Select2 to display it). |
|
| 118 | + 'options' => $current_post ? array( $current_post->ID => $current_post->post_title ) : array(), |
|
| 119 | + // The list of available options. |
|
| 120 | + 'data' => $data, |
|
| 121 | + // The HTML template for each option. |
|
| 122 | + 'template-result' => "<div class='wl-select2-result'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'> </span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>", |
|
| 123 | + // The HTML template for the selected option. |
|
| 124 | + 'template-selection' => "<div class='wl-select2-selection'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'> </span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>", |
|
| 125 | + ) ); |
|
| 126 | + |
|
| 127 | + // Finally return the element instance. |
|
| 128 | + return $this; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | 131 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
| 46 | 46 | * @param \Wordlift_Admin_Select2_Element $select_element The {@link Wordlift_Admin_Select_Element} instance. |
| 47 | 47 | */ |
| 48 | - function __construct( $publisher_service, $select_element ) { |
|
| 48 | + function __construct($publisher_service, $select_element) { |
|
| 49 | 49 | |
| 50 | 50 | $this->publisher_service = $publisher_service; |
| 51 | 51 | |
@@ -57,29 +57,29 @@ discard block |
||
| 57 | 57 | /** |
| 58 | 58 | * @inheritdoc |
| 59 | 59 | */ |
| 60 | - public function render( $args ) { |
|
| 60 | + public function render($args) { |
|
| 61 | 61 | |
| 62 | 62 | // Parse the arguments and merge with default values. |
| 63 | - $params = wp_parse_args( $args, array( |
|
| 64 | - 'id' => uniqid( 'wl-input-' ), |
|
| 65 | - 'name' => uniqid( 'wl-input-' ), |
|
| 63 | + $params = wp_parse_args($args, array( |
|
| 64 | + 'id' => uniqid('wl-input-'), |
|
| 65 | + 'name' => uniqid('wl-input-'), |
|
| 66 | 66 | 'current_entity' => 0, |
| 67 | - ) ); |
|
| 67 | + )); |
|
| 68 | 68 | |
| 69 | 69 | $current_entity_id = $params['current_entity']; |
| 70 | 70 | $data = $this->publisher_service->query(); |
| 71 | 71 | |
| 72 | 72 | // Set a default to show when no entity is associated and a way to unassign. |
| 73 | - array_unshift( $data, array( |
|
| 73 | + array_unshift($data, array( |
|
| 74 | 74 | 'id' => '0', |
| 75 | - 'text' => __( '<em>(none)</em>', 'wordlift' ), |
|
| 75 | + 'text' => __('<em>(none)</em>', 'wordlift'), |
|
| 76 | 76 | 'type' => '', |
| 77 | - 'thumbnail_url' => plugin_dir_url( dirname( __FILE__ ) ) . 'images/pixel.png', |
|
| 78 | - ) ); |
|
| 77 | + 'thumbnail_url' => plugin_dir_url(dirname(__FILE__)).'images/pixel.png', |
|
| 78 | + )); |
|
| 79 | 79 | |
| 80 | 80 | // Finally do the render, passing along also the current selected entity |
| 81 | 81 | // id and the options data. |
| 82 | - return $this->do_render( $params, $current_entity_id, $data ); |
|
| 82 | + return $this->do_render($params, $current_entity_id, $data); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -93,21 +93,21 @@ discard block |
||
| 93 | 93 | * |
| 94 | 94 | * @return \Wordlift_Admin_Author_Element $this Return this element. |
| 95 | 95 | */ |
| 96 | - protected function do_render( $params, $current_post_id, $data ) { |
|
| 96 | + protected function do_render($params, $current_post_id, $data) { |
|
| 97 | 97 | |
| 98 | 98 | // Queue the script which will initialize the select and style it. |
| 99 | - wp_enqueue_script( 'wl-author-element', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/author.js', array( 'wordlift-select2' ) ); |
|
| 100 | - wp_enqueue_style( 'wl-author-element', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/author.css' ); |
|
| 99 | + wp_enqueue_script('wl-author-element', plugin_dir_url(dirname(__FILE__)).'admin/js/1/author.js', array('wordlift-select2')); |
|
| 100 | + wp_enqueue_style('wl-author-element', plugin_dir_url(dirname(__FILE__)).'admin/js/1/author.css'); |
|
| 101 | 101 | |
| 102 | 102 | // Prepare the URLs for entities which don't have logos. |
| 103 | - $person_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/person.png'; |
|
| 104 | - $organization_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/organization.png'; |
|
| 103 | + $person_thumbnail_url = plugin_dir_url(dirname(__FILE__)).'images/person.png'; |
|
| 104 | + $organization_thumbnail_url = plugin_dir_url(dirname(__FILE__)).'images/organization.png'; |
|
| 105 | 105 | |
| 106 | 106 | // Get the current post. |
| 107 | - $current_post = $current_post_id ? get_post( $current_post_id ) : null; |
|
| 107 | + $current_post = $current_post_id ? get_post($current_post_id) : null; |
|
| 108 | 108 | |
| 109 | 109 | // Finally render the Select. |
| 110 | - $this->select_element->render( array( |
|
| 110 | + $this->select_element->render(array( |
|
| 111 | 111 | // Id. |
| 112 | 112 | 'id' => $params['id'], |
| 113 | 113 | // Name. |
@@ -115,14 +115,14 @@ discard block |
||
| 115 | 115 | // The selected id. |
| 116 | 116 | 'value' => $current_post_id, |
| 117 | 117 | // The selected item (must be in the options for Select2 to display it). |
| 118 | - 'options' => $current_post ? array( $current_post->ID => $current_post->post_title ) : array(), |
|
| 118 | + 'options' => $current_post ? array($current_post->ID => $current_post->post_title) : array(), |
|
| 119 | 119 | // The list of available options. |
| 120 | 120 | 'data' => $data, |
| 121 | 121 | // The HTML template for each option. |
| 122 | 122 | 'template-result' => "<div class='wl-select2-result'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'> </span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>", |
| 123 | 123 | // The HTML template for the selected option. |
| 124 | 124 | 'template-selection' => "<div class='wl-select2-selection'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'> </span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>", |
| 125 | - ) ); |
|
| 125 | + )); |
|
| 126 | 126 | |
| 127 | 127 | // Finally return the element instance. |
| 128 | 128 | return $this; |
@@ -18,52 +18,52 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Admin_Post_Edit_Page { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The {@link Wordlift} plugin instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.11.0 |
|
| 25 | - * |
|
| 26 | - * @var \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 27 | - */ |
|
| 28 | - private $plugin; |
|
| 21 | + /** |
|
| 22 | + * The {@link Wordlift} plugin instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.11.0 |
|
| 25 | + * |
|
| 26 | + * @var \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 27 | + */ |
|
| 28 | + private $plugin; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Create the {@link Wordlift_Admin_Post_Edit_Page} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.11.0 |
|
| 34 | - * |
|
| 35 | - * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 36 | - */ |
|
| 37 | - function __construct( $plugin ) { |
|
| 30 | + /** |
|
| 31 | + * Create the {@link Wordlift_Admin_Post_Edit_Page} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.11.0 |
|
| 34 | + * |
|
| 35 | + * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 36 | + */ |
|
| 37 | + function __construct( $plugin ) { |
|
| 38 | 38 | |
| 39 | - // Define the callback. |
|
| 40 | - $callback = array( $this, 'enqueue_scripts', ); |
|
| 39 | + // Define the callback. |
|
| 40 | + $callback = array( $this, 'enqueue_scripts', ); |
|
| 41 | 41 | |
| 42 | - // Set a hook to enqueue scripts only when the edit page is displayed. |
|
| 43 | - add_action( 'admin_print_scripts-post.php', $callback ); |
|
| 44 | - add_action( 'admin_print_scripts-post-new.php', $callback ); |
|
| 42 | + // Set a hook to enqueue scripts only when the edit page is displayed. |
|
| 43 | + add_action( 'admin_print_scripts-post.php', $callback ); |
|
| 44 | + add_action( 'admin_print_scripts-post-new.php', $callback ); |
|
| 45 | 45 | |
| 46 | - $this->plugin = $plugin; |
|
| 47 | - } |
|
| 46 | + $this->plugin = $plugin; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Enqueue scripts and styles for the edit page. |
|
| 51 | - * |
|
| 52 | - * @since 3.11.0 |
|
| 53 | - */ |
|
| 54 | - public function enqueue_scripts() { |
|
| 49 | + /** |
|
| 50 | + * Enqueue scripts and styles for the edit page. |
|
| 51 | + * |
|
| 52 | + * @since 3.11.0 |
|
| 53 | + */ |
|
| 54 | + public function enqueue_scripts() { |
|
| 55 | 55 | |
| 56 | - // Enqueue the edit screen JavaScript. The `wordlift-admin.bundle.js` file |
|
| 57 | - // is scheduled to replace the older `wordlift-admin.min.js` once client-side |
|
| 58 | - // code is properly refactored. |
|
| 59 | - wp_enqueue_script( |
|
| 60 | - 'wordlift-admin-edit-page', plugin_dir_url( __FILE__ ) . 'js/1/edit.js', |
|
| 61 | - array( $this->plugin->get_plugin_name(), 'jquery' ), |
|
| 62 | - $this->plugin->get_version(), |
|
| 63 | - false |
|
| 64 | - ); |
|
| 65 | - wp_enqueue_style( 'wordlift-admin-edit-page', plugin_dir_url( __FILE__ ) . 'js/1/edit.css', array(), $this->plugin->get_version() ); |
|
| 56 | + // Enqueue the edit screen JavaScript. The `wordlift-admin.bundle.js` file |
|
| 57 | + // is scheduled to replace the older `wordlift-admin.min.js` once client-side |
|
| 58 | + // code is properly refactored. |
|
| 59 | + wp_enqueue_script( |
|
| 60 | + 'wordlift-admin-edit-page', plugin_dir_url( __FILE__ ) . 'js/1/edit.js', |
|
| 61 | + array( $this->plugin->get_plugin_name(), 'jquery' ), |
|
| 62 | + $this->plugin->get_version(), |
|
| 63 | + false |
|
| 64 | + ); |
|
| 65 | + wp_enqueue_style( 'wordlift-admin-edit-page', plugin_dir_url( __FILE__ ) . 'js/1/edit.css', array(), $this->plugin->get_version() ); |
|
| 66 | 66 | |
| 67 | - } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | 69 | } |
@@ -34,14 +34,14 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
| 36 | 36 | */ |
| 37 | - function __construct( $plugin ) { |
|
| 37 | + function __construct($plugin) { |
|
| 38 | 38 | |
| 39 | 39 | // Define the callback. |
| 40 | - $callback = array( $this, 'enqueue_scripts', ); |
|
| 40 | + $callback = array($this, 'enqueue_scripts',); |
|
| 41 | 41 | |
| 42 | 42 | // Set a hook to enqueue scripts only when the edit page is displayed. |
| 43 | - add_action( 'admin_print_scripts-post.php', $callback ); |
|
| 44 | - add_action( 'admin_print_scripts-post-new.php', $callback ); |
|
| 43 | + add_action('admin_print_scripts-post.php', $callback); |
|
| 44 | + add_action('admin_print_scripts-post-new.php', $callback); |
|
| 45 | 45 | |
| 46 | 46 | $this->plugin = $plugin; |
| 47 | 47 | } |
@@ -57,12 +57,12 @@ discard block |
||
| 57 | 57 | // is scheduled to replace the older `wordlift-admin.min.js` once client-side |
| 58 | 58 | // code is properly refactored. |
| 59 | 59 | wp_enqueue_script( |
| 60 | - 'wordlift-admin-edit-page', plugin_dir_url( __FILE__ ) . 'js/1/edit.js', |
|
| 61 | - array( $this->plugin->get_plugin_name(), 'jquery' ), |
|
| 60 | + 'wordlift-admin-edit-page', plugin_dir_url(__FILE__).'js/1/edit.js', |
|
| 61 | + array($this->plugin->get_plugin_name(), 'jquery'), |
|
| 62 | 62 | $this->plugin->get_version(), |
| 63 | 63 | false |
| 64 | 64 | ); |
| 65 | - wp_enqueue_style( 'wordlift-admin-edit-page', plugin_dir_url( __FILE__ ) . 'js/1/edit.css', array(), $this->plugin->get_version() ); |
|
| 65 | + wp_enqueue_style('wordlift-admin-edit-page', plugin_dir_url(__FILE__).'js/1/edit.css', array(), $this->plugin->get_version()); |
|
| 66 | 66 | |
| 67 | 67 | } |
| 68 | 68 | |
@@ -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,88 +20,88 @@ 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 | - * @param array|string $exclude The exclude parameter string. |
|
| 60 | - * |
|
| 61 | - * @return array $response The API response. |
|
| 62 | - */ |
|
| 63 | - public function make_request( $query, $exclude = '' ) { |
|
| 64 | - $url = $this->build_request_url( $query, $exclude ); |
|
| 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 | + * @param array|string $exclude The exclude parameter string. |
|
| 60 | + * |
|
| 61 | + * @return array $response The API response. |
|
| 62 | + */ |
|
| 63 | + public function make_request( $query, $exclude = '' ) { |
|
| 64 | + $url = $this->build_request_url( $query, $exclude ); |
|
| 65 | 65 | |
| 66 | - // Make request. |
|
| 67 | - $response = wp_remote_get( $url ); |
|
| 66 | + // Make request. |
|
| 67 | + $response = wp_remote_get( $url ); |
|
| 68 | 68 | |
| 69 | - // Return the response. |
|
| 70 | - return $response; |
|
| 71 | - } |
|
| 69 | + // Return the response. |
|
| 70 | + return $response; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Build the autocomplete url. |
|
| 75 | - * |
|
| 76 | - * @since 3.15.0 |
|
| 77 | - * |
|
| 78 | - * @param string $query The search string. |
|
| 79 | - * @param array|string $exclude The exclude parameter. |
|
| 80 | - * |
|
| 81 | - * @return string Built url. |
|
| 82 | - */ |
|
| 83 | - private function build_request_url( $query, $exclude ) { |
|
| 84 | - $args = array( |
|
| 85 | - 'key' => $this->configuration_service->get_key(), |
|
| 86 | - 'language' => $this->configuration_service->get_language_code(), |
|
| 87 | - 'query' => $query, |
|
| 88 | - 'limit' => 100, |
|
| 89 | - ); |
|
| 73 | + /** |
|
| 74 | + * Build the autocomplete url. |
|
| 75 | + * |
|
| 76 | + * @since 3.15.0 |
|
| 77 | + * |
|
| 78 | + * @param string $query The search string. |
|
| 79 | + * @param array|string $exclude The exclude parameter. |
|
| 80 | + * |
|
| 81 | + * @return string Built url. |
|
| 82 | + */ |
|
| 83 | + private function build_request_url( $query, $exclude ) { |
|
| 84 | + $args = array( |
|
| 85 | + 'key' => $this->configuration_service->get_key(), |
|
| 86 | + 'language' => $this->configuration_service->get_language_code(), |
|
| 87 | + 'query' => $query, |
|
| 88 | + 'limit' => 100, |
|
| 89 | + ); |
|
| 90 | 90 | |
| 91 | - // Add args to URL. |
|
| 92 | - $request_url = add_query_arg( |
|
| 93 | - urlencode_deep( $args ), |
|
| 94 | - $this->configuration_service->get_autocomplete_url() |
|
| 95 | - ); |
|
| 91 | + // Add args to URL. |
|
| 92 | + $request_url = add_query_arg( |
|
| 93 | + urlencode_deep( $args ), |
|
| 94 | + $this->configuration_service->get_autocomplete_url() |
|
| 95 | + ); |
|
| 96 | 96 | |
| 97 | - // Add the exclude parameter. |
|
| 98 | - if ( ! empty( $exclude ) ) { |
|
| 99 | - foreach ( (array) $exclude as $item ) { |
|
| 100 | - $request_url .= "&exclude=" . urlencode( $item ); |
|
| 101 | - } |
|
| 102 | - } |
|
| 97 | + // Add the exclude parameter. |
|
| 98 | + if ( ! empty( $exclude ) ) { |
|
| 99 | + foreach ( (array) $exclude as $item ) { |
|
| 100 | + $request_url .= "&exclude=" . urlencode( $item ); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - // return the built url. |
|
| 105 | - return $request_url; |
|
| 106 | - } |
|
| 104 | + // return the built url. |
|
| 105 | + return $request_url; |
|
| 106 | + } |
|
| 107 | 107 | } |
@@ -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 | /** |
@@ -60,11 +60,11 @@ discard block |
||
| 60 | 60 | * |
| 61 | 61 | * @return array $response The API response. |
| 62 | 62 | */ |
| 63 | - public function make_request( $query, $exclude = '' ) { |
|
| 64 | - $url = $this->build_request_url( $query, $exclude ); |
|
| 63 | + public function make_request($query, $exclude = '') { |
|
| 64 | + $url = $this->build_request_url($query, $exclude); |
|
| 65 | 65 | |
| 66 | 66 | // Make request. |
| 67 | - $response = wp_remote_get( $url ); |
|
| 67 | + $response = wp_remote_get($url); |
|
| 68 | 68 | |
| 69 | 69 | // Return the response. |
| 70 | 70 | return $response; |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @return string Built url. |
| 82 | 82 | */ |
| 83 | - private function build_request_url( $query, $exclude ) { |
|
| 83 | + private function build_request_url($query, $exclude) { |
|
| 84 | 84 | $args = array( |
| 85 | 85 | 'key' => $this->configuration_service->get_key(), |
| 86 | 86 | 'language' => $this->configuration_service->get_language_code(), |
@@ -90,14 +90,14 @@ discard block |
||
| 90 | 90 | |
| 91 | 91 | // Add args to URL. |
| 92 | 92 | $request_url = add_query_arg( |
| 93 | - urlencode_deep( $args ), |
|
| 93 | + urlencode_deep($args), |
|
| 94 | 94 | $this->configuration_service->get_autocomplete_url() |
| 95 | 95 | ); |
| 96 | 96 | |
| 97 | 97 | // Add the exclude parameter. |
| 98 | - if ( ! empty( $exclude ) ) { |
|
| 99 | - foreach ( (array) $exclude as $item ) { |
|
| 100 | - $request_url .= "&exclude=" . urlencode( $item ); |
|
| 98 | + if ( ! empty($exclude)) { |
|
| 99 | + foreach ((array) $exclude as $item) { |
|
| 100 | + $request_url .= "&exclude=".urlencode($item); |
|
| 101 | 101 | } |
| 102 | 102 | } |
| 103 | 103 | |