@@ -20,51 +20,51 @@ 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_type = %s" |
|
| 34 | - . " AND p.post_title LIKE %s" |
|
| 35 | - . " AND t.term_id = tt.term_id" |
|
| 36 | - . " AND tt.taxonomy = %s" |
|
| 37 | - . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 38 | - . " AND tr.object_id = p.ID" |
|
| 39 | - // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 40 | - . " AND p.post_status != 'trash'"; |
|
| 41 | - |
|
| 42 | - if ( $include_alias ) { |
|
| 43 | - |
|
| 44 | - $query .= " UNION" |
|
| 45 | - . " 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" |
|
| 46 | - . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t, $wpdb->postmeta m" |
|
| 47 | - . " WHERE p.post_type = %s" |
|
| 48 | - . " AND m.meta_key = %s AND m.meta_value LIKE %s" |
|
| 49 | - . " AND m.post_id = p.ID" |
|
| 50 | - . " AND t.term_id = tt.term_id" |
|
| 51 | - . " AND tt.taxonomy = %s" |
|
| 52 | - . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 53 | - . " AND tr.object_id = p.ID" |
|
| 54 | - // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 55 | - . " AND p.post_status != 'trash'"; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 59 | - $query, |
|
| 60 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 61 | - $title, |
|
| 62 | - Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 63 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 64 | - Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
|
| 65 | - $title, |
|
| 66 | - Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME |
|
| 67 | - ) ); |
|
| 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_type = %s" |
|
| 34 | + . " AND p.post_title LIKE %s" |
|
| 35 | + . " AND t.term_id = tt.term_id" |
|
| 36 | + . " AND tt.taxonomy = %s" |
|
| 37 | + . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 38 | + . " AND tr.object_id = p.ID" |
|
| 39 | + // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 40 | + . " AND p.post_status != 'trash'"; |
|
| 41 | + |
|
| 42 | + if ( $include_alias ) { |
|
| 43 | + |
|
| 44 | + $query .= " UNION" |
|
| 45 | + . " 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" |
|
| 46 | + . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t, $wpdb->postmeta m" |
|
| 47 | + . " WHERE p.post_type = %s" |
|
| 48 | + . " AND m.meta_key = %s AND m.meta_value LIKE %s" |
|
| 49 | + . " AND m.post_id = p.ID" |
|
| 50 | + . " AND t.term_id = tt.term_id" |
|
| 51 | + . " AND tt.taxonomy = %s" |
|
| 52 | + . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 53 | + . " AND tr.object_id = p.ID" |
|
| 54 | + // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 55 | + . " AND p.post_status != 'trash'"; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + return $wpdb->get_results( $wpdb->prepare( |
|
| 59 | + $query, |
|
| 60 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 61 | + $title, |
|
| 62 | + Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 63 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 64 | + Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
|
| 65 | + $title, |
|
| 66 | + Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME |
|
| 67 | + ) ); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -74,41 +74,41 @@ discard block |
||
| 74 | 74 | */ |
| 75 | 75 | function wl_entity_ajax_get_by_title() { |
| 76 | 76 | |
| 77 | - // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 78 | - // |
|
| 79 | - // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 80 | - // Get the title to search. |
|
| 81 | - if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { |
|
| 82 | - ob_clean(); |
|
| 83 | - wp_send_json_error( 'The title parameter is required.' ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 87 | - // |
|
| 88 | - // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 89 | - $title = $_POST['title'] ?: $_GET['title']; |
|
| 90 | - |
|
| 91 | - // Are we searching for a specific title or for a containing title? |
|
| 92 | - $autocomplete = isset( $_GET['autocomplete'] ); |
|
| 93 | - |
|
| 94 | - // Are we searching also for the aliases? |
|
| 95 | - $include_alias = isset( $_GET['alias'] ); |
|
| 96 | - |
|
| 97 | - // Get the edit link. |
|
| 98 | - $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 99 | - $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 100 | - |
|
| 101 | - // Prepare the response with the edit link. |
|
| 102 | - $response = array( |
|
| 103 | - 'edit_link' => $edit_link, |
|
| 104 | - 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 105 | - ); |
|
| 106 | - |
|
| 107 | - // Clean any buffer. |
|
| 108 | - ob_clean(); |
|
| 109 | - |
|
| 110 | - // Send the success response. |
|
| 111 | - wp_send_json_success( $response ); |
|
| 77 | + // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 78 | + // |
|
| 79 | + // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 80 | + // Get the title to search. |
|
| 81 | + if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { |
|
| 82 | + ob_clean(); |
|
| 83 | + wp_send_json_error( 'The title parameter is required.' ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 87 | + // |
|
| 88 | + // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 89 | + $title = $_POST['title'] ?: $_GET['title']; |
|
| 90 | + |
|
| 91 | + // Are we searching for a specific title or for a containing title? |
|
| 92 | + $autocomplete = isset( $_GET['autocomplete'] ); |
|
| 93 | + |
|
| 94 | + // Are we searching also for the aliases? |
|
| 95 | + $include_alias = isset( $_GET['alias'] ); |
|
| 96 | + |
|
| 97 | + // Get the edit link. |
|
| 98 | + $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 99 | + $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 100 | + |
|
| 101 | + // Prepare the response with the edit link. |
|
| 102 | + $response = array( |
|
| 103 | + 'edit_link' => $edit_link, |
|
| 104 | + 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 105 | + ); |
|
| 106 | + |
|
| 107 | + // Clean any buffer. |
|
| 108 | + ob_clean(); |
|
| 109 | + |
|
| 110 | + // Send the success response. |
|
| 111 | + wp_send_json_success( $response ); |
|
| 112 | 112 | |
| 113 | 113 | } |
| 114 | 114 | |
@@ -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. |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
| 40 | 40 | . " AND p.post_status != 'trash'"; |
| 41 | 41 | |
| 42 | - if ( $include_alias ) { |
|
| 42 | + if ($include_alias) { |
|
| 43 | 43 | |
| 44 | 44 | $query .= " UNION" |
| 45 | 45 | . " 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" |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | . " AND p.post_status != 'trash'"; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 58 | + return $wpdb->get_results($wpdb->prepare( |
|
| 59 | 59 | $query, |
| 60 | 60 | Wordlift_Entity_Service::TYPE_NAME, |
| 61 | 61 | $title, |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
| 65 | 65 | $title, |
| 66 | 66 | Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME |
| 67 | - ) ); |
|
| 67 | + )); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | // |
| 79 | 79 | // See https://github.com/insideout10/wordlift-plugin/issues/438. |
| 80 | 80 | // Get the title to search. |
| 81 | - if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { |
|
| 81 | + if (empty($_POST['title']) && empty($_GET['title'])) { |
|
| 82 | 82 | ob_clean(); |
| 83 | - wp_send_json_error( 'The title parameter is required.' ); |
|
| 83 | + wp_send_json_error('The title parameter is required.'); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
@@ -89,27 +89,27 @@ discard block |
||
| 89 | 89 | $title = $_POST['title'] ?: $_GET['title']; |
| 90 | 90 | |
| 91 | 91 | // Are we searching for a specific title or for a containing title? |
| 92 | - $autocomplete = isset( $_GET['autocomplete'] ); |
|
| 92 | + $autocomplete = isset($_GET['autocomplete']); |
|
| 93 | 93 | |
| 94 | 94 | // Are we searching also for the aliases? |
| 95 | - $include_alias = isset( $_GET['alias'] ); |
|
| 95 | + $include_alias = isset($_GET['alias']); |
|
| 96 | 96 | |
| 97 | 97 | // Get the edit link. |
| 98 | - $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 99 | - $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 98 | + $post_type_object = get_post_type_object(Wordlift_Entity_Service::TYPE_NAME); |
|
| 99 | + $edit_link = $post_type_object->_edit_link.'&action=edit'; |
|
| 100 | 100 | |
| 101 | 101 | // Prepare the response with the edit link. |
| 102 | 102 | $response = array( |
| 103 | 103 | 'edit_link' => $edit_link, |
| 104 | - 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 104 | + 'results' => wl_entity_get_by_title($title, $autocomplete, $include_alias), |
|
| 105 | 105 | ); |
| 106 | 106 | |
| 107 | 107 | // Clean any buffer. |
| 108 | 108 | ob_clean(); |
| 109 | 109 | |
| 110 | 110 | // Send the success response. |
| 111 | - wp_send_json_success( $response ); |
|
| 111 | + wp_send_json_success($response); |
|
| 112 | 112 | |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | -add_action( 'wp_ajax_entity_by_title', 'wl_entity_ajax_get_by_title' ); |
|
| 115 | +add_action('wp_ajax_entity_by_title', 'wl_entity_ajax_get_by_title'); |
|
@@ -15,791 +15,791 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Wordlift_Schema_Service { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The 'location created' field name. |
|
| 20 | - * |
|
| 21 | - * @since 3.5.0 |
|
| 22 | - */ |
|
| 23 | - const FIELD_LOCATION_CREATED = 'wl_location_created'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * The 'topic' field name. |
|
| 27 | - * |
|
| 28 | - * @since 3.5.0 |
|
| 29 | - */ |
|
| 30 | - const FIELD_TOPIC = 'wl_topic'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The 'author' field name. |
|
| 34 | - * |
|
| 35 | - * @since 3.1.0 |
|
| 36 | - */ |
|
| 37 | - const FIELD_AUTHOR = 'wl_author'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The 'same as' field name. |
|
| 41 | - * |
|
| 42 | - * @since 3.1.0 |
|
| 43 | - */ |
|
| 44 | - const FIELD_SAME_AS = 'entity_same_as'; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The 'date start' field name. |
|
| 48 | - * |
|
| 49 | - * @since 3.1.0 |
|
| 50 | - */ |
|
| 51 | - const FIELD_DATE_START = 'wl_cal_date_start'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The 'date end' field name. |
|
| 55 | - * |
|
| 56 | - * @since 3.1.0 |
|
| 57 | - */ |
|
| 58 | - const FIELD_DATE_END = 'wl_cal_date_end'; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The 'location' field name. |
|
| 62 | - * |
|
| 63 | - * @since 3.1.0 |
|
| 64 | - */ |
|
| 65 | - const FIELD_LOCATION = 'wl_location'; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * The 'founder' field name. |
|
| 69 | - * |
|
| 70 | - * @since 3.1.0 |
|
| 71 | - */ |
|
| 72 | - const FIELD_FOUNDER = 'wl_founder'; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * The 'knows' field name. |
|
| 76 | - * |
|
| 77 | - * @since 3.1.0 |
|
| 78 | - */ |
|
| 79 | - const FIELD_KNOWS = 'wl_knows'; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * The 'birth date' field name. |
|
| 83 | - * |
|
| 84 | - * @since 3.1.0 |
|
| 85 | - */ |
|
| 86 | - const FIELD_BIRTH_DATE = 'wl_birth_date'; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * The 'birth place' field name. |
|
| 90 | - * |
|
| 91 | - * @since 3.1.0 |
|
| 92 | - */ |
|
| 93 | - const FIELD_BIRTH_PLACE = 'wl_birth_place'; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * The 'latitude' field name. |
|
| 97 | - * |
|
| 98 | - * @since 3.1.0 |
|
| 99 | - */ |
|
| 100 | - const FIELD_GEO_LATITUDE = 'wl_geo_latitude'; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * The 'longitude' field name. |
|
| 104 | - * |
|
| 105 | - * @since 3.1.0 |
|
| 106 | - */ |
|
| 107 | - const FIELD_GEO_LONGITUDE = 'wl_geo_longitude'; |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * The 'streetAddress' field name. |
|
| 111 | - * |
|
| 112 | - * @since 3.1.0 |
|
| 113 | - */ |
|
| 114 | - const FIELD_ADDRESS = 'wl_address'; |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * The 'postOfficeBoxNumber' field name. |
|
| 118 | - * |
|
| 119 | - * @since 3.3.0 |
|
| 120 | - */ |
|
| 121 | - const FIELD_ADDRESS_PO_BOX = 'wl_address_post_office_box'; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * The 'postalCode' field name. |
|
| 125 | - * |
|
| 126 | - * @since 3.3.0 |
|
| 127 | - */ |
|
| 128 | - const FIELD_ADDRESS_POSTAL_CODE = 'wl_address_postal_code'; |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * The 'addressLocality' field name. |
|
| 132 | - * |
|
| 133 | - * @since 3.3.0 |
|
| 134 | - */ |
|
| 135 | - const FIELD_ADDRESS_LOCALITY = 'wl_address_locality'; |
|
| 136 | - /** |
|
| 137 | - * The 'addressRegion' field name. |
|
| 138 | - * |
|
| 139 | - * @since 3.3.0 |
|
| 140 | - */ |
|
| 141 | - const FIELD_ADDRESS_REGION = 'wl_address_region'; |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * The 'addressCountry' field name. |
|
| 145 | - * |
|
| 146 | - * @since 3.3.0 |
|
| 147 | - */ |
|
| 148 | - const FIELD_ADDRESS_COUNTRY = 'wl_address_country'; |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * The 'entity type' field name. |
|
| 152 | - * |
|
| 153 | - * @since 3.1.0 |
|
| 154 | - */ |
|
| 155 | - const FIELD_ENTITY_TYPE = 'wl_entity_type_uri'; |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * The 'email' field name. |
|
| 159 | - * |
|
| 160 | - * @since 3.2.0 |
|
| 161 | - */ |
|
| 162 | - const FIELD_EMAIL = 'wl_email'; |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * The 'affiliation' field name. |
|
| 166 | - * |
|
| 167 | - * @since 3.2.0 |
|
| 168 | - */ |
|
| 169 | - const FIELD_AFFILIATION = 'wl_affiliation'; |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * The 'telephone' field name. |
|
| 173 | - * |
|
| 174 | - * @since 3.8.0 |
|
| 175 | - */ |
|
| 176 | - const FIELD_TELEPHONE = 'wl_schema_telephone'; |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * The 'URI' data type name. |
|
| 180 | - * |
|
| 181 | - * @since 3.1.0 |
|
| 182 | - */ |
|
| 183 | - const DATA_TYPE_URI = 'uri'; |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * The 'date' data type name. |
|
| 187 | - * |
|
| 188 | - * @since 3.1.0 |
|
| 189 | - */ |
|
| 190 | - const DATA_TYPE_DATE = 'date'; |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * The 'double' data type name. |
|
| 194 | - * |
|
| 195 | - * @since 3.1.0 |
|
| 196 | - */ |
|
| 197 | - const DATA_TYPE_DOUBLE = 'double'; |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * The 'string' data type name. |
|
| 201 | - * |
|
| 202 | - * @since 3.1.0 |
|
| 203 | - */ |
|
| 204 | - const DATA_TYPE_STRING = 'string'; |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * The 'integer' data type name. |
|
| 208 | - * |
|
| 209 | - * @since 3.1.0 |
|
| 210 | - */ |
|
| 211 | - const DATA_TYPE_INTEGER = 'int'; |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * The 'boolean' data type name. |
|
| 215 | - * |
|
| 216 | - * @since 3.1.0 |
|
| 217 | - */ |
|
| 218 | - const DATA_TYPE_BOOLEAN = 'bool'; |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * The schema.org Event type URI. |
|
| 222 | - * |
|
| 223 | - * @since 3.1.0 |
|
| 224 | - */ |
|
| 225 | - const SCHEMA_EVENT_TYPE = 'http://schema.org/Event'; |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * The Schema service singleton instance. |
|
| 229 | - * |
|
| 230 | - * @since 3.1.0 |
|
| 231 | - * @access private |
|
| 232 | - * @var \Wordlift_Schema_Service $instance The Schema service singleton instance. |
|
| 233 | - */ |
|
| 234 | - private static $instance; |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * WordLift's schema. |
|
| 238 | - * |
|
| 239 | - * @since 3.1.0 |
|
| 240 | - * @access private |
|
| 241 | - * @var array $schema WordLift's schema. |
|
| 242 | - */ |
|
| 243 | - private $schema; |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * The Log service. |
|
| 247 | - * |
|
| 248 | - * @since 3.1.0 |
|
| 249 | - * @access private |
|
| 250 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 251 | - */ |
|
| 252 | - private $log_service; |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Wordlift_Schema_Service constructor. |
|
| 256 | - * |
|
| 257 | - * @since 3.1.0 |
|
| 258 | - */ |
|
| 259 | - public function __construct() { |
|
| 260 | - |
|
| 261 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Schema_Service' ); |
|
| 262 | - |
|
| 263 | - // Create a singleton instance of the Schema service, useful to provide static functions to global functions. |
|
| 264 | - self::$instance = $this; |
|
| 265 | - |
|
| 266 | - // Set the taxonomy data. |
|
| 267 | - // Note: parent types must be defined before child types. |
|
| 268 | - $this->schema = array( |
|
| 269 | - 'thing' => $this->get_thing_schema(), |
|
| 270 | - 'creative-work' => $this->get_creative_work_schema(), |
|
| 271 | - 'event' => $this->get_event_schema(), |
|
| 272 | - 'organization' => $this->get_organization_schema(), |
|
| 273 | - 'person' => $this->get_person_schema(), |
|
| 274 | - 'place' => $this->get_place_schema(), |
|
| 275 | - 'localbusiness' => $this->get_local_business_schema(), |
|
| 276 | - ); |
|
| 277 | - |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Get a reference to the Schema service. |
|
| 282 | - * |
|
| 283 | - * @since 3.1.0 |
|
| 284 | - * |
|
| 285 | - * @return Wordlift_Schema_Service A reference to the Schema service. |
|
| 286 | - */ |
|
| 287 | - public static function get_instance() { |
|
| 288 | - |
|
| 289 | - return self::$instance; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Get the properties for a field with the specified key. The key is used as |
|
| 294 | - * meta key when the field's value is stored in WordPress meta data table. |
|
| 295 | - * |
|
| 296 | - * @since 3.6.0 |
|
| 297 | - * |
|
| 298 | - * @param string $key The field's key. |
|
| 299 | - * |
|
| 300 | - * @return null|array An array of field's properties or null if the field is not found. |
|
| 301 | - */ |
|
| 302 | - public function get_field( $key ) { |
|
| 303 | - |
|
| 304 | - // Parse each schema's fields until we find the one we're looking for, then |
|
| 305 | - // return its properties. |
|
| 306 | - foreach ( $this->schema as $_ => $schema ) { |
|
| 307 | - |
|
| 308 | - if ( ! isset( $schema['custom_fields'] ) ) { |
|
| 309 | - break; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - foreach ( $schema['custom_fields'] as $field => $props ) { |
|
| 313 | - if ( $key === $field ) { |
|
| 314 | - return $props; |
|
| 315 | - } |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - return null; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Get the WordLift's schema. |
|
| 324 | - * |
|
| 325 | - * @param string $name The schema name. |
|
| 326 | - * |
|
| 327 | - * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
| 328 | - * |
|
| 329 | - * @since 3.1.0 |
|
| 330 | - */ |
|
| 331 | - public function get_schema( $name ) { |
|
| 332 | - |
|
| 333 | - // Check if the schema exists and, if not, return NULL. |
|
| 334 | - if ( ! isset( $this->schema[ $name ] ) ) { |
|
| 335 | - return null; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - // Return the requested schema. |
|
| 339 | - return $this->schema[ $name ]; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Get the WordLift's schema trough schema type uri. |
|
| 344 | - * |
|
| 345 | - * @param string $uri The schema uri. |
|
| 346 | - * |
|
| 347 | - * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
| 348 | - * |
|
| 349 | - * @since 3.3.0 |
|
| 350 | - */ |
|
| 351 | - public function get_schema_by_uri( $uri ) { |
|
| 352 | - |
|
| 353 | - foreach ( $this->schema as $name => $schema ) { |
|
| 354 | - if ( $schema['uri'] === $uri ) { |
|
| 355 | - return $schema; |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - return null; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Get the 'thing' schema. |
|
| 364 | - * |
|
| 365 | - * @return array An array with the schema configuration. |
|
| 366 | - * |
|
| 367 | - * @since 3.1.0 |
|
| 368 | - */ |
|
| 369 | - private function get_thing_schema() { |
|
| 370 | - |
|
| 371 | - return array( |
|
| 372 | - 'css_class' => 'wl-thing', |
|
| 373 | - 'uri' => 'http://schema.org/Thing', |
|
| 374 | - 'same_as' => array( '*' ), |
|
| 375 | - // set as default. |
|
| 376 | - 'custom_fields' => array( |
|
| 377 | - self::FIELD_SAME_AS => array( |
|
| 378 | - 'predicate' => 'http://schema.org/sameAs', |
|
| 379 | - 'type' => self::DATA_TYPE_URI, |
|
| 380 | - 'export_type' => 'http://schema.org/Thing', |
|
| 381 | - 'constraints' => array( |
|
| 382 | - 'cardinality' => INF, |
|
| 383 | - ), |
|
| 384 | - // We need a custom metabox. |
|
| 385 | - 'input_field' => 'sameas', |
|
| 386 | - ), |
|
| 387 | - // Add the schema:url property. |
|
| 388 | - Wordlift_Schema_Url_Property_Service::META_KEY => Wordlift_Schema_Url_Property_Service::get_instance() |
|
| 389 | - ->get_compat_definition(), |
|
| 390 | - ), |
|
| 391 | - // {{sameAs}} not present in the microdata template, |
|
| 392 | - // because it is treated separately in *wl_content_embed_item_microdata* |
|
| 393 | - 'templates' => array( |
|
| 394 | - 'subtitle' => '{{id}}', |
|
| 395 | - ), |
|
| 396 | - ); |
|
| 397 | - |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Get the 'creative work' schema. |
|
| 402 | - * |
|
| 403 | - * @return array An array with the schema configuration. |
|
| 404 | - * |
|
| 405 | - * @since 3.1.0 |
|
| 406 | - */ |
|
| 407 | - private function get_creative_work_schema() { |
|
| 408 | - |
|
| 409 | - $schema = array( |
|
| 410 | - 'label' => 'CreativeWork', |
|
| 411 | - 'description' => 'A creative work (or a Music Album).', |
|
| 412 | - 'parents' => array( 'thing' ), |
|
| 413 | - // Give term slug as parent. |
|
| 414 | - 'css_class' => 'wl-creative-work', |
|
| 415 | - 'uri' => 'http://schema.org/CreativeWork', |
|
| 416 | - 'same_as' => array( |
|
| 417 | - 'http://schema.org/MusicAlbum', |
|
| 418 | - 'http://schema.org/Product', |
|
| 419 | - ), |
|
| 420 | - 'custom_fields' => array( |
|
| 421 | - self::FIELD_AUTHOR => array( |
|
| 422 | - 'predicate' => 'http://schema.org/author', |
|
| 423 | - 'type' => self::DATA_TYPE_URI, |
|
| 424 | - 'export_type' => 'http://schema.org/Person', |
|
| 425 | - 'constraints' => array( |
|
| 426 | - 'uri_type' => array( 'Person', 'Organization' ), |
|
| 427 | - 'cardinality' => INF, |
|
| 428 | - ), |
|
| 429 | - ), |
|
| 430 | - ), |
|
| 431 | - 'templates' => array( |
|
| 432 | - 'subtitle' => '{{id}}', |
|
| 433 | - ), |
|
| 434 | - ); |
|
| 435 | - |
|
| 436 | - // Merge the custom fields with those provided by the thing schema. |
|
| 437 | - $thing_schema = $this->get_thing_schema(); |
|
| 438 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 439 | - |
|
| 440 | - return $schema; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Get the 'event' schema. |
|
| 445 | - * |
|
| 446 | - * @return array An array with the schema configuration. |
|
| 447 | - * |
|
| 448 | - * @since 3.1.0 |
|
| 449 | - */ |
|
| 450 | - private function get_event_schema() { |
|
| 451 | - |
|
| 452 | - $schema = array( |
|
| 453 | - 'label' => 'Event', |
|
| 454 | - 'description' => 'An event . ', |
|
| 455 | - 'parents' => array( 'thing' ), |
|
| 456 | - 'css_class' => 'wl-event', |
|
| 457 | - 'uri' => self::SCHEMA_EVENT_TYPE, |
|
| 458 | - 'same_as' => array( 'http://dbpedia.org/ontology/Event' ), |
|
| 459 | - 'custom_fields' => array( |
|
| 460 | - self::FIELD_DATE_START => array( |
|
| 461 | - 'predicate' => 'http://schema.org/startDate', |
|
| 462 | - 'type' => self::DATA_TYPE_DATE, |
|
| 463 | - 'export_type' => 'xsd:datetime', |
|
| 464 | - 'constraints' => '', |
|
| 465 | - ), |
|
| 466 | - self::FIELD_DATE_END => array( |
|
| 467 | - 'predicate' => 'http://schema.org/endDate', |
|
| 468 | - 'type' => self::DATA_TYPE_DATE, |
|
| 469 | - 'export_type' => 'xsd:datetime', |
|
| 470 | - 'constraints' => '', |
|
| 471 | - ), |
|
| 472 | - self::FIELD_LOCATION => array( |
|
| 473 | - 'predicate' => 'http://schema.org/location', |
|
| 474 | - 'type' => self::DATA_TYPE_URI, |
|
| 475 | - 'export_type' => 'http://schema.org/PostalAddress', |
|
| 476 | - 'constraints' => array( |
|
| 477 | - 'uri_type' => array( 'Place', 'LocalBusiness' ), |
|
| 478 | - 'cardinality' => INF, |
|
| 479 | - ), |
|
| 480 | - ), |
|
| 481 | - ), |
|
| 482 | - 'templates' => array( |
|
| 483 | - 'subtitle' => '{{id}}', |
|
| 484 | - ), |
|
| 485 | - ); |
|
| 486 | - |
|
| 487 | - // Merge the custom fields with those provided by the thing schema. |
|
| 488 | - $thing_schema = $this->get_thing_schema(); |
|
| 489 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 490 | - |
|
| 491 | - return $schema; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * Get the 'organization' schema. |
|
| 496 | - * |
|
| 497 | - * @return array An array with the schema configuration. |
|
| 498 | - * |
|
| 499 | - * @since 3.1.0 |
|
| 500 | - */ |
|
| 501 | - private function get_organization_schema() { |
|
| 502 | - |
|
| 503 | - $schema = array( |
|
| 504 | - 'label' => 'Organization', |
|
| 505 | - 'description' => 'An organization, including a government or a newspaper.', |
|
| 506 | - 'parents' => array( 'thing' ), |
|
| 507 | - 'css_class' => 'wl-organization', |
|
| 508 | - 'uri' => 'http://schema.org/Organization', |
|
| 509 | - 'same_as' => array( |
|
| 510 | - 'http://rdf.freebase.com/ns/organization.organization', |
|
| 511 | - 'http://rdf.freebase.com/ns/government.government', |
|
| 512 | - 'http://schema.org/Newspaper', |
|
| 513 | - ), |
|
| 514 | - 'custom_fields' => array( |
|
| 515 | - self::FIELD_FOUNDER => array( |
|
| 516 | - 'predicate' => 'http://schema.org/founder', |
|
| 517 | - 'type' => self::DATA_TYPE_URI, |
|
| 518 | - 'export_type' => 'http://schema.org/Person', |
|
| 519 | - 'constraints' => array( |
|
| 520 | - 'uri_type' => 'Person', |
|
| 521 | - 'cardinality' => INF, |
|
| 522 | - ), |
|
| 523 | - ), |
|
| 524 | - self::FIELD_ADDRESS => array( |
|
| 525 | - 'predicate' => 'http://schema.org/streetAddress', |
|
| 526 | - 'type' => self::DATA_TYPE_STRING, |
|
| 527 | - 'export_type' => 'xsd:string', |
|
| 528 | - 'constraints' => '', |
|
| 529 | - // To build custom metabox. |
|
| 530 | - 'input_field' => 'address', |
|
| 531 | - ), |
|
| 532 | - self::FIELD_ADDRESS_PO_BOX => array( |
|
| 533 | - 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
| 534 | - 'type' => self::DATA_TYPE_STRING, |
|
| 535 | - 'export_type' => 'xsd:string', |
|
| 536 | - 'constraints' => '', |
|
| 537 | - // To build custom metabox. |
|
| 538 | - 'input_field' => 'address', |
|
| 539 | - ), |
|
| 540 | - self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
| 541 | - 'predicate' => 'http://schema.org/postalCode', |
|
| 542 | - 'type' => self::DATA_TYPE_STRING, |
|
| 543 | - 'export_type' => 'xsd:string', |
|
| 544 | - 'constraints' => '', |
|
| 545 | - // To build custom metabox. |
|
| 546 | - 'input_field' => 'address', |
|
| 547 | - ), |
|
| 548 | - self::FIELD_ADDRESS_LOCALITY => array( |
|
| 549 | - 'predicate' => 'http://schema.org/addressLocality', |
|
| 550 | - 'type' => self::DATA_TYPE_STRING, |
|
| 551 | - 'export_type' => 'xsd:string', |
|
| 552 | - 'constraints' => '', |
|
| 553 | - // To build custom metabox. |
|
| 554 | - 'input_field' => 'address', |
|
| 555 | - ), |
|
| 556 | - self::FIELD_ADDRESS_REGION => array( |
|
| 557 | - 'predicate' => 'http://schema.org/addressRegion', |
|
| 558 | - 'type' => self::DATA_TYPE_STRING, |
|
| 559 | - 'export_type' => 'xsd:string', |
|
| 560 | - 'constraints' => '', |
|
| 561 | - // To build custom metabox. |
|
| 562 | - 'input_field' => 'address', |
|
| 563 | - ), |
|
| 564 | - self::FIELD_ADDRESS_COUNTRY => array( |
|
| 565 | - 'predicate' => 'http://schema.org/addressCountry', |
|
| 566 | - 'type' => self::DATA_TYPE_STRING, |
|
| 567 | - 'export_type' => 'xsd:string', |
|
| 568 | - 'constraints' => '', |
|
| 569 | - // To build custom metabox. |
|
| 570 | - 'input_field' => 'address', |
|
| 571 | - ), |
|
| 572 | - self::FIELD_EMAIL => array( |
|
| 573 | - 'predicate' => 'http://schema.org/email', |
|
| 574 | - 'type' => self::DATA_TYPE_STRING, |
|
| 575 | - 'export_type' => 'xsd:string', |
|
| 576 | - 'constraints' => '', |
|
| 577 | - ), |
|
| 578 | - self::FIELD_TELEPHONE => array( |
|
| 579 | - 'predicate' => 'http://schema.org/telephone', |
|
| 580 | - 'type' => self::DATA_TYPE_STRING, |
|
| 581 | - 'export_type' => 'xsd:string', |
|
| 582 | - 'constraints' => '', |
|
| 583 | - ), |
|
| 584 | - ), |
|
| 585 | - 'templates' => array( |
|
| 586 | - 'subtitle' => '{{id}}', |
|
| 587 | - ), |
|
| 588 | - ); |
|
| 589 | - |
|
| 590 | - // Merge the custom fields with those provided by the thing schema. |
|
| 591 | - $thing_schema = $this->get_thing_schema(); |
|
| 592 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 593 | - |
|
| 594 | - return $schema; |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * Get the 'person' schema. |
|
| 599 | - * |
|
| 600 | - * @return array An array with the schema configuration. |
|
| 601 | - * |
|
| 602 | - * @since 3.1.0 |
|
| 603 | - */ |
|
| 604 | - private function get_person_schema() { |
|
| 605 | - |
|
| 606 | - $schema = array( |
|
| 607 | - 'label' => 'Person', |
|
| 608 | - 'description' => 'A person (or a music artist).', |
|
| 609 | - 'parents' => array( 'thing' ), |
|
| 610 | - 'css_class' => 'wl-person', |
|
| 611 | - 'uri' => 'http://schema.org/Person', |
|
| 612 | - 'same_as' => array( |
|
| 613 | - 'http://rdf.freebase.com/ns/people.person', |
|
| 614 | - 'http://rdf.freebase.com/ns/music.artist', |
|
| 615 | - 'http://dbpedia.org/class/yago/LivingPeople', |
|
| 616 | - ), |
|
| 617 | - 'custom_fields' => array( |
|
| 618 | - self::FIELD_KNOWS => array( |
|
| 619 | - 'predicate' => 'http://schema.org/knows', |
|
| 620 | - 'type' => self::DATA_TYPE_URI, |
|
| 621 | - 'export_type' => 'http://schema.org/Person', |
|
| 622 | - 'constraints' => array( |
|
| 623 | - 'uri_type' => 'Person', |
|
| 624 | - 'cardinality' => INF, |
|
| 625 | - ), |
|
| 626 | - ), |
|
| 627 | - self::FIELD_BIRTH_DATE => array( |
|
| 628 | - 'predicate' => 'http://schema.org/birthDate', |
|
| 629 | - 'type' => self::DATA_TYPE_DATE, |
|
| 630 | - 'export_type' => 'xsd:date', |
|
| 631 | - 'constraints' => '', |
|
| 632 | - ), |
|
| 633 | - self::FIELD_BIRTH_PLACE => array( |
|
| 634 | - 'predicate' => 'http://schema.org/birthPlace', |
|
| 635 | - 'type' => self::DATA_TYPE_URI, |
|
| 636 | - 'export_type' => 'http://schema.org/Place', |
|
| 637 | - 'constraints' => array( |
|
| 638 | - 'uri_type' => 'Place', |
|
| 639 | - ), |
|
| 640 | - ), |
|
| 641 | - self::FIELD_AFFILIATION => array( |
|
| 642 | - 'predicate' => 'http://schema.org/affiliation', |
|
| 643 | - 'type' => self::DATA_TYPE_URI, |
|
| 644 | - 'export_type' => 'http://schema.org/Organization', |
|
| 645 | - 'constraints' => array( |
|
| 646 | - 'uri_type' => array( |
|
| 647 | - 'Organization', |
|
| 648 | - 'LocalBusiness', |
|
| 649 | - ), |
|
| 650 | - 'cardinality' => INF, |
|
| 651 | - ), |
|
| 652 | - ), |
|
| 653 | - self::FIELD_EMAIL => array( |
|
| 654 | - 'predicate' => 'http://schema.org/email', |
|
| 655 | - 'type' => self::DATA_TYPE_STRING, |
|
| 656 | - 'export_type' => 'xsd:string', |
|
| 657 | - 'constraints' => array( |
|
| 658 | - 'cardinality' => INF, |
|
| 659 | - ), |
|
| 660 | - ), |
|
| 661 | - ), |
|
| 662 | - 'templates' => array( |
|
| 663 | - 'subtitle' => '{{id}}', |
|
| 664 | - ), |
|
| 665 | - ); |
|
| 666 | - |
|
| 667 | - // Merge the custom fields with those provided by the thing schema. |
|
| 668 | - $thing_schema = $this->get_thing_schema(); |
|
| 669 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 670 | - |
|
| 671 | - return $schema; |
|
| 672 | - |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - /** |
|
| 676 | - * Get the 'place' schema. |
|
| 677 | - * |
|
| 678 | - * @return array An array with the schema configuration. |
|
| 679 | - * |
|
| 680 | - * @since 3.1.0 |
|
| 681 | - */ |
|
| 682 | - private function get_place_schema() { |
|
| 683 | - |
|
| 684 | - $schema = array( |
|
| 685 | - 'label' => 'Place', |
|
| 686 | - 'description' => 'A place.', |
|
| 687 | - 'parents' => array( 'thing' ), |
|
| 688 | - 'css_class' => 'wl-place', |
|
| 689 | - 'uri' => 'http://schema.org/Place', |
|
| 690 | - 'same_as' => array( |
|
| 691 | - 'http://rdf.freebase.com/ns/location.location', |
|
| 692 | - 'http://www.opengis.net/gml/_Feature', |
|
| 693 | - ), |
|
| 694 | - 'custom_fields' => array( |
|
| 695 | - self::FIELD_GEO_LATITUDE => array( |
|
| 696 | - 'predicate' => 'http://schema.org/latitude', |
|
| 697 | - 'type' => self::DATA_TYPE_DOUBLE, |
|
| 698 | - 'export_type' => 'xsd:double', |
|
| 699 | - 'constraints' => '', |
|
| 700 | - // To build custom metabox. |
|
| 701 | - 'input_field' => 'coordinates', |
|
| 702 | - ), |
|
| 703 | - self::FIELD_GEO_LONGITUDE => array( |
|
| 704 | - 'predicate' => 'http://schema.org/longitude', |
|
| 705 | - 'type' => self::DATA_TYPE_DOUBLE, |
|
| 706 | - 'export_type' => 'xsd:double', |
|
| 707 | - 'constraints' => '', |
|
| 708 | - // To build custom metabox. |
|
| 709 | - 'input_field' => 'coordinates', |
|
| 710 | - ), |
|
| 711 | - self::FIELD_ADDRESS => array( |
|
| 712 | - 'predicate' => 'http://schema.org/streetAddress', |
|
| 713 | - 'type' => self::DATA_TYPE_STRING, |
|
| 714 | - 'export_type' => 'xsd:string', |
|
| 715 | - 'constraints' => '', |
|
| 716 | - // To build custom metabox. |
|
| 717 | - 'input_field' => 'address', |
|
| 718 | - ), |
|
| 719 | - self::FIELD_ADDRESS_PO_BOX => array( |
|
| 720 | - 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
| 721 | - 'type' => self::DATA_TYPE_STRING, |
|
| 722 | - 'export_type' => 'xsd:string', |
|
| 723 | - 'constraints' => '', |
|
| 724 | - // To build custom metabox. |
|
| 725 | - 'input_field' => 'address', |
|
| 726 | - ), |
|
| 727 | - self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
| 728 | - 'predicate' => 'http://schema.org/postalCode', |
|
| 729 | - 'type' => self::DATA_TYPE_STRING, |
|
| 730 | - 'export_type' => 'xsd:string', |
|
| 731 | - 'constraints' => '', |
|
| 732 | - // To build custom metabox. |
|
| 733 | - 'input_field' => 'address', |
|
| 734 | - ), |
|
| 735 | - self::FIELD_ADDRESS_LOCALITY => array( |
|
| 736 | - 'predicate' => 'http://schema.org/addressLocality', |
|
| 737 | - 'type' => self::DATA_TYPE_STRING, |
|
| 738 | - 'export_type' => 'xsd:string', |
|
| 739 | - 'constraints' => '', |
|
| 740 | - // To build custom metabox. |
|
| 741 | - 'input_field' => 'address', |
|
| 742 | - ), |
|
| 743 | - self::FIELD_ADDRESS_REGION => array( |
|
| 744 | - 'predicate' => 'http://schema.org/addressRegion', |
|
| 745 | - 'type' => self::DATA_TYPE_STRING, |
|
| 746 | - 'export_type' => 'xsd:string', |
|
| 747 | - 'constraints' => '', |
|
| 748 | - // To build custom metabox. |
|
| 749 | - 'input_field' => 'address', |
|
| 750 | - ), |
|
| 751 | - self::FIELD_ADDRESS_COUNTRY => array( |
|
| 752 | - 'predicate' => 'http://schema.org/addressCountry', |
|
| 753 | - 'type' => self::DATA_TYPE_STRING, |
|
| 754 | - 'export_type' => 'xsd:string', |
|
| 755 | - 'constraints' => '', |
|
| 756 | - // To build custom metabox. |
|
| 757 | - 'input_field' => 'address', |
|
| 758 | - ), |
|
| 759 | - ), |
|
| 760 | - 'templates' => array( |
|
| 761 | - 'subtitle' => '{{id}}', |
|
| 762 | - ), |
|
| 763 | - ); |
|
| 764 | - |
|
| 765 | - // Merge the custom fields with those provided by the thing schema. |
|
| 766 | - $thing_schema = $this->get_thing_schema(); |
|
| 767 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 768 | - |
|
| 769 | - return $schema; |
|
| 770 | - } |
|
| 771 | - |
|
| 772 | - /** |
|
| 773 | - * Get the 'local business' schema. |
|
| 774 | - * |
|
| 775 | - * @return array An array with the schema configuration. |
|
| 776 | - * |
|
| 777 | - * @since 3.1.0 |
|
| 778 | - */ |
|
| 779 | - private function get_local_business_schema() { |
|
| 780 | - |
|
| 781 | - $schema = array( |
|
| 782 | - 'label' => 'LocalBusiness', |
|
| 783 | - 'description' => 'A local business.', |
|
| 784 | - 'parents' => array( 'place', 'organization' ), |
|
| 785 | - 'css_class' => 'wl-local-business', |
|
| 786 | - 'uri' => 'http://schema.org/LocalBusiness', |
|
| 787 | - 'same_as' => array( |
|
| 788 | - 'http://rdf.freebase.com/ns/business/business_location', |
|
| 789 | - 'https://schema.org/Store', |
|
| 790 | - ), |
|
| 791 | - 'custom_fields' => array(), |
|
| 792 | - 'templates' => array( |
|
| 793 | - 'subtitle' => '{{id}}', |
|
| 794 | - ), |
|
| 795 | - ); |
|
| 796 | - |
|
| 797 | - // Merge the custom fields with those provided by the place and organization schema. |
|
| 798 | - $place_schema = $this->get_place_schema(); |
|
| 799 | - $organization_schema = $this->get_organization_schema(); |
|
| 800 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields'] ); |
|
| 801 | - |
|
| 802 | - return $schema; |
|
| 803 | - } |
|
| 18 | + /** |
|
| 19 | + * The 'location created' field name. |
|
| 20 | + * |
|
| 21 | + * @since 3.5.0 |
|
| 22 | + */ |
|
| 23 | + const FIELD_LOCATION_CREATED = 'wl_location_created'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * The 'topic' field name. |
|
| 27 | + * |
|
| 28 | + * @since 3.5.0 |
|
| 29 | + */ |
|
| 30 | + const FIELD_TOPIC = 'wl_topic'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The 'author' field name. |
|
| 34 | + * |
|
| 35 | + * @since 3.1.0 |
|
| 36 | + */ |
|
| 37 | + const FIELD_AUTHOR = 'wl_author'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The 'same as' field name. |
|
| 41 | + * |
|
| 42 | + * @since 3.1.0 |
|
| 43 | + */ |
|
| 44 | + const FIELD_SAME_AS = 'entity_same_as'; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The 'date start' field name. |
|
| 48 | + * |
|
| 49 | + * @since 3.1.0 |
|
| 50 | + */ |
|
| 51 | + const FIELD_DATE_START = 'wl_cal_date_start'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The 'date end' field name. |
|
| 55 | + * |
|
| 56 | + * @since 3.1.0 |
|
| 57 | + */ |
|
| 58 | + const FIELD_DATE_END = 'wl_cal_date_end'; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The 'location' field name. |
|
| 62 | + * |
|
| 63 | + * @since 3.1.0 |
|
| 64 | + */ |
|
| 65 | + const FIELD_LOCATION = 'wl_location'; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * The 'founder' field name. |
|
| 69 | + * |
|
| 70 | + * @since 3.1.0 |
|
| 71 | + */ |
|
| 72 | + const FIELD_FOUNDER = 'wl_founder'; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * The 'knows' field name. |
|
| 76 | + * |
|
| 77 | + * @since 3.1.0 |
|
| 78 | + */ |
|
| 79 | + const FIELD_KNOWS = 'wl_knows'; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * The 'birth date' field name. |
|
| 83 | + * |
|
| 84 | + * @since 3.1.0 |
|
| 85 | + */ |
|
| 86 | + const FIELD_BIRTH_DATE = 'wl_birth_date'; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * The 'birth place' field name. |
|
| 90 | + * |
|
| 91 | + * @since 3.1.0 |
|
| 92 | + */ |
|
| 93 | + const FIELD_BIRTH_PLACE = 'wl_birth_place'; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * The 'latitude' field name. |
|
| 97 | + * |
|
| 98 | + * @since 3.1.0 |
|
| 99 | + */ |
|
| 100 | + const FIELD_GEO_LATITUDE = 'wl_geo_latitude'; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * The 'longitude' field name. |
|
| 104 | + * |
|
| 105 | + * @since 3.1.0 |
|
| 106 | + */ |
|
| 107 | + const FIELD_GEO_LONGITUDE = 'wl_geo_longitude'; |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * The 'streetAddress' field name. |
|
| 111 | + * |
|
| 112 | + * @since 3.1.0 |
|
| 113 | + */ |
|
| 114 | + const FIELD_ADDRESS = 'wl_address'; |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * The 'postOfficeBoxNumber' field name. |
|
| 118 | + * |
|
| 119 | + * @since 3.3.0 |
|
| 120 | + */ |
|
| 121 | + const FIELD_ADDRESS_PO_BOX = 'wl_address_post_office_box'; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * The 'postalCode' field name. |
|
| 125 | + * |
|
| 126 | + * @since 3.3.0 |
|
| 127 | + */ |
|
| 128 | + const FIELD_ADDRESS_POSTAL_CODE = 'wl_address_postal_code'; |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * The 'addressLocality' field name. |
|
| 132 | + * |
|
| 133 | + * @since 3.3.0 |
|
| 134 | + */ |
|
| 135 | + const FIELD_ADDRESS_LOCALITY = 'wl_address_locality'; |
|
| 136 | + /** |
|
| 137 | + * The 'addressRegion' field name. |
|
| 138 | + * |
|
| 139 | + * @since 3.3.0 |
|
| 140 | + */ |
|
| 141 | + const FIELD_ADDRESS_REGION = 'wl_address_region'; |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * The 'addressCountry' field name. |
|
| 145 | + * |
|
| 146 | + * @since 3.3.0 |
|
| 147 | + */ |
|
| 148 | + const FIELD_ADDRESS_COUNTRY = 'wl_address_country'; |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * The 'entity type' field name. |
|
| 152 | + * |
|
| 153 | + * @since 3.1.0 |
|
| 154 | + */ |
|
| 155 | + const FIELD_ENTITY_TYPE = 'wl_entity_type_uri'; |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * The 'email' field name. |
|
| 159 | + * |
|
| 160 | + * @since 3.2.0 |
|
| 161 | + */ |
|
| 162 | + const FIELD_EMAIL = 'wl_email'; |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * The 'affiliation' field name. |
|
| 166 | + * |
|
| 167 | + * @since 3.2.0 |
|
| 168 | + */ |
|
| 169 | + const FIELD_AFFILIATION = 'wl_affiliation'; |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * The 'telephone' field name. |
|
| 173 | + * |
|
| 174 | + * @since 3.8.0 |
|
| 175 | + */ |
|
| 176 | + const FIELD_TELEPHONE = 'wl_schema_telephone'; |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * The 'URI' data type name. |
|
| 180 | + * |
|
| 181 | + * @since 3.1.0 |
|
| 182 | + */ |
|
| 183 | + const DATA_TYPE_URI = 'uri'; |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * The 'date' data type name. |
|
| 187 | + * |
|
| 188 | + * @since 3.1.0 |
|
| 189 | + */ |
|
| 190 | + const DATA_TYPE_DATE = 'date'; |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * The 'double' data type name. |
|
| 194 | + * |
|
| 195 | + * @since 3.1.0 |
|
| 196 | + */ |
|
| 197 | + const DATA_TYPE_DOUBLE = 'double'; |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * The 'string' data type name. |
|
| 201 | + * |
|
| 202 | + * @since 3.1.0 |
|
| 203 | + */ |
|
| 204 | + const DATA_TYPE_STRING = 'string'; |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * The 'integer' data type name. |
|
| 208 | + * |
|
| 209 | + * @since 3.1.0 |
|
| 210 | + */ |
|
| 211 | + const DATA_TYPE_INTEGER = 'int'; |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * The 'boolean' data type name. |
|
| 215 | + * |
|
| 216 | + * @since 3.1.0 |
|
| 217 | + */ |
|
| 218 | + const DATA_TYPE_BOOLEAN = 'bool'; |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * The schema.org Event type URI. |
|
| 222 | + * |
|
| 223 | + * @since 3.1.0 |
|
| 224 | + */ |
|
| 225 | + const SCHEMA_EVENT_TYPE = 'http://schema.org/Event'; |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * The Schema service singleton instance. |
|
| 229 | + * |
|
| 230 | + * @since 3.1.0 |
|
| 231 | + * @access private |
|
| 232 | + * @var \Wordlift_Schema_Service $instance The Schema service singleton instance. |
|
| 233 | + */ |
|
| 234 | + private static $instance; |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * WordLift's schema. |
|
| 238 | + * |
|
| 239 | + * @since 3.1.0 |
|
| 240 | + * @access private |
|
| 241 | + * @var array $schema WordLift's schema. |
|
| 242 | + */ |
|
| 243 | + private $schema; |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * The Log service. |
|
| 247 | + * |
|
| 248 | + * @since 3.1.0 |
|
| 249 | + * @access private |
|
| 250 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 251 | + */ |
|
| 252 | + private $log_service; |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Wordlift_Schema_Service constructor. |
|
| 256 | + * |
|
| 257 | + * @since 3.1.0 |
|
| 258 | + */ |
|
| 259 | + public function __construct() { |
|
| 260 | + |
|
| 261 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Schema_Service' ); |
|
| 262 | + |
|
| 263 | + // Create a singleton instance of the Schema service, useful to provide static functions to global functions. |
|
| 264 | + self::$instance = $this; |
|
| 265 | + |
|
| 266 | + // Set the taxonomy data. |
|
| 267 | + // Note: parent types must be defined before child types. |
|
| 268 | + $this->schema = array( |
|
| 269 | + 'thing' => $this->get_thing_schema(), |
|
| 270 | + 'creative-work' => $this->get_creative_work_schema(), |
|
| 271 | + 'event' => $this->get_event_schema(), |
|
| 272 | + 'organization' => $this->get_organization_schema(), |
|
| 273 | + 'person' => $this->get_person_schema(), |
|
| 274 | + 'place' => $this->get_place_schema(), |
|
| 275 | + 'localbusiness' => $this->get_local_business_schema(), |
|
| 276 | + ); |
|
| 277 | + |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Get a reference to the Schema service. |
|
| 282 | + * |
|
| 283 | + * @since 3.1.0 |
|
| 284 | + * |
|
| 285 | + * @return Wordlift_Schema_Service A reference to the Schema service. |
|
| 286 | + */ |
|
| 287 | + public static function get_instance() { |
|
| 288 | + |
|
| 289 | + return self::$instance; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Get the properties for a field with the specified key. The key is used as |
|
| 294 | + * meta key when the field's value is stored in WordPress meta data table. |
|
| 295 | + * |
|
| 296 | + * @since 3.6.0 |
|
| 297 | + * |
|
| 298 | + * @param string $key The field's key. |
|
| 299 | + * |
|
| 300 | + * @return null|array An array of field's properties or null if the field is not found. |
|
| 301 | + */ |
|
| 302 | + public function get_field( $key ) { |
|
| 303 | + |
|
| 304 | + // Parse each schema's fields until we find the one we're looking for, then |
|
| 305 | + // return its properties. |
|
| 306 | + foreach ( $this->schema as $_ => $schema ) { |
|
| 307 | + |
|
| 308 | + if ( ! isset( $schema['custom_fields'] ) ) { |
|
| 309 | + break; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + foreach ( $schema['custom_fields'] as $field => $props ) { |
|
| 313 | + if ( $key === $field ) { |
|
| 314 | + return $props; |
|
| 315 | + } |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + return null; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Get the WordLift's schema. |
|
| 324 | + * |
|
| 325 | + * @param string $name The schema name. |
|
| 326 | + * |
|
| 327 | + * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
| 328 | + * |
|
| 329 | + * @since 3.1.0 |
|
| 330 | + */ |
|
| 331 | + public function get_schema( $name ) { |
|
| 332 | + |
|
| 333 | + // Check if the schema exists and, if not, return NULL. |
|
| 334 | + if ( ! isset( $this->schema[ $name ] ) ) { |
|
| 335 | + return null; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + // Return the requested schema. |
|
| 339 | + return $this->schema[ $name ]; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Get the WordLift's schema trough schema type uri. |
|
| 344 | + * |
|
| 345 | + * @param string $uri The schema uri. |
|
| 346 | + * |
|
| 347 | + * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
| 348 | + * |
|
| 349 | + * @since 3.3.0 |
|
| 350 | + */ |
|
| 351 | + public function get_schema_by_uri( $uri ) { |
|
| 352 | + |
|
| 353 | + foreach ( $this->schema as $name => $schema ) { |
|
| 354 | + if ( $schema['uri'] === $uri ) { |
|
| 355 | + return $schema; |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + return null; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Get the 'thing' schema. |
|
| 364 | + * |
|
| 365 | + * @return array An array with the schema configuration. |
|
| 366 | + * |
|
| 367 | + * @since 3.1.0 |
|
| 368 | + */ |
|
| 369 | + private function get_thing_schema() { |
|
| 370 | + |
|
| 371 | + return array( |
|
| 372 | + 'css_class' => 'wl-thing', |
|
| 373 | + 'uri' => 'http://schema.org/Thing', |
|
| 374 | + 'same_as' => array( '*' ), |
|
| 375 | + // set as default. |
|
| 376 | + 'custom_fields' => array( |
|
| 377 | + self::FIELD_SAME_AS => array( |
|
| 378 | + 'predicate' => 'http://schema.org/sameAs', |
|
| 379 | + 'type' => self::DATA_TYPE_URI, |
|
| 380 | + 'export_type' => 'http://schema.org/Thing', |
|
| 381 | + 'constraints' => array( |
|
| 382 | + 'cardinality' => INF, |
|
| 383 | + ), |
|
| 384 | + // We need a custom metabox. |
|
| 385 | + 'input_field' => 'sameas', |
|
| 386 | + ), |
|
| 387 | + // Add the schema:url property. |
|
| 388 | + Wordlift_Schema_Url_Property_Service::META_KEY => Wordlift_Schema_Url_Property_Service::get_instance() |
|
| 389 | + ->get_compat_definition(), |
|
| 390 | + ), |
|
| 391 | + // {{sameAs}} not present in the microdata template, |
|
| 392 | + // because it is treated separately in *wl_content_embed_item_microdata* |
|
| 393 | + 'templates' => array( |
|
| 394 | + 'subtitle' => '{{id}}', |
|
| 395 | + ), |
|
| 396 | + ); |
|
| 397 | + |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Get the 'creative work' schema. |
|
| 402 | + * |
|
| 403 | + * @return array An array with the schema configuration. |
|
| 404 | + * |
|
| 405 | + * @since 3.1.0 |
|
| 406 | + */ |
|
| 407 | + private function get_creative_work_schema() { |
|
| 408 | + |
|
| 409 | + $schema = array( |
|
| 410 | + 'label' => 'CreativeWork', |
|
| 411 | + 'description' => 'A creative work (or a Music Album).', |
|
| 412 | + 'parents' => array( 'thing' ), |
|
| 413 | + // Give term slug as parent. |
|
| 414 | + 'css_class' => 'wl-creative-work', |
|
| 415 | + 'uri' => 'http://schema.org/CreativeWork', |
|
| 416 | + 'same_as' => array( |
|
| 417 | + 'http://schema.org/MusicAlbum', |
|
| 418 | + 'http://schema.org/Product', |
|
| 419 | + ), |
|
| 420 | + 'custom_fields' => array( |
|
| 421 | + self::FIELD_AUTHOR => array( |
|
| 422 | + 'predicate' => 'http://schema.org/author', |
|
| 423 | + 'type' => self::DATA_TYPE_URI, |
|
| 424 | + 'export_type' => 'http://schema.org/Person', |
|
| 425 | + 'constraints' => array( |
|
| 426 | + 'uri_type' => array( 'Person', 'Organization' ), |
|
| 427 | + 'cardinality' => INF, |
|
| 428 | + ), |
|
| 429 | + ), |
|
| 430 | + ), |
|
| 431 | + 'templates' => array( |
|
| 432 | + 'subtitle' => '{{id}}', |
|
| 433 | + ), |
|
| 434 | + ); |
|
| 435 | + |
|
| 436 | + // Merge the custom fields with those provided by the thing schema. |
|
| 437 | + $thing_schema = $this->get_thing_schema(); |
|
| 438 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 439 | + |
|
| 440 | + return $schema; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Get the 'event' schema. |
|
| 445 | + * |
|
| 446 | + * @return array An array with the schema configuration. |
|
| 447 | + * |
|
| 448 | + * @since 3.1.0 |
|
| 449 | + */ |
|
| 450 | + private function get_event_schema() { |
|
| 451 | + |
|
| 452 | + $schema = array( |
|
| 453 | + 'label' => 'Event', |
|
| 454 | + 'description' => 'An event . ', |
|
| 455 | + 'parents' => array( 'thing' ), |
|
| 456 | + 'css_class' => 'wl-event', |
|
| 457 | + 'uri' => self::SCHEMA_EVENT_TYPE, |
|
| 458 | + 'same_as' => array( 'http://dbpedia.org/ontology/Event' ), |
|
| 459 | + 'custom_fields' => array( |
|
| 460 | + self::FIELD_DATE_START => array( |
|
| 461 | + 'predicate' => 'http://schema.org/startDate', |
|
| 462 | + 'type' => self::DATA_TYPE_DATE, |
|
| 463 | + 'export_type' => 'xsd:datetime', |
|
| 464 | + 'constraints' => '', |
|
| 465 | + ), |
|
| 466 | + self::FIELD_DATE_END => array( |
|
| 467 | + 'predicate' => 'http://schema.org/endDate', |
|
| 468 | + 'type' => self::DATA_TYPE_DATE, |
|
| 469 | + 'export_type' => 'xsd:datetime', |
|
| 470 | + 'constraints' => '', |
|
| 471 | + ), |
|
| 472 | + self::FIELD_LOCATION => array( |
|
| 473 | + 'predicate' => 'http://schema.org/location', |
|
| 474 | + 'type' => self::DATA_TYPE_URI, |
|
| 475 | + 'export_type' => 'http://schema.org/PostalAddress', |
|
| 476 | + 'constraints' => array( |
|
| 477 | + 'uri_type' => array( 'Place', 'LocalBusiness' ), |
|
| 478 | + 'cardinality' => INF, |
|
| 479 | + ), |
|
| 480 | + ), |
|
| 481 | + ), |
|
| 482 | + 'templates' => array( |
|
| 483 | + 'subtitle' => '{{id}}', |
|
| 484 | + ), |
|
| 485 | + ); |
|
| 486 | + |
|
| 487 | + // Merge the custom fields with those provided by the thing schema. |
|
| 488 | + $thing_schema = $this->get_thing_schema(); |
|
| 489 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 490 | + |
|
| 491 | + return $schema; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * Get the 'organization' schema. |
|
| 496 | + * |
|
| 497 | + * @return array An array with the schema configuration. |
|
| 498 | + * |
|
| 499 | + * @since 3.1.0 |
|
| 500 | + */ |
|
| 501 | + private function get_organization_schema() { |
|
| 502 | + |
|
| 503 | + $schema = array( |
|
| 504 | + 'label' => 'Organization', |
|
| 505 | + 'description' => 'An organization, including a government or a newspaper.', |
|
| 506 | + 'parents' => array( 'thing' ), |
|
| 507 | + 'css_class' => 'wl-organization', |
|
| 508 | + 'uri' => 'http://schema.org/Organization', |
|
| 509 | + 'same_as' => array( |
|
| 510 | + 'http://rdf.freebase.com/ns/organization.organization', |
|
| 511 | + 'http://rdf.freebase.com/ns/government.government', |
|
| 512 | + 'http://schema.org/Newspaper', |
|
| 513 | + ), |
|
| 514 | + 'custom_fields' => array( |
|
| 515 | + self::FIELD_FOUNDER => array( |
|
| 516 | + 'predicate' => 'http://schema.org/founder', |
|
| 517 | + 'type' => self::DATA_TYPE_URI, |
|
| 518 | + 'export_type' => 'http://schema.org/Person', |
|
| 519 | + 'constraints' => array( |
|
| 520 | + 'uri_type' => 'Person', |
|
| 521 | + 'cardinality' => INF, |
|
| 522 | + ), |
|
| 523 | + ), |
|
| 524 | + self::FIELD_ADDRESS => array( |
|
| 525 | + 'predicate' => 'http://schema.org/streetAddress', |
|
| 526 | + 'type' => self::DATA_TYPE_STRING, |
|
| 527 | + 'export_type' => 'xsd:string', |
|
| 528 | + 'constraints' => '', |
|
| 529 | + // To build custom metabox. |
|
| 530 | + 'input_field' => 'address', |
|
| 531 | + ), |
|
| 532 | + self::FIELD_ADDRESS_PO_BOX => array( |
|
| 533 | + 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
| 534 | + 'type' => self::DATA_TYPE_STRING, |
|
| 535 | + 'export_type' => 'xsd:string', |
|
| 536 | + 'constraints' => '', |
|
| 537 | + // To build custom metabox. |
|
| 538 | + 'input_field' => 'address', |
|
| 539 | + ), |
|
| 540 | + self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
| 541 | + 'predicate' => 'http://schema.org/postalCode', |
|
| 542 | + 'type' => self::DATA_TYPE_STRING, |
|
| 543 | + 'export_type' => 'xsd:string', |
|
| 544 | + 'constraints' => '', |
|
| 545 | + // To build custom metabox. |
|
| 546 | + 'input_field' => 'address', |
|
| 547 | + ), |
|
| 548 | + self::FIELD_ADDRESS_LOCALITY => array( |
|
| 549 | + 'predicate' => 'http://schema.org/addressLocality', |
|
| 550 | + 'type' => self::DATA_TYPE_STRING, |
|
| 551 | + 'export_type' => 'xsd:string', |
|
| 552 | + 'constraints' => '', |
|
| 553 | + // To build custom metabox. |
|
| 554 | + 'input_field' => 'address', |
|
| 555 | + ), |
|
| 556 | + self::FIELD_ADDRESS_REGION => array( |
|
| 557 | + 'predicate' => 'http://schema.org/addressRegion', |
|
| 558 | + 'type' => self::DATA_TYPE_STRING, |
|
| 559 | + 'export_type' => 'xsd:string', |
|
| 560 | + 'constraints' => '', |
|
| 561 | + // To build custom metabox. |
|
| 562 | + 'input_field' => 'address', |
|
| 563 | + ), |
|
| 564 | + self::FIELD_ADDRESS_COUNTRY => array( |
|
| 565 | + 'predicate' => 'http://schema.org/addressCountry', |
|
| 566 | + 'type' => self::DATA_TYPE_STRING, |
|
| 567 | + 'export_type' => 'xsd:string', |
|
| 568 | + 'constraints' => '', |
|
| 569 | + // To build custom metabox. |
|
| 570 | + 'input_field' => 'address', |
|
| 571 | + ), |
|
| 572 | + self::FIELD_EMAIL => array( |
|
| 573 | + 'predicate' => 'http://schema.org/email', |
|
| 574 | + 'type' => self::DATA_TYPE_STRING, |
|
| 575 | + 'export_type' => 'xsd:string', |
|
| 576 | + 'constraints' => '', |
|
| 577 | + ), |
|
| 578 | + self::FIELD_TELEPHONE => array( |
|
| 579 | + 'predicate' => 'http://schema.org/telephone', |
|
| 580 | + 'type' => self::DATA_TYPE_STRING, |
|
| 581 | + 'export_type' => 'xsd:string', |
|
| 582 | + 'constraints' => '', |
|
| 583 | + ), |
|
| 584 | + ), |
|
| 585 | + 'templates' => array( |
|
| 586 | + 'subtitle' => '{{id}}', |
|
| 587 | + ), |
|
| 588 | + ); |
|
| 589 | + |
|
| 590 | + // Merge the custom fields with those provided by the thing schema. |
|
| 591 | + $thing_schema = $this->get_thing_schema(); |
|
| 592 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 593 | + |
|
| 594 | + return $schema; |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * Get the 'person' schema. |
|
| 599 | + * |
|
| 600 | + * @return array An array with the schema configuration. |
|
| 601 | + * |
|
| 602 | + * @since 3.1.0 |
|
| 603 | + */ |
|
| 604 | + private function get_person_schema() { |
|
| 605 | + |
|
| 606 | + $schema = array( |
|
| 607 | + 'label' => 'Person', |
|
| 608 | + 'description' => 'A person (or a music artist).', |
|
| 609 | + 'parents' => array( 'thing' ), |
|
| 610 | + 'css_class' => 'wl-person', |
|
| 611 | + 'uri' => 'http://schema.org/Person', |
|
| 612 | + 'same_as' => array( |
|
| 613 | + 'http://rdf.freebase.com/ns/people.person', |
|
| 614 | + 'http://rdf.freebase.com/ns/music.artist', |
|
| 615 | + 'http://dbpedia.org/class/yago/LivingPeople', |
|
| 616 | + ), |
|
| 617 | + 'custom_fields' => array( |
|
| 618 | + self::FIELD_KNOWS => array( |
|
| 619 | + 'predicate' => 'http://schema.org/knows', |
|
| 620 | + 'type' => self::DATA_TYPE_URI, |
|
| 621 | + 'export_type' => 'http://schema.org/Person', |
|
| 622 | + 'constraints' => array( |
|
| 623 | + 'uri_type' => 'Person', |
|
| 624 | + 'cardinality' => INF, |
|
| 625 | + ), |
|
| 626 | + ), |
|
| 627 | + self::FIELD_BIRTH_DATE => array( |
|
| 628 | + 'predicate' => 'http://schema.org/birthDate', |
|
| 629 | + 'type' => self::DATA_TYPE_DATE, |
|
| 630 | + 'export_type' => 'xsd:date', |
|
| 631 | + 'constraints' => '', |
|
| 632 | + ), |
|
| 633 | + self::FIELD_BIRTH_PLACE => array( |
|
| 634 | + 'predicate' => 'http://schema.org/birthPlace', |
|
| 635 | + 'type' => self::DATA_TYPE_URI, |
|
| 636 | + 'export_type' => 'http://schema.org/Place', |
|
| 637 | + 'constraints' => array( |
|
| 638 | + 'uri_type' => 'Place', |
|
| 639 | + ), |
|
| 640 | + ), |
|
| 641 | + self::FIELD_AFFILIATION => array( |
|
| 642 | + 'predicate' => 'http://schema.org/affiliation', |
|
| 643 | + 'type' => self::DATA_TYPE_URI, |
|
| 644 | + 'export_type' => 'http://schema.org/Organization', |
|
| 645 | + 'constraints' => array( |
|
| 646 | + 'uri_type' => array( |
|
| 647 | + 'Organization', |
|
| 648 | + 'LocalBusiness', |
|
| 649 | + ), |
|
| 650 | + 'cardinality' => INF, |
|
| 651 | + ), |
|
| 652 | + ), |
|
| 653 | + self::FIELD_EMAIL => array( |
|
| 654 | + 'predicate' => 'http://schema.org/email', |
|
| 655 | + 'type' => self::DATA_TYPE_STRING, |
|
| 656 | + 'export_type' => 'xsd:string', |
|
| 657 | + 'constraints' => array( |
|
| 658 | + 'cardinality' => INF, |
|
| 659 | + ), |
|
| 660 | + ), |
|
| 661 | + ), |
|
| 662 | + 'templates' => array( |
|
| 663 | + 'subtitle' => '{{id}}', |
|
| 664 | + ), |
|
| 665 | + ); |
|
| 666 | + |
|
| 667 | + // Merge the custom fields with those provided by the thing schema. |
|
| 668 | + $thing_schema = $this->get_thing_schema(); |
|
| 669 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 670 | + |
|
| 671 | + return $schema; |
|
| 672 | + |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + /** |
|
| 676 | + * Get the 'place' schema. |
|
| 677 | + * |
|
| 678 | + * @return array An array with the schema configuration. |
|
| 679 | + * |
|
| 680 | + * @since 3.1.0 |
|
| 681 | + */ |
|
| 682 | + private function get_place_schema() { |
|
| 683 | + |
|
| 684 | + $schema = array( |
|
| 685 | + 'label' => 'Place', |
|
| 686 | + 'description' => 'A place.', |
|
| 687 | + 'parents' => array( 'thing' ), |
|
| 688 | + 'css_class' => 'wl-place', |
|
| 689 | + 'uri' => 'http://schema.org/Place', |
|
| 690 | + 'same_as' => array( |
|
| 691 | + 'http://rdf.freebase.com/ns/location.location', |
|
| 692 | + 'http://www.opengis.net/gml/_Feature', |
|
| 693 | + ), |
|
| 694 | + 'custom_fields' => array( |
|
| 695 | + self::FIELD_GEO_LATITUDE => array( |
|
| 696 | + 'predicate' => 'http://schema.org/latitude', |
|
| 697 | + 'type' => self::DATA_TYPE_DOUBLE, |
|
| 698 | + 'export_type' => 'xsd:double', |
|
| 699 | + 'constraints' => '', |
|
| 700 | + // To build custom metabox. |
|
| 701 | + 'input_field' => 'coordinates', |
|
| 702 | + ), |
|
| 703 | + self::FIELD_GEO_LONGITUDE => array( |
|
| 704 | + 'predicate' => 'http://schema.org/longitude', |
|
| 705 | + 'type' => self::DATA_TYPE_DOUBLE, |
|
| 706 | + 'export_type' => 'xsd:double', |
|
| 707 | + 'constraints' => '', |
|
| 708 | + // To build custom metabox. |
|
| 709 | + 'input_field' => 'coordinates', |
|
| 710 | + ), |
|
| 711 | + self::FIELD_ADDRESS => array( |
|
| 712 | + 'predicate' => 'http://schema.org/streetAddress', |
|
| 713 | + 'type' => self::DATA_TYPE_STRING, |
|
| 714 | + 'export_type' => 'xsd:string', |
|
| 715 | + 'constraints' => '', |
|
| 716 | + // To build custom metabox. |
|
| 717 | + 'input_field' => 'address', |
|
| 718 | + ), |
|
| 719 | + self::FIELD_ADDRESS_PO_BOX => array( |
|
| 720 | + 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
| 721 | + 'type' => self::DATA_TYPE_STRING, |
|
| 722 | + 'export_type' => 'xsd:string', |
|
| 723 | + 'constraints' => '', |
|
| 724 | + // To build custom metabox. |
|
| 725 | + 'input_field' => 'address', |
|
| 726 | + ), |
|
| 727 | + self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
| 728 | + 'predicate' => 'http://schema.org/postalCode', |
|
| 729 | + 'type' => self::DATA_TYPE_STRING, |
|
| 730 | + 'export_type' => 'xsd:string', |
|
| 731 | + 'constraints' => '', |
|
| 732 | + // To build custom metabox. |
|
| 733 | + 'input_field' => 'address', |
|
| 734 | + ), |
|
| 735 | + self::FIELD_ADDRESS_LOCALITY => array( |
|
| 736 | + 'predicate' => 'http://schema.org/addressLocality', |
|
| 737 | + 'type' => self::DATA_TYPE_STRING, |
|
| 738 | + 'export_type' => 'xsd:string', |
|
| 739 | + 'constraints' => '', |
|
| 740 | + // To build custom metabox. |
|
| 741 | + 'input_field' => 'address', |
|
| 742 | + ), |
|
| 743 | + self::FIELD_ADDRESS_REGION => array( |
|
| 744 | + 'predicate' => 'http://schema.org/addressRegion', |
|
| 745 | + 'type' => self::DATA_TYPE_STRING, |
|
| 746 | + 'export_type' => 'xsd:string', |
|
| 747 | + 'constraints' => '', |
|
| 748 | + // To build custom metabox. |
|
| 749 | + 'input_field' => 'address', |
|
| 750 | + ), |
|
| 751 | + self::FIELD_ADDRESS_COUNTRY => array( |
|
| 752 | + 'predicate' => 'http://schema.org/addressCountry', |
|
| 753 | + 'type' => self::DATA_TYPE_STRING, |
|
| 754 | + 'export_type' => 'xsd:string', |
|
| 755 | + 'constraints' => '', |
|
| 756 | + // To build custom metabox. |
|
| 757 | + 'input_field' => 'address', |
|
| 758 | + ), |
|
| 759 | + ), |
|
| 760 | + 'templates' => array( |
|
| 761 | + 'subtitle' => '{{id}}', |
|
| 762 | + ), |
|
| 763 | + ); |
|
| 764 | + |
|
| 765 | + // Merge the custom fields with those provided by the thing schema. |
|
| 766 | + $thing_schema = $this->get_thing_schema(); |
|
| 767 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 768 | + |
|
| 769 | + return $schema; |
|
| 770 | + } |
|
| 771 | + |
|
| 772 | + /** |
|
| 773 | + * Get the 'local business' schema. |
|
| 774 | + * |
|
| 775 | + * @return array An array with the schema configuration. |
|
| 776 | + * |
|
| 777 | + * @since 3.1.0 |
|
| 778 | + */ |
|
| 779 | + private function get_local_business_schema() { |
|
| 780 | + |
|
| 781 | + $schema = array( |
|
| 782 | + 'label' => 'LocalBusiness', |
|
| 783 | + 'description' => 'A local business.', |
|
| 784 | + 'parents' => array( 'place', 'organization' ), |
|
| 785 | + 'css_class' => 'wl-local-business', |
|
| 786 | + 'uri' => 'http://schema.org/LocalBusiness', |
|
| 787 | + 'same_as' => array( |
|
| 788 | + 'http://rdf.freebase.com/ns/business/business_location', |
|
| 789 | + 'https://schema.org/Store', |
|
| 790 | + ), |
|
| 791 | + 'custom_fields' => array(), |
|
| 792 | + 'templates' => array( |
|
| 793 | + 'subtitle' => '{{id}}', |
|
| 794 | + ), |
|
| 795 | + ); |
|
| 796 | + |
|
| 797 | + // Merge the custom fields with those provided by the place and organization schema. |
|
| 798 | + $place_schema = $this->get_place_schema(); |
|
| 799 | + $organization_schema = $this->get_organization_schema(); |
|
| 800 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields'] ); |
|
| 801 | + |
|
| 802 | + return $schema; |
|
| 803 | + } |
|
| 804 | 804 | |
| 805 | 805 | } |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | */ |
| 259 | 259 | public function __construct() { |
| 260 | 260 | |
| 261 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Schema_Service' ); |
|
| 261 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Schema_Service'); |
|
| 262 | 262 | |
| 263 | 263 | // Create a singleton instance of the Schema service, useful to provide static functions to global functions. |
| 264 | 264 | self::$instance = $this; |
@@ -299,18 +299,18 @@ discard block |
||
| 299 | 299 | * |
| 300 | 300 | * @return null|array An array of field's properties or null if the field is not found. |
| 301 | 301 | */ |
| 302 | - public function get_field( $key ) { |
|
| 302 | + public function get_field($key) { |
|
| 303 | 303 | |
| 304 | 304 | // Parse each schema's fields until we find the one we're looking for, then |
| 305 | 305 | // return its properties. |
| 306 | - foreach ( $this->schema as $_ => $schema ) { |
|
| 306 | + foreach ($this->schema as $_ => $schema) { |
|
| 307 | 307 | |
| 308 | - if ( ! isset( $schema['custom_fields'] ) ) { |
|
| 308 | + if ( ! isset($schema['custom_fields'])) { |
|
| 309 | 309 | break; |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - foreach ( $schema['custom_fields'] as $field => $props ) { |
|
| 313 | - if ( $key === $field ) { |
|
| 312 | + foreach ($schema['custom_fields'] as $field => $props) { |
|
| 313 | + if ($key === $field) { |
|
| 314 | 314 | return $props; |
| 315 | 315 | } |
| 316 | 316 | } |
@@ -328,15 +328,15 @@ discard block |
||
| 328 | 328 | * |
| 329 | 329 | * @since 3.1.0 |
| 330 | 330 | */ |
| 331 | - public function get_schema( $name ) { |
|
| 331 | + public function get_schema($name) { |
|
| 332 | 332 | |
| 333 | 333 | // Check if the schema exists and, if not, return NULL. |
| 334 | - if ( ! isset( $this->schema[ $name ] ) ) { |
|
| 334 | + if ( ! isset($this->schema[$name])) { |
|
| 335 | 335 | return null; |
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | // Return the requested schema. |
| 339 | - return $this->schema[ $name ]; |
|
| 339 | + return $this->schema[$name]; |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | /** |
@@ -348,10 +348,10 @@ discard block |
||
| 348 | 348 | * |
| 349 | 349 | * @since 3.3.0 |
| 350 | 350 | */ |
| 351 | - public function get_schema_by_uri( $uri ) { |
|
| 351 | + public function get_schema_by_uri($uri) { |
|
| 352 | 352 | |
| 353 | - foreach ( $this->schema as $name => $schema ) { |
|
| 354 | - if ( $schema['uri'] === $uri ) { |
|
| 353 | + foreach ($this->schema as $name => $schema) { |
|
| 354 | + if ($schema['uri'] === $uri) { |
|
| 355 | 355 | return $schema; |
| 356 | 356 | } |
| 357 | 357 | } |
@@ -371,7 +371,7 @@ discard block |
||
| 371 | 371 | return array( |
| 372 | 372 | 'css_class' => 'wl-thing', |
| 373 | 373 | 'uri' => 'http://schema.org/Thing', |
| 374 | - 'same_as' => array( '*' ), |
|
| 374 | + 'same_as' => array('*'), |
|
| 375 | 375 | // set as default. |
| 376 | 376 | 'custom_fields' => array( |
| 377 | 377 | self::FIELD_SAME_AS => array( |
@@ -409,7 +409,7 @@ discard block |
||
| 409 | 409 | $schema = array( |
| 410 | 410 | 'label' => 'CreativeWork', |
| 411 | 411 | 'description' => 'A creative work (or a Music Album).', |
| 412 | - 'parents' => array( 'thing' ), |
|
| 412 | + 'parents' => array('thing'), |
|
| 413 | 413 | // Give term slug as parent. |
| 414 | 414 | 'css_class' => 'wl-creative-work', |
| 415 | 415 | 'uri' => 'http://schema.org/CreativeWork', |
@@ -423,7 +423,7 @@ discard block |
||
| 423 | 423 | 'type' => self::DATA_TYPE_URI, |
| 424 | 424 | 'export_type' => 'http://schema.org/Person', |
| 425 | 425 | 'constraints' => array( |
| 426 | - 'uri_type' => array( 'Person', 'Organization' ), |
|
| 426 | + 'uri_type' => array('Person', 'Organization'), |
|
| 427 | 427 | 'cardinality' => INF, |
| 428 | 428 | ), |
| 429 | 429 | ), |
@@ -435,7 +435,7 @@ discard block |
||
| 435 | 435 | |
| 436 | 436 | // Merge the custom fields with those provided by the thing schema. |
| 437 | 437 | $thing_schema = $this->get_thing_schema(); |
| 438 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 438 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $thing_schema['custom_fields']); |
|
| 439 | 439 | |
| 440 | 440 | return $schema; |
| 441 | 441 | } |
@@ -452,10 +452,10 @@ discard block |
||
| 452 | 452 | $schema = array( |
| 453 | 453 | 'label' => 'Event', |
| 454 | 454 | 'description' => 'An event . ', |
| 455 | - 'parents' => array( 'thing' ), |
|
| 455 | + 'parents' => array('thing'), |
|
| 456 | 456 | 'css_class' => 'wl-event', |
| 457 | 457 | 'uri' => self::SCHEMA_EVENT_TYPE, |
| 458 | - 'same_as' => array( 'http://dbpedia.org/ontology/Event' ), |
|
| 458 | + 'same_as' => array('http://dbpedia.org/ontology/Event'), |
|
| 459 | 459 | 'custom_fields' => array( |
| 460 | 460 | self::FIELD_DATE_START => array( |
| 461 | 461 | 'predicate' => 'http://schema.org/startDate', |
@@ -474,7 +474,7 @@ discard block |
||
| 474 | 474 | 'type' => self::DATA_TYPE_URI, |
| 475 | 475 | 'export_type' => 'http://schema.org/PostalAddress', |
| 476 | 476 | 'constraints' => array( |
| 477 | - 'uri_type' => array( 'Place', 'LocalBusiness' ), |
|
| 477 | + 'uri_type' => array('Place', 'LocalBusiness'), |
|
| 478 | 478 | 'cardinality' => INF, |
| 479 | 479 | ), |
| 480 | 480 | ), |
@@ -486,7 +486,7 @@ discard block |
||
| 486 | 486 | |
| 487 | 487 | // Merge the custom fields with those provided by the thing schema. |
| 488 | 488 | $thing_schema = $this->get_thing_schema(); |
| 489 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 489 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $thing_schema['custom_fields']); |
|
| 490 | 490 | |
| 491 | 491 | return $schema; |
| 492 | 492 | } |
@@ -503,7 +503,7 @@ discard block |
||
| 503 | 503 | $schema = array( |
| 504 | 504 | 'label' => 'Organization', |
| 505 | 505 | 'description' => 'An organization, including a government or a newspaper.', |
| 506 | - 'parents' => array( 'thing' ), |
|
| 506 | + 'parents' => array('thing'), |
|
| 507 | 507 | 'css_class' => 'wl-organization', |
| 508 | 508 | 'uri' => 'http://schema.org/Organization', |
| 509 | 509 | 'same_as' => array( |
@@ -589,7 +589,7 @@ discard block |
||
| 589 | 589 | |
| 590 | 590 | // Merge the custom fields with those provided by the thing schema. |
| 591 | 591 | $thing_schema = $this->get_thing_schema(); |
| 592 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 592 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $thing_schema['custom_fields']); |
|
| 593 | 593 | |
| 594 | 594 | return $schema; |
| 595 | 595 | } |
@@ -606,7 +606,7 @@ discard block |
||
| 606 | 606 | $schema = array( |
| 607 | 607 | 'label' => 'Person', |
| 608 | 608 | 'description' => 'A person (or a music artist).', |
| 609 | - 'parents' => array( 'thing' ), |
|
| 609 | + 'parents' => array('thing'), |
|
| 610 | 610 | 'css_class' => 'wl-person', |
| 611 | 611 | 'uri' => 'http://schema.org/Person', |
| 612 | 612 | 'same_as' => array( |
@@ -666,7 +666,7 @@ discard block |
||
| 666 | 666 | |
| 667 | 667 | // Merge the custom fields with those provided by the thing schema. |
| 668 | 668 | $thing_schema = $this->get_thing_schema(); |
| 669 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 669 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $thing_schema['custom_fields']); |
|
| 670 | 670 | |
| 671 | 671 | return $schema; |
| 672 | 672 | |
@@ -684,7 +684,7 @@ discard block |
||
| 684 | 684 | $schema = array( |
| 685 | 685 | 'label' => 'Place', |
| 686 | 686 | 'description' => 'A place.', |
| 687 | - 'parents' => array( 'thing' ), |
|
| 687 | + 'parents' => array('thing'), |
|
| 688 | 688 | 'css_class' => 'wl-place', |
| 689 | 689 | 'uri' => 'http://schema.org/Place', |
| 690 | 690 | 'same_as' => array( |
@@ -764,7 +764,7 @@ discard block |
||
| 764 | 764 | |
| 765 | 765 | // Merge the custom fields with those provided by the thing schema. |
| 766 | 766 | $thing_schema = $this->get_thing_schema(); |
| 767 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $thing_schema['custom_fields'] ); |
|
| 767 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $thing_schema['custom_fields']); |
|
| 768 | 768 | |
| 769 | 769 | return $schema; |
| 770 | 770 | } |
@@ -781,7 +781,7 @@ discard block |
||
| 781 | 781 | $schema = array( |
| 782 | 782 | 'label' => 'LocalBusiness', |
| 783 | 783 | 'description' => 'A local business.', |
| 784 | - 'parents' => array( 'place', 'organization' ), |
|
| 784 | + 'parents' => array('place', 'organization'), |
|
| 785 | 785 | 'css_class' => 'wl-local-business', |
| 786 | 786 | 'uri' => 'http://schema.org/LocalBusiness', |
| 787 | 787 | 'same_as' => array( |
@@ -797,7 +797,7 @@ discard block |
||
| 797 | 797 | // Merge the custom fields with those provided by the place and organization schema. |
| 798 | 798 | $place_schema = $this->get_place_schema(); |
| 799 | 799 | $organization_schema = $this->get_organization_schema(); |
| 800 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields'] ); |
|
| 800 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields']); |
|
| 801 | 801 | |
| 802 | 802 | return $schema; |
| 803 | 803 | } |
@@ -2,56 +2,56 @@ |
||
| 2 | 2 | |
| 3 | 3 | class Wordlift_Chord_Widget extends WP_Widget { |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * Sets up the widgets name etc |
|
| 7 | - */ |
|
| 8 | - public function __construct() { |
|
| 9 | - // widget actual processes |
|
| 10 | - parent::__construct( |
|
| 11 | - 'wl_chord_widget', // Base ID |
|
| 12 | - __( 'Chord Widget', 'wordlift' ), // Name |
|
| 13 | - array( 'description' => __( 'The Chord Widget depicts the main topics of your blog in concise graph.', 'wordlift' ), ) // Args |
|
| 14 | - ); |
|
| 15 | - } |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * Outputs the content of the widget |
|
| 19 | - * |
|
| 20 | - * @param array $args |
|
| 21 | - * @param array $instance |
|
| 22 | - */ |
|
| 23 | - public function widget( $args, $instance ) { |
|
| 24 | - // outputs the content of the widget |
|
| 25 | - echo do_shortcode( '[wl_chord global=true]' ); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Outputs the options form on admin |
|
| 30 | - * |
|
| 31 | - * @param array $instance The widget options |
|
| 32 | - * |
|
| 33 | - * @return string|void |
|
| 34 | - */ |
|
| 35 | - public function form( $instance ) { |
|
| 36 | - // outputs the options form on admin |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Processing widget options on save |
|
| 41 | - * |
|
| 42 | - * @param array $new_instance The new options |
|
| 43 | - * @param array $old_instance The previous options |
|
| 44 | - * |
|
| 45 | - * @return array|void |
|
| 46 | - */ |
|
| 47 | - public function update( $new_instance, $old_instance ) { |
|
| 48 | - // processes widget options to be saved |
|
| 49 | - } |
|
| 5 | + /** |
|
| 6 | + * Sets up the widgets name etc |
|
| 7 | + */ |
|
| 8 | + public function __construct() { |
|
| 9 | + // widget actual processes |
|
| 10 | + parent::__construct( |
|
| 11 | + 'wl_chord_widget', // Base ID |
|
| 12 | + __( 'Chord Widget', 'wordlift' ), // Name |
|
| 13 | + array( 'description' => __( 'The Chord Widget depicts the main topics of your blog in concise graph.', 'wordlift' ), ) // Args |
|
| 14 | + ); |
|
| 15 | + } |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * Outputs the content of the widget |
|
| 19 | + * |
|
| 20 | + * @param array $args |
|
| 21 | + * @param array $instance |
|
| 22 | + */ |
|
| 23 | + public function widget( $args, $instance ) { |
|
| 24 | + // outputs the content of the widget |
|
| 25 | + echo do_shortcode( '[wl_chord global=true]' ); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Outputs the options form on admin |
|
| 30 | + * |
|
| 31 | + * @param array $instance The widget options |
|
| 32 | + * |
|
| 33 | + * @return string|void |
|
| 34 | + */ |
|
| 35 | + public function form( $instance ) { |
|
| 36 | + // outputs the options form on admin |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Processing widget options on save |
|
| 41 | + * |
|
| 42 | + * @param array $new_instance The new options |
|
| 43 | + * @param array $old_instance The previous options |
|
| 44 | + * |
|
| 45 | + * @return array|void |
|
| 46 | + */ |
|
| 47 | + public function update( $new_instance, $old_instance ) { |
|
| 48 | + // processes widget options to be saved |
|
| 49 | + } |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | function wl_register_chord_widget() { |
| 53 | 53 | |
| 54 | - register_widget( 'WordLift_Chord_Widget' ); |
|
| 54 | + register_widget( 'WordLift_Chord_Widget' ); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | add_action( 'widgets_init', 'wl_register_chord_widget' ); |
@@ -9,8 +9,8 @@ discard block |
||
| 9 | 9 | // widget actual processes |
| 10 | 10 | parent::__construct( |
| 11 | 11 | 'wl_chord_widget', // Base ID |
| 12 | - __( 'Chord Widget', 'wordlift' ), // Name |
|
| 13 | - array( 'description' => __( 'The Chord Widget depicts the main topics of your blog in concise graph.', 'wordlift' ), ) // Args |
|
| 12 | + __('Chord Widget', 'wordlift'), // Name |
|
| 13 | + array('description' => __('The Chord Widget depicts the main topics of your blog in concise graph.', 'wordlift'),) // Args |
|
| 14 | 14 | ); |
| 15 | 15 | } |
| 16 | 16 | |
@@ -20,9 +20,9 @@ discard block |
||
| 20 | 20 | * @param array $args |
| 21 | 21 | * @param array $instance |
| 22 | 22 | */ |
| 23 | - public function widget( $args, $instance ) { |
|
| 23 | + public function widget($args, $instance) { |
|
| 24 | 24 | // outputs the content of the widget |
| 25 | - echo do_shortcode( '[wl_chord global=true]' ); |
|
| 25 | + echo do_shortcode('[wl_chord global=true]'); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * |
| 33 | 33 | * @return string|void |
| 34 | 34 | */ |
| 35 | - public function form( $instance ) { |
|
| 35 | + public function form($instance) { |
|
| 36 | 36 | // outputs the options form on admin |
| 37 | 37 | } |
| 38 | 38 | |
@@ -44,15 +44,15 @@ discard block |
||
| 44 | 44 | * |
| 45 | 45 | * @return array|void |
| 46 | 46 | */ |
| 47 | - public function update( $new_instance, $old_instance ) { |
|
| 47 | + public function update($new_instance, $old_instance) { |
|
| 48 | 48 | // processes widget options to be saved |
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | function wl_register_chord_widget() { |
| 53 | 53 | |
| 54 | - register_widget( 'WordLift_Chord_Widget' ); |
|
| 54 | + register_widget('WordLift_Chord_Widget'); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | -add_action( 'widgets_init', 'wl_register_chord_widget' ); |
|
| 58 | -add_filter( 'widget_text', 'do_shortcode' ); |
|
| 57 | +add_action('widgets_init', 'wl_register_chord_widget'); |
|
| 58 | +add_filter('widget_text', 'do_shortcode'); |
|
@@ -10,28 +10,28 @@ discard block |
||
| 10 | 10 | */ |
| 11 | 11 | function wl_schema_reset_value( $post_id, $property_name ) { |
| 12 | 12 | |
| 13 | - // Some checks on the parameters |
|
| 14 | - if ( ! is_numeric( $post_id ) || is_null( $property_name ) ) { |
|
| 15 | - return false; |
|
| 16 | - } |
|
| 13 | + // Some checks on the parameters |
|
| 14 | + if ( ! is_numeric( $post_id ) || is_null( $property_name ) ) { |
|
| 15 | + return false; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - // Build full schema uri if necessary |
|
| 19 | - $property_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 18 | + // Build full schema uri if necessary |
|
| 19 | + $property_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 20 | 20 | |
| 21 | - // Get accepted properties |
|
| 22 | - $accepted_fields = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 21 | + // Get accepted properties |
|
| 22 | + $accepted_fields = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 23 | 23 | |
| 24 | - // Find the name of the custom-field managing the schema property |
|
| 25 | - foreach ( $accepted_fields as $wl_constant => $field ) { |
|
| 26 | - if ( $field['predicate'] == $property_name ) { |
|
| 24 | + // Find the name of the custom-field managing the schema property |
|
| 25 | + foreach ( $accepted_fields as $wl_constant => $field ) { |
|
| 26 | + if ( $field['predicate'] == $property_name ) { |
|
| 27 | 27 | |
| 28 | - delete_post_meta( $post_id, $wl_constant ); |
|
| 28 | + delete_post_meta( $post_id, $wl_constant ); |
|
| 29 | 29 | |
| 30 | - return true; |
|
| 31 | - } |
|
| 32 | - } |
|
| 30 | + return true; |
|
| 31 | + } |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - return false; |
|
| 34 | + return false; |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -44,34 +44,34 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | function wl_schema_get_value( $post_id, $property_name ) { |
| 46 | 46 | |
| 47 | - // Property name must be defined. |
|
| 48 | - if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 49 | - return null; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - // store eventual schema name in different variable |
|
| 53 | - $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 54 | - |
|
| 55 | - // Establish entity id. |
|
| 56 | - if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 57 | - $post_id = get_the_ID(); |
|
| 58 | - if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 59 | - return null; |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - // Get custom fields. |
|
| 64 | - $term_mapping = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 65 | - // Search for the required meta value (by constant name or schema name) |
|
| 66 | - foreach ( $term_mapping as $wl_constant => $property_info ) { |
|
| 67 | - $found_constant = ( $wl_constant == $property_name ); |
|
| 68 | - $found_predicate = ( isset( $property_info['predicate'] ) && $property_info['predicate'] == $property_schema_name ); |
|
| 69 | - if ( $found_constant || $found_predicate ) { |
|
| 70 | - return get_post_meta( $post_id, $wl_constant ); |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return null; |
|
| 47 | + // Property name must be defined. |
|
| 48 | + if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 49 | + return null; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + // store eventual schema name in different variable |
|
| 53 | + $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 54 | + |
|
| 55 | + // Establish entity id. |
|
| 56 | + if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 57 | + $post_id = get_the_ID(); |
|
| 58 | + if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 59 | + return null; |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + // Get custom fields. |
|
| 64 | + $term_mapping = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 65 | + // Search for the required meta value (by constant name or schema name) |
|
| 66 | + foreach ( $term_mapping as $wl_constant => $property_info ) { |
|
| 67 | + $found_constant = ( $wl_constant == $property_name ); |
|
| 68 | + $found_predicate = ( isset( $property_info['predicate'] ) && $property_info['predicate'] == $property_schema_name ); |
|
| 69 | + if ( $found_constant || $found_predicate ) { |
|
| 70 | + return get_post_meta( $post_id, $wl_constant ); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return null; |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /** |
@@ -85,16 +85,16 @@ discard block |
||
| 85 | 85 | */ |
| 86 | 86 | function wl_schema_add_value( $post_id, $property_name, $property_value ) { |
| 87 | 87 | |
| 88 | - if ( ! is_array( $property_value ) ) { |
|
| 89 | - $property_value = array( $property_value ); |
|
| 90 | - } |
|
| 88 | + if ( ! is_array( $property_value ) ) { |
|
| 89 | + $property_value = array( $property_value ); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - // Get the old values or set an empty array. |
|
| 93 | - $old_values = wl_schema_get_value( $post_id, $property_name ) ?: array(); |
|
| 92 | + // Get the old values or set an empty array. |
|
| 93 | + $old_values = wl_schema_get_value( $post_id, $property_name ) ?: array(); |
|
| 94 | 94 | |
| 95 | - $merged_property_value = array_unique( array_merge( $property_value, $old_values ) ); |
|
| 95 | + $merged_property_value = array_unique( array_merge( $property_value, $old_values ) ); |
|
| 96 | 96 | |
| 97 | - wl_schema_set_value( $post_id, $property_name, $merged_property_value ); |
|
| 97 | + wl_schema_set_value( $post_id, $property_name, $merged_property_value ); |
|
| 98 | 98 | |
| 99 | 99 | } |
| 100 | 100 | |
@@ -109,38 +109,38 @@ discard block |
||
| 109 | 109 | */ |
| 110 | 110 | function wl_schema_set_value( $post_id, $property_name, $property_value ) { |
| 111 | 111 | |
| 112 | - // Some checks on the parameters |
|
| 113 | - if ( ! is_numeric( $post_id ) || is_null( $property_name ) || empty( $property_value ) || is_null( $property_value ) ) { |
|
| 114 | - return false; |
|
| 115 | - } |
|
| 112 | + // Some checks on the parameters |
|
| 113 | + if ( ! is_numeric( $post_id ) || is_null( $property_name ) || empty( $property_value ) || is_null( $property_value ) ) { |
|
| 114 | + return false; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - // Build full schema uri if necessary |
|
| 118 | - $property_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 117 | + // Build full schema uri if necessary |
|
| 118 | + $property_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 119 | 119 | |
| 120 | - // Get accepted properties |
|
| 121 | - $accepted_fields = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 120 | + // Get accepted properties |
|
| 121 | + $accepted_fields = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 122 | 122 | |
| 123 | - // Find the name of the custom-field managing the schema property |
|
| 124 | - foreach ( $accepted_fields as $wl_constant => $field ) { |
|
| 125 | - if ( $field['predicate'] == $property_name ) { |
|
| 123 | + // Find the name of the custom-field managing the schema property |
|
| 124 | + foreach ( $accepted_fields as $wl_constant => $field ) { |
|
| 125 | + if ( $field['predicate'] == $property_name ) { |
|
| 126 | 126 | |
| 127 | - // Deal with single values |
|
| 128 | - if ( ! is_array( $property_value ) ) { |
|
| 129 | - $property_value = array( $property_value ); |
|
| 130 | - } |
|
| 127 | + // Deal with single values |
|
| 128 | + if ( ! is_array( $property_value ) ) { |
|
| 129 | + $property_value = array( $property_value ); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - // Delete present meta |
|
| 133 | - delete_post_meta( $post_id, $wl_constant ); |
|
| 132 | + // Delete present meta |
|
| 133 | + delete_post_meta( $post_id, $wl_constant ); |
|
| 134 | 134 | |
| 135 | - foreach ( $property_value as $value ) { |
|
| 136 | - add_post_meta( $post_id, $wl_constant, $value ); |
|
| 137 | - } |
|
| 135 | + foreach ( $property_value as $value ) { |
|
| 136 | + add_post_meta( $post_id, $wl_constant, $value ); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - return true; |
|
| 140 | - } |
|
| 141 | - } |
|
| 139 | + return true; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - return false; |
|
| 143 | + return false; |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | |
@@ -154,18 +154,18 @@ discard block |
||
| 154 | 154 | */ |
| 155 | 155 | function wl_schema_get_types( $post_id ) { |
| 156 | 156 | |
| 157 | - // Some checks on the parameters |
|
| 158 | - if ( ! is_numeric( $post_id ) ) { |
|
| 159 | - return null; |
|
| 160 | - } |
|
| 157 | + // Some checks on the parameters |
|
| 158 | + if ( ! is_numeric( $post_id ) ) { |
|
| 159 | + return null; |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - $type = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 162 | + $type = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 163 | 163 | |
| 164 | - if ( isset( $type['uri'] ) ) { |
|
| 165 | - return array( $type['uri'] ); |
|
| 166 | - } |
|
| 164 | + if ( isset( $type['uri'] ) ) { |
|
| 165 | + return array( $type['uri'] ); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - return null; |
|
| 168 | + return null; |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | /** |
@@ -178,21 +178,21 @@ discard block |
||
| 178 | 178 | */ |
| 179 | 179 | function wl_schema_set_types( $post_id, $type_names ) { |
| 180 | 180 | |
| 181 | - // Some checks on the parameters |
|
| 182 | - if ( ! is_numeric( $post_id ) || empty( $type_names ) || is_null( $type_names ) ) { |
|
| 183 | - return null; |
|
| 184 | - } |
|
| 181 | + // Some checks on the parameters |
|
| 182 | + if ( ! is_numeric( $post_id ) || empty( $type_names ) || is_null( $type_names ) ) { |
|
| 183 | + return null; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - // TODO: support more than one type. |
|
| 187 | - if ( is_array( $type_names ) ) { |
|
| 188 | - $type_names = $type_names[0]; |
|
| 189 | - } |
|
| 186 | + // TODO: support more than one type. |
|
| 187 | + if ( is_array( $type_names ) ) { |
|
| 188 | + $type_names = $type_names[0]; |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - // Get the schema URI (e.g. http://schema.org/Thing) |
|
| 192 | - $type_names = wl_build_full_schema_uri_from_schema_slug( $type_names ); |
|
| 191 | + // Get the schema URI (e.g. http://schema.org/Thing) |
|
| 192 | + $type_names = wl_build_full_schema_uri_from_schema_slug( $type_names ); |
|
| 193 | 193 | |
| 194 | - // Actually sets the taxonomy type |
|
| 195 | - wl_set_entity_main_type( $post_id, $type_names ); |
|
| 194 | + // Actually sets the taxonomy type |
|
| 195 | + wl_set_entity_main_type( $post_id, $type_names ); |
|
| 196 | 196 | |
| 197 | 197 | } |
| 198 | 198 | |
@@ -208,24 +208,24 @@ discard block |
||
| 208 | 208 | */ |
| 209 | 209 | function wl_schema_get_type_properties( $type_name ) { |
| 210 | 210 | |
| 211 | - // Build full schema uri if necessary |
|
| 212 | - $type_name = wl_build_full_schema_uri_from_schema_slug( $type_name ); |
|
| 211 | + // Build full schema uri if necessary |
|
| 212 | + $type_name = wl_build_full_schema_uri_from_schema_slug( $type_name ); |
|
| 213 | 213 | |
| 214 | - // Get all custom fields |
|
| 215 | - $all_types_and_fields = wl_entity_taxonomy_get_custom_fields(); |
|
| 214 | + // Get all custom fields |
|
| 215 | + $all_types_and_fields = wl_entity_taxonomy_get_custom_fields(); |
|
| 216 | 216 | |
| 217 | - $schema_root_address = 'http://schema.org/'; |
|
| 218 | - $type_properties = array(); |
|
| 217 | + $schema_root_address = 'http://schema.org/'; |
|
| 218 | + $type_properties = array(); |
|
| 219 | 219 | |
| 220 | - // Search for the entity type which has the requested name as uri |
|
| 221 | - if ( isset( $all_types_and_fields[ $type_name ] ) ) { |
|
| 222 | - foreach ( $all_types_and_fields[ $type_name ] as $field ) { |
|
| 223 | - // Convert to schema slug and store in array |
|
| 224 | - $type_properties[] = str_replace( $schema_root_address, '', $field['predicate'] ); |
|
| 225 | - } |
|
| 226 | - } |
|
| 220 | + // Search for the entity type which has the requested name as uri |
|
| 221 | + if ( isset( $all_types_and_fields[ $type_name ] ) ) { |
|
| 222 | + foreach ( $all_types_and_fields[ $type_name ] as $field ) { |
|
| 223 | + // Convert to schema slug and store in array |
|
| 224 | + $type_properties[] = str_replace( $schema_root_address, '', $field['predicate'] ); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - return $type_properties; |
|
| 228 | + return $type_properties; |
|
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | /** |
@@ -237,11 +237,11 @@ discard block |
||
| 237 | 237 | */ |
| 238 | 238 | function wl_build_full_schema_uri_from_schema_slug( $schema_name ) { |
| 239 | 239 | |
| 240 | - $schema_root_address = 'http://schema.org/'; |
|
| 240 | + $schema_root_address = 'http://schema.org/'; |
|
| 241 | 241 | |
| 242 | - if ( strpos( $schema_name, $schema_root_address ) === false ) { // === necessary |
|
| 243 | - $schema_name = $schema_root_address . $schema_name; |
|
| 244 | - } |
|
| 242 | + if ( strpos( $schema_name, $schema_root_address ) === false ) { // === necessary |
|
| 243 | + $schema_name = $schema_root_address . $schema_name; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - return $schema_name; |
|
| 246 | + return $schema_name; |
|
| 247 | 247 | } |
| 248 | 248 | \ No newline at end of file |
@@ -8,24 +8,24 @@ discard block |
||
| 8 | 8 | * |
| 9 | 9 | * @return boolean The method returns true if everything went ok, false otherwise. |
| 10 | 10 | */ |
| 11 | -function wl_schema_reset_value( $post_id, $property_name ) { |
|
| 11 | +function wl_schema_reset_value($post_id, $property_name) { |
|
| 12 | 12 | |
| 13 | 13 | // Some checks on the parameters |
| 14 | - if ( ! is_numeric( $post_id ) || is_null( $property_name ) ) { |
|
| 14 | + if ( ! is_numeric($post_id) || is_null($property_name)) { |
|
| 15 | 15 | return false; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | // Build full schema uri if necessary |
| 19 | - $property_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 19 | + $property_name = wl_build_full_schema_uri_from_schema_slug($property_name); |
|
| 20 | 20 | |
| 21 | 21 | // Get accepted properties |
| 22 | - $accepted_fields = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 22 | + $accepted_fields = wl_entity_taxonomy_get_custom_fields($post_id); |
|
| 23 | 23 | |
| 24 | 24 | // Find the name of the custom-field managing the schema property |
| 25 | - foreach ( $accepted_fields as $wl_constant => $field ) { |
|
| 26 | - if ( $field['predicate'] == $property_name ) { |
|
| 25 | + foreach ($accepted_fields as $wl_constant => $field) { |
|
| 26 | + if ($field['predicate'] == $property_name) { |
|
| 27 | 27 | |
| 28 | - delete_post_meta( $post_id, $wl_constant ); |
|
| 28 | + delete_post_meta($post_id, $wl_constant); |
|
| 29 | 29 | |
| 30 | 30 | return true; |
| 31 | 31 | } |
@@ -42,32 +42,32 @@ discard block |
||
| 42 | 42 | * |
| 43 | 43 | * @return array|null An array of values or NULL in case of no values (or error). |
| 44 | 44 | */ |
| 45 | -function wl_schema_get_value( $post_id, $property_name ) { |
|
| 45 | +function wl_schema_get_value($post_id, $property_name) { |
|
| 46 | 46 | |
| 47 | 47 | // Property name must be defined. |
| 48 | - if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 48 | + if ( ! isset($property_name) || is_null($property_name)) { |
|
| 49 | 49 | return null; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | // store eventual schema name in different variable |
| 53 | - $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 53 | + $property_schema_name = wl_build_full_schema_uri_from_schema_slug($property_name); |
|
| 54 | 54 | |
| 55 | 55 | // Establish entity id. |
| 56 | - if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 56 | + if (is_null($post_id) || ! is_numeric($post_id)) { |
|
| 57 | 57 | $post_id = get_the_ID(); |
| 58 | - if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 58 | + if (is_null($post_id) || ! is_numeric($post_id)) { |
|
| 59 | 59 | return null; |
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | // Get custom fields. |
| 64 | - $term_mapping = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 64 | + $term_mapping = wl_entity_taxonomy_get_custom_fields($post_id); |
|
| 65 | 65 | // Search for the required meta value (by constant name or schema name) |
| 66 | - foreach ( $term_mapping as $wl_constant => $property_info ) { |
|
| 67 | - $found_constant = ( $wl_constant == $property_name ); |
|
| 68 | - $found_predicate = ( isset( $property_info['predicate'] ) && $property_info['predicate'] == $property_schema_name ); |
|
| 69 | - if ( $found_constant || $found_predicate ) { |
|
| 70 | - return get_post_meta( $post_id, $wl_constant ); |
|
| 66 | + foreach ($term_mapping as $wl_constant => $property_info) { |
|
| 67 | + $found_constant = ($wl_constant == $property_name); |
|
| 68 | + $found_predicate = (isset($property_info['predicate']) && $property_info['predicate'] == $property_schema_name); |
|
| 69 | + if ($found_constant || $found_predicate) { |
|
| 70 | + return get_post_meta($post_id, $wl_constant); |
|
| 71 | 71 | } |
| 72 | 72 | } |
| 73 | 73 | |
@@ -83,18 +83,18 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return array An array of values or NULL in case of no values (or error). |
| 85 | 85 | */ |
| 86 | -function wl_schema_add_value( $post_id, $property_name, $property_value ) { |
|
| 86 | +function wl_schema_add_value($post_id, $property_name, $property_value) { |
|
| 87 | 87 | |
| 88 | - if ( ! is_array( $property_value ) ) { |
|
| 89 | - $property_value = array( $property_value ); |
|
| 88 | + if ( ! is_array($property_value)) { |
|
| 89 | + $property_value = array($property_value); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | // Get the old values or set an empty array. |
| 93 | - $old_values = wl_schema_get_value( $post_id, $property_name ) ?: array(); |
|
| 93 | + $old_values = wl_schema_get_value($post_id, $property_name) ?: array(); |
|
| 94 | 94 | |
| 95 | - $merged_property_value = array_unique( array_merge( $property_value, $old_values ) ); |
|
| 95 | + $merged_property_value = array_unique(array_merge($property_value, $old_values)); |
|
| 96 | 96 | |
| 97 | - wl_schema_set_value( $post_id, $property_name, $merged_property_value ); |
|
| 97 | + wl_schema_set_value($post_id, $property_name, $merged_property_value); |
|
| 98 | 98 | |
| 99 | 99 | } |
| 100 | 100 | |
@@ -107,33 +107,33 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return boolean The method returns true if everything went ok, an error string otherwise. |
| 109 | 109 | */ |
| 110 | -function wl_schema_set_value( $post_id, $property_name, $property_value ) { |
|
| 110 | +function wl_schema_set_value($post_id, $property_name, $property_value) { |
|
| 111 | 111 | |
| 112 | 112 | // Some checks on the parameters |
| 113 | - if ( ! is_numeric( $post_id ) || is_null( $property_name ) || empty( $property_value ) || is_null( $property_value ) ) { |
|
| 113 | + if ( ! is_numeric($post_id) || is_null($property_name) || empty($property_value) || is_null($property_value)) { |
|
| 114 | 114 | return false; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // Build full schema uri if necessary |
| 118 | - $property_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 118 | + $property_name = wl_build_full_schema_uri_from_schema_slug($property_name); |
|
| 119 | 119 | |
| 120 | 120 | // Get accepted properties |
| 121 | - $accepted_fields = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 121 | + $accepted_fields = wl_entity_taxonomy_get_custom_fields($post_id); |
|
| 122 | 122 | |
| 123 | 123 | // Find the name of the custom-field managing the schema property |
| 124 | - foreach ( $accepted_fields as $wl_constant => $field ) { |
|
| 125 | - if ( $field['predicate'] == $property_name ) { |
|
| 124 | + foreach ($accepted_fields as $wl_constant => $field) { |
|
| 125 | + if ($field['predicate'] == $property_name) { |
|
| 126 | 126 | |
| 127 | 127 | // Deal with single values |
| 128 | - if ( ! is_array( $property_value ) ) { |
|
| 129 | - $property_value = array( $property_value ); |
|
| 128 | + if ( ! is_array($property_value)) { |
|
| 129 | + $property_value = array($property_value); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | // Delete present meta |
| 133 | - delete_post_meta( $post_id, $wl_constant ); |
|
| 133 | + delete_post_meta($post_id, $wl_constant); |
|
| 134 | 134 | |
| 135 | - foreach ( $property_value as $value ) { |
|
| 136 | - add_post_meta( $post_id, $wl_constant, $value ); |
|
| 135 | + foreach ($property_value as $value) { |
|
| 136 | + add_post_meta($post_id, $wl_constant, $value); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | return true; |
@@ -152,17 +152,17 @@ discard block |
||
| 152 | 152 | * @return array Array of type(s) (e.g. Type, for the http://schema.org/Type) |
| 153 | 153 | * or NULL in case of no values (or error). |
| 154 | 154 | */ |
| 155 | -function wl_schema_get_types( $post_id ) { |
|
| 155 | +function wl_schema_get_types($post_id) { |
|
| 156 | 156 | |
| 157 | 157 | // Some checks on the parameters |
| 158 | - if ( ! is_numeric( $post_id ) ) { |
|
| 158 | + if ( ! is_numeric($post_id)) { |
|
| 159 | 159 | return null; |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - $type = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 162 | + $type = wl_entity_type_taxonomy_get_type($post_id); |
|
| 163 | 163 | |
| 164 | - if ( isset( $type['uri'] ) ) { |
|
| 165 | - return array( $type['uri'] ); |
|
| 164 | + if (isset($type['uri'])) { |
|
| 165 | + return array($type['uri']); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | return null; |
@@ -176,23 +176,23 @@ discard block |
||
| 176 | 176 | * |
| 177 | 177 | * @return boolean True if everything went ok, an error string otherwise. |
| 178 | 178 | */ |
| 179 | -function wl_schema_set_types( $post_id, $type_names ) { |
|
| 179 | +function wl_schema_set_types($post_id, $type_names) { |
|
| 180 | 180 | |
| 181 | 181 | // Some checks on the parameters |
| 182 | - if ( ! is_numeric( $post_id ) || empty( $type_names ) || is_null( $type_names ) ) { |
|
| 182 | + if ( ! is_numeric($post_id) || empty($type_names) || is_null($type_names)) { |
|
| 183 | 183 | return null; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | // TODO: support more than one type. |
| 187 | - if ( is_array( $type_names ) ) { |
|
| 187 | + if (is_array($type_names)) { |
|
| 188 | 188 | $type_names = $type_names[0]; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | // Get the schema URI (e.g. http://schema.org/Thing) |
| 192 | - $type_names = wl_build_full_schema_uri_from_schema_slug( $type_names ); |
|
| 192 | + $type_names = wl_build_full_schema_uri_from_schema_slug($type_names); |
|
| 193 | 193 | |
| 194 | 194 | // Actually sets the taxonomy type |
| 195 | - wl_set_entity_main_type( $post_id, $type_names ); |
|
| 195 | + wl_set_entity_main_type($post_id, $type_names); |
|
| 196 | 196 | |
| 197 | 197 | } |
| 198 | 198 | |
@@ -206,10 +206,10 @@ discard block |
||
| 206 | 206 | * @return array The method returns an array of supported properties for the type, e.g. (‘startDate’, ‘endDate’) for an Event. |
| 207 | 207 | * You can call wl_schema_get_property_expected_type on each to know which data type they expect. |
| 208 | 208 | */ |
| 209 | -function wl_schema_get_type_properties( $type_name ) { |
|
| 209 | +function wl_schema_get_type_properties($type_name) { |
|
| 210 | 210 | |
| 211 | 211 | // Build full schema uri if necessary |
| 212 | - $type_name = wl_build_full_schema_uri_from_schema_slug( $type_name ); |
|
| 212 | + $type_name = wl_build_full_schema_uri_from_schema_slug($type_name); |
|
| 213 | 213 | |
| 214 | 214 | // Get all custom fields |
| 215 | 215 | $all_types_and_fields = wl_entity_taxonomy_get_custom_fields(); |
@@ -218,10 +218,10 @@ discard block |
||
| 218 | 218 | $type_properties = array(); |
| 219 | 219 | |
| 220 | 220 | // Search for the entity type which has the requested name as uri |
| 221 | - if ( isset( $all_types_and_fields[ $type_name ] ) ) { |
|
| 222 | - foreach ( $all_types_and_fields[ $type_name ] as $field ) { |
|
| 221 | + if (isset($all_types_and_fields[$type_name])) { |
|
| 222 | + foreach ($all_types_and_fields[$type_name] as $field) { |
|
| 223 | 223 | // Convert to schema slug and store in array |
| 224 | - $type_properties[] = str_replace( $schema_root_address, '', $field['predicate'] ); |
|
| 224 | + $type_properties[] = str_replace($schema_root_address, '', $field['predicate']); |
|
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | |
@@ -235,12 +235,12 @@ discard block |
||
| 235 | 235 | * |
| 236 | 236 | * @return string The full schema uri (es. 'latitude' returns 'http://schema.org/latitude') |
| 237 | 237 | */ |
| 238 | -function wl_build_full_schema_uri_from_schema_slug( $schema_name ) { |
|
| 238 | +function wl_build_full_schema_uri_from_schema_slug($schema_name) { |
|
| 239 | 239 | |
| 240 | 240 | $schema_root_address = 'http://schema.org/'; |
| 241 | 241 | |
| 242 | - if ( strpos( $schema_name, $schema_root_address ) === false ) { // === necessary |
|
| 243 | - $schema_name = $schema_root_address . $schema_name; |
|
| 242 | + if (strpos($schema_name, $schema_root_address) === false) { // === necessary |
|
| 243 | + $schema_name = $schema_root_address.$schema_name; |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | return $schema_name; |
@@ -172,9 +172,9 @@ |
||
| 172 | 172 | * Sets the entity type(s) for the specified post ID. Support is now for only one type per entity. |
| 173 | 173 | * |
| 174 | 174 | * @param $post_id numeric The numeric post ID |
| 175 | - * @param $type_names array An array of strings, each defining a type (e.g. Type, for the http://schema.org/Type) |
|
| 175 | + * @param string $type_names array An array of strings, each defining a type (e.g. Type, for the http://schema.org/Type) |
|
| 176 | 176 | * |
| 177 | - * @return boolean True if everything went ok, an error string otherwise. |
|
| 177 | + * @return boolean|null True if everything went ok, an error string otherwise. |
|
| 178 | 178 | */ |
| 179 | 179 | function wl_schema_set_types( $post_id, $type_names ) { |
| 180 | 180 | |
@@ -22,112 +22,112 @@ |
||
| 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 | - * Initialize the class and set its properties. |
|
| 45 | - * |
|
| 46 | - * @since 1.0.0 |
|
| 47 | - * |
|
| 48 | - * @param string $plugin_name The name of this plugin. |
|
| 49 | - * @param string $version The version of this plugin. |
|
| 50 | - */ |
|
| 51 | - public function __construct( $plugin_name, $version ) { |
|
| 52 | - |
|
| 53 | - $this->plugin_name = $plugin_name; |
|
| 54 | - $this->version = $version; |
|
| 55 | - |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Register the stylesheets for the admin area. |
|
| 60 | - * |
|
| 61 | - * @since 1.0.0 |
|
| 62 | - */ |
|
| 63 | - public function enqueue_styles() { |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * This function is provided for demonstration purposes only. |
|
| 67 | - * |
|
| 68 | - * An instance of this class should be passed to the run() function |
|
| 69 | - * defined in Wordlift_Loader as all of the hooks are defined |
|
| 70 | - * in that particular class. |
|
| 71 | - * |
|
| 72 | - * The Wordlift_Loader will then create the relationship |
|
| 73 | - * between the defined hooks and the functions defined in this |
|
| 74 | - * class. |
|
| 75 | - */ |
|
| 76 | - |
|
| 77 | - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' ); |
|
| 78 | - |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Register the JavaScript for the admin area. |
|
| 83 | - * |
|
| 84 | - * @since 1.0.0 |
|
| 85 | - */ |
|
| 86 | - public function enqueue_scripts() { |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * This function is provided for demonstration purposes only. |
|
| 90 | - * |
|
| 91 | - * An instance of this class should be passed to the run() function |
|
| 92 | - * defined in Wordlift_Loader as all of the hooks are defined |
|
| 93 | - * in that particular class. |
|
| 94 | - * |
|
| 95 | - * The Wordlift_Loader will then create the relationship |
|
| 96 | - * between the defined hooks and the functions defined in this |
|
| 97 | - * class. |
|
| 98 | - */ |
|
| 99 | - |
|
| 100 | - // Enqueue our script. |
|
| 101 | - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wordlift-admin.bundle.js', array( 'jquery' ), $this->version, false ); |
|
| 102 | - |
|
| 103 | - // Set the basic params. |
|
| 104 | - $params = array( |
|
| 105 | - // @todo scripts in admin should use wp.post. |
|
| 106 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 107 | - // @todo remove specific actions from settings. |
|
| 108 | - 'action' => 'entity_by_title', |
|
| 109 | - 'language' => Wordlift_Configuration_Service::get_instance()->get_language_code(), |
|
| 110 | - 'l10n' => array( |
|
| 111 | - 'You already published an entity with the same name' => __( 'You already published an entity with the same name: ', 'wordlift' ), |
|
| 112 | - ), |
|
| 113 | - ); |
|
| 114 | - |
|
| 115 | - // Set post-related values if there's a current post. |
|
| 116 | - if ( null !== $post = $entity_being_edited = get_post() ) { |
|
| 117 | - |
|
| 118 | - $params['post_id'] = $entity_being_edited->ID; |
|
| 119 | - $params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_being_edited->post_type == Wordlift_Entity_Service::TYPE_NAME && is_numeric( get_the_ID() ); |
|
| 120 | - // We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it |
|
| 121 | - // from the results, since we don't want the current entity to be discovered by the analysis. |
|
| 122 | - // |
|
| 123 | - // See https://github.com/insideout10/wordlift-plugin/issues/345 |
|
| 124 | - $params['itemId'] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_being_edited->ID ); |
|
| 125 | - |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // Finally output the params as `wlSettings` for JavaScript code. |
|
| 129 | - wp_localize_script( $this->plugin_name, 'wlSettings', $params ); |
|
| 130 | - |
|
| 131 | - } |
|
| 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 | + * Initialize the class and set its properties. |
|
| 45 | + * |
|
| 46 | + * @since 1.0.0 |
|
| 47 | + * |
|
| 48 | + * @param string $plugin_name The name of this plugin. |
|
| 49 | + * @param string $version The version of this plugin. |
|
| 50 | + */ |
|
| 51 | + public function __construct( $plugin_name, $version ) { |
|
| 52 | + |
|
| 53 | + $this->plugin_name = $plugin_name; |
|
| 54 | + $this->version = $version; |
|
| 55 | + |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Register the stylesheets for the admin area. |
|
| 60 | + * |
|
| 61 | + * @since 1.0.0 |
|
| 62 | + */ |
|
| 63 | + public function enqueue_styles() { |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * This function is provided for demonstration purposes only. |
|
| 67 | + * |
|
| 68 | + * An instance of this class should be passed to the run() function |
|
| 69 | + * defined in Wordlift_Loader as all of the hooks are defined |
|
| 70 | + * in that particular class. |
|
| 71 | + * |
|
| 72 | + * The Wordlift_Loader will then create the relationship |
|
| 73 | + * between the defined hooks and the functions defined in this |
|
| 74 | + * class. |
|
| 75 | + */ |
|
| 76 | + |
|
| 77 | + wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' ); |
|
| 78 | + |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Register the JavaScript for the admin area. |
|
| 83 | + * |
|
| 84 | + * @since 1.0.0 |
|
| 85 | + */ |
|
| 86 | + public function enqueue_scripts() { |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * This function is provided for demonstration purposes only. |
|
| 90 | + * |
|
| 91 | + * An instance of this class should be passed to the run() function |
|
| 92 | + * defined in Wordlift_Loader as all of the hooks are defined |
|
| 93 | + * in that particular class. |
|
| 94 | + * |
|
| 95 | + * The Wordlift_Loader will then create the relationship |
|
| 96 | + * between the defined hooks and the functions defined in this |
|
| 97 | + * class. |
|
| 98 | + */ |
|
| 99 | + |
|
| 100 | + // Enqueue our script. |
|
| 101 | + wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wordlift-admin.bundle.js', array( 'jquery' ), $this->version, false ); |
|
| 102 | + |
|
| 103 | + // Set the basic params. |
|
| 104 | + $params = array( |
|
| 105 | + // @todo scripts in admin should use wp.post. |
|
| 106 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 107 | + // @todo remove specific actions from settings. |
|
| 108 | + 'action' => 'entity_by_title', |
|
| 109 | + 'language' => Wordlift_Configuration_Service::get_instance()->get_language_code(), |
|
| 110 | + 'l10n' => array( |
|
| 111 | + 'You already published an entity with the same name' => __( 'You already published an entity with the same name: ', 'wordlift' ), |
|
| 112 | + ), |
|
| 113 | + ); |
|
| 114 | + |
|
| 115 | + // Set post-related values if there's a current post. |
|
| 116 | + if ( null !== $post = $entity_being_edited = get_post() ) { |
|
| 117 | + |
|
| 118 | + $params['post_id'] = $entity_being_edited->ID; |
|
| 119 | + $params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_being_edited->post_type == Wordlift_Entity_Service::TYPE_NAME && is_numeric( get_the_ID() ); |
|
| 120 | + // We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it |
|
| 121 | + // from the results, since we don't want the current entity to be discovered by the analysis. |
|
| 122 | + // |
|
| 123 | + // See https://github.com/insideout10/wordlift-plugin/issues/345 |
|
| 124 | + $params['itemId'] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_being_edited->ID ); |
|
| 125 | + |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // Finally output the params as `wlSettings` for JavaScript code. |
|
| 129 | + wp_localize_script( $this->plugin_name, 'wlSettings', $params ); |
|
| 130 | + |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | 133 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * @param string $plugin_name The name of this plugin. |
| 49 | 49 | * @param string $version The version of this plugin. |
| 50 | 50 | */ |
| 51 | - public function __construct( $plugin_name, $version ) { |
|
| 51 | + public function __construct($plugin_name, $version) { |
|
| 52 | 52 | |
| 53 | 53 | $this->plugin_name = $plugin_name; |
| 54 | 54 | $this->version = $version; |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | * class. |
| 75 | 75 | */ |
| 76 | 76 | |
| 77 | - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-admin.css', array(), $this->version, 'all' ); |
|
| 77 | + wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__).'css/wordlift-admin.css', array(), $this->version, 'all'); |
|
| 78 | 78 | |
| 79 | 79 | } |
| 80 | 80 | |
@@ -98,35 +98,35 @@ discard block |
||
| 98 | 98 | */ |
| 99 | 99 | |
| 100 | 100 | // Enqueue our script. |
| 101 | - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wordlift-admin.bundle.js', array( 'jquery' ), $this->version, false ); |
|
| 101 | + wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__).'js/wordlift-admin.bundle.js', array('jquery'), $this->version, false); |
|
| 102 | 102 | |
| 103 | 103 | // Set the basic params. |
| 104 | 104 | $params = array( |
| 105 | 105 | // @todo scripts in admin should use wp.post. |
| 106 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 106 | + 'ajax_url' => admin_url('admin-ajax.php'), |
|
| 107 | 107 | // @todo remove specific actions from settings. |
| 108 | 108 | 'action' => 'entity_by_title', |
| 109 | 109 | 'language' => Wordlift_Configuration_Service::get_instance()->get_language_code(), |
| 110 | 110 | 'l10n' => array( |
| 111 | - 'You already published an entity with the same name' => __( 'You already published an entity with the same name: ', 'wordlift' ), |
|
| 111 | + 'You already published an entity with the same name' => __('You already published an entity with the same name: ', 'wordlift'), |
|
| 112 | 112 | ), |
| 113 | 113 | ); |
| 114 | 114 | |
| 115 | 115 | // Set post-related values if there's a current post. |
| 116 | - if ( null !== $post = $entity_being_edited = get_post() ) { |
|
| 116 | + if (null !== $post = $entity_being_edited = get_post()) { |
|
| 117 | 117 | |
| 118 | 118 | $params['post_id'] = $entity_being_edited->ID; |
| 119 | - $params['entityBeingEdited'] = isset( $entity_being_edited->post_type ) && $entity_being_edited->post_type == Wordlift_Entity_Service::TYPE_NAME && is_numeric( get_the_ID() ); |
|
| 119 | + $params['entityBeingEdited'] = isset($entity_being_edited->post_type) && $entity_being_edited->post_type == Wordlift_Entity_Service::TYPE_NAME && is_numeric(get_the_ID()); |
|
| 120 | 120 | // We add the `itemId` here to give a chance to the analysis to use it in order to tell WLS to exclude it |
| 121 | 121 | // from the results, since we don't want the current entity to be discovered by the analysis. |
| 122 | 122 | // |
| 123 | 123 | // See https://github.com/insideout10/wordlift-plugin/issues/345 |
| 124 | - $params['itemId'] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_being_edited->ID ); |
|
| 124 | + $params['itemId'] = Wordlift_Entity_Service::get_instance()->get_uri($entity_being_edited->ID); |
|
| 125 | 125 | |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | // Finally output the params as `wlSettings` for JavaScript code. |
| 129 | - wp_localize_script( $this->plugin_name, 'wlSettings', $params ); |
|
| 129 | + wp_localize_script($this->plugin_name, 'wlSettings', $params); |
|
| 130 | 130 | |
| 131 | 131 | } |
| 132 | 132 | |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | * |
| 207 | 207 | * @since 3.4.0 |
| 208 | 208 | * |
| 209 | - * @return int Average entities rating. |
|
| 209 | + * @return double Average entities rating. |
|
| 210 | 210 | */ |
| 211 | 211 | private function average_entities_rating() { |
| 212 | 212 | |
@@ -265,6 +265,9 @@ discard block |
||
| 265 | 265 | return (int) false; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | + /** |
|
| 269 | + * @param string $query |
|
| 270 | + */ |
|
| 268 | 271 | private function rl_sparql_select( $query ) { |
| 269 | 272 | |
| 270 | 273 | // Prepare the SPARQL statement by prepending the default namespaces. |
@@ -277,6 +280,9 @@ discard block |
||
| 277 | 280 | return wp_remote_get( $url, $args ); |
| 278 | 281 | } |
| 279 | 282 | |
| 283 | + /** |
|
| 284 | + * @param string $param |
|
| 285 | + */ |
|
| 280 | 286 | private function render_stat_param( $param ) { |
| 281 | 287 | |
| 282 | 288 | return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
@@ -14,81 +14,81 @@ discard block |
||
| 14 | 14 | class Wordlift_Dashboard_Service { |
| 15 | 15 | |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Transient Name |
|
| 19 | - * |
|
| 20 | - * @since 3.4.0 |
|
| 21 | - */ |
|
| 22 | - const TRANSIENT_NAME = 'wl_dashboard_stats'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Transient Expiration (in seconds) |
|
| 26 | - * |
|
| 27 | - * @since 3.4.0 |
|
| 28 | - */ |
|
| 29 | - const TRANSIENT_EXPIRATION = 86400; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * A {@link Wordlift_Rating_Service} instance. |
|
| 33 | - * |
|
| 34 | - * @since 3.10.0 |
|
| 35 | - * @access private |
|
| 36 | - * @var \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 37 | - */ |
|
| 38 | - private $rating_service; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Create a Wordlift_Entity_List_Service. |
|
| 42 | - * |
|
| 43 | - * @since 3.4.0 |
|
| 44 | - * |
|
| 45 | - * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 46 | - */ |
|
| 47 | - public function __construct( $rating_service ) { |
|
| 48 | - |
|
| 49 | - $this->rating_service = $rating_service; |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Return stats layout |
|
| 55 | - * |
|
| 56 | - * @since 3.4.0 |
|
| 57 | - * |
|
| 58 | - * @return string Dashboard widget html markup |
|
| 59 | - */ |
|
| 60 | - public function dashboard_widget_callback( $post ) { |
|
| 61 | - |
|
| 62 | - $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 63 | - $enriched_posts_caption = sprintf( wp_kses( |
|
| 64 | - __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 65 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 66 | - $this->render_stat_param( 'annotated_posts_percentage' ), |
|
| 67 | - esc_url( admin_url( 'edit.php' ) ), |
|
| 68 | - $this->render_stat_param( 'annotated_posts' ), |
|
| 69 | - $this->render_stat_param( 'posts' ) |
|
| 70 | - ); |
|
| 71 | - |
|
| 72 | - $rating_title = __( 'average entity rating', 'wordlift' ); |
|
| 73 | - $rating_caption = sprintf( wp_kses( |
|
| 74 | - __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift' ), |
|
| 75 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 76 | - $this->render_stat_param( 'entities' ), |
|
| 77 | - esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 78 | - $this->render_stat_param( 'rating' ) |
|
| 79 | - ); |
|
| 80 | - |
|
| 81 | - $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 82 | - $graph_caption = sprintf( wp_kses( |
|
| 83 | - __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 84 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 85 | - $this->render_stat_param( 'wikidata' ), |
|
| 86 | - esc_url( 'https://www.wikidata.org/' ) |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - $triples_label = __( 'triples', 'wordlift' ); |
|
| 90 | - |
|
| 91 | - echo <<<EOF |
|
| 17 | + /** |
|
| 18 | + * Transient Name |
|
| 19 | + * |
|
| 20 | + * @since 3.4.0 |
|
| 21 | + */ |
|
| 22 | + const TRANSIENT_NAME = 'wl_dashboard_stats'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Transient Expiration (in seconds) |
|
| 26 | + * |
|
| 27 | + * @since 3.4.0 |
|
| 28 | + */ |
|
| 29 | + const TRANSIENT_EXPIRATION = 86400; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * A {@link Wordlift_Rating_Service} instance. |
|
| 33 | + * |
|
| 34 | + * @since 3.10.0 |
|
| 35 | + * @access private |
|
| 36 | + * @var \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 37 | + */ |
|
| 38 | + private $rating_service; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Create a Wordlift_Entity_List_Service. |
|
| 42 | + * |
|
| 43 | + * @since 3.4.0 |
|
| 44 | + * |
|
| 45 | + * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
|
| 46 | + */ |
|
| 47 | + public function __construct( $rating_service ) { |
|
| 48 | + |
|
| 49 | + $this->rating_service = $rating_service; |
|
| 50 | + |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Return stats layout |
|
| 55 | + * |
|
| 56 | + * @since 3.4.0 |
|
| 57 | + * |
|
| 58 | + * @return string Dashboard widget html markup |
|
| 59 | + */ |
|
| 60 | + public function dashboard_widget_callback( $post ) { |
|
| 61 | + |
|
| 62 | + $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 63 | + $enriched_posts_caption = sprintf( wp_kses( |
|
| 64 | + __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 65 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 66 | + $this->render_stat_param( 'annotated_posts_percentage' ), |
|
| 67 | + esc_url( admin_url( 'edit.php' ) ), |
|
| 68 | + $this->render_stat_param( 'annotated_posts' ), |
|
| 69 | + $this->render_stat_param( 'posts' ) |
|
| 70 | + ); |
|
| 71 | + |
|
| 72 | + $rating_title = __( 'average entity rating', 'wordlift' ); |
|
| 73 | + $rating_caption = sprintf( wp_kses( |
|
| 74 | + __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift' ), |
|
| 75 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 76 | + $this->render_stat_param( 'entities' ), |
|
| 77 | + esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 78 | + $this->render_stat_param( 'rating' ) |
|
| 79 | + ); |
|
| 80 | + |
|
| 81 | + $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 82 | + $graph_caption = sprintf( wp_kses( |
|
| 83 | + __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 84 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 85 | + $this->render_stat_param( 'wikidata' ), |
|
| 86 | + esc_url( 'https://www.wikidata.org/' ) |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + $triples_label = __( 'triples', 'wordlift' ); |
|
| 90 | + |
|
| 91 | + echo <<<EOF |
|
| 92 | 92 | <div id="wl-dashboard-widget-inner-wrapper"> |
| 93 | 93 | <div class="wl-stat-card"> |
| 94 | 94 | <div class="wl-stat-graph-wrapper"> |
@@ -117,169 +117,169 @@ discard block |
||
| 117 | 117 | </div> |
| 118 | 118 | EOF; |
| 119 | 119 | |
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Return stats |
|
| 124 | - * |
|
| 125 | - * @since 3.4.0 |
|
| 126 | - * |
|
| 127 | - * @return string markup |
|
| 128 | - */ |
|
| 129 | - public function add_dashboard_widgets() { |
|
| 130 | - wp_add_dashboard_widget( 'wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 131 | - $this, |
|
| 132 | - 'dashboard_widget_callback', |
|
| 133 | - ) ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Return stats |
|
| 138 | - * @uses https://codex.wordpress.org/Function_Reference/set_transient |
|
| 139 | - * |
|
| 140 | - * @since 3.4.0 |
|
| 141 | - * |
|
| 142 | - * @return string JSON obj with all available stats. |
|
| 143 | - */ |
|
| 144 | - public function ajax_get_stats() { |
|
| 145 | - |
|
| 146 | - // If needed, the transient is force to reloaed |
|
| 147 | - if ( isset( $_GET['force_reload'] ) ) { |
|
| 148 | - delete_transient( self::TRANSIENT_NAME ); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // Try to retrieve the transient |
|
| 152 | - $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 153 | - |
|
| 154 | - if ( ! $stats ) { |
|
| 155 | - // Calculate stats |
|
| 156 | - $stats = array( |
|
| 157 | - 'entities' => $this->count_entities(), |
|
| 158 | - 'posts' => $this->count_posts(), |
|
| 159 | - 'annotated_posts' => $this->count_annotated_posts(), |
|
| 160 | - 'triples' => $this->count_triples() ?: '-', |
|
| 161 | - 'rating' => $this->average_entities_rating(), |
|
| 162 | - ); |
|
| 163 | - // Cache stats results trough transient |
|
| 164 | - set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 165 | - } |
|
| 166 | - // Return stats as json object |
|
| 167 | - wl_core_send_json( $stats ); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Calculate total number of published posts |
|
| 172 | - * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 173 | - * @since 3.4.0 |
|
| 174 | - * |
|
| 175 | - * @return int Total number of published posts. |
|
| 176 | - */ |
|
| 177 | - public function count_posts() { |
|
| 178 | - |
|
| 179 | - return (int) wp_count_posts()->publish; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Calculate total number of annotated published posts |
|
| 184 | - * @since 3.4.0 |
|
| 185 | - * |
|
| 186 | - * @return int Total number of annotated published posts. |
|
| 187 | - */ |
|
| 188 | - public function count_annotated_posts() { |
|
| 189 | - |
|
| 190 | - // Prepare interaction with db |
|
| 191 | - global $wpdb; |
|
| 192 | - // Retrieve Wordlift relation instances table name |
|
| 193 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 194 | - // Calculate sql statement |
|
| 195 | - $sql_statement = <<<EOF |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Return stats |
|
| 124 | + * |
|
| 125 | + * @since 3.4.0 |
|
| 126 | + * |
|
| 127 | + * @return string markup |
|
| 128 | + */ |
|
| 129 | + public function add_dashboard_widgets() { |
|
| 130 | + wp_add_dashboard_widget( 'wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 131 | + $this, |
|
| 132 | + 'dashboard_widget_callback', |
|
| 133 | + ) ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Return stats |
|
| 138 | + * @uses https://codex.wordpress.org/Function_Reference/set_transient |
|
| 139 | + * |
|
| 140 | + * @since 3.4.0 |
|
| 141 | + * |
|
| 142 | + * @return string JSON obj with all available stats. |
|
| 143 | + */ |
|
| 144 | + public function ajax_get_stats() { |
|
| 145 | + |
|
| 146 | + // If needed, the transient is force to reloaed |
|
| 147 | + if ( isset( $_GET['force_reload'] ) ) { |
|
| 148 | + delete_transient( self::TRANSIENT_NAME ); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // Try to retrieve the transient |
|
| 152 | + $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 153 | + |
|
| 154 | + if ( ! $stats ) { |
|
| 155 | + // Calculate stats |
|
| 156 | + $stats = array( |
|
| 157 | + 'entities' => $this->count_entities(), |
|
| 158 | + 'posts' => $this->count_posts(), |
|
| 159 | + 'annotated_posts' => $this->count_annotated_posts(), |
|
| 160 | + 'triples' => $this->count_triples() ?: '-', |
|
| 161 | + 'rating' => $this->average_entities_rating(), |
|
| 162 | + ); |
|
| 163 | + // Cache stats results trough transient |
|
| 164 | + set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 165 | + } |
|
| 166 | + // Return stats as json object |
|
| 167 | + wl_core_send_json( $stats ); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Calculate total number of published posts |
|
| 172 | + * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 173 | + * @since 3.4.0 |
|
| 174 | + * |
|
| 175 | + * @return int Total number of published posts. |
|
| 176 | + */ |
|
| 177 | + public function count_posts() { |
|
| 178 | + |
|
| 179 | + return (int) wp_count_posts()->publish; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Calculate total number of annotated published posts |
|
| 184 | + * @since 3.4.0 |
|
| 185 | + * |
|
| 186 | + * @return int Total number of annotated published posts. |
|
| 187 | + */ |
|
| 188 | + public function count_annotated_posts() { |
|
| 189 | + |
|
| 190 | + // Prepare interaction with db |
|
| 191 | + global $wpdb; |
|
| 192 | + // Retrieve Wordlift relation instances table name |
|
| 193 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 194 | + // Calculate sql statement |
|
| 195 | + $sql_statement = <<<EOF |
|
| 196 | 196 | SELECT COUNT(distinct(p.id)) FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.subject_id AND p.post_type = 'post' AND p.post_status = 'publish'; |
| 197 | 197 | EOF; |
| 198 | 198 | |
| 199 | - // Perform the query |
|
| 200 | - return (int) $wpdb->get_var( $sql_statement ); |
|
| 201 | - |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Calculate the average entities rating. |
|
| 206 | - * |
|
| 207 | - * @since 3.4.0 |
|
| 208 | - * |
|
| 209 | - * @return int Average entities rating. |
|
| 210 | - */ |
|
| 211 | - private function average_entities_rating() { |
|
| 212 | - |
|
| 213 | - // Prepare interaction with db |
|
| 214 | - global $wpdb; |
|
| 215 | - $query = $wpdb->prepare( |
|
| 216 | - "SELECT AVG(meta_value) FROM $wpdb->postmeta where meta_key = %s", |
|
| 217 | - Wordlift_Rating_Service::RATING_RAW_SCORE_META_KEY |
|
| 218 | - ); |
|
| 219 | - |
|
| 220 | - // Perform the query. |
|
| 221 | - return $this->rating_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Calculate total number of published entities. |
|
| 226 | - * |
|
| 227 | - * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 228 | - * |
|
| 229 | - * @since 3.4.0 |
|
| 230 | - * |
|
| 231 | - * @return int Total number of posts. |
|
| 232 | - */ |
|
| 233 | - private function count_entities() { |
|
| 234 | - |
|
| 235 | - return (int) wp_count_posts( Wordlift_Entity_Service::TYPE_NAME )->publish; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Calculate total number of published rdf triples |
|
| 240 | - * @since 3.4.0 |
|
| 241 | - * |
|
| 242 | - * @return int Total number of triples. |
|
| 243 | - */ |
|
| 244 | - public function count_triples() { |
|
| 245 | - |
|
| 246 | - // Set the SPARQL query. |
|
| 247 | - $sparql = 'SELECT (COUNT(*) AS ?no) { ?s ?p ?o }'; |
|
| 248 | - // Send the request. |
|
| 249 | - $response = $this->rl_sparql_select( $sparql ); |
|
| 250 | - |
|
| 251 | - // Return the error in case of failure. |
|
| 252 | - if ( is_wp_error( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 253 | - return (int) false; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - // Get the body. |
|
| 257 | - $body = $response['body']; |
|
| 258 | - // Get the values. |
|
| 259 | - $matches = array(); |
|
| 260 | - if ( 1 === preg_match( '/(\d+)/im', $body, $matches ) && 2 === count( $matches ) ) { |
|
| 261 | - // Return the counts. |
|
| 262 | - return (int) $matches[1]; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - return (int) false; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - private function rl_sparql_select( $query ) { |
|
| 269 | - |
|
| 270 | - // Prepare the SPARQL statement by prepending the default namespaces. |
|
| 271 | - $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 272 | - // Get the SPARQL SELECT URL. |
|
| 273 | - $url = wl_configuration_get_query_select_url( 'csv' ) . urlencode( $sparql ); |
|
| 274 | - // Prepare the request. |
|
| 275 | - $args = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 276 | - |
|
| 277 | - return wp_remote_get( $url, $args ); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - private function render_stat_param( $param ) { |
|
| 281 | - |
|
| 282 | - return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
|
| 283 | - } |
|
| 199 | + // Perform the query |
|
| 200 | + return (int) $wpdb->get_var( $sql_statement ); |
|
| 201 | + |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Calculate the average entities rating. |
|
| 206 | + * |
|
| 207 | + * @since 3.4.0 |
|
| 208 | + * |
|
| 209 | + * @return int Average entities rating. |
|
| 210 | + */ |
|
| 211 | + private function average_entities_rating() { |
|
| 212 | + |
|
| 213 | + // Prepare interaction with db |
|
| 214 | + global $wpdb; |
|
| 215 | + $query = $wpdb->prepare( |
|
| 216 | + "SELECT AVG(meta_value) FROM $wpdb->postmeta where meta_key = %s", |
|
| 217 | + Wordlift_Rating_Service::RATING_RAW_SCORE_META_KEY |
|
| 218 | + ); |
|
| 219 | + |
|
| 220 | + // Perform the query. |
|
| 221 | + return $this->rating_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Calculate total number of published entities. |
|
| 226 | + * |
|
| 227 | + * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 228 | + * |
|
| 229 | + * @since 3.4.0 |
|
| 230 | + * |
|
| 231 | + * @return int Total number of posts. |
|
| 232 | + */ |
|
| 233 | + private function count_entities() { |
|
| 234 | + |
|
| 235 | + return (int) wp_count_posts( Wordlift_Entity_Service::TYPE_NAME )->publish; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Calculate total number of published rdf triples |
|
| 240 | + * @since 3.4.0 |
|
| 241 | + * |
|
| 242 | + * @return int Total number of triples. |
|
| 243 | + */ |
|
| 244 | + public function count_triples() { |
|
| 245 | + |
|
| 246 | + // Set the SPARQL query. |
|
| 247 | + $sparql = 'SELECT (COUNT(*) AS ?no) { ?s ?p ?o }'; |
|
| 248 | + // Send the request. |
|
| 249 | + $response = $this->rl_sparql_select( $sparql ); |
|
| 250 | + |
|
| 251 | + // Return the error in case of failure. |
|
| 252 | + if ( is_wp_error( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 253 | + return (int) false; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + // Get the body. |
|
| 257 | + $body = $response['body']; |
|
| 258 | + // Get the values. |
|
| 259 | + $matches = array(); |
|
| 260 | + if ( 1 === preg_match( '/(\d+)/im', $body, $matches ) && 2 === count( $matches ) ) { |
|
| 261 | + // Return the counts. |
|
| 262 | + return (int) $matches[1]; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + return (int) false; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + private function rl_sparql_select( $query ) { |
|
| 269 | + |
|
| 270 | + // Prepare the SPARQL statement by prepending the default namespaces. |
|
| 271 | + $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 272 | + // Get the SPARQL SELECT URL. |
|
| 273 | + $url = wl_configuration_get_query_select_url( 'csv' ) . urlencode( $sparql ); |
|
| 274 | + // Prepare the request. |
|
| 275 | + $args = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 276 | + |
|
| 277 | + return wp_remote_get( $url, $args ); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + private function render_stat_param( $param ) { |
|
| 281 | + |
|
| 282 | + return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | 285 | } |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | * |
| 45 | 45 | * @param \Wordlift_Rating_Service $rating_service A {@link Wordlift_Rating_Service} instance. |
| 46 | 46 | */ |
| 47 | - public function __construct( $rating_service ) { |
|
| 47 | + public function __construct($rating_service) { |
|
| 48 | 48 | |
| 49 | 49 | $this->rating_service = $rating_service; |
| 50 | 50 | |
@@ -57,36 +57,36 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return string Dashboard widget html markup |
| 59 | 59 | */ |
| 60 | - public function dashboard_widget_callback( $post ) { |
|
| 61 | - |
|
| 62 | - $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 63 | - $enriched_posts_caption = sprintf( wp_kses( |
|
| 64 | - __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 65 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 66 | - $this->render_stat_param( 'annotated_posts_percentage' ), |
|
| 67 | - esc_url( admin_url( 'edit.php' ) ), |
|
| 68 | - $this->render_stat_param( 'annotated_posts' ), |
|
| 69 | - $this->render_stat_param( 'posts' ) |
|
| 60 | + public function dashboard_widget_callback($post) { |
|
| 61 | + |
|
| 62 | + $enriched_posts_title = __('enriched posts', 'wordlift'); |
|
| 63 | + $enriched_posts_caption = sprintf(wp_kses( |
|
| 64 | + __('%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift'), |
|
| 65 | + array('a' => array('href' => array())) ), |
|
| 66 | + $this->render_stat_param('annotated_posts_percentage'), |
|
| 67 | + esc_url(admin_url('edit.php')), |
|
| 68 | + $this->render_stat_param('annotated_posts'), |
|
| 69 | + $this->render_stat_param('posts') |
|
| 70 | 70 | ); |
| 71 | 71 | |
| 72 | - $rating_title = __( 'average entity rating', 'wordlift' ); |
|
| 73 | - $rating_caption = sprintf( wp_kses( |
|
| 74 | - __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift' ), |
|
| 75 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 76 | - $this->render_stat_param( 'entities' ), |
|
| 77 | - esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 78 | - $this->render_stat_param( 'rating' ) |
|
| 72 | + $rating_title = __('average entity rating', 'wordlift'); |
|
| 73 | + $rating_caption = sprintf(wp_kses( |
|
| 74 | + __('You have %1$s entities in your <a href="%2$s">vocabulary</a> with an average rating of %3$s.', 'wordlift'), |
|
| 75 | + array('a' => array('href' => array())) ), |
|
| 76 | + $this->render_stat_param('entities'), |
|
| 77 | + esc_url(admin_url('edit.php?post_type=entity')), |
|
| 78 | + $this->render_stat_param('rating') |
|
| 79 | 79 | ); |
| 80 | 80 | |
| 81 | - $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 82 | - $graph_caption = sprintf( wp_kses( |
|
| 83 | - __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 84 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 85 | - $this->render_stat_param( 'wikidata' ), |
|
| 86 | - esc_url( 'https://www.wikidata.org/' ) |
|
| 81 | + $graph_title = __('triples in your graph', 'wordlift'); |
|
| 82 | + $graph_caption = sprintf(wp_kses( |
|
| 83 | + __('Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift'), |
|
| 84 | + array('a' => array('href' => array())) ), |
|
| 85 | + $this->render_stat_param('wikidata'), |
|
| 86 | + esc_url('https://www.wikidata.org/') |
|
| 87 | 87 | ); |
| 88 | 88 | |
| 89 | - $triples_label = __( 'triples', 'wordlift' ); |
|
| 89 | + $triples_label = __('triples', 'wordlift'); |
|
| 90 | 90 | |
| 91 | 91 | echo <<<EOF |
| 92 | 92 | <div id="wl-dashboard-widget-inner-wrapper"> |
@@ -127,10 +127,10 @@ discard block |
||
| 127 | 127 | * @return string markup |
| 128 | 128 | */ |
| 129 | 129 | public function add_dashboard_widgets() { |
| 130 | - wp_add_dashboard_widget( 'wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 130 | + wp_add_dashboard_widget('wordlift-dashboard-widget', 'WordLift Dashboard', array( |
|
| 131 | 131 | $this, |
| 132 | 132 | 'dashboard_widget_callback', |
| 133 | - ) ); |
|
| 133 | + )); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -144,14 +144,14 @@ discard block |
||
| 144 | 144 | public function ajax_get_stats() { |
| 145 | 145 | |
| 146 | 146 | // If needed, the transient is force to reloaed |
| 147 | - if ( isset( $_GET['force_reload'] ) ) { |
|
| 148 | - delete_transient( self::TRANSIENT_NAME ); |
|
| 147 | + if (isset($_GET['force_reload'])) { |
|
| 148 | + delete_transient(self::TRANSIENT_NAME); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | // Try to retrieve the transient |
| 152 | - $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 152 | + $stats = get_transient(self::TRANSIENT_NAME); |
|
| 153 | 153 | |
| 154 | - if ( ! $stats ) { |
|
| 154 | + if ( ! $stats) { |
|
| 155 | 155 | // Calculate stats |
| 156 | 156 | $stats = array( |
| 157 | 157 | 'entities' => $this->count_entities(), |
@@ -161,10 +161,10 @@ discard block |
||
| 161 | 161 | 'rating' => $this->average_entities_rating(), |
| 162 | 162 | ); |
| 163 | 163 | // Cache stats results trough transient |
| 164 | - set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 164 | + set_transient(self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION); |
|
| 165 | 165 | } |
| 166 | 166 | // Return stats as json object |
| 167 | - wl_core_send_json( $stats ); |
|
| 167 | + wl_core_send_json($stats); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | EOF; |
| 198 | 198 | |
| 199 | 199 | // Perform the query |
| 200 | - return (int) $wpdb->get_var( $sql_statement ); |
|
| 200 | + return (int) $wpdb->get_var($sql_statement); |
|
| 201 | 201 | |
| 202 | 202 | } |
| 203 | 203 | |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | ); |
| 219 | 219 | |
| 220 | 220 | // Perform the query. |
| 221 | - return $this->rating_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 221 | + return $this->rating_service->convert_raw_score_to_percentage($wpdb->get_var($query)); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | */ |
| 233 | 233 | private function count_entities() { |
| 234 | 234 | |
| 235 | - return (int) wp_count_posts( Wordlift_Entity_Service::TYPE_NAME )->publish; |
|
| 235 | + return (int) wp_count_posts(Wordlift_Entity_Service::TYPE_NAME)->publish; |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
@@ -246,10 +246,10 @@ discard block |
||
| 246 | 246 | // Set the SPARQL query. |
| 247 | 247 | $sparql = 'SELECT (COUNT(*) AS ?no) { ?s ?p ?o }'; |
| 248 | 248 | // Send the request. |
| 249 | - $response = $this->rl_sparql_select( $sparql ); |
|
| 249 | + $response = $this->rl_sparql_select($sparql); |
|
| 250 | 250 | |
| 251 | 251 | // Return the error in case of failure. |
| 252 | - if ( is_wp_error( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 252 | + if (is_wp_error($response) || 200 !== (int) $response['response']['code']) { |
|
| 253 | 253 | return (int) false; |
| 254 | 254 | } |
| 255 | 255 | |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | $body = $response['body']; |
| 258 | 258 | // Get the values. |
| 259 | 259 | $matches = array(); |
| 260 | - if ( 1 === preg_match( '/(\d+)/im', $body, $matches ) && 2 === count( $matches ) ) { |
|
| 260 | + if (1 === preg_match('/(\d+)/im', $body, $matches) && 2 === count($matches)) { |
|
| 261 | 261 | // Return the counts. |
| 262 | 262 | return (int) $matches[1]; |
| 263 | 263 | } |
@@ -265,21 +265,21 @@ discard block |
||
| 265 | 265 | return (int) false; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | - private function rl_sparql_select( $query ) { |
|
| 268 | + private function rl_sparql_select($query) { |
|
| 269 | 269 | |
| 270 | 270 | // Prepare the SPARQL statement by prepending the default namespaces. |
| 271 | - $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 271 | + $sparql = rl_sparql_prefixes()."\n".$query; |
|
| 272 | 272 | // Get the SPARQL SELECT URL. |
| 273 | - $url = wl_configuration_get_query_select_url( 'csv' ) . urlencode( $sparql ); |
|
| 273 | + $url = wl_configuration_get_query_select_url('csv').urlencode($sparql); |
|
| 274 | 274 | // Prepare the request. |
| 275 | - $args = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 275 | + $args = unserialize(WL_REDLINK_API_HTTP_OPTIONS); |
|
| 276 | 276 | |
| 277 | - return wp_remote_get( $url, $args ); |
|
| 277 | + return wp_remote_get($url, $args); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - private function render_stat_param( $param ) { |
|
| 280 | + private function render_stat_param($param) { |
|
| 281 | 281 | |
| 282 | - return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
|
| 282 | + return '<span id="wl-dashboard-widget-'.$param.'" class="wl-stat-value"></span>'; |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | } |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | * |
| 186 | 186 | * @param int $uri An uri. |
| 187 | 187 | * |
| 188 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 188 | + * @return boolean if the uri internal to the current dataset otherwise false. |
|
| 189 | 189 | */ |
| 190 | 190 | public function is_internal_uri( $uri ) { |
| 191 | 191 | |
@@ -424,7 +424,7 @@ discard block |
||
| 424 | 424 | * @param null $logo The entity logo id (or NULL if none). |
| 425 | 425 | * @param string $status The post status, by default 'publish'. |
| 426 | 426 | * |
| 427 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 427 | + * @return integer The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 428 | 428 | */ |
| 429 | 429 | public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
| 430 | 430 | |
@@ -7,465 +7,465 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Entity_Service { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The Log service. |
|
| 12 | - * |
|
| 13 | - * @since 3.2.0 |
|
| 14 | - * @access private |
|
| 15 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | - */ |
|
| 17 | - private $log; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The UI service. |
|
| 21 | - * |
|
| 22 | - * @since 3.2.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | - */ |
|
| 26 | - private $ui_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The entity post type name. |
|
| 30 | - * |
|
| 31 | - * @since 3.1.0 |
|
| 32 | - */ |
|
| 33 | - const TYPE_NAME = 'entity'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The alternative label meta key. |
|
| 37 | - * |
|
| 38 | - * @since 3.2.0 |
|
| 39 | - */ |
|
| 40 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The alternative label input template. |
|
| 44 | - * |
|
| 45 | - * @since 3.2.0 |
|
| 46 | - */ |
|
| 47 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 48 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 10 | + /** |
|
| 11 | + * The Log service. |
|
| 12 | + * |
|
| 13 | + * @since 3.2.0 |
|
| 14 | + * @access private |
|
| 15 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | + */ |
|
| 17 | + private $log; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The UI service. |
|
| 21 | + * |
|
| 22 | + * @since 3.2.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | + */ |
|
| 26 | + private $ui_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The entity post type name. |
|
| 30 | + * |
|
| 31 | + * @since 3.1.0 |
|
| 32 | + */ |
|
| 33 | + const TYPE_NAME = 'entity'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The alternative label meta key. |
|
| 37 | + * |
|
| 38 | + * @since 3.2.0 |
|
| 39 | + */ |
|
| 40 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The alternative label input template. |
|
| 44 | + * |
|
| 45 | + * @since 3.2.0 |
|
| 46 | + */ |
|
| 47 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 48 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 49 | 49 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 50 | 50 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 51 | 51 | <button class="button wl-delete-button">%s</button> |
| 52 | 52 | </div>'; |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * A singleton instance of the Entity service. |
|
| 56 | - * |
|
| 57 | - * @since 3.2.0 |
|
| 58 | - * @access private |
|
| 59 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 60 | - */ |
|
| 61 | - private static $instance; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Create a Wordlift_Entity_Service instance. |
|
| 65 | - * |
|
| 66 | - * @since 3.2.0 |
|
| 67 | - * |
|
| 68 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 69 | - */ |
|
| 70 | - public function __construct( $ui_service ) { |
|
| 71 | - |
|
| 72 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 73 | - |
|
| 74 | - // Set the UI service. |
|
| 75 | - $this->ui_service = $ui_service; |
|
| 76 | - |
|
| 77 | - // Set the singleton instance. |
|
| 78 | - self::$instance = $this; |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Get the singleton instance of the Entity service. |
|
| 84 | - * |
|
| 85 | - * @since 3.2.0 |
|
| 86 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 87 | - */ |
|
| 88 | - public static function get_instance() { |
|
| 89 | - |
|
| 90 | - return self::$instance; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Determines whether a post is an entity or not. |
|
| 95 | - * |
|
| 96 | - * @since 3.1.0 |
|
| 97 | - * |
|
| 98 | - * @param int $post_id A post id. |
|
| 99 | - * |
|
| 100 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 101 | - */ |
|
| 102 | - public function is_entity( $post_id ) { |
|
| 103 | - |
|
| 104 | - return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get the proper classification scope for a given entity post |
|
| 109 | - * |
|
| 110 | - * @since 3.5.0 |
|
| 111 | - * |
|
| 112 | - * @param integer $post_id An entity post id. |
|
| 113 | - * |
|
| 114 | - * @return string Returns an uri. |
|
| 115 | - */ |
|
| 116 | - public function get_classification_scope_for( $post_id ) { |
|
| 117 | - |
|
| 118 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 119 | - return null; |
|
| 120 | - } |
|
| 121 | - // Retrieve the entity type |
|
| 122 | - $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 124 | - // Retrieve classification boxes configuration |
|
| 125 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | - foreach ( $classification_boxes as $cb ) { |
|
| 127 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 128 | - return $cb['id']; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // or null |
|
| 133 | - return null; |
|
| 134 | - |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - public function is_used( $post_id ) { |
|
| 139 | - |
|
| 140 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 141 | - return null; |
|
| 142 | - } |
|
| 143 | - // Retrieve the post |
|
| 144 | - $entity = get_post( $post_id ); |
|
| 145 | - |
|
| 146 | - global $wpdb; |
|
| 147 | - // Retrieve Wordlift relation instances table name |
|
| 148 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 149 | - |
|
| 150 | - // Check is it's referenced / related to another post / entity |
|
| 151 | - $stmt = $wpdb->prepare( |
|
| 152 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 153 | - $entity->ID |
|
| 154 | - ); |
|
| 155 | - |
|
| 156 | - // Perform the query |
|
| 157 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 158 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 159 | - if ( 0 < $relation_instances ) { |
|
| 160 | - return true; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // Check if the entity uri is used as meta_value |
|
| 164 | - $stmt = $wpdb->prepare( |
|
| 165 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 166 | - $entity->ID, |
|
| 167 | - wl_get_entity_uri( $entity->ID ) |
|
| 168 | - ); |
|
| 169 | - // Perform the query |
|
| 170 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 171 | - |
|
| 172 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 173 | - if ( 0 < $meta_instances ) { |
|
| 174 | - return true; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - // If we are here, it means the current entity is not used at the moment |
|
| 178 | - return false; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Determines whether a given uri is an internal uri or not. |
|
| 183 | - * |
|
| 184 | - * @since 3.3.2 |
|
| 185 | - * |
|
| 186 | - * @param int $uri An uri. |
|
| 187 | - * |
|
| 188 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 189 | - */ |
|
| 190 | - public function is_internal_uri( $uri ) { |
|
| 191 | - |
|
| 192 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 197 | - * |
|
| 198 | - * @since 3.2.0 |
|
| 199 | - * |
|
| 200 | - * @param string $uri The entity URI. |
|
| 201 | - * |
|
| 202 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 203 | - */ |
|
| 204 | - public function get_entity_post_by_uri( $uri ) { |
|
| 205 | - |
|
| 206 | - // Check if we've been provided with a value otherwise return null. |
|
| 207 | - if ( empty( $uri ) ) { |
|
| 208 | - return null; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $query_args = array( |
|
| 212 | - 'posts_per_page' => 1, |
|
| 213 | - 'post_status' => 'any', |
|
| 214 | - 'post_type' => self::TYPE_NAME, |
|
| 215 | - 'meta_query' => array( |
|
| 216 | - array( |
|
| 217 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 218 | - 'value' => $uri, |
|
| 219 | - 'compare' => '=', |
|
| 220 | - ), |
|
| 221 | - ), |
|
| 222 | - ); |
|
| 223 | - |
|
| 224 | - // Only if the current uri is not an internal uri |
|
| 225 | - // entity search is performed also looking at sameAs values |
|
| 226 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 227 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 228 | - |
|
| 229 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 230 | - $query_args['meta_query'][] = array( |
|
| 231 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 232 | - 'value' => $uri, |
|
| 233 | - 'compare' => '=', |
|
| 234 | - ); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - $query = new WP_Query( $query_args ); |
|
| 238 | - |
|
| 239 | - // Get the matching entity posts. |
|
| 240 | - $posts = $query->get_posts(); |
|
| 241 | - |
|
| 242 | - // Return null if no post is found. |
|
| 243 | - if ( 0 === count( $posts ) ) { |
|
| 244 | - return null; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - // Return the found post. |
|
| 248 | - return $posts[0]; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 253 | - * we check that the post we're saving is the current post. |
|
| 254 | - * |
|
| 255 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 256 | - * |
|
| 257 | - * @since 3.2.0 |
|
| 258 | - * |
|
| 259 | - * @param int $post_id Post ID. |
|
| 260 | - * @param WP_Post $post Post object. |
|
| 261 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 262 | - */ |
|
| 263 | - public function save_post( $post_id, $post, $update ) { |
|
| 264 | - |
|
| 265 | - // We're setting the alternative label that have been provided via the UI |
|
| 266 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 267 | - // programmatically by some other function: we need to check therefore if |
|
| 268 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 269 | - // |
|
| 270 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 271 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 272 | - return; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - // Get the alt labels from the request (or empty array). |
|
| 276 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 277 | - |
|
| 278 | - // Set the alternative labels. |
|
| 279 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 280 | - |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Set the alternative labels. |
|
| 285 | - * |
|
| 286 | - * @since 3.2.0 |
|
| 287 | - * |
|
| 288 | - * @param int $post_id The post id. |
|
| 289 | - * @param array $alt_labels An array of labels. |
|
| 290 | - */ |
|
| 291 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 292 | - |
|
| 293 | - // Force $alt_labels to be an array |
|
| 294 | - if ( ! is_array( $alt_labels ) ) { |
|
| 295 | - $alt_labels = array( $alt_labels ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 299 | - |
|
| 300 | - // Delete all the existing alternate labels. |
|
| 301 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 302 | - |
|
| 303 | - // Set the alternative labels. |
|
| 304 | - foreach ( $alt_labels as $alt_label ) { |
|
| 305 | - if ( ! empty( $alt_label ) ) { |
|
| 306 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Retrieve the alternate labels. |
|
| 314 | - * |
|
| 315 | - * @since 3.2.0 |
|
| 316 | - * |
|
| 317 | - * @param int $post_id Post id. |
|
| 318 | - * |
|
| 319 | - * @return mixed An array of alternative labels. |
|
| 320 | - */ |
|
| 321 | - public function get_alternative_labels( $post_id ) { |
|
| 322 | - |
|
| 323 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 328 | - * |
|
| 329 | - * @since 3.2.0 |
|
| 330 | - * |
|
| 331 | - * @param WP_Post $post Post object. |
|
| 332 | - */ |
|
| 333 | - public function edit_form_before_permalink( $post ) { |
|
| 334 | - |
|
| 335 | - // If it's not an entity, return. |
|
| 336 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 337 | - return; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - // Print the input template. |
|
| 341 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 342 | - |
|
| 343 | - // Print all the currently set alternative labels. |
|
| 344 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 345 | - |
|
| 346 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 347 | - |
|
| 348 | - }; |
|
| 349 | - |
|
| 350 | - // Print the button. |
|
| 351 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 352 | - |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Get the URI for the entity with the specified post id. |
|
| 357 | - * |
|
| 358 | - * @since 3.6.0 |
|
| 359 | - * |
|
| 360 | - * @param int $post_id The entity post id. |
|
| 361 | - * |
|
| 362 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 363 | - */ |
|
| 364 | - public function get_uri( $post_id ) { |
|
| 365 | - |
|
| 366 | - // If a null is given, nothing to do |
|
| 367 | - if ( null == $post_id ) { |
|
| 368 | - return null; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 372 | - |
|
| 373 | - // If the dataset uri is not properly configured, null is returned |
|
| 374 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 375 | - return null; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - // Set the URI if it isn't set yet. |
|
| 379 | - $post_status = get_post_status( $post_id ); |
|
| 380 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 381 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 382 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - return $uri; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Get the alternative label input HTML code. |
|
| 391 | - * |
|
| 392 | - * @since 3.2.0 |
|
| 393 | - * |
|
| 394 | - * @param string $value The input value. |
|
| 395 | - * |
|
| 396 | - * @return string The input HTML code. |
|
| 397 | - */ |
|
| 398 | - private function get_alternative_label_input( $value = '' ) { |
|
| 399 | - |
|
| 400 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Get the number of entity posts published in this blog. |
|
| 405 | - * |
|
| 406 | - * @since 3.6.0 |
|
| 407 | - * |
|
| 408 | - * @return int The number of published entity posts. |
|
| 409 | - */ |
|
| 410 | - public function count() { |
|
| 411 | - |
|
| 412 | - $count = wp_count_posts( self::TYPE_NAME ); |
|
| 413 | - |
|
| 414 | - return $count->publish; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * Create a new entity. |
|
| 419 | - * |
|
| 420 | - * @since 3.9.0 |
|
| 421 | - * |
|
| 422 | - * @param string $name The entity name. |
|
| 423 | - * @param string $type_uri The entity's type URI. |
|
| 424 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 425 | - * @param string $status The post status, by default 'publish'. |
|
| 426 | - * |
|
| 427 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 428 | - */ |
|
| 429 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 430 | - |
|
| 431 | - // Create an entity for the publisher. |
|
| 432 | - $post_id = wp_insert_post( array( |
|
| 433 | - 'post_type' => self::TYPE_NAME, |
|
| 434 | - 'post_title' => $name, |
|
| 435 | - 'post_status' => $status, |
|
| 436 | - 'post_content' => '', |
|
| 437 | - ) ); |
|
| 438 | - |
|
| 439 | - // Return the error if any. |
|
| 440 | - if ( is_wp_error( $post_id ) ) { |
|
| 441 | - return $post_id; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - // Set the entity logo. |
|
| 445 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 446 | - set_post_thumbnail( $post_id, $logo ); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - // Set the entity type. |
|
| 450 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 451 | - |
|
| 452 | - return $post_id; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Get the entities related to the one with the specified id. By default only |
|
| 457 | - * published entities will be returned. |
|
| 458 | - * |
|
| 459 | - * @since 3.10.0 |
|
| 460 | - * |
|
| 461 | - * @param int $id The post id. |
|
| 462 | - * @param string $post_status The target post status (default = publish). |
|
| 463 | - * |
|
| 464 | - * @return array An array of post ids. |
|
| 465 | - */ |
|
| 466 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 467 | - |
|
| 468 | - return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 469 | - } |
|
| 54 | + /** |
|
| 55 | + * A singleton instance of the Entity service. |
|
| 56 | + * |
|
| 57 | + * @since 3.2.0 |
|
| 58 | + * @access private |
|
| 59 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 60 | + */ |
|
| 61 | + private static $instance; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Create a Wordlift_Entity_Service instance. |
|
| 65 | + * |
|
| 66 | + * @since 3.2.0 |
|
| 67 | + * |
|
| 68 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 69 | + */ |
|
| 70 | + public function __construct( $ui_service ) { |
|
| 71 | + |
|
| 72 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 73 | + |
|
| 74 | + // Set the UI service. |
|
| 75 | + $this->ui_service = $ui_service; |
|
| 76 | + |
|
| 77 | + // Set the singleton instance. |
|
| 78 | + self::$instance = $this; |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Get the singleton instance of the Entity service. |
|
| 84 | + * |
|
| 85 | + * @since 3.2.0 |
|
| 86 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 87 | + */ |
|
| 88 | + public static function get_instance() { |
|
| 89 | + |
|
| 90 | + return self::$instance; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Determines whether a post is an entity or not. |
|
| 95 | + * |
|
| 96 | + * @since 3.1.0 |
|
| 97 | + * |
|
| 98 | + * @param int $post_id A post id. |
|
| 99 | + * |
|
| 100 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 101 | + */ |
|
| 102 | + public function is_entity( $post_id ) { |
|
| 103 | + |
|
| 104 | + return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get the proper classification scope for a given entity post |
|
| 109 | + * |
|
| 110 | + * @since 3.5.0 |
|
| 111 | + * |
|
| 112 | + * @param integer $post_id An entity post id. |
|
| 113 | + * |
|
| 114 | + * @return string Returns an uri. |
|
| 115 | + */ |
|
| 116 | + public function get_classification_scope_for( $post_id ) { |
|
| 117 | + |
|
| 118 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 119 | + return null; |
|
| 120 | + } |
|
| 121 | + // Retrieve the entity type |
|
| 122 | + $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 124 | + // Retrieve classification boxes configuration |
|
| 125 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | + foreach ( $classification_boxes as $cb ) { |
|
| 127 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 128 | + return $cb['id']; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // or null |
|
| 133 | + return null; |
|
| 134 | + |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + public function is_used( $post_id ) { |
|
| 139 | + |
|
| 140 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 141 | + return null; |
|
| 142 | + } |
|
| 143 | + // Retrieve the post |
|
| 144 | + $entity = get_post( $post_id ); |
|
| 145 | + |
|
| 146 | + global $wpdb; |
|
| 147 | + // Retrieve Wordlift relation instances table name |
|
| 148 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 149 | + |
|
| 150 | + // Check is it's referenced / related to another post / entity |
|
| 151 | + $stmt = $wpdb->prepare( |
|
| 152 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 153 | + $entity->ID |
|
| 154 | + ); |
|
| 155 | + |
|
| 156 | + // Perform the query |
|
| 157 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 158 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 159 | + if ( 0 < $relation_instances ) { |
|
| 160 | + return true; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // Check if the entity uri is used as meta_value |
|
| 164 | + $stmt = $wpdb->prepare( |
|
| 165 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 166 | + $entity->ID, |
|
| 167 | + wl_get_entity_uri( $entity->ID ) |
|
| 168 | + ); |
|
| 169 | + // Perform the query |
|
| 170 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 171 | + |
|
| 172 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 173 | + if ( 0 < $meta_instances ) { |
|
| 174 | + return true; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + // If we are here, it means the current entity is not used at the moment |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Determines whether a given uri is an internal uri or not. |
|
| 183 | + * |
|
| 184 | + * @since 3.3.2 |
|
| 185 | + * |
|
| 186 | + * @param int $uri An uri. |
|
| 187 | + * |
|
| 188 | + * @return true if the uri internal to the current dataset otherwise false. |
|
| 189 | + */ |
|
| 190 | + public function is_internal_uri( $uri ) { |
|
| 191 | + |
|
| 192 | + return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 197 | + * |
|
| 198 | + * @since 3.2.0 |
|
| 199 | + * |
|
| 200 | + * @param string $uri The entity URI. |
|
| 201 | + * |
|
| 202 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 203 | + */ |
|
| 204 | + public function get_entity_post_by_uri( $uri ) { |
|
| 205 | + |
|
| 206 | + // Check if we've been provided with a value otherwise return null. |
|
| 207 | + if ( empty( $uri ) ) { |
|
| 208 | + return null; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $query_args = array( |
|
| 212 | + 'posts_per_page' => 1, |
|
| 213 | + 'post_status' => 'any', |
|
| 214 | + 'post_type' => self::TYPE_NAME, |
|
| 215 | + 'meta_query' => array( |
|
| 216 | + array( |
|
| 217 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 218 | + 'value' => $uri, |
|
| 219 | + 'compare' => '=', |
|
| 220 | + ), |
|
| 221 | + ), |
|
| 222 | + ); |
|
| 223 | + |
|
| 224 | + // Only if the current uri is not an internal uri |
|
| 225 | + // entity search is performed also looking at sameAs values |
|
| 226 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 227 | + if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 228 | + |
|
| 229 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 230 | + $query_args['meta_query'][] = array( |
|
| 231 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 232 | + 'value' => $uri, |
|
| 233 | + 'compare' => '=', |
|
| 234 | + ); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + $query = new WP_Query( $query_args ); |
|
| 238 | + |
|
| 239 | + // Get the matching entity posts. |
|
| 240 | + $posts = $query->get_posts(); |
|
| 241 | + |
|
| 242 | + // Return null if no post is found. |
|
| 243 | + if ( 0 === count( $posts ) ) { |
|
| 244 | + return null; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + // Return the found post. |
|
| 248 | + return $posts[0]; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 253 | + * we check that the post we're saving is the current post. |
|
| 254 | + * |
|
| 255 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 256 | + * |
|
| 257 | + * @since 3.2.0 |
|
| 258 | + * |
|
| 259 | + * @param int $post_id Post ID. |
|
| 260 | + * @param WP_Post $post Post object. |
|
| 261 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 262 | + */ |
|
| 263 | + public function save_post( $post_id, $post, $update ) { |
|
| 264 | + |
|
| 265 | + // We're setting the alternative label that have been provided via the UI |
|
| 266 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 267 | + // programmatically by some other function: we need to check therefore if |
|
| 268 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 269 | + // |
|
| 270 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 271 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 272 | + return; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + // Get the alt labels from the request (or empty array). |
|
| 276 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 277 | + |
|
| 278 | + // Set the alternative labels. |
|
| 279 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 280 | + |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Set the alternative labels. |
|
| 285 | + * |
|
| 286 | + * @since 3.2.0 |
|
| 287 | + * |
|
| 288 | + * @param int $post_id The post id. |
|
| 289 | + * @param array $alt_labels An array of labels. |
|
| 290 | + */ |
|
| 291 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 292 | + |
|
| 293 | + // Force $alt_labels to be an array |
|
| 294 | + if ( ! is_array( $alt_labels ) ) { |
|
| 295 | + $alt_labels = array( $alt_labels ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 299 | + |
|
| 300 | + // Delete all the existing alternate labels. |
|
| 301 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 302 | + |
|
| 303 | + // Set the alternative labels. |
|
| 304 | + foreach ( $alt_labels as $alt_label ) { |
|
| 305 | + if ( ! empty( $alt_label ) ) { |
|
| 306 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Retrieve the alternate labels. |
|
| 314 | + * |
|
| 315 | + * @since 3.2.0 |
|
| 316 | + * |
|
| 317 | + * @param int $post_id Post id. |
|
| 318 | + * |
|
| 319 | + * @return mixed An array of alternative labels. |
|
| 320 | + */ |
|
| 321 | + public function get_alternative_labels( $post_id ) { |
|
| 322 | + |
|
| 323 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 328 | + * |
|
| 329 | + * @since 3.2.0 |
|
| 330 | + * |
|
| 331 | + * @param WP_Post $post Post object. |
|
| 332 | + */ |
|
| 333 | + public function edit_form_before_permalink( $post ) { |
|
| 334 | + |
|
| 335 | + // If it's not an entity, return. |
|
| 336 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 337 | + return; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + // Print the input template. |
|
| 341 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 342 | + |
|
| 343 | + // Print all the currently set alternative labels. |
|
| 344 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 345 | + |
|
| 346 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 347 | + |
|
| 348 | + }; |
|
| 349 | + |
|
| 350 | + // Print the button. |
|
| 351 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 352 | + |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Get the URI for the entity with the specified post id. |
|
| 357 | + * |
|
| 358 | + * @since 3.6.0 |
|
| 359 | + * |
|
| 360 | + * @param int $post_id The entity post id. |
|
| 361 | + * |
|
| 362 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 363 | + */ |
|
| 364 | + public function get_uri( $post_id ) { |
|
| 365 | + |
|
| 366 | + // If a null is given, nothing to do |
|
| 367 | + if ( null == $post_id ) { |
|
| 368 | + return null; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 372 | + |
|
| 373 | + // If the dataset uri is not properly configured, null is returned |
|
| 374 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 375 | + return null; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + // Set the URI if it isn't set yet. |
|
| 379 | + $post_status = get_post_status( $post_id ); |
|
| 380 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 381 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 382 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + return $uri; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Get the alternative label input HTML code. |
|
| 391 | + * |
|
| 392 | + * @since 3.2.0 |
|
| 393 | + * |
|
| 394 | + * @param string $value The input value. |
|
| 395 | + * |
|
| 396 | + * @return string The input HTML code. |
|
| 397 | + */ |
|
| 398 | + private function get_alternative_label_input( $value = '' ) { |
|
| 399 | + |
|
| 400 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Get the number of entity posts published in this blog. |
|
| 405 | + * |
|
| 406 | + * @since 3.6.0 |
|
| 407 | + * |
|
| 408 | + * @return int The number of published entity posts. |
|
| 409 | + */ |
|
| 410 | + public function count() { |
|
| 411 | + |
|
| 412 | + $count = wp_count_posts( self::TYPE_NAME ); |
|
| 413 | + |
|
| 414 | + return $count->publish; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * Create a new entity. |
|
| 419 | + * |
|
| 420 | + * @since 3.9.0 |
|
| 421 | + * |
|
| 422 | + * @param string $name The entity name. |
|
| 423 | + * @param string $type_uri The entity's type URI. |
|
| 424 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 425 | + * @param string $status The post status, by default 'publish'. |
|
| 426 | + * |
|
| 427 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 428 | + */ |
|
| 429 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 430 | + |
|
| 431 | + // Create an entity for the publisher. |
|
| 432 | + $post_id = wp_insert_post( array( |
|
| 433 | + 'post_type' => self::TYPE_NAME, |
|
| 434 | + 'post_title' => $name, |
|
| 435 | + 'post_status' => $status, |
|
| 436 | + 'post_content' => '', |
|
| 437 | + ) ); |
|
| 438 | + |
|
| 439 | + // Return the error if any. |
|
| 440 | + if ( is_wp_error( $post_id ) ) { |
|
| 441 | + return $post_id; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + // Set the entity logo. |
|
| 445 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 446 | + set_post_thumbnail( $post_id, $logo ); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + // Set the entity type. |
|
| 450 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 451 | + |
|
| 452 | + return $post_id; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Get the entities related to the one with the specified id. By default only |
|
| 457 | + * published entities will be returned. |
|
| 458 | + * |
|
| 459 | + * @since 3.10.0 |
|
| 460 | + * |
|
| 461 | + * @param int $id The post id. |
|
| 462 | + * @param string $post_status The target post status (default = publish). |
|
| 463 | + * |
|
| 464 | + * @return array An array of post ids. |
|
| 465 | + */ |
|
| 466 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 467 | + |
|
| 468 | + return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 469 | + } |
|
| 470 | 470 | |
| 471 | 471 | } |
@@ -67,9 +67,9 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @param \Wordlift_UI_Service $ui_service The UI service. |
| 69 | 69 | */ |
| 70 | - public function __construct( $ui_service ) { |
|
| 70 | + public function __construct($ui_service) { |
|
| 71 | 71 | |
| 72 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 72 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 73 | 73 | |
| 74 | 74 | // Set the UI service. |
| 75 | 75 | $this->ui_service = $ui_service; |
@@ -99,9 +99,9 @@ discard block |
||
| 99 | 99 | * |
| 100 | 100 | * @return bool Return true if the post is an entity otherwise false. |
| 101 | 101 | */ |
| 102 | - public function is_entity( $post_id ) { |
|
| 102 | + public function is_entity($post_id) { |
|
| 103 | 103 | |
| 104 | - return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 104 | + return (self::TYPE_NAME === get_post_type($post_id)); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -113,18 +113,18 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @return string Returns an uri. |
| 115 | 115 | */ |
| 116 | - public function get_classification_scope_for( $post_id ) { |
|
| 116 | + public function get_classification_scope_for($post_id) { |
|
| 117 | 117 | |
| 118 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 118 | + if (false === $this->is_entity($post_id)) { |
|
| 119 | 119 | return null; |
| 120 | 120 | } |
| 121 | 121 | // Retrieve the entity type |
| 122 | - $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 122 | + $entity_type_arr = wl_entity_type_taxonomy_get_type($post_id); |
|
| 123 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 124 | 124 | // Retrieve classification boxes configuration |
| 125 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | - foreach ( $classification_boxes as $cb ) { |
|
| 127 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 125 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 126 | + foreach ($classification_boxes as $cb) { |
|
| 127 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 128 | 128 | return $cb['id']; |
| 129 | 129 | } |
| 130 | 130 | } |
@@ -135,13 +135,13 @@ discard block |
||
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | |
| 138 | - public function is_used( $post_id ) { |
|
| 138 | + public function is_used($post_id) { |
|
| 139 | 139 | |
| 140 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 140 | + if (false === $this->is_entity($post_id)) { |
|
| 141 | 141 | return null; |
| 142 | 142 | } |
| 143 | 143 | // Retrieve the post |
| 144 | - $entity = get_post( $post_id ); |
|
| 144 | + $entity = get_post($post_id); |
|
| 145 | 145 | |
| 146 | 146 | global $wpdb; |
| 147 | 147 | // Retrieve Wordlift relation instances table name |
@@ -154,9 +154,9 @@ discard block |
||
| 154 | 154 | ); |
| 155 | 155 | |
| 156 | 156 | // Perform the query |
| 157 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 157 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 158 | 158 | // If there is at least one relation instance for the current entity, then it's used |
| 159 | - if ( 0 < $relation_instances ) { |
|
| 159 | + if (0 < $relation_instances) { |
|
| 160 | 160 | return true; |
| 161 | 161 | } |
| 162 | 162 | |
@@ -164,13 +164,13 @@ discard block |
||
| 164 | 164 | $stmt = $wpdb->prepare( |
| 165 | 165 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 166 | 166 | $entity->ID, |
| 167 | - wl_get_entity_uri( $entity->ID ) |
|
| 167 | + wl_get_entity_uri($entity->ID) |
|
| 168 | 168 | ); |
| 169 | 169 | // Perform the query |
| 170 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 170 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 171 | 171 | |
| 172 | 172 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 173 | - if ( 0 < $meta_instances ) { |
|
| 173 | + if (0 < $meta_instances) { |
|
| 174 | 174 | return true; |
| 175 | 175 | } |
| 176 | 176 | |
@@ -187,9 +187,9 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return true if the uri internal to the current dataset otherwise false. |
| 189 | 189 | */ |
| 190 | - public function is_internal_uri( $uri ) { |
|
| 190 | + public function is_internal_uri($uri) { |
|
| 191 | 191 | |
| 192 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 192 | + return (0 === strrpos($uri, wl_configuration_get_redlink_dataset_uri())); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | /** |
@@ -201,10 +201,10 @@ discard block |
||
| 201 | 201 | * |
| 202 | 202 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 203 | 203 | */ |
| 204 | - public function get_entity_post_by_uri( $uri ) { |
|
| 204 | + public function get_entity_post_by_uri($uri) { |
|
| 205 | 205 | |
| 206 | 206 | // Check if we've been provided with a value otherwise return null. |
| 207 | - if ( empty( $uri ) ) { |
|
| 207 | + if (empty($uri)) { |
|
| 208 | 208 | return null; |
| 209 | 209 | } |
| 210 | 210 | |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | // Only if the current uri is not an internal uri |
| 225 | 225 | // entity search is performed also looking at sameAs values |
| 226 | 226 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 227 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 227 | + if ( ! $this->is_internal_uri($uri)) { |
|
| 228 | 228 | |
| 229 | 229 | $query_args['meta_query']['relation'] = 'OR'; |
| 230 | 230 | $query_args['meta_query'][] = array( |
@@ -234,13 +234,13 @@ discard block |
||
| 234 | 234 | ); |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | - $query = new WP_Query( $query_args ); |
|
| 237 | + $query = new WP_Query($query_args); |
|
| 238 | 238 | |
| 239 | 239 | // Get the matching entity posts. |
| 240 | 240 | $posts = $query->get_posts(); |
| 241 | 241 | |
| 242 | 242 | // Return null if no post is found. |
| 243 | - if ( 0 === count( $posts ) ) { |
|
| 243 | + if (0 === count($posts)) { |
|
| 244 | 244 | return null; |
| 245 | 245 | } |
| 246 | 246 | |
@@ -260,7 +260,7 @@ discard block |
||
| 260 | 260 | * @param WP_Post $post Post object. |
| 261 | 261 | * @param bool $update Whether this is an existing post being updated or not. |
| 262 | 262 | */ |
| 263 | - public function save_post( $post_id, $post, $update ) { |
|
| 263 | + public function save_post($post_id, $post, $update) { |
|
| 264 | 264 | |
| 265 | 265 | // We're setting the alternative label that have been provided via the UI |
| 266 | 266 | // (in fact we're using $_REQUEST), while save_post may be also called |
@@ -268,15 +268,15 @@ discard block |
||
| 268 | 268 | // the $post_id in the save_post call matches the post id set in the request. |
| 269 | 269 | // |
| 270 | 270 | // If this is not the current post being saved or if it's not an entity, return. |
| 271 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 271 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 272 | 272 | return; |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | // Get the alt labels from the request (or empty array). |
| 276 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 276 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 277 | 277 | |
| 278 | 278 | // Set the alternative labels. |
| 279 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 279 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 280 | 280 | |
| 281 | 281 | } |
| 282 | 282 | |
@@ -288,22 +288,22 @@ discard block |
||
| 288 | 288 | * @param int $post_id The post id. |
| 289 | 289 | * @param array $alt_labels An array of labels. |
| 290 | 290 | */ |
| 291 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 291 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 292 | 292 | |
| 293 | 293 | // Force $alt_labels to be an array |
| 294 | - if ( ! is_array( $alt_labels ) ) { |
|
| 295 | - $alt_labels = array( $alt_labels ); |
|
| 294 | + if ( ! is_array($alt_labels)) { |
|
| 295 | + $alt_labels = array($alt_labels); |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 298 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 299 | 299 | |
| 300 | 300 | // Delete all the existing alternate labels. |
| 301 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 301 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 302 | 302 | |
| 303 | 303 | // Set the alternative labels. |
| 304 | - foreach ( $alt_labels as $alt_label ) { |
|
| 305 | - if ( ! empty( $alt_label ) ) { |
|
| 306 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 304 | + foreach ($alt_labels as $alt_label) { |
|
| 305 | + if ( ! empty($alt_label)) { |
|
| 306 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 307 | 307 | } |
| 308 | 308 | } |
| 309 | 309 | |
@@ -318,9 +318,9 @@ discard block |
||
| 318 | 318 | * |
| 319 | 319 | * @return mixed An array of alternative labels. |
| 320 | 320 | */ |
| 321 | - public function get_alternative_labels( $post_id ) { |
|
| 321 | + public function get_alternative_labels($post_id) { |
|
| 322 | 322 | |
| 323 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 323 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | /** |
@@ -330,25 +330,25 @@ discard block |
||
| 330 | 330 | * |
| 331 | 331 | * @param WP_Post $post Post object. |
| 332 | 332 | */ |
| 333 | - public function edit_form_before_permalink( $post ) { |
|
| 333 | + public function edit_form_before_permalink($post) { |
|
| 334 | 334 | |
| 335 | 335 | // If it's not an entity, return. |
| 336 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 336 | + if ( ! $this->is_entity($post->ID)) { |
|
| 337 | 337 | return; |
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | // Print the input template. |
| 341 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 341 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 342 | 342 | |
| 343 | 343 | // Print all the currently set alternative labels. |
| 344 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 344 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 345 | 345 | |
| 346 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 346 | + echo $this->get_alternative_label_input($alt_label); |
|
| 347 | 347 | |
| 348 | 348 | }; |
| 349 | 349 | |
| 350 | 350 | // Print the button. |
| 351 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 351 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 352 | 352 | |
| 353 | 353 | } |
| 354 | 354 | |
@@ -361,25 +361,25 @@ discard block |
||
| 361 | 361 | * |
| 362 | 362 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
| 363 | 363 | */ |
| 364 | - public function get_uri( $post_id ) { |
|
| 364 | + public function get_uri($post_id) { |
|
| 365 | 365 | |
| 366 | 366 | // If a null is given, nothing to do |
| 367 | - if ( null == $post_id ) { |
|
| 367 | + if (null == $post_id) { |
|
| 368 | 368 | return null; |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 371 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 372 | 372 | |
| 373 | 373 | // If the dataset uri is not properly configured, null is returned |
| 374 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 374 | + if ('' === wl_configuration_get_redlink_dataset_uri()) { |
|
| 375 | 375 | return null; |
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | // Set the URI if it isn't set yet. |
| 379 | - $post_status = get_post_status( $post_id ); |
|
| 380 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 381 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 382 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 379 | + $post_status = get_post_status($post_id); |
|
| 380 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 381 | + $uri = wl_build_entity_uri($post_id); |
|
| 382 | + wl_set_entity_uri($post_id, $uri); |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | return $uri; |
@@ -395,9 +395,9 @@ discard block |
||
| 395 | 395 | * |
| 396 | 396 | * @return string The input HTML code. |
| 397 | 397 | */ |
| 398 | - private function get_alternative_label_input( $value = '' ) { |
|
| 398 | + private function get_alternative_label_input($value = '') { |
|
| 399 | 399 | |
| 400 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 400 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 401 | 401 | } |
| 402 | 402 | |
| 403 | 403 | /** |
@@ -409,7 +409,7 @@ discard block |
||
| 409 | 409 | */ |
| 410 | 410 | public function count() { |
| 411 | 411 | |
| 412 | - $count = wp_count_posts( self::TYPE_NAME ); |
|
| 412 | + $count = wp_count_posts(self::TYPE_NAME); |
|
| 413 | 413 | |
| 414 | 414 | return $count->publish; |
| 415 | 415 | } |
@@ -426,28 +426,28 @@ discard block |
||
| 426 | 426 | * |
| 427 | 427 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
| 428 | 428 | */ |
| 429 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 429 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 430 | 430 | |
| 431 | 431 | // Create an entity for the publisher. |
| 432 | - $post_id = wp_insert_post( array( |
|
| 432 | + $post_id = wp_insert_post(array( |
|
| 433 | 433 | 'post_type' => self::TYPE_NAME, |
| 434 | 434 | 'post_title' => $name, |
| 435 | 435 | 'post_status' => $status, |
| 436 | 436 | 'post_content' => '', |
| 437 | - ) ); |
|
| 437 | + )); |
|
| 438 | 438 | |
| 439 | 439 | // Return the error if any. |
| 440 | - if ( is_wp_error( $post_id ) ) { |
|
| 440 | + if (is_wp_error($post_id)) { |
|
| 441 | 441 | return $post_id; |
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | // Set the entity logo. |
| 445 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 446 | - set_post_thumbnail( $post_id, $logo ); |
|
| 445 | + if ($logo && is_numeric($logo)) { |
|
| 446 | + set_post_thumbnail($post_id, $logo); |
|
| 447 | 447 | } |
| 448 | 448 | |
| 449 | 449 | // Set the entity type. |
| 450 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 450 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 451 | 451 | |
| 452 | 452 | return $post_id; |
| 453 | 453 | } |
@@ -463,9 +463,9 @@ discard block |
||
| 463 | 463 | * |
| 464 | 464 | * @return array An array of post ids. |
| 465 | 465 | */ |
| 466 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 466 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 467 | 467 | |
| 468 | - return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 468 | + return wl_core_inner_get_related_entities('post_ids', $id, null, $post_status); |
|
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | } |
@@ -173,7 +173,6 @@ discard block |
||
| 173 | 173 | * @since 3.3.0 |
| 174 | 174 | * |
| 175 | 175 | * @param int $post_id The entity post id. |
| 176 | - * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 177 | 176 | * |
| 178 | 177 | * @return array An array representing the rating obj. |
| 179 | 178 | */ |
@@ -332,7 +331,7 @@ discard block |
||
| 332 | 331 | * |
| 333 | 332 | * @param int $score The rating score for a given entity. |
| 334 | 333 | * |
| 335 | - * @return string The input HTML code. |
|
| 334 | + * @return double The input HTML code. |
|
| 336 | 335 | */ |
| 337 | 336 | public function convert_raw_score_to_percentage( $score ) { |
| 338 | 337 | |
@@ -14,370 +14,370 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Rating_Service { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Entity rating max. |
|
| 19 | - * |
|
| 20 | - * @since 3.3.0 |
|
| 21 | - */ |
|
| 22 | - const RATING_MAX = 7; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Entity rating score meta key. |
|
| 26 | - * |
|
| 27 | - * @since 3.3.0 |
|
| 28 | - */ |
|
| 29 | - const RATING_RAW_SCORE_META_KEY = '_wl_entity_rating_raw_score'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Entity rating warnings meta key. |
|
| 33 | - * |
|
| 34 | - * @since 3.3.0 |
|
| 35 | - */ |
|
| 36 | - const RATING_WARNINGS_META_KEY = '_wl_entity_rating_warnings'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Entity warning has related post identifier. |
|
| 40 | - * |
|
| 41 | - * @since 3.3.0 |
|
| 42 | - */ |
|
| 43 | - const RATING_WARNING_HAS_RELATED_POSTS = 'There are no related posts for the current entity.'; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Entity warning has content post identifier. |
|
| 47 | - * |
|
| 48 | - * @since 3.3.0 |
|
| 49 | - */ |
|
| 50 | - const RATING_WARNING_HAS_CONTENT_POST = 'This entity has not description.'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Entity warning has related entities identifier. |
|
| 54 | - * |
|
| 55 | - * @since 3.3.0 |
|
| 56 | - */ |
|
| 57 | - const RATING_WARNING_HAS_RELATED_ENTITIES = 'There are no related entities for the current entity.'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Entity warning is published identifier. |
|
| 61 | - * |
|
| 62 | - * @since 3.3.0 |
|
| 63 | - */ |
|
| 64 | - const RATING_WARNING_IS_PUBLISHED = 'This entity is not published. It will not appear within analysis results.'; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Entity warning has thumbnail identifier. |
|
| 68 | - * |
|
| 69 | - * @since 3.3.0 |
|
| 70 | - */ |
|
| 71 | - const RATING_WARNING_HAS_THUMBNAIL = 'This entity has no featured image yet.'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Entity warning has same as identifier. |
|
| 75 | - * |
|
| 76 | - * @since 3.3.0 |
|
| 77 | - */ |
|
| 78 | - const RATING_WARNING_HAS_SAME_AS = 'There are no sameAs configured for this entity.'; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Entity warning has completed metadata identifier. |
|
| 82 | - * |
|
| 83 | - * @since 3.3.0 |
|
| 84 | - */ |
|
| 85 | - const RATING_WARNING_HAS_COMPLETED_METADATA = 'Schema.org metadata for this entity are not completed.'; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 89 | - * |
|
| 90 | - * @since 3.10.0 |
|
| 91 | - * @access private |
|
| 92 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 93 | - */ |
|
| 94 | - private $entity_service; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 98 | - * |
|
| 99 | - * @since 3.10.0 |
|
| 100 | - * @access private |
|
| 101 | - * @var Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 102 | - */ |
|
| 103 | - private $entity_type_service; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * The Notice service. |
|
| 107 | - * |
|
| 108 | - * @since 3.3.0 |
|
| 109 | - * @access private |
|
| 110 | - * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
| 111 | - */ |
|
| 112 | - private $notice_service; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * A {@link Wordlift_Log_Service} instance. |
|
| 116 | - * |
|
| 117 | - * @since 3.10.0 |
|
| 118 | - * @access private |
|
| 119 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 120 | - */ |
|
| 121 | - private $log; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Create a {@link Wordlift_Rating_Service} instance. |
|
| 125 | - * |
|
| 126 | - * @since 3.10.0 |
|
| 127 | - * |
|
| 128 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 129 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 130 | - * @param \Wordlift_Notice_Service $notice_service A {@link Wordlift_Notice_Service} instance. |
|
| 131 | - */ |
|
| 132 | - public function __construct( $entity_service, $entity_type_service, $notice_service ) { |
|
| 133 | - |
|
| 134 | - $this->entity_service = $entity_service; |
|
| 135 | - $this->entity_type_service = $entity_type_service; |
|
| 136 | - $this->notice_service = $notice_service; |
|
| 137 | - |
|
| 138 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 139 | - |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Set rating for a given entity. |
|
| 144 | - * |
|
| 145 | - * @since 3.3.0 |
|
| 146 | - * |
|
| 147 | - * @param int $post_id The entity post id. |
|
| 148 | - * |
|
| 149 | - * @return array An array representing the rating obj. |
|
| 150 | - */ |
|
| 151 | - public function set_rating_for( $post_id ) { |
|
| 152 | - |
|
| 153 | - // Calculate rating for the given post. |
|
| 154 | - $rating = $this->calculate_rating_for( $post_id ); |
|
| 155 | - |
|
| 156 | - // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
|
| 157 | - // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
|
| 158 | - // |
|
| 159 | - // See https://codex.wordpress.org/Function_Reference/update_post_meta |
|
| 160 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 161 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 162 | - |
|
| 163 | - $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 164 | - |
|
| 165 | - // Finally returns the rating |
|
| 166 | - return $rating; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Get or calculate rating for a given entity |
|
| 172 | - * |
|
| 173 | - * @since 3.3.0 |
|
| 174 | - * |
|
| 175 | - * @param int $post_id The entity post id. |
|
| 176 | - * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 177 | - * |
|
| 178 | - * @return array An array representing the rating obj. |
|
| 179 | - */ |
|
| 180 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 181 | - |
|
| 182 | - // If forced reload is required or rating is missing. |
|
| 183 | - if ( $force_reload ) { |
|
| 184 | - $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 185 | - |
|
| 186 | - return $this->set_rating_for( $post_id ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 190 | - |
|
| 191 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
| 192 | - $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 193 | - |
|
| 194 | - return $this->set_rating_for( $post_id ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 198 | - |
|
| 199 | - // Finally return score and warnings |
|
| 200 | - return array( |
|
| 201 | - 'raw_score' => $current_raw_score, |
|
| 202 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 203 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 204 | - 'warnings' => $current_warnings, |
|
| 205 | - ); |
|
| 206 | - |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Calculate rating for a given entity. |
|
| 211 | - * |
|
| 212 | - * Rating depends from following criteria: |
|
| 213 | - * |
|
| 214 | - * 1. Is the current entity related to at least 1 post? |
|
| 215 | - * 2. Is the current entity content post not empty? |
|
| 216 | - * 3. Is the current entity related to at least 1 entity? |
|
| 217 | - * 4. Is the entity published? |
|
| 218 | - * 5. There is a a thumbnail associated to the entity? |
|
| 219 | - * 6. Has the entity a sameas defined? |
|
| 220 | - * 7. Are all schema.org required metadata compiled? |
|
| 221 | - * |
|
| 222 | - * Each positive check means +1 in terms of rating score. |
|
| 223 | - * |
|
| 224 | - * @since 3.3.0 |
|
| 225 | - * |
|
| 226 | - * @param int $post_id The entity post id. |
|
| 227 | - * |
|
| 228 | - * @return array An array representing the rating obj. |
|
| 229 | - */ |
|
| 230 | - private function calculate_rating_for( $post_id ) { |
|
| 231 | - |
|
| 232 | - // If it's not an entity, return. |
|
| 233 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 234 | - return array(); |
|
| 235 | - } |
|
| 236 | - // Retrieve the post object. |
|
| 237 | - $post = get_post( $post_id ); |
|
| 238 | - |
|
| 239 | - // Rating value. |
|
| 240 | - $score = 0; |
|
| 241 | - |
|
| 242 | - // Store warning messages. |
|
| 243 | - $warnings = array(); |
|
| 244 | - |
|
| 245 | - // Is the current entity related to at least 1 post? |
|
| 246 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 247 | - $score ++ : |
|
| 248 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift' ) ); |
|
| 249 | - |
|
| 250 | - // Is the post content not empty? |
|
| 251 | - ( ! empty( $post->post_content ) ) ? |
|
| 252 | - $score ++ : |
|
| 253 | - array_push( $warnings, __( self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift' ) ); |
|
| 254 | - |
|
| 255 | - // Is the current entity related to at least 1 entity? |
|
| 256 | - // Was the current entity already disambiguated? |
|
| 257 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 258 | - $score ++ : |
|
| 259 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift' ) ); |
|
| 260 | - |
|
| 261 | - // Is the entity published? |
|
| 262 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 263 | - $score ++ : |
|
| 264 | - array_push( $warnings, __( self::RATING_WARNING_IS_PUBLISHED, 'wordlift' ) ); |
|
| 265 | - |
|
| 266 | - // Has a thumbnail? |
|
| 267 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
| 268 | - $score ++ : |
|
| 269 | - array_push( $warnings, __( self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift' ) ); |
|
| 270 | - |
|
| 271 | - // Get all post meta keys for the current post |
|
| 272 | - global $wpdb; |
|
| 273 | - |
|
| 274 | - $query = $wpdb->prepare( |
|
| 275 | - "SELECT DISTINCT (meta_key) FROM $wpdb->postmeta WHERE post_id = %d", $post->ID |
|
| 276 | - ); |
|
| 277 | - |
|
| 278 | - // Check intersection between available meta keys and expected ones |
|
| 279 | - // arrays to detect missing values. |
|
| 280 | - $available_meta_keys = $wpdb->get_col( $query ); |
|
| 281 | - |
|
| 282 | - // If each expected key is contained in available keys array ... |
|
| 283 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 284 | - $score ++ : |
|
| 285 | - array_push( $warnings, __( self::RATING_WARNING_HAS_SAME_AS, 'wordlift' ) ); |
|
| 286 | - |
|
| 287 | - $schema = $this->entity_type_service->get( $post_id ); |
|
| 288 | - |
|
| 289 | - $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 290 | - array() : |
|
| 291 | - array_keys( $schema['custom_fields'] ); |
|
| 292 | - |
|
| 293 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 294 | - // If each expected key is contained in available keys array ... |
|
| 295 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 296 | - $score ++ : |
|
| 297 | - array_push( $warnings, __( self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift' ) ); |
|
| 298 | - |
|
| 299 | - // Finally return score and warnings |
|
| 300 | - return array( |
|
| 301 | - 'raw_score' => $score, |
|
| 302 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 303 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 304 | - 'warnings' => $warnings, |
|
| 305 | - ); |
|
| 306 | - |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Get as rating as input and convert in a traffic-light rating |
|
| 311 | - * |
|
| 312 | - * @since 3.3.0 |
|
| 313 | - * |
|
| 314 | - * @param int $score The rating score for a given entity. |
|
| 315 | - * |
|
| 316 | - * @return string The input HTML code. |
|
| 317 | - */ |
|
| 318 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
| 319 | - // RATING_MAX : $score = 3 : x |
|
| 320 | - // See http://php.net/manual/en/function.round.php |
|
| 321 | - $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 322 | - |
|
| 323 | - // If rating is 0, return 1, otherwise return rating |
|
| 324 | - return ( 0 == $rating ) ? 1 : $rating; |
|
| 325 | - |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Get as rating as input and convert in a traffic-light rating |
|
| 330 | - * |
|
| 331 | - * @since 3.3.0 |
|
| 332 | - * |
|
| 333 | - * @param int $score The rating score for a given entity. |
|
| 334 | - * |
|
| 335 | - * @return string The input HTML code. |
|
| 336 | - */ |
|
| 337 | - public function convert_raw_score_to_percentage( $score ) { |
|
| 338 | - |
|
| 339 | - // RATING_MAX : $score = 100 : x |
|
| 340 | - return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Add admin notices for the current entity depending on the current rating. |
|
| 346 | - * |
|
| 347 | - * @since 3.3.0 |
|
| 348 | - */ |
|
| 349 | - public function in_admin_header() { |
|
| 350 | - |
|
| 351 | - // Return safely if get_current_screen() is not defined (yet) |
|
| 352 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 353 | - return; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - $screen = get_current_screen(); |
|
| 357 | - // If there is any valid screen nothing to do |
|
| 358 | - if ( null === $screen ) { |
|
| 359 | - return; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - // If you're not in the entity post edit page, return. |
|
| 363 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 364 | - return; |
|
| 365 | - } |
|
| 366 | - // Retrieve the current global post |
|
| 367 | - global $post; |
|
| 368 | - // If it's not an entity, return. |
|
| 369 | - if ( ! $this->entity_service->is_entity( $post->ID ) ) { |
|
| 370 | - return; |
|
| 371 | - } |
|
| 372 | - // Retrieve an updated rating for the current entity |
|
| 373 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
| 374 | - |
|
| 375 | - // If there is at least 1 warning |
|
| 376 | - if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 377 | - // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
| 378 | - $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - } |
|
| 17 | + /** |
|
| 18 | + * Entity rating max. |
|
| 19 | + * |
|
| 20 | + * @since 3.3.0 |
|
| 21 | + */ |
|
| 22 | + const RATING_MAX = 7; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Entity rating score meta key. |
|
| 26 | + * |
|
| 27 | + * @since 3.3.0 |
|
| 28 | + */ |
|
| 29 | + const RATING_RAW_SCORE_META_KEY = '_wl_entity_rating_raw_score'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Entity rating warnings meta key. |
|
| 33 | + * |
|
| 34 | + * @since 3.3.0 |
|
| 35 | + */ |
|
| 36 | + const RATING_WARNINGS_META_KEY = '_wl_entity_rating_warnings'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Entity warning has related post identifier. |
|
| 40 | + * |
|
| 41 | + * @since 3.3.0 |
|
| 42 | + */ |
|
| 43 | + const RATING_WARNING_HAS_RELATED_POSTS = 'There are no related posts for the current entity.'; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Entity warning has content post identifier. |
|
| 47 | + * |
|
| 48 | + * @since 3.3.0 |
|
| 49 | + */ |
|
| 50 | + const RATING_WARNING_HAS_CONTENT_POST = 'This entity has not description.'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Entity warning has related entities identifier. |
|
| 54 | + * |
|
| 55 | + * @since 3.3.0 |
|
| 56 | + */ |
|
| 57 | + const RATING_WARNING_HAS_RELATED_ENTITIES = 'There are no related entities for the current entity.'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Entity warning is published identifier. |
|
| 61 | + * |
|
| 62 | + * @since 3.3.0 |
|
| 63 | + */ |
|
| 64 | + const RATING_WARNING_IS_PUBLISHED = 'This entity is not published. It will not appear within analysis results.'; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Entity warning has thumbnail identifier. |
|
| 68 | + * |
|
| 69 | + * @since 3.3.0 |
|
| 70 | + */ |
|
| 71 | + const RATING_WARNING_HAS_THUMBNAIL = 'This entity has no featured image yet.'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Entity warning has same as identifier. |
|
| 75 | + * |
|
| 76 | + * @since 3.3.0 |
|
| 77 | + */ |
|
| 78 | + const RATING_WARNING_HAS_SAME_AS = 'There are no sameAs configured for this entity.'; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Entity warning has completed metadata identifier. |
|
| 82 | + * |
|
| 83 | + * @since 3.3.0 |
|
| 84 | + */ |
|
| 85 | + const RATING_WARNING_HAS_COMPLETED_METADATA = 'Schema.org metadata for this entity are not completed.'; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 89 | + * |
|
| 90 | + * @since 3.10.0 |
|
| 91 | + * @access private |
|
| 92 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 93 | + */ |
|
| 94 | + private $entity_service; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 98 | + * |
|
| 99 | + * @since 3.10.0 |
|
| 100 | + * @access private |
|
| 101 | + * @var Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 102 | + */ |
|
| 103 | + private $entity_type_service; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * The Notice service. |
|
| 107 | + * |
|
| 108 | + * @since 3.3.0 |
|
| 109 | + * @access private |
|
| 110 | + * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
| 111 | + */ |
|
| 112 | + private $notice_service; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * A {@link Wordlift_Log_Service} instance. |
|
| 116 | + * |
|
| 117 | + * @since 3.10.0 |
|
| 118 | + * @access private |
|
| 119 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 120 | + */ |
|
| 121 | + private $log; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Create a {@link Wordlift_Rating_Service} instance. |
|
| 125 | + * |
|
| 126 | + * @since 3.10.0 |
|
| 127 | + * |
|
| 128 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 129 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 130 | + * @param \Wordlift_Notice_Service $notice_service A {@link Wordlift_Notice_Service} instance. |
|
| 131 | + */ |
|
| 132 | + public function __construct( $entity_service, $entity_type_service, $notice_service ) { |
|
| 133 | + |
|
| 134 | + $this->entity_service = $entity_service; |
|
| 135 | + $this->entity_type_service = $entity_type_service; |
|
| 136 | + $this->notice_service = $notice_service; |
|
| 137 | + |
|
| 138 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 139 | + |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Set rating for a given entity. |
|
| 144 | + * |
|
| 145 | + * @since 3.3.0 |
|
| 146 | + * |
|
| 147 | + * @param int $post_id The entity post id. |
|
| 148 | + * |
|
| 149 | + * @return array An array representing the rating obj. |
|
| 150 | + */ |
|
| 151 | + public function set_rating_for( $post_id ) { |
|
| 152 | + |
|
| 153 | + // Calculate rating for the given post. |
|
| 154 | + $rating = $this->calculate_rating_for( $post_id ); |
|
| 155 | + |
|
| 156 | + // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
|
| 157 | + // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
|
| 158 | + // |
|
| 159 | + // See https://codex.wordpress.org/Function_Reference/update_post_meta |
|
| 160 | + update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 161 | + update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 162 | + |
|
| 163 | + $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 164 | + |
|
| 165 | + // Finally returns the rating |
|
| 166 | + return $rating; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Get or calculate rating for a given entity |
|
| 172 | + * |
|
| 173 | + * @since 3.3.0 |
|
| 174 | + * |
|
| 175 | + * @param int $post_id The entity post id. |
|
| 176 | + * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 177 | + * |
|
| 178 | + * @return array An array representing the rating obj. |
|
| 179 | + */ |
|
| 180 | + public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 181 | + |
|
| 182 | + // If forced reload is required or rating is missing. |
|
| 183 | + if ( $force_reload ) { |
|
| 184 | + $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 185 | + |
|
| 186 | + return $this->set_rating_for( $post_id ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 190 | + |
|
| 191 | + if ( ! is_numeric( $current_raw_score ) ) { |
|
| 192 | + $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 193 | + |
|
| 194 | + return $this->set_rating_for( $post_id ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 198 | + |
|
| 199 | + // Finally return score and warnings |
|
| 200 | + return array( |
|
| 201 | + 'raw_score' => $current_raw_score, |
|
| 202 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 203 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 204 | + 'warnings' => $current_warnings, |
|
| 205 | + ); |
|
| 206 | + |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Calculate rating for a given entity. |
|
| 211 | + * |
|
| 212 | + * Rating depends from following criteria: |
|
| 213 | + * |
|
| 214 | + * 1. Is the current entity related to at least 1 post? |
|
| 215 | + * 2. Is the current entity content post not empty? |
|
| 216 | + * 3. Is the current entity related to at least 1 entity? |
|
| 217 | + * 4. Is the entity published? |
|
| 218 | + * 5. There is a a thumbnail associated to the entity? |
|
| 219 | + * 6. Has the entity a sameas defined? |
|
| 220 | + * 7. Are all schema.org required metadata compiled? |
|
| 221 | + * |
|
| 222 | + * Each positive check means +1 in terms of rating score. |
|
| 223 | + * |
|
| 224 | + * @since 3.3.0 |
|
| 225 | + * |
|
| 226 | + * @param int $post_id The entity post id. |
|
| 227 | + * |
|
| 228 | + * @return array An array representing the rating obj. |
|
| 229 | + */ |
|
| 230 | + private function calculate_rating_for( $post_id ) { |
|
| 231 | + |
|
| 232 | + // If it's not an entity, return. |
|
| 233 | + if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 234 | + return array(); |
|
| 235 | + } |
|
| 236 | + // Retrieve the post object. |
|
| 237 | + $post = get_post( $post_id ); |
|
| 238 | + |
|
| 239 | + // Rating value. |
|
| 240 | + $score = 0; |
|
| 241 | + |
|
| 242 | + // Store warning messages. |
|
| 243 | + $warnings = array(); |
|
| 244 | + |
|
| 245 | + // Is the current entity related to at least 1 post? |
|
| 246 | + ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 247 | + $score ++ : |
|
| 248 | + array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift' ) ); |
|
| 249 | + |
|
| 250 | + // Is the post content not empty? |
|
| 251 | + ( ! empty( $post->post_content ) ) ? |
|
| 252 | + $score ++ : |
|
| 253 | + array_push( $warnings, __( self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift' ) ); |
|
| 254 | + |
|
| 255 | + // Is the current entity related to at least 1 entity? |
|
| 256 | + // Was the current entity already disambiguated? |
|
| 257 | + ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 258 | + $score ++ : |
|
| 259 | + array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift' ) ); |
|
| 260 | + |
|
| 261 | + // Is the entity published? |
|
| 262 | + ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 263 | + $score ++ : |
|
| 264 | + array_push( $warnings, __( self::RATING_WARNING_IS_PUBLISHED, 'wordlift' ) ); |
|
| 265 | + |
|
| 266 | + // Has a thumbnail? |
|
| 267 | + ( has_post_thumbnail( $post->ID ) ) ? |
|
| 268 | + $score ++ : |
|
| 269 | + array_push( $warnings, __( self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift' ) ); |
|
| 270 | + |
|
| 271 | + // Get all post meta keys for the current post |
|
| 272 | + global $wpdb; |
|
| 273 | + |
|
| 274 | + $query = $wpdb->prepare( |
|
| 275 | + "SELECT DISTINCT (meta_key) FROM $wpdb->postmeta WHERE post_id = %d", $post->ID |
|
| 276 | + ); |
|
| 277 | + |
|
| 278 | + // Check intersection between available meta keys and expected ones |
|
| 279 | + // arrays to detect missing values. |
|
| 280 | + $available_meta_keys = $wpdb->get_col( $query ); |
|
| 281 | + |
|
| 282 | + // If each expected key is contained in available keys array ... |
|
| 283 | + ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 284 | + $score ++ : |
|
| 285 | + array_push( $warnings, __( self::RATING_WARNING_HAS_SAME_AS, 'wordlift' ) ); |
|
| 286 | + |
|
| 287 | + $schema = $this->entity_type_service->get( $post_id ); |
|
| 288 | + |
|
| 289 | + $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 290 | + array() : |
|
| 291 | + array_keys( $schema['custom_fields'] ); |
|
| 292 | + |
|
| 293 | + $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 294 | + // If each expected key is contained in available keys array ... |
|
| 295 | + ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 296 | + $score ++ : |
|
| 297 | + array_push( $warnings, __( self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift' ) ); |
|
| 298 | + |
|
| 299 | + // Finally return score and warnings |
|
| 300 | + return array( |
|
| 301 | + 'raw_score' => $score, |
|
| 302 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 303 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 304 | + 'warnings' => $warnings, |
|
| 305 | + ); |
|
| 306 | + |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Get as rating as input and convert in a traffic-light rating |
|
| 311 | + * |
|
| 312 | + * @since 3.3.0 |
|
| 313 | + * |
|
| 314 | + * @param int $score The rating score for a given entity. |
|
| 315 | + * |
|
| 316 | + * @return string The input HTML code. |
|
| 317 | + */ |
|
| 318 | + private function convert_raw_score_to_traffic_light( $score ) { |
|
| 319 | + // RATING_MAX : $score = 3 : x |
|
| 320 | + // See http://php.net/manual/en/function.round.php |
|
| 321 | + $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 322 | + |
|
| 323 | + // If rating is 0, return 1, otherwise return rating |
|
| 324 | + return ( 0 == $rating ) ? 1 : $rating; |
|
| 325 | + |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Get as rating as input and convert in a traffic-light rating |
|
| 330 | + * |
|
| 331 | + * @since 3.3.0 |
|
| 332 | + * |
|
| 333 | + * @param int $score The rating score for a given entity. |
|
| 334 | + * |
|
| 335 | + * @return string The input HTML code. |
|
| 336 | + */ |
|
| 337 | + public function convert_raw_score_to_percentage( $score ) { |
|
| 338 | + |
|
| 339 | + // RATING_MAX : $score = 100 : x |
|
| 340 | + return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Add admin notices for the current entity depending on the current rating. |
|
| 346 | + * |
|
| 347 | + * @since 3.3.0 |
|
| 348 | + */ |
|
| 349 | + public function in_admin_header() { |
|
| 350 | + |
|
| 351 | + // Return safely if get_current_screen() is not defined (yet) |
|
| 352 | + if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 353 | + return; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + $screen = get_current_screen(); |
|
| 357 | + // If there is any valid screen nothing to do |
|
| 358 | + if ( null === $screen ) { |
|
| 359 | + return; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + // If you're not in the entity post edit page, return. |
|
| 363 | + if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 364 | + return; |
|
| 365 | + } |
|
| 366 | + // Retrieve the current global post |
|
| 367 | + global $post; |
|
| 368 | + // If it's not an entity, return. |
|
| 369 | + if ( ! $this->entity_service->is_entity( $post->ID ) ) { |
|
| 370 | + return; |
|
| 371 | + } |
|
| 372 | + // Retrieve an updated rating for the current entity |
|
| 373 | + $rating = $this->get_rating_for( $post->ID, true ); |
|
| 374 | + |
|
| 375 | + // If there is at least 1 warning |
|
| 376 | + if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 377 | + // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
| 378 | + $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | 383 | } |
@@ -129,13 +129,13 @@ discard block |
||
| 129 | 129 | * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
| 130 | 130 | * @param \Wordlift_Notice_Service $notice_service A {@link Wordlift_Notice_Service} instance. |
| 131 | 131 | */ |
| 132 | - public function __construct( $entity_service, $entity_type_service, $notice_service ) { |
|
| 132 | + public function __construct($entity_service, $entity_type_service, $notice_service) { |
|
| 133 | 133 | |
| 134 | 134 | $this->entity_service = $entity_service; |
| 135 | 135 | $this->entity_type_service = $entity_type_service; |
| 136 | 136 | $this->notice_service = $notice_service; |
| 137 | 137 | |
| 138 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 138 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Rating_Service'); |
|
| 139 | 139 | |
| 140 | 140 | } |
| 141 | 141 | |
@@ -148,19 +148,19 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @return array An array representing the rating obj. |
| 150 | 150 | */ |
| 151 | - public function set_rating_for( $post_id ) { |
|
| 151 | + public function set_rating_for($post_id) { |
|
| 152 | 152 | |
| 153 | 153 | // Calculate rating for the given post. |
| 154 | - $rating = $this->calculate_rating_for( $post_id ); |
|
| 154 | + $rating = $this->calculate_rating_for($post_id); |
|
| 155 | 155 | |
| 156 | 156 | // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
| 157 | 157 | // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
| 158 | 158 | // |
| 159 | 159 | // See https://codex.wordpress.org/Function_Reference/update_post_meta |
| 160 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 161 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 160 | + update_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score']); |
|
| 161 | + update_post_meta($post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings']); |
|
| 162 | 162 | |
| 163 | - $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 163 | + $this->log->trace(sprintf("Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'])); |
|
| 164 | 164 | |
| 165 | 165 | // Finally returns the rating |
| 166 | 166 | return $rating; |
@@ -177,30 +177,30 @@ discard block |
||
| 177 | 177 | * |
| 178 | 178 | * @return array An array representing the rating obj. |
| 179 | 179 | */ |
| 180 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 180 | + public function get_rating_for($post_id, $force_reload = false) { |
|
| 181 | 181 | |
| 182 | 182 | // If forced reload is required or rating is missing. |
| 183 | - if ( $force_reload ) { |
|
| 184 | - $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 183 | + if ($force_reload) { |
|
| 184 | + $this->log->trace("Force rating reload [ post_id :: $post_id ]"); |
|
| 185 | 185 | |
| 186 | - return $this->set_rating_for( $post_id ); |
|
| 186 | + return $this->set_rating_for($post_id); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 189 | + $current_raw_score = get_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, true); |
|
| 190 | 190 | |
| 191 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
| 192 | - $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 191 | + if ( ! is_numeric($current_raw_score)) { |
|
| 192 | + $this->log->trace("Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]"); |
|
| 193 | 193 | |
| 194 | - return $this->set_rating_for( $post_id ); |
|
| 194 | + return $this->set_rating_for($post_id); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 197 | + $current_warnings = get_post_meta($post_id, self::RATING_WARNINGS_META_KEY, true); |
|
| 198 | 198 | |
| 199 | 199 | // Finally return score and warnings |
| 200 | 200 | return array( |
| 201 | 201 | 'raw_score' => $current_raw_score, |
| 202 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 203 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 202 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($current_raw_score), |
|
| 203 | + 'percentage_score' => $this->convert_raw_score_to_percentage($current_raw_score), |
|
| 204 | 204 | 'warnings' => $current_warnings, |
| 205 | 205 | ); |
| 206 | 206 | |
@@ -227,14 +227,14 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | * @return array An array representing the rating obj. |
| 229 | 229 | */ |
| 230 | - private function calculate_rating_for( $post_id ) { |
|
| 230 | + private function calculate_rating_for($post_id) { |
|
| 231 | 231 | |
| 232 | 232 | // If it's not an entity, return. |
| 233 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 233 | + if ( ! $this->entity_service->is_entity($post_id)) { |
|
| 234 | 234 | return array(); |
| 235 | 235 | } |
| 236 | 236 | // Retrieve the post object. |
| 237 | - $post = get_post( $post_id ); |
|
| 237 | + $post = get_post($post_id); |
|
| 238 | 238 | |
| 239 | 239 | // Rating value. |
| 240 | 240 | $score = 0; |
@@ -243,30 +243,25 @@ discard block |
||
| 243 | 243 | $warnings = array(); |
| 244 | 244 | |
| 245 | 245 | // Is the current entity related to at least 1 post? |
| 246 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 247 | - $score ++ : |
|
| 248 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift' ) ); |
|
| 246 | + (0 < count(wl_core_get_related_post_ids($post->ID))) ? |
|
| 247 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift')); |
|
| 249 | 248 | |
| 250 | 249 | // Is the post content not empty? |
| 251 | - ( ! empty( $post->post_content ) ) ? |
|
| 252 | - $score ++ : |
|
| 253 | - array_push( $warnings, __( self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift' ) ); |
|
| 250 | + ( ! empty($post->post_content)) ? |
|
| 251 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift')); |
|
| 254 | 252 | |
| 255 | 253 | // Is the current entity related to at least 1 entity? |
| 256 | 254 | // Was the current entity already disambiguated? |
| 257 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 258 | - $score ++ : |
|
| 259 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift' ) ); |
|
| 255 | + (0 < count(wl_core_get_related_entity_ids($post->ID))) ? |
|
| 256 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift')); |
|
| 260 | 257 | |
| 261 | 258 | // Is the entity published? |
| 262 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 263 | - $score ++ : |
|
| 264 | - array_push( $warnings, __( self::RATING_WARNING_IS_PUBLISHED, 'wordlift' ) ); |
|
| 259 | + ('publish' === get_post_status($post->ID)) ? |
|
| 260 | + $score++ : array_push($warnings, __(self::RATING_WARNING_IS_PUBLISHED, 'wordlift')); |
|
| 265 | 261 | |
| 266 | 262 | // Has a thumbnail? |
| 267 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
| 268 | - $score ++ : |
|
| 269 | - array_push( $warnings, __( self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift' ) ); |
|
| 263 | + (has_post_thumbnail($post->ID)) ? |
|
| 264 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift')); |
|
| 270 | 265 | |
| 271 | 266 | // Get all post meta keys for the current post |
| 272 | 267 | global $wpdb; |
@@ -277,30 +272,27 @@ discard block |
||
| 277 | 272 | |
| 278 | 273 | // Check intersection between available meta keys and expected ones |
| 279 | 274 | // arrays to detect missing values. |
| 280 | - $available_meta_keys = $wpdb->get_col( $query ); |
|
| 275 | + $available_meta_keys = $wpdb->get_col($query); |
|
| 281 | 276 | |
| 282 | 277 | // If each expected key is contained in available keys array ... |
| 283 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 284 | - $score ++ : |
|
| 285 | - array_push( $warnings, __( self::RATING_WARNING_HAS_SAME_AS, 'wordlift' ) ); |
|
| 278 | + (in_array(Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys)) ? |
|
| 279 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_SAME_AS, 'wordlift')); |
|
| 286 | 280 | |
| 287 | - $schema = $this->entity_type_service->get( $post_id ); |
|
| 281 | + $schema = $this->entity_type_service->get($post_id); |
|
| 288 | 282 | |
| 289 | - $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 290 | - array() : |
|
| 291 | - array_keys( $schema['custom_fields'] ); |
|
| 283 | + $expected_meta_keys = (null === $schema['custom_fields']) ? |
|
| 284 | + array() : array_keys($schema['custom_fields']); |
|
| 292 | 285 | |
| 293 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 286 | + $intersection = array_intersect($expected_meta_keys, $available_meta_keys); |
|
| 294 | 287 | // If each expected key is contained in available keys array ... |
| 295 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 296 | - $score ++ : |
|
| 297 | - array_push( $warnings, __( self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift' ) ); |
|
| 288 | + (count($intersection) === count($expected_meta_keys)) ? |
|
| 289 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift')); |
|
| 298 | 290 | |
| 299 | 291 | // Finally return score and warnings |
| 300 | 292 | return array( |
| 301 | 293 | 'raw_score' => $score, |
| 302 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 303 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 294 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($score), |
|
| 295 | + 'percentage_score' => $this->convert_raw_score_to_percentage($score), |
|
| 304 | 296 | 'warnings' => $warnings, |
| 305 | 297 | ); |
| 306 | 298 | |
@@ -315,13 +307,13 @@ discard block |
||
| 315 | 307 | * |
| 316 | 308 | * @return string The input HTML code. |
| 317 | 309 | */ |
| 318 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
| 310 | + private function convert_raw_score_to_traffic_light($score) { |
|
| 319 | 311 | // RATING_MAX : $score = 3 : x |
| 320 | 312 | // See http://php.net/manual/en/function.round.php |
| 321 | - $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 313 | + $rating = round(($score * 3) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP); |
|
| 322 | 314 | |
| 323 | 315 | // If rating is 0, return 1, otherwise return rating |
| 324 | - return ( 0 == $rating ) ? 1 : $rating; |
|
| 316 | + return (0 == $rating) ? 1 : $rating; |
|
| 325 | 317 | |
| 326 | 318 | } |
| 327 | 319 | |
@@ -334,10 +326,10 @@ discard block |
||
| 334 | 326 | * |
| 335 | 327 | * @return string The input HTML code. |
| 336 | 328 | */ |
| 337 | - public function convert_raw_score_to_percentage( $score ) { |
|
| 329 | + public function convert_raw_score_to_percentage($score) { |
|
| 338 | 330 | |
| 339 | 331 | // RATING_MAX : $score = 100 : x |
| 340 | - return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 332 | + return round(($score * 100) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP); |
|
| 341 | 333 | } |
| 342 | 334 | |
| 343 | 335 | |
@@ -349,33 +341,33 @@ discard block |
||
| 349 | 341 | public function in_admin_header() { |
| 350 | 342 | |
| 351 | 343 | // Return safely if get_current_screen() is not defined (yet) |
| 352 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 344 | + if (false === function_exists('get_current_screen')) { |
|
| 353 | 345 | return; |
| 354 | 346 | } |
| 355 | 347 | |
| 356 | 348 | $screen = get_current_screen(); |
| 357 | 349 | // If there is any valid screen nothing to do |
| 358 | - if ( null === $screen ) { |
|
| 350 | + if (null === $screen) { |
|
| 359 | 351 | return; |
| 360 | 352 | } |
| 361 | 353 | |
| 362 | 354 | // If you're not in the entity post edit page, return. |
| 363 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 355 | + if (Wordlift_Entity_Service::TYPE_NAME !== $screen->id) { |
|
| 364 | 356 | return; |
| 365 | 357 | } |
| 366 | 358 | // Retrieve the current global post |
| 367 | 359 | global $post; |
| 368 | 360 | // If it's not an entity, return. |
| 369 | - if ( ! $this->entity_service->is_entity( $post->ID ) ) { |
|
| 361 | + if ( ! $this->entity_service->is_entity($post->ID)) { |
|
| 370 | 362 | return; |
| 371 | 363 | } |
| 372 | 364 | // Retrieve an updated rating for the current entity |
| 373 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
| 365 | + $rating = $this->get_rating_for($post->ID, true); |
|
| 374 | 366 | |
| 375 | 367 | // If there is at least 1 warning |
| 376 | - if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 368 | + if (isset($rating['warnings']) && 0 < count($rating['warnings'])) { |
|
| 377 | 369 | // TODO - Pass Wordlift_Notice_Service trough the service constructor |
| 378 | - $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 370 | + $this->notice_service->add_suggestion($rating['warnings']); |
|
| 379 | 371 | } |
| 380 | 372 | |
| 381 | 373 | } |
@@ -269,8 +269,8 @@ discard block |
||
| 269 | 269 | * If $predicate is omitted, $predicate filter is not applied |
| 270 | 270 | * Do not use it directly. Use wl_core_get_related_entities or wl_core_get_related_entity_ids instead. |
| 271 | 271 | * |
| 272 | - * @param $get |
|
| 273 | - * @param $item_id |
|
| 272 | + * @param string $get |
|
| 273 | + * @param integer $item_id |
|
| 274 | 274 | * @param string $predicate Name of the relation: null | 'what' | 'where' | 'when' | 'who' |
| 275 | 275 | * @param string $post_status Filter by post status null | 'publish' | 'draft' | 'pending' | 'trash'. null means *any* post status |
| 276 | 276 | * |
@@ -345,9 +345,10 @@ discard block |
||
| 345 | 345 | * If $predicate is omitted, $predicate filter is not applied |
| 346 | 346 | * Not use it directly. Use wl_core_get_related_posts or wl_core_get_related_posts_ids instead. |
| 347 | 347 | * |
| 348 | - * @param int $object_id The entity ID or the post ID. |
|
| 349 | 348 | * @param string $predicate Name of the relation: null | 'what' | 'where' | 'when' | 'who' |
| 350 | 349 | * @param string $post_status Filter by post status null | 'publish' | 'draft' | 'pending' | 'trash'. null means *any* post status |
| 350 | + * @param string $get |
|
| 351 | + * @param integer $item_id |
|
| 351 | 352 | * |
| 352 | 353 | * @return array Array of objects. |
| 353 | 354 | */ |
@@ -9,12 +9,12 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | function wl_core_check_relation_predicate_is_supported( $predicate ) { |
| 11 | 11 | |
| 12 | - return in_array( $predicate, array( |
|
| 13 | - WL_WHAT_RELATION, |
|
| 14 | - WL_WHEN_RELATION, |
|
| 15 | - WL_WHERE_RELATION, |
|
| 16 | - WL_WHO_RELATION, |
|
| 17 | - ) ); |
|
| 12 | + return in_array( $predicate, array( |
|
| 13 | + WL_WHAT_RELATION, |
|
| 14 | + WL_WHEN_RELATION, |
|
| 15 | + WL_WHERE_RELATION, |
|
| 16 | + WL_WHO_RELATION, |
|
| 17 | + ) ); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | function wl_core_get_validation_rules() { |
| 26 | 26 | |
| 27 | - return unserialize( WL_CORE_GET_POSTS_VALIDATION_RULES ); |
|
| 27 | + return unserialize( WL_CORE_GET_POSTS_VALIDATION_RULES ); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -34,10 +34,10 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | function wl_core_get_relation_instances_table_name() { |
| 36 | 36 | |
| 37 | - global $wpdb; |
|
| 38 | - $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
| 37 | + global $wpdb; |
|
| 38 | + $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
| 39 | 39 | |
| 40 | - return $table_name; |
|
| 40 | + return $table_name; |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -52,33 +52,33 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | function wl_core_add_relation_instance( $subject_id, $predicate, $object_id ) { |
| 54 | 54 | |
| 55 | - // Checks on subject and object |
|
| 56 | - if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 57 | - return false; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - // Checks on the given relation |
|
| 61 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 62 | - return false; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // Prepare interaction with db |
|
| 66 | - global $wpdb; |
|
| 67 | - |
|
| 68 | - // Checks passed. Add relation if not exists: |
|
| 69 | - // See https://codex.wordpress.org/Class_Reference/wpdb#REPLACE_row |
|
| 70 | - $wpdb->replace( |
|
| 71 | - wl_core_get_relation_instances_table_name(), |
|
| 72 | - array( |
|
| 73 | - 'subject_id' => $subject_id, |
|
| 74 | - 'predicate' => $predicate, |
|
| 75 | - 'object_id' => $object_id, |
|
| 76 | - ), |
|
| 77 | - array( '%d', '%s', '%d' ) |
|
| 78 | - ); |
|
| 79 | - |
|
| 80 | - // Return record id |
|
| 81 | - return $wpdb->insert_id; |
|
| 55 | + // Checks on subject and object |
|
| 56 | + if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 57 | + return false; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + // Checks on the given relation |
|
| 61 | + if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 62 | + return false; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // Prepare interaction with db |
|
| 66 | + global $wpdb; |
|
| 67 | + |
|
| 68 | + // Checks passed. Add relation if not exists: |
|
| 69 | + // See https://codex.wordpress.org/Class_Reference/wpdb#REPLACE_row |
|
| 70 | + $wpdb->replace( |
|
| 71 | + wl_core_get_relation_instances_table_name(), |
|
| 72 | + array( |
|
| 73 | + 'subject_id' => $subject_id, |
|
| 74 | + 'predicate' => $predicate, |
|
| 75 | + 'object_id' => $object_id, |
|
| 76 | + ), |
|
| 77 | + array( '%d', '%s', '%d' ) |
|
| 78 | + ); |
|
| 79 | + |
|
| 80 | + // Return record id |
|
| 81 | + return $wpdb->insert_id; |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -93,33 +93,33 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | function wl_core_delete_relation_instance( $subject_id, $predicate, $object_id ) { |
| 95 | 95 | |
| 96 | - // Checks on subject and object |
|
| 97 | - if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // Checks on the given relation |
|
| 102 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 103 | - return false; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - // Prepare interaction with db |
|
| 107 | - global $wpdb; |
|
| 108 | - |
|
| 109 | - wl_write_log( "Going to delete relation instace [ subject_id :: $subject_id ] [ object_id :: $object_id ] [ predicate :: $predicate ]" ); |
|
| 110 | - |
|
| 111 | - // @see ttps://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
|
| 112 | - $wpdb->delete( |
|
| 113 | - wl_core_get_relation_instances_table_name(), |
|
| 114 | - array( |
|
| 115 | - 'subject_id' => $subject_id, |
|
| 116 | - 'predicate' => $predicate, |
|
| 117 | - 'object_id' => $object_id, |
|
| 118 | - ), |
|
| 119 | - array( '%d', '%s', '%d' ) |
|
| 120 | - ); |
|
| 121 | - |
|
| 122 | - return true; |
|
| 96 | + // Checks on subject and object |
|
| 97 | + if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // Checks on the given relation |
|
| 102 | + if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 103 | + return false; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + // Prepare interaction with db |
|
| 107 | + global $wpdb; |
|
| 108 | + |
|
| 109 | + wl_write_log( "Going to delete relation instace [ subject_id :: $subject_id ] [ object_id :: $object_id ] [ predicate :: $predicate ]" ); |
|
| 110 | + |
|
| 111 | + // @see ttps://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
|
| 112 | + $wpdb->delete( |
|
| 113 | + wl_core_get_relation_instances_table_name(), |
|
| 114 | + array( |
|
| 115 | + 'subject_id' => $subject_id, |
|
| 116 | + 'predicate' => $predicate, |
|
| 117 | + 'object_id' => $object_id, |
|
| 118 | + ), |
|
| 119 | + array( '%d', '%s', '%d' ) |
|
| 120 | + ); |
|
| 121 | + |
|
| 122 | + return true; |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
@@ -134,29 +134,29 @@ discard block |
||
| 134 | 134 | */ |
| 135 | 135 | function wl_core_add_relation_instances( $subject_id, $predicate, $object_ids ) { |
| 136 | 136 | |
| 137 | - // Checks on subject and object |
|
| 138 | - if ( ! is_numeric( $subject_id ) ) { |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - // Checks on the given relation |
|
| 143 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 144 | - return false; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // Check $object_ids is an array |
|
| 148 | - if ( ! is_array( $object_ids ) || empty( $object_ids ) ) { |
|
| 149 | - return false; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // Call method to check and add each single relation |
|
| 153 | - $inserted_records_ids = array(); |
|
| 154 | - foreach ( $object_ids as $object_id ) { |
|
| 155 | - $new_record_id = wl_core_add_relation_instance( $subject_id, $predicate, $object_id ); |
|
| 156 | - $inserted_records_ids[] = $new_record_id; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return $inserted_records_ids; |
|
| 137 | + // Checks on subject and object |
|
| 138 | + if ( ! is_numeric( $subject_id ) ) { |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + // Checks on the given relation |
|
| 143 | + if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 144 | + return false; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // Check $object_ids is an array |
|
| 148 | + if ( ! is_array( $object_ids ) || empty( $object_ids ) ) { |
|
| 149 | + return false; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // Call method to check and add each single relation |
|
| 153 | + $inserted_records_ids = array(); |
|
| 154 | + foreach ( $object_ids as $object_id ) { |
|
| 155 | + $new_record_id = wl_core_add_relation_instance( $subject_id, $predicate, $object_id ); |
|
| 156 | + $inserted_records_ids[] = $new_record_id; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return $inserted_records_ids; |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | /** |
@@ -169,26 +169,26 @@ discard block |
||
| 169 | 169 | */ |
| 170 | 170 | function wl_core_delete_relation_instances( $subject_id ) { |
| 171 | 171 | |
| 172 | - // Checks on subject and object |
|
| 173 | - if ( ! is_numeric( $subject_id ) ) { |
|
| 174 | - return false; |
|
| 175 | - } |
|
| 172 | + // Checks on subject and object |
|
| 173 | + if ( ! is_numeric( $subject_id ) ) { |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - // Prepare interaction with db |
|
| 178 | - global $wpdb; |
|
| 177 | + // Prepare interaction with db |
|
| 178 | + global $wpdb; |
|
| 179 | 179 | |
| 180 | - // wl_write_log( "Going to delete relation instances [ subject_id :: $subject_id ]"); |
|
| 180 | + // wl_write_log( "Going to delete relation instances [ subject_id :: $subject_id ]"); |
|
| 181 | 181 | |
| 182 | - // @see ttps://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
|
| 183 | - $wpdb->delete( |
|
| 184 | - wl_core_get_relation_instances_table_name(), |
|
| 185 | - array( |
|
| 186 | - 'subject_id' => $subject_id, |
|
| 187 | - ), |
|
| 188 | - array( '%d' ) |
|
| 189 | - ); |
|
| 182 | + // @see ttps://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
|
| 183 | + $wpdb->delete( |
|
| 184 | + wl_core_get_relation_instances_table_name(), |
|
| 185 | + array( |
|
| 186 | + 'subject_id' => $subject_id, |
|
| 187 | + ), |
|
| 188 | + array( '%d' ) |
|
| 189 | + ); |
|
| 190 | 190 | |
| 191 | - return true; |
|
| 191 | + return true; |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
@@ -204,18 +204,18 @@ discard block |
||
| 204 | 204 | */ |
| 205 | 205 | function wl_core_validate_filters_for_related( $filters ) { |
| 206 | 206 | |
| 207 | - if ( ! is_array( $filters ) ) { |
|
| 208 | - $filters = array(); |
|
| 209 | - } |
|
| 207 | + if ( ! is_array( $filters ) ) { |
|
| 208 | + $filters = array(); |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - if ( ! isset( $filters['predicate'] ) ) { |
|
| 212 | - $filters['predicate'] = null; |
|
| 213 | - } |
|
| 214 | - if ( ! isset( $filters['status'] ) ) { |
|
| 215 | - $filters['status'] = null; |
|
| 216 | - } |
|
| 211 | + if ( ! isset( $filters['predicate'] ) ) { |
|
| 212 | + $filters['predicate'] = null; |
|
| 213 | + } |
|
| 214 | + if ( ! isset( $filters['status'] ) ) { |
|
| 215 | + $filters['status'] = null; |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - return $filters; |
|
| 218 | + return $filters; |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
@@ -236,9 +236,9 @@ discard block |
||
| 236 | 236 | */ |
| 237 | 237 | function wl_core_get_related_entities( $subject_id, $filters = array() ) { |
| 238 | 238 | |
| 239 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 239 | + $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 240 | 240 | |
| 241 | - return wl_core_inner_get_related_entities( "posts", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 241 | + return wl_core_inner_get_related_entities( "posts", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -259,9 +259,9 @@ discard block |
||
| 259 | 259 | */ |
| 260 | 260 | function wl_core_get_related_entity_ids( $subject_id, $filters = array() ) { |
| 261 | 261 | |
| 262 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 262 | + $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 263 | 263 | |
| 264 | - return wl_core_inner_get_related_entities( "post_ids", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 264 | + return wl_core_inner_get_related_entities( "post_ids", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
@@ -278,20 +278,20 @@ discard block |
||
| 278 | 278 | */ |
| 279 | 279 | function wl_core_inner_get_related_entities( $get, $item_id, $predicate = null, $post_status = null ) { |
| 280 | 280 | |
| 281 | - if ( $results = wl_core_get_posts( array( |
|
| 282 | - 'get' => $get, |
|
| 283 | - 'post_type' => 'entity', |
|
| 284 | - 'post_status' => $post_status, |
|
| 285 | - 'related_to' => $item_id, |
|
| 286 | - 'as' => 'object', |
|
| 287 | - 'with_predicate' => $predicate, |
|
| 288 | - ) ) |
|
| 289 | - ) { |
|
| 290 | - return $results; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - // If wl_core_get_posts return false then an empty array is returned |
|
| 294 | - return array(); |
|
| 281 | + if ( $results = wl_core_get_posts( array( |
|
| 282 | + 'get' => $get, |
|
| 283 | + 'post_type' => 'entity', |
|
| 284 | + 'post_status' => $post_status, |
|
| 285 | + 'related_to' => $item_id, |
|
| 286 | + 'as' => 'object', |
|
| 287 | + 'with_predicate' => $predicate, |
|
| 288 | + ) ) |
|
| 289 | + ) { |
|
| 290 | + return $results; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + // If wl_core_get_posts return false then an empty array is returned |
|
| 294 | + return array(); |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** |
@@ -312,9 +312,9 @@ discard block |
||
| 312 | 312 | */ |
| 313 | 313 | function wl_core_get_related_posts( $object_id, $filters = array() ) { |
| 314 | 314 | |
| 315 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 315 | + $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 316 | 316 | |
| 317 | - return wl_core_inner_get_related_posts( "posts", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 317 | + return wl_core_inner_get_related_posts( "posts", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -335,9 +335,9 @@ discard block |
||
| 335 | 335 | */ |
| 336 | 336 | function wl_core_get_related_post_ids( $object_id, $filters = array() ) { |
| 337 | 337 | |
| 338 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 338 | + $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 339 | 339 | |
| 340 | - return wl_core_inner_get_related_posts( "post_ids", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 340 | + return wl_core_inner_get_related_posts( "post_ids", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | /** |
@@ -353,41 +353,41 @@ discard block |
||
| 353 | 353 | */ |
| 354 | 354 | function wl_core_inner_get_related_posts( $get, $item_id, $predicate = null, $post_status = null ) { |
| 355 | 355 | |
| 356 | - // Retrieve the post object |
|
| 357 | - $post = get_post( $item_id ); |
|
| 358 | - if ( null === $post ) { |
|
| 359 | - return array(); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - if ( "entity" === $post->post_type ) { |
|
| 363 | - if ( $results = wl_core_get_posts( array( |
|
| 364 | - 'get' => $get, |
|
| 365 | - 'post_type' => 'post', |
|
| 366 | - 'post_status' => $post_status, |
|
| 367 | - 'related_to' => $item_id, |
|
| 368 | - 'as' => 'subject', |
|
| 369 | - 'with_predicate' => $predicate, |
|
| 370 | - ) ) |
|
| 371 | - ) { |
|
| 372 | - return $results; |
|
| 373 | - } |
|
| 374 | - } else { |
|
| 375 | - if ( $results = wl_core_get_posts( array( |
|
| 376 | - 'get' => $get, |
|
| 377 | - 'post_type' => 'post', |
|
| 378 | - 'post_status' => $post_status, |
|
| 379 | - 'post__not_in' => array( $item_id ), |
|
| 380 | - 'related_to__in' => wl_core_get_related_entity_ids( $post->ID ), |
|
| 381 | - 'as' => 'subject', |
|
| 382 | - 'with_predicate' => $predicate, |
|
| 383 | - ) ) |
|
| 384 | - ) { |
|
| 385 | - return $results; |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - // If wl_core_get_posts return false then an empty array is returned |
|
| 390 | - return array(); |
|
| 356 | + // Retrieve the post object |
|
| 357 | + $post = get_post( $item_id ); |
|
| 358 | + if ( null === $post ) { |
|
| 359 | + return array(); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + if ( "entity" === $post->post_type ) { |
|
| 363 | + if ( $results = wl_core_get_posts( array( |
|
| 364 | + 'get' => $get, |
|
| 365 | + 'post_type' => 'post', |
|
| 366 | + 'post_status' => $post_status, |
|
| 367 | + 'related_to' => $item_id, |
|
| 368 | + 'as' => 'subject', |
|
| 369 | + 'with_predicate' => $predicate, |
|
| 370 | + ) ) |
|
| 371 | + ) { |
|
| 372 | + return $results; |
|
| 373 | + } |
|
| 374 | + } else { |
|
| 375 | + if ( $results = wl_core_get_posts( array( |
|
| 376 | + 'get' => $get, |
|
| 377 | + 'post_type' => 'post', |
|
| 378 | + 'post_status' => $post_status, |
|
| 379 | + 'post__not_in' => array( $item_id ), |
|
| 380 | + 'related_to__in' => wl_core_get_related_entity_ids( $post->ID ), |
|
| 381 | + 'as' => 'subject', |
|
| 382 | + 'with_predicate' => $predicate, |
|
| 383 | + ) ) |
|
| 384 | + ) { |
|
| 385 | + return $results; |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + // If wl_core_get_posts return false then an empty array is returned |
|
| 390 | + return array(); |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | /** |
@@ -417,75 +417,75 @@ discard block |
||
| 417 | 417 | */ |
| 418 | 418 | function wl_core_sql_query_builder( $args ) { |
| 419 | 419 | |
| 420 | - // Prepare interaction with db |
|
| 421 | - global $wpdb; |
|
| 422 | - // Retrieve Wordlift relation instances table name |
|
| 423 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 424 | - |
|
| 425 | - // Sql Action |
|
| 426 | - $sql = "SELECT "; |
|
| 427 | - // Determine what has to be returned depending on 'get' argument value |
|
| 428 | - switch ( $args['get'] ) { |
|
| 429 | - case 'posts': |
|
| 430 | - $sql .= "p.*"; |
|
| 431 | - break; |
|
| 432 | - case 'post_ids': |
|
| 433 | - $sql .= "p.id"; |
|
| 434 | - break; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - // If we look for posts related as objects the JOIN has to be done with the object_id column and viceversa |
|
| 438 | - $join_column = $args['as'] . "_id"; |
|
| 439 | - |
|
| 440 | - $sql .= " FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.$join_column"; |
|
| 441 | - |
|
| 442 | - // Sql add post type filter |
|
| 443 | - $sql .= $wpdb->prepare( " AND p.post_type = %s AND", $args['post_type'] ); |
|
| 444 | - |
|
| 445 | - // Sql add post status filter |
|
| 446 | - if ( isset( $args['post_status'] ) && ! is_null( $args['post_status'] ) ) { |
|
| 447 | - $sql .= $wpdb->prepare( " p.post_status = %s AND", $args['post_status'] ); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - // Add filtering conditions |
|
| 451 | - // If we look for posts related as objects this means that |
|
| 452 | - // related_to is a reference for a subject: subject_id is the filtering column |
|
| 453 | - // If we look for posts related as subject this means that |
|
| 454 | - // related_to is reference for an object: object_id is the filtering column |
|
| 455 | - |
|
| 456 | - $filtering_column = ( 'object' == $args['as'] ) ? "subject_id" : "object_id"; |
|
| 457 | - |
|
| 458 | - if ( isset( $args['related_to'] ) ) { |
|
| 459 | - $sql .= $wpdb->prepare( " r.$filtering_column = %d", $args['related_to'] ); |
|
| 460 | - } |
|
| 461 | - if ( isset( $args['related_to'] ) && isset( $args['related_to__in'] ) ) { |
|
| 462 | - $sql .= " AND"; |
|
| 463 | - } |
|
| 464 | - if ( isset( $args['related_to__in'] ) ) { |
|
| 465 | - $sql .= " r.$filtering_column IN (" . implode( ",", $args['related_to__in'] ) . ")"; |
|
| 466 | - } |
|
| 467 | - if ( isset( $args['post__not_in'] ) ) { |
|
| 468 | - $sql .= " AND r." . $args['as'] . "_id NOT IN (" . implode( ",", $args['post__not_in'] ) . ")"; |
|
| 469 | - } |
|
| 470 | - if ( isset( $args['post__in'] ) ) { |
|
| 471 | - $sql .= " AND r." . $args['as'] . "_id IN (" . implode( ",", $args['post__in'] ) . ")"; |
|
| 472 | - } |
|
| 473 | - // Add predicate filter if required |
|
| 474 | - if ( isset( $args['with_predicate'] ) ) { |
|
| 475 | - // Sql Inner Join clausole |
|
| 476 | - $sql .= $wpdb->prepare( " AND r.predicate = %s", $args['with_predicate'] ); |
|
| 477 | - } |
|
| 478 | - // Add a group by clousole to avoid duplicated rows |
|
| 479 | - $sql .= " GROUP BY p.id"; |
|
| 480 | - |
|
| 481 | - if ( isset( $args['first'] ) && is_numeric( $args['first'] ) ) { |
|
| 482 | - // Sql Inner Join clausole |
|
| 483 | - $sql .= $wpdb->prepare( " LIMIT %d", $args['first'] ); |
|
| 484 | - } |
|
| 485 | - // Close sql statement |
|
| 486 | - $sql .= ";"; |
|
| 487 | - |
|
| 488 | - return $sql; |
|
| 420 | + // Prepare interaction with db |
|
| 421 | + global $wpdb; |
|
| 422 | + // Retrieve Wordlift relation instances table name |
|
| 423 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 424 | + |
|
| 425 | + // Sql Action |
|
| 426 | + $sql = "SELECT "; |
|
| 427 | + // Determine what has to be returned depending on 'get' argument value |
|
| 428 | + switch ( $args['get'] ) { |
|
| 429 | + case 'posts': |
|
| 430 | + $sql .= "p.*"; |
|
| 431 | + break; |
|
| 432 | + case 'post_ids': |
|
| 433 | + $sql .= "p.id"; |
|
| 434 | + break; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + // If we look for posts related as objects the JOIN has to be done with the object_id column and viceversa |
|
| 438 | + $join_column = $args['as'] . "_id"; |
|
| 439 | + |
|
| 440 | + $sql .= " FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.$join_column"; |
|
| 441 | + |
|
| 442 | + // Sql add post type filter |
|
| 443 | + $sql .= $wpdb->prepare( " AND p.post_type = %s AND", $args['post_type'] ); |
|
| 444 | + |
|
| 445 | + // Sql add post status filter |
|
| 446 | + if ( isset( $args['post_status'] ) && ! is_null( $args['post_status'] ) ) { |
|
| 447 | + $sql .= $wpdb->prepare( " p.post_status = %s AND", $args['post_status'] ); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + // Add filtering conditions |
|
| 451 | + // If we look for posts related as objects this means that |
|
| 452 | + // related_to is a reference for a subject: subject_id is the filtering column |
|
| 453 | + // If we look for posts related as subject this means that |
|
| 454 | + // related_to is reference for an object: object_id is the filtering column |
|
| 455 | + |
|
| 456 | + $filtering_column = ( 'object' == $args['as'] ) ? "subject_id" : "object_id"; |
|
| 457 | + |
|
| 458 | + if ( isset( $args['related_to'] ) ) { |
|
| 459 | + $sql .= $wpdb->prepare( " r.$filtering_column = %d", $args['related_to'] ); |
|
| 460 | + } |
|
| 461 | + if ( isset( $args['related_to'] ) && isset( $args['related_to__in'] ) ) { |
|
| 462 | + $sql .= " AND"; |
|
| 463 | + } |
|
| 464 | + if ( isset( $args['related_to__in'] ) ) { |
|
| 465 | + $sql .= " r.$filtering_column IN (" . implode( ",", $args['related_to__in'] ) . ")"; |
|
| 466 | + } |
|
| 467 | + if ( isset( $args['post__not_in'] ) ) { |
|
| 468 | + $sql .= " AND r." . $args['as'] . "_id NOT IN (" . implode( ",", $args['post__not_in'] ) . ")"; |
|
| 469 | + } |
|
| 470 | + if ( isset( $args['post__in'] ) ) { |
|
| 471 | + $sql .= " AND r." . $args['as'] . "_id IN (" . implode( ",", $args['post__in'] ) . ")"; |
|
| 472 | + } |
|
| 473 | + // Add predicate filter if required |
|
| 474 | + if ( isset( $args['with_predicate'] ) ) { |
|
| 475 | + // Sql Inner Join clausole |
|
| 476 | + $sql .= $wpdb->prepare( " AND r.predicate = %s", $args['with_predicate'] ); |
|
| 477 | + } |
|
| 478 | + // Add a group by clousole to avoid duplicated rows |
|
| 479 | + $sql .= " GROUP BY p.id"; |
|
| 480 | + |
|
| 481 | + if ( isset( $args['first'] ) && is_numeric( $args['first'] ) ) { |
|
| 482 | + // Sql Inner Join clausole |
|
| 483 | + $sql .= $wpdb->prepare( " LIMIT %d", $args['first'] ); |
|
| 484 | + } |
|
| 485 | + // Close sql statement |
|
| 486 | + $sql .= ";"; |
|
| 487 | + |
|
| 488 | + return $sql; |
|
| 489 | 489 | |
| 490 | 490 | } |
| 491 | 491 | |
@@ -501,72 +501,72 @@ discard block |
||
| 501 | 501 | */ |
| 502 | 502 | function wl_core_get_posts( $args, $returned_type = OBJECT ) { |
| 503 | 503 | |
| 504 | - // Merge given args with defaults args value |
|
| 505 | - $args = array_merge( array( |
|
| 506 | - 'with_predicate' => null, |
|
| 507 | - 'as' => 'subject', |
|
| 508 | - 'post_type' => 'post', |
|
| 509 | - 'get' => 'posts', |
|
| 510 | - 'post_status' => null, |
|
| 511 | - ), $args ); |
|
| 512 | - |
|
| 513 | - // Arguments validation rules |
|
| 514 | - // At least one between related_to and related_to__in has to be set |
|
| 515 | - if ( ! isset( $args['related_to'] ) && ! isset( $args['related_to__in'] ) ) { |
|
| 516 | - return false; |
|
| 517 | - } |
|
| 518 | - if ( isset( $args['related_to'] ) && ! is_numeric( $args['related_to'] ) ) { |
|
| 519 | - return false; |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - // The same check is applied to post_in, post__not_in and related_to__in options |
|
| 523 | - // Only arrays with at least one numeric value are considerad valid |
|
| 524 | - // The argument value is further sanitized in order to clean up not numeric values |
|
| 525 | - foreach ( |
|
| 526 | - array( |
|
| 527 | - 'post__in', |
|
| 528 | - 'post__not_in', |
|
| 529 | - 'related_to__in', |
|
| 530 | - ) as $option_name |
|
| 531 | - ) { |
|
| 532 | - if ( isset( $args[ $option_name ] ) ) { |
|
| 533 | - if ( ! is_array( $args[ $option_name ] ) || 0 == count( array_filter( $args[ $option_name ], "is_numeric" ) ) ) { |
|
| 534 | - return false; |
|
| 535 | - } |
|
| 536 | - // Sanitize value removing non numeric values from the array |
|
| 537 | - $args[ $option_name ] = array_filter( $args[ $option_name ], "is_numeric" ); |
|
| 538 | - } |
|
| 539 | - } |
|
| 540 | - // Performing validation rules |
|
| 541 | - foreach ( wl_core_get_validation_rules() as $option_name => $accepeted_values ) { |
|
| 542 | - if ( isset( $args[ $option_name ] ) && ! is_null( $args[ $option_name ] ) ) { |
|
| 543 | - if ( ! in_array( $args[ $option_name ], $accepeted_values ) ) { |
|
| 544 | - return false; |
|
| 545 | - } |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - // Prepare interaction with db |
|
| 550 | - global $wpdb; |
|
| 551 | - // Build sql statement with given arguments |
|
| 552 | - $sql_statement = wl_core_sql_query_builder( $args ); |
|
| 553 | - |
|
| 554 | - // wl_write_log( "Going to execute sql statement: $sql_statement " ); |
|
| 555 | - // $results = array(); |
|
| 556 | - |
|
| 557 | - // If ids are required, returns a one-dimensional array containing ids. |
|
| 558 | - // Otherwise an array of associative arrays representing the post | relation object |
|
| 559 | - if ( 'post_ids' == $args['get'] ) { |
|
| 560 | - # See https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column |
|
| 561 | - $results = $wpdb->get_col( $sql_statement ); |
|
| 562 | - } else { |
|
| 563 | - $results = $wpdb->get_results( $sql_statement, $returned_type ); |
|
| 564 | - } |
|
| 565 | - // If there were an error performing the query then false is returned |
|
| 566 | - if ( ! empty( $wpdb->last_error ) ) { |
|
| 567 | - return false; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - // Finally |
|
| 571 | - return $results; |
|
| 504 | + // Merge given args with defaults args value |
|
| 505 | + $args = array_merge( array( |
|
| 506 | + 'with_predicate' => null, |
|
| 507 | + 'as' => 'subject', |
|
| 508 | + 'post_type' => 'post', |
|
| 509 | + 'get' => 'posts', |
|
| 510 | + 'post_status' => null, |
|
| 511 | + ), $args ); |
|
| 512 | + |
|
| 513 | + // Arguments validation rules |
|
| 514 | + // At least one between related_to and related_to__in has to be set |
|
| 515 | + if ( ! isset( $args['related_to'] ) && ! isset( $args['related_to__in'] ) ) { |
|
| 516 | + return false; |
|
| 517 | + } |
|
| 518 | + if ( isset( $args['related_to'] ) && ! is_numeric( $args['related_to'] ) ) { |
|
| 519 | + return false; |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + // The same check is applied to post_in, post__not_in and related_to__in options |
|
| 523 | + // Only arrays with at least one numeric value are considerad valid |
|
| 524 | + // The argument value is further sanitized in order to clean up not numeric values |
|
| 525 | + foreach ( |
|
| 526 | + array( |
|
| 527 | + 'post__in', |
|
| 528 | + 'post__not_in', |
|
| 529 | + 'related_to__in', |
|
| 530 | + ) as $option_name |
|
| 531 | + ) { |
|
| 532 | + if ( isset( $args[ $option_name ] ) ) { |
|
| 533 | + if ( ! is_array( $args[ $option_name ] ) || 0 == count( array_filter( $args[ $option_name ], "is_numeric" ) ) ) { |
|
| 534 | + return false; |
|
| 535 | + } |
|
| 536 | + // Sanitize value removing non numeric values from the array |
|
| 537 | + $args[ $option_name ] = array_filter( $args[ $option_name ], "is_numeric" ); |
|
| 538 | + } |
|
| 539 | + } |
|
| 540 | + // Performing validation rules |
|
| 541 | + foreach ( wl_core_get_validation_rules() as $option_name => $accepeted_values ) { |
|
| 542 | + if ( isset( $args[ $option_name ] ) && ! is_null( $args[ $option_name ] ) ) { |
|
| 543 | + if ( ! in_array( $args[ $option_name ], $accepeted_values ) ) { |
|
| 544 | + return false; |
|
| 545 | + } |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + // Prepare interaction with db |
|
| 550 | + global $wpdb; |
|
| 551 | + // Build sql statement with given arguments |
|
| 552 | + $sql_statement = wl_core_sql_query_builder( $args ); |
|
| 553 | + |
|
| 554 | + // wl_write_log( "Going to execute sql statement: $sql_statement " ); |
|
| 555 | + // $results = array(); |
|
| 556 | + |
|
| 557 | + // If ids are required, returns a one-dimensional array containing ids. |
|
| 558 | + // Otherwise an array of associative arrays representing the post | relation object |
|
| 559 | + if ( 'post_ids' == $args['get'] ) { |
|
| 560 | + # See https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column |
|
| 561 | + $results = $wpdb->get_col( $sql_statement ); |
|
| 562 | + } else { |
|
| 563 | + $results = $wpdb->get_results( $sql_statement, $returned_type ); |
|
| 564 | + } |
|
| 565 | + // If there were an error performing the query then false is returned |
|
| 566 | + if ( ! empty( $wpdb->last_error ) ) { |
|
| 567 | + return false; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + // Finally |
|
| 571 | + return $results; |
|
| 572 | 572 | } |
@@ -7,14 +7,14 @@ discard block |
||
| 7 | 7 | * |
| 8 | 8 | * @return boolean Return true if supported, false otherwise |
| 9 | 9 | */ |
| 10 | -function wl_core_check_relation_predicate_is_supported( $predicate ) { |
|
| 10 | +function wl_core_check_relation_predicate_is_supported($predicate) { |
|
| 11 | 11 | |
| 12 | - return in_array( $predicate, array( |
|
| 12 | + return in_array($predicate, array( |
|
| 13 | 13 | WL_WHAT_RELATION, |
| 14 | 14 | WL_WHEN_RELATION, |
| 15 | 15 | WL_WHERE_RELATION, |
| 16 | 16 | WL_WHO_RELATION, |
| 17 | - ) ); |
|
| 17 | + )); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | function wl_core_get_validation_rules() { |
| 26 | 26 | |
| 27 | - return unserialize( WL_CORE_GET_POSTS_VALIDATION_RULES ); |
|
| 27 | + return unserialize(WL_CORE_GET_POSTS_VALIDATION_RULES); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | function wl_core_get_relation_instances_table_name() { |
| 36 | 36 | |
| 37 | 37 | global $wpdb; |
| 38 | - $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
| 38 | + $table_name = $wpdb->prefix.WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
| 39 | 39 | |
| 40 | 40 | return $table_name; |
| 41 | 41 | } |
@@ -50,15 +50,15 @@ discard block |
||
| 50 | 50 | * @uses $wpdb->replace() to perform the query |
| 51 | 51 | * @return (integer|boolean) Return then relation instance ID or false |
| 52 | 52 | */ |
| 53 | -function wl_core_add_relation_instance( $subject_id, $predicate, $object_id ) { |
|
| 53 | +function wl_core_add_relation_instance($subject_id, $predicate, $object_id) { |
|
| 54 | 54 | |
| 55 | 55 | // Checks on subject and object |
| 56 | - if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 56 | + if ( ! is_numeric($subject_id) || ! is_numeric($object_id)) { |
|
| 57 | 57 | return false; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | // Checks on the given relation |
| 61 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 61 | + if ( ! wl_core_check_relation_predicate_is_supported($predicate)) { |
|
| 62 | 62 | return false; |
| 63 | 63 | } |
| 64 | 64 | |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | 'predicate' => $predicate, |
| 75 | 75 | 'object_id' => $object_id, |
| 76 | 76 | ), |
| 77 | - array( '%d', '%s', '%d' ) |
|
| 77 | + array('%d', '%s', '%d') |
|
| 78 | 78 | ); |
| 79 | 79 | |
| 80 | 80 | // Return record id |
@@ -91,22 +91,22 @@ discard block |
||
| 91 | 91 | * |
| 92 | 92 | * @return (boolean) False for failure. True for success. |
| 93 | 93 | */ |
| 94 | -function wl_core_delete_relation_instance( $subject_id, $predicate, $object_id ) { |
|
| 94 | +function wl_core_delete_relation_instance($subject_id, $predicate, $object_id) { |
|
| 95 | 95 | |
| 96 | 96 | // Checks on subject and object |
| 97 | - if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
| 97 | + if ( ! is_numeric($subject_id) || ! is_numeric($object_id)) { |
|
| 98 | 98 | return false; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | // Checks on the given relation |
| 102 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 102 | + if ( ! wl_core_check_relation_predicate_is_supported($predicate)) { |
|
| 103 | 103 | return false; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | // Prepare interaction with db |
| 107 | 107 | global $wpdb; |
| 108 | 108 | |
| 109 | - wl_write_log( "Going to delete relation instace [ subject_id :: $subject_id ] [ object_id :: $object_id ] [ predicate :: $predicate ]" ); |
|
| 109 | + wl_write_log("Going to delete relation instace [ subject_id :: $subject_id ] [ object_id :: $object_id ] [ predicate :: $predicate ]"); |
|
| 110 | 110 | |
| 111 | 111 | // @see ttps://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
| 112 | 112 | $wpdb->delete( |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | 'predicate' => $predicate, |
| 117 | 117 | 'object_id' => $object_id, |
| 118 | 118 | ), |
| 119 | - array( '%d', '%s', '%d' ) |
|
| 119 | + array('%d', '%s', '%d') |
|
| 120 | 120 | ); |
| 121 | 121 | |
| 122 | 122 | return true; |
@@ -132,27 +132,27 @@ discard block |
||
| 132 | 132 | * |
| 133 | 133 | * @return (integer|boolean) Return the relation instances IDs or false |
| 134 | 134 | */ |
| 135 | -function wl_core_add_relation_instances( $subject_id, $predicate, $object_ids ) { |
|
| 135 | +function wl_core_add_relation_instances($subject_id, $predicate, $object_ids) { |
|
| 136 | 136 | |
| 137 | 137 | // Checks on subject and object |
| 138 | - if ( ! is_numeric( $subject_id ) ) { |
|
| 138 | + if ( ! is_numeric($subject_id)) { |
|
| 139 | 139 | return false; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | // Checks on the given relation |
| 143 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
| 143 | + if ( ! wl_core_check_relation_predicate_is_supported($predicate)) { |
|
| 144 | 144 | return false; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | // Check $object_ids is an array |
| 148 | - if ( ! is_array( $object_ids ) || empty( $object_ids ) ) { |
|
| 148 | + if ( ! is_array($object_ids) || empty($object_ids)) { |
|
| 149 | 149 | return false; |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | // Call method to check and add each single relation |
| 153 | 153 | $inserted_records_ids = array(); |
| 154 | - foreach ( $object_ids as $object_id ) { |
|
| 155 | - $new_record_id = wl_core_add_relation_instance( $subject_id, $predicate, $object_id ); |
|
| 154 | + foreach ($object_ids as $object_id) { |
|
| 155 | + $new_record_id = wl_core_add_relation_instance($subject_id, $predicate, $object_id); |
|
| 156 | 156 | $inserted_records_ids[] = $new_record_id; |
| 157 | 157 | } |
| 158 | 158 | |
@@ -167,10 +167,10 @@ discard block |
||
| 167 | 167 | * |
| 168 | 168 | * @return (boolean) False for failure. True for success. |
| 169 | 169 | */ |
| 170 | -function wl_core_delete_relation_instances( $subject_id ) { |
|
| 170 | +function wl_core_delete_relation_instances($subject_id) { |
|
| 171 | 171 | |
| 172 | 172 | // Checks on subject and object |
| 173 | - if ( ! is_numeric( $subject_id ) ) { |
|
| 173 | + if ( ! is_numeric($subject_id)) { |
|
| 174 | 174 | return false; |
| 175 | 175 | } |
| 176 | 176 | |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | array( |
| 186 | 186 | 'subject_id' => $subject_id, |
| 187 | 187 | ), |
| 188 | - array( '%d' ) |
|
| 188 | + array('%d') |
|
| 189 | 189 | ); |
| 190 | 190 | |
| 191 | 191 | return true; |
@@ -202,16 +202,16 @@ discard block |
||
| 202 | 202 | * 'status' => null |
| 203 | 203 | * ); |
| 204 | 204 | */ |
| 205 | -function wl_core_validate_filters_for_related( $filters ) { |
|
| 205 | +function wl_core_validate_filters_for_related($filters) { |
|
| 206 | 206 | |
| 207 | - if ( ! is_array( $filters ) ) { |
|
| 207 | + if ( ! is_array($filters)) { |
|
| 208 | 208 | $filters = array(); |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - if ( ! isset( $filters['predicate'] ) ) { |
|
| 211 | + if ( ! isset($filters['predicate'])) { |
|
| 212 | 212 | $filters['predicate'] = null; |
| 213 | 213 | } |
| 214 | - if ( ! isset( $filters['status'] ) ) { |
|
| 214 | + if ( ! isset($filters['status'])) { |
|
| 215 | 215 | $filters['status'] = null; |
| 216 | 216 | } |
| 217 | 217 | |
@@ -234,11 +234,11 @@ discard block |
||
| 234 | 234 | * |
| 235 | 235 | * @return array Array of post entity objects. |
| 236 | 236 | */ |
| 237 | -function wl_core_get_related_entities( $subject_id, $filters = array() ) { |
|
| 237 | +function wl_core_get_related_entities($subject_id, $filters = array()) { |
|
| 238 | 238 | |
| 239 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 239 | + $filters = wl_core_validate_filters_for_related($filters); |
|
| 240 | 240 | |
| 241 | - return wl_core_inner_get_related_entities( "posts", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 241 | + return wl_core_inner_get_related_entities("posts", $subject_id, $filters['predicate'], $filters['status']); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -257,11 +257,11 @@ discard block |
||
| 257 | 257 | * |
| 258 | 258 | * @return array Array of post entity objects. |
| 259 | 259 | */ |
| 260 | -function wl_core_get_related_entity_ids( $subject_id, $filters = array() ) { |
|
| 260 | +function wl_core_get_related_entity_ids($subject_id, $filters = array()) { |
|
| 261 | 261 | |
| 262 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 262 | + $filters = wl_core_validate_filters_for_related($filters); |
|
| 263 | 263 | |
| 264 | - return wl_core_inner_get_related_entities( "post_ids", $subject_id, $filters['predicate'], $filters['status'] ); |
|
| 264 | + return wl_core_inner_get_related_entities("post_ids", $subject_id, $filters['predicate'], $filters['status']); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
@@ -276,16 +276,16 @@ discard block |
||
| 276 | 276 | * |
| 277 | 277 | * @return array Array of ids. |
| 278 | 278 | */ |
| 279 | -function wl_core_inner_get_related_entities( $get, $item_id, $predicate = null, $post_status = null ) { |
|
| 279 | +function wl_core_inner_get_related_entities($get, $item_id, $predicate = null, $post_status = null) { |
|
| 280 | 280 | |
| 281 | - if ( $results = wl_core_get_posts( array( |
|
| 281 | + if ($results = wl_core_get_posts(array( |
|
| 282 | 282 | 'get' => $get, |
| 283 | 283 | 'post_type' => 'entity', |
| 284 | 284 | 'post_status' => $post_status, |
| 285 | 285 | 'related_to' => $item_id, |
| 286 | 286 | 'as' => 'object', |
| 287 | 287 | 'with_predicate' => $predicate, |
| 288 | - ) ) |
|
| 288 | + )) |
|
| 289 | 289 | ) { |
| 290 | 290 | return $results; |
| 291 | 291 | } |
@@ -310,11 +310,11 @@ discard block |
||
| 310 | 310 | * |
| 311 | 311 | * @return array Array of objects. |
| 312 | 312 | */ |
| 313 | -function wl_core_get_related_posts( $object_id, $filters = array() ) { |
|
| 313 | +function wl_core_get_related_posts($object_id, $filters = array()) { |
|
| 314 | 314 | |
| 315 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 315 | + $filters = wl_core_validate_filters_for_related($filters); |
|
| 316 | 316 | |
| 317 | - return wl_core_inner_get_related_posts( "posts", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 317 | + return wl_core_inner_get_related_posts("posts", $object_id, $filters['predicate'], $filters['status']); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -333,11 +333,11 @@ discard block |
||
| 333 | 333 | * |
| 334 | 334 | * @return array Array of objects. |
| 335 | 335 | */ |
| 336 | -function wl_core_get_related_post_ids( $object_id, $filters = array() ) { |
|
| 336 | +function wl_core_get_related_post_ids($object_id, $filters = array()) { |
|
| 337 | 337 | |
| 338 | - $filters = wl_core_validate_filters_for_related( $filters ); |
|
| 338 | + $filters = wl_core_validate_filters_for_related($filters); |
|
| 339 | 339 | |
| 340 | - return wl_core_inner_get_related_posts( "post_ids", $object_id, $filters['predicate'], $filters['status'] ); |
|
| 340 | + return wl_core_inner_get_related_posts("post_ids", $object_id, $filters['predicate'], $filters['status']); |
|
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | /** |
@@ -351,36 +351,36 @@ discard block |
||
| 351 | 351 | * |
| 352 | 352 | * @return array Array of objects. |
| 353 | 353 | */ |
| 354 | -function wl_core_inner_get_related_posts( $get, $item_id, $predicate = null, $post_status = null ) { |
|
| 354 | +function wl_core_inner_get_related_posts($get, $item_id, $predicate = null, $post_status = null) { |
|
| 355 | 355 | |
| 356 | 356 | // Retrieve the post object |
| 357 | - $post = get_post( $item_id ); |
|
| 358 | - if ( null === $post ) { |
|
| 357 | + $post = get_post($item_id); |
|
| 358 | + if (null === $post) { |
|
| 359 | 359 | return array(); |
| 360 | 360 | } |
| 361 | 361 | |
| 362 | - if ( "entity" === $post->post_type ) { |
|
| 363 | - if ( $results = wl_core_get_posts( array( |
|
| 362 | + if ("entity" === $post->post_type) { |
|
| 363 | + if ($results = wl_core_get_posts(array( |
|
| 364 | 364 | 'get' => $get, |
| 365 | 365 | 'post_type' => 'post', |
| 366 | 366 | 'post_status' => $post_status, |
| 367 | 367 | 'related_to' => $item_id, |
| 368 | 368 | 'as' => 'subject', |
| 369 | 369 | 'with_predicate' => $predicate, |
| 370 | - ) ) |
|
| 370 | + )) |
|
| 371 | 371 | ) { |
| 372 | 372 | return $results; |
| 373 | 373 | } |
| 374 | 374 | } else { |
| 375 | - if ( $results = wl_core_get_posts( array( |
|
| 375 | + if ($results = wl_core_get_posts(array( |
|
| 376 | 376 | 'get' => $get, |
| 377 | 377 | 'post_type' => 'post', |
| 378 | 378 | 'post_status' => $post_status, |
| 379 | - 'post__not_in' => array( $item_id ), |
|
| 380 | - 'related_to__in' => wl_core_get_related_entity_ids( $post->ID ), |
|
| 379 | + 'post__not_in' => array($item_id), |
|
| 380 | + 'related_to__in' => wl_core_get_related_entity_ids($post->ID), |
|
| 381 | 381 | 'as' => 'subject', |
| 382 | 382 | 'with_predicate' => $predicate, |
| 383 | - ) ) |
|
| 383 | + )) |
|
| 384 | 384 | ) { |
| 385 | 385 | return $results; |
| 386 | 386 | } |
@@ -415,7 +415,7 @@ discard block |
||
| 415 | 415 | * |
| 416 | 416 | * @return string | false String representing a sql statement, or false in case of error |
| 417 | 417 | */ |
| 418 | -function wl_core_sql_query_builder( $args ) { |
|
| 418 | +function wl_core_sql_query_builder($args) { |
|
| 419 | 419 | |
| 420 | 420 | // Prepare interaction with db |
| 421 | 421 | global $wpdb; |
@@ -425,7 +425,7 @@ discard block |
||
| 425 | 425 | // Sql Action |
| 426 | 426 | $sql = "SELECT "; |
| 427 | 427 | // Determine what has to be returned depending on 'get' argument value |
| 428 | - switch ( $args['get'] ) { |
|
| 428 | + switch ($args['get']) { |
|
| 429 | 429 | case 'posts': |
| 430 | 430 | $sql .= "p.*"; |
| 431 | 431 | break; |
@@ -435,16 +435,16 @@ discard block |
||
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | // If we look for posts related as objects the JOIN has to be done with the object_id column and viceversa |
| 438 | - $join_column = $args['as'] . "_id"; |
|
| 438 | + $join_column = $args['as']."_id"; |
|
| 439 | 439 | |
| 440 | 440 | $sql .= " FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.$join_column"; |
| 441 | 441 | |
| 442 | 442 | // Sql add post type filter |
| 443 | - $sql .= $wpdb->prepare( " AND p.post_type = %s AND", $args['post_type'] ); |
|
| 443 | + $sql .= $wpdb->prepare(" AND p.post_type = %s AND", $args['post_type']); |
|
| 444 | 444 | |
| 445 | 445 | // Sql add post status filter |
| 446 | - if ( isset( $args['post_status'] ) && ! is_null( $args['post_status'] ) ) { |
|
| 447 | - $sql .= $wpdb->prepare( " p.post_status = %s AND", $args['post_status'] ); |
|
| 446 | + if (isset($args['post_status']) && ! is_null($args['post_status'])) { |
|
| 447 | + $sql .= $wpdb->prepare(" p.post_status = %s AND", $args['post_status']); |
|
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | // Add filtering conditions |
@@ -453,34 +453,34 @@ discard block |
||
| 453 | 453 | // If we look for posts related as subject this means that |
| 454 | 454 | // related_to is reference for an object: object_id is the filtering column |
| 455 | 455 | |
| 456 | - $filtering_column = ( 'object' == $args['as'] ) ? "subject_id" : "object_id"; |
|
| 456 | + $filtering_column = ('object' == $args['as']) ? "subject_id" : "object_id"; |
|
| 457 | 457 | |
| 458 | - if ( isset( $args['related_to'] ) ) { |
|
| 459 | - $sql .= $wpdb->prepare( " r.$filtering_column = %d", $args['related_to'] ); |
|
| 458 | + if (isset($args['related_to'])) { |
|
| 459 | + $sql .= $wpdb->prepare(" r.$filtering_column = %d", $args['related_to']); |
|
| 460 | 460 | } |
| 461 | - if ( isset( $args['related_to'] ) && isset( $args['related_to__in'] ) ) { |
|
| 461 | + if (isset($args['related_to']) && isset($args['related_to__in'])) { |
|
| 462 | 462 | $sql .= " AND"; |
| 463 | 463 | } |
| 464 | - if ( isset( $args['related_to__in'] ) ) { |
|
| 465 | - $sql .= " r.$filtering_column IN (" . implode( ",", $args['related_to__in'] ) . ")"; |
|
| 464 | + if (isset($args['related_to__in'])) { |
|
| 465 | + $sql .= " r.$filtering_column IN (".implode(",", $args['related_to__in']).")"; |
|
| 466 | 466 | } |
| 467 | - if ( isset( $args['post__not_in'] ) ) { |
|
| 468 | - $sql .= " AND r." . $args['as'] . "_id NOT IN (" . implode( ",", $args['post__not_in'] ) . ")"; |
|
| 467 | + if (isset($args['post__not_in'])) { |
|
| 468 | + $sql .= " AND r.".$args['as']."_id NOT IN (".implode(",", $args['post__not_in']).")"; |
|
| 469 | 469 | } |
| 470 | - if ( isset( $args['post__in'] ) ) { |
|
| 471 | - $sql .= " AND r." . $args['as'] . "_id IN (" . implode( ",", $args['post__in'] ) . ")"; |
|
| 470 | + if (isset($args['post__in'])) { |
|
| 471 | + $sql .= " AND r.".$args['as']."_id IN (".implode(",", $args['post__in']).")"; |
|
| 472 | 472 | } |
| 473 | 473 | // Add predicate filter if required |
| 474 | - if ( isset( $args['with_predicate'] ) ) { |
|
| 474 | + if (isset($args['with_predicate'])) { |
|
| 475 | 475 | // Sql Inner Join clausole |
| 476 | - $sql .= $wpdb->prepare( " AND r.predicate = %s", $args['with_predicate'] ); |
|
| 476 | + $sql .= $wpdb->prepare(" AND r.predicate = %s", $args['with_predicate']); |
|
| 477 | 477 | } |
| 478 | 478 | // Add a group by clousole to avoid duplicated rows |
| 479 | 479 | $sql .= " GROUP BY p.id"; |
| 480 | 480 | |
| 481 | - if ( isset( $args['first'] ) && is_numeric( $args['first'] ) ) { |
|
| 481 | + if (isset($args['first']) && is_numeric($args['first'])) { |
|
| 482 | 482 | // Sql Inner Join clausole |
| 483 | - $sql .= $wpdb->prepare( " LIMIT %d", $args['first'] ); |
|
| 483 | + $sql .= $wpdb->prepare(" LIMIT %d", $args['first']); |
|
| 484 | 484 | } |
| 485 | 485 | // Close sql statement |
| 486 | 486 | $sql .= ";"; |
@@ -499,23 +499,23 @@ discard block |
||
| 499 | 499 | * |
| 500 | 500 | * @return array|false List of WP_Post objects or list of WP_Post ids. False in case of error or invalid params |
| 501 | 501 | */ |
| 502 | -function wl_core_get_posts( $args, $returned_type = OBJECT ) { |
|
| 502 | +function wl_core_get_posts($args, $returned_type = OBJECT) { |
|
| 503 | 503 | |
| 504 | 504 | // Merge given args with defaults args value |
| 505 | - $args = array_merge( array( |
|
| 505 | + $args = array_merge(array( |
|
| 506 | 506 | 'with_predicate' => null, |
| 507 | 507 | 'as' => 'subject', |
| 508 | 508 | 'post_type' => 'post', |
| 509 | 509 | 'get' => 'posts', |
| 510 | 510 | 'post_status' => null, |
| 511 | - ), $args ); |
|
| 511 | + ), $args); |
|
| 512 | 512 | |
| 513 | 513 | // Arguments validation rules |
| 514 | 514 | // At least one between related_to and related_to__in has to be set |
| 515 | - if ( ! isset( $args['related_to'] ) && ! isset( $args['related_to__in'] ) ) { |
|
| 515 | + if ( ! isset($args['related_to']) && ! isset($args['related_to__in'])) { |
|
| 516 | 516 | return false; |
| 517 | 517 | } |
| 518 | - if ( isset( $args['related_to'] ) && ! is_numeric( $args['related_to'] ) ) { |
|
| 518 | + if (isset($args['related_to']) && ! is_numeric($args['related_to'])) { |
|
| 519 | 519 | return false; |
| 520 | 520 | } |
| 521 | 521 | |
@@ -529,18 +529,18 @@ discard block |
||
| 529 | 529 | 'related_to__in', |
| 530 | 530 | ) as $option_name |
| 531 | 531 | ) { |
| 532 | - if ( isset( $args[ $option_name ] ) ) { |
|
| 533 | - if ( ! is_array( $args[ $option_name ] ) || 0 == count( array_filter( $args[ $option_name ], "is_numeric" ) ) ) { |
|
| 532 | + if (isset($args[$option_name])) { |
|
| 533 | + if ( ! is_array($args[$option_name]) || 0 == count(array_filter($args[$option_name], "is_numeric"))) { |
|
| 534 | 534 | return false; |
| 535 | 535 | } |
| 536 | 536 | // Sanitize value removing non numeric values from the array |
| 537 | - $args[ $option_name ] = array_filter( $args[ $option_name ], "is_numeric" ); |
|
| 537 | + $args[$option_name] = array_filter($args[$option_name], "is_numeric"); |
|
| 538 | 538 | } |
| 539 | 539 | } |
| 540 | 540 | // Performing validation rules |
| 541 | - foreach ( wl_core_get_validation_rules() as $option_name => $accepeted_values ) { |
|
| 542 | - if ( isset( $args[ $option_name ] ) && ! is_null( $args[ $option_name ] ) ) { |
|
| 543 | - if ( ! in_array( $args[ $option_name ], $accepeted_values ) ) { |
|
| 541 | + foreach (wl_core_get_validation_rules() as $option_name => $accepeted_values) { |
|
| 542 | + if (isset($args[$option_name]) && ! is_null($args[$option_name])) { |
|
| 543 | + if ( ! in_array($args[$option_name], $accepeted_values)) { |
|
| 544 | 544 | return false; |
| 545 | 545 | } |
| 546 | 546 | } |
@@ -549,21 +549,21 @@ discard block |
||
| 549 | 549 | // Prepare interaction with db |
| 550 | 550 | global $wpdb; |
| 551 | 551 | // Build sql statement with given arguments |
| 552 | - $sql_statement = wl_core_sql_query_builder( $args ); |
|
| 552 | + $sql_statement = wl_core_sql_query_builder($args); |
|
| 553 | 553 | |
| 554 | 554 | // wl_write_log( "Going to execute sql statement: $sql_statement " ); |
| 555 | 555 | // $results = array(); |
| 556 | 556 | |
| 557 | 557 | // If ids are required, returns a one-dimensional array containing ids. |
| 558 | 558 | // Otherwise an array of associative arrays representing the post | relation object |
| 559 | - if ( 'post_ids' == $args['get'] ) { |
|
| 559 | + if ('post_ids' == $args['get']) { |
|
| 560 | 560 | # See https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column |
| 561 | - $results = $wpdb->get_col( $sql_statement ); |
|
| 561 | + $results = $wpdb->get_col($sql_statement); |
|
| 562 | 562 | } else { |
| 563 | - $results = $wpdb->get_results( $sql_statement, $returned_type ); |
|
| 563 | + $results = $wpdb->get_results($sql_statement, $returned_type); |
|
| 564 | 564 | } |
| 565 | 565 | // If there were an error performing the query then false is returned |
| 566 | - if ( ! empty( $wpdb->last_error ) ) { |
|
| 566 | + if ( ! empty($wpdb->last_error)) { |
|
| 567 | 567 | return false; |
| 568 | 568 | } |
| 569 | 569 | |