@@ -15,21 +15,21 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | function wl_serialize_entity( $entity ) { |
| 17 | 17 | |
| 18 | - $entity = ( is_numeric( $entity ) ) ? get_post( $entity ) : $entity; |
|
| 18 | + $entity = ( is_numeric( $entity ) ) ? get_post( $entity ) : $entity; |
|
| 19 | 19 | |
| 20 | - $type = wl_entity_type_taxonomy_get_type( $entity->ID ); |
|
| 21 | - $images = wl_get_image_urls( $entity->ID ); |
|
| 20 | + $type = wl_entity_type_taxonomy_get_type( $entity->ID ); |
|
| 21 | + $images = wl_get_image_urls( $entity->ID ); |
|
| 22 | 22 | |
| 23 | - return array( |
|
| 24 | - 'id' => wl_get_entity_uri( $entity->ID ), |
|
| 25 | - 'label' => $entity->post_title, |
|
| 26 | - 'description' => $entity->post_content, |
|
| 27 | - 'sameAs' => wl_schema_get_value( $entity->ID, 'sameAs' ), |
|
| 28 | - 'mainType' => str_replace( 'wl-', '', $type['css_class'] ), |
|
| 29 | - 'types' => wl_get_entity_rdf_types( $entity->ID ), |
|
| 30 | - 'images' => $images, |
|
| 23 | + return array( |
|
| 24 | + 'id' => wl_get_entity_uri( $entity->ID ), |
|
| 25 | + 'label' => $entity->post_title, |
|
| 26 | + 'description' => $entity->post_content, |
|
| 27 | + 'sameAs' => wl_schema_get_value( $entity->ID, 'sameAs' ), |
|
| 28 | + 'mainType' => str_replace( 'wl-', '', $type['css_class'] ), |
|
| 29 | + 'types' => wl_get_entity_rdf_types( $entity->ID ), |
|
| 30 | + 'images' => $images, |
|
| 31 | 31 | |
| 32 | - ); |
|
| 32 | + ); |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
@@ -43,23 +43,23 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | function wl_remove_text_annotations( $data ) { |
| 45 | 45 | |
| 46 | - // Remove blank elements that can interfere with annoataions removing |
|
| 47 | - // See https://github.com/insideout10/wordlift-plugin/issues/234 |
|
| 48 | - // Just blank attributes without any attribtues are cleaned up |
|
| 49 | - $pattern = '/<(\w+)><\/\1>/im'; |
|
| 50 | - // Remove the pattern while it is found (match nested annotations). |
|
| 51 | - while ( 1 === preg_match( $pattern, $data['post_content'] ) ) { |
|
| 52 | - $data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count ); |
|
| 53 | - } |
|
| 54 | - // Remove text annotations |
|
| 55 | - // <span class="textannotation" id="urn:enhancement-777cbed4-b131-00fb-54a4-ed9b26ae57ea"> |
|
| 56 | - $pattern = '/<(\w+)[^>]*\sclass=\\\"textannotation(?!\sdisambiguated)[^\\"]*\\\"[^>]*>([^<]+)<\/\1>/im'; |
|
| 57 | - // Remove the pattern while it is found (match nested annotations). |
|
| 58 | - while ( 1 === preg_match( $pattern, $data['post_content'] ) ) { |
|
| 59 | - $data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count ); |
|
| 60 | - } |
|
| 46 | + // Remove blank elements that can interfere with annoataions removing |
|
| 47 | + // See https://github.com/insideout10/wordlift-plugin/issues/234 |
|
| 48 | + // Just blank attributes without any attribtues are cleaned up |
|
| 49 | + $pattern = '/<(\w+)><\/\1>/im'; |
|
| 50 | + // Remove the pattern while it is found (match nested annotations). |
|
| 51 | + while ( 1 === preg_match( $pattern, $data['post_content'] ) ) { |
|
| 52 | + $data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count ); |
|
| 53 | + } |
|
| 54 | + // Remove text annotations |
|
| 55 | + // <span class="textannotation" id="urn:enhancement-777cbed4-b131-00fb-54a4-ed9b26ae57ea"> |
|
| 56 | + $pattern = '/<(\w+)[^>]*\sclass=\\\"textannotation(?!\sdisambiguated)[^\\"]*\\\"[^>]*>([^<]+)<\/\1>/im'; |
|
| 57 | + // Remove the pattern while it is found (match nested annotations). |
|
| 58 | + while ( 1 === preg_match( $pattern, $data['post_content'] ) ) { |
|
| 59 | + $data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count ); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - return $data; |
|
| 62 | + return $data; |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | add_filter( 'wp_insert_post_data', 'wl_remove_text_annotations', '98', 1 ); |
@@ -75,5 +75,5 @@ discard block |
||
| 75 | 75 | */ |
| 76 | 76 | function wl_admin_metaboxes_add_css_class( $classes = array() ){ |
| 77 | 77 | |
| 78 | - return array_merge( $classes, array( 'wl-metabox' ) ); |
|
| 78 | + return array_merge( $classes, array( 'wl-metabox' ) ); |
|
| 79 | 79 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | // Add the Admin menu. |
| 7 | -require_once( 'wordlift_admin_menu.php' ); |
|
| 7 | +require_once('wordlift_admin_menu.php'); |
|
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * Serialize an entity post. |
@@ -13,20 +13,20 @@ discard block |
||
| 13 | 13 | * |
| 14 | 14 | * @return array mixed The entity data array. |
| 15 | 15 | */ |
| 16 | -function wl_serialize_entity( $entity ) { |
|
| 16 | +function wl_serialize_entity($entity) { |
|
| 17 | 17 | |
| 18 | - $entity = ( is_numeric( $entity ) ) ? get_post( $entity ) : $entity; |
|
| 18 | + $entity = (is_numeric($entity)) ? get_post($entity) : $entity; |
|
| 19 | 19 | |
| 20 | - $type = wl_entity_type_taxonomy_get_type( $entity->ID ); |
|
| 21 | - $images = wl_get_image_urls( $entity->ID ); |
|
| 20 | + $type = wl_entity_type_taxonomy_get_type($entity->ID); |
|
| 21 | + $images = wl_get_image_urls($entity->ID); |
|
| 22 | 22 | |
| 23 | 23 | return array( |
| 24 | - 'id' => wl_get_entity_uri( $entity->ID ), |
|
| 24 | + 'id' => wl_get_entity_uri($entity->ID), |
|
| 25 | 25 | 'label' => $entity->post_title, |
| 26 | 26 | 'description' => $entity->post_content, |
| 27 | - 'sameAs' => wl_schema_get_value( $entity->ID, 'sameAs' ), |
|
| 28 | - 'mainType' => str_replace( 'wl-', '', $type['css_class'] ), |
|
| 29 | - 'types' => wl_get_entity_rdf_types( $entity->ID ), |
|
| 27 | + 'sameAs' => wl_schema_get_value($entity->ID, 'sameAs'), |
|
| 28 | + 'mainType' => str_replace('wl-', '', $type['css_class']), |
|
| 29 | + 'types' => wl_get_entity_rdf_types($entity->ID), |
|
| 30 | 30 | 'images' => $images, |
| 31 | 31 | |
| 32 | 32 | ); |
@@ -41,28 +41,28 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @return array mixed The post data array. |
| 43 | 43 | */ |
| 44 | -function wl_remove_text_annotations( $data ) { |
|
| 44 | +function wl_remove_text_annotations($data) { |
|
| 45 | 45 | |
| 46 | 46 | // Remove blank elements that can interfere with annoataions removing |
| 47 | 47 | // See https://github.com/insideout10/wordlift-plugin/issues/234 |
| 48 | 48 | // Just blank attributes without any attribtues are cleaned up |
| 49 | 49 | $pattern = '/<(\w+)><\/\1>/im'; |
| 50 | 50 | // Remove the pattern while it is found (match nested annotations). |
| 51 | - while ( 1 === preg_match( $pattern, $data['post_content'] ) ) { |
|
| 52 | - $data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count ); |
|
| 51 | + while (1 === preg_match($pattern, $data['post_content'])) { |
|
| 52 | + $data['post_content'] = preg_replace($pattern, '$2', $data['post_content'], -1, $count); |
|
| 53 | 53 | } |
| 54 | 54 | // Remove text annotations |
| 55 | 55 | // <span class="textannotation" id="urn:enhancement-777cbed4-b131-00fb-54a4-ed9b26ae57ea"> |
| 56 | 56 | $pattern = '/<(\w+)[^>]*\sclass=\\\"textannotation(?!\sdisambiguated)[^\\"]*\\\"[^>]*>([^<]+)<\/\1>/im'; |
| 57 | 57 | // Remove the pattern while it is found (match nested annotations). |
| 58 | - while ( 1 === preg_match( $pattern, $data['post_content'] ) ) { |
|
| 59 | - $data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count ); |
|
| 58 | + while (1 === preg_match($pattern, $data['post_content'])) { |
|
| 59 | + $data['post_content'] = preg_replace($pattern, '$2', $data['post_content'], -1, $count); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | return $data; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | -add_filter( 'wp_insert_post_data', 'wl_remove_text_annotations', '98', 1 ); |
|
| 65 | +add_filter('wp_insert_post_data', 'wl_remove_text_annotations', '98', 1); |
|
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | 68 | * Adds wl-metabox CSS class to a metabox. |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | * |
| 74 | 74 | * @return array The updated list of CSS classes. |
| 75 | 75 | */ |
| 76 | -function wl_admin_metaboxes_add_css_class( $classes = array() ){ |
|
| 76 | +function wl_admin_metaboxes_add_css_class($classes = array()) { |
|
| 77 | 77 | |
| 78 | - return array_merge( $classes, array( 'wl-metabox' ) ); |
|
| 78 | + return array_merge($classes, array('wl-metabox')); |
|
| 79 | 79 | } |
@@ -154,6 +154,8 @@ discard block |
||
| 154 | 154 | * @since 3.2.0 |
| 155 | 155 | * |
| 156 | 156 | * @param \Wordlift_UI_Service $ui_service The UI service. |
| 157 | + * @param Wordlift_Schema_Service $schema_service |
|
| 158 | + * @param Wordlift_Notice_Service $notice_service |
|
| 157 | 159 | */ |
| 158 | 160 | public function __construct( $ui_service, $schema_service, $notice_service ) { |
| 159 | 161 | |
@@ -236,7 +238,7 @@ discard block |
||
| 236 | 238 | * |
| 237 | 239 | * @param int $post_id A post id. |
| 238 | 240 | * |
| 239 | - * @return true if the post is an entity otherwise false. |
|
| 241 | + * @return boolean if the post is an entity otherwise false. |
|
| 240 | 242 | */ |
| 241 | 243 | public function is_entity( $post_id ) { |
| 242 | 244 | |
@@ -250,7 +252,7 @@ discard block |
||
| 250 | 252 | * |
| 251 | 253 | * @param int $uri An uri. |
| 252 | 254 | * |
| 253 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 255 | + * @return boolean if the uri internal to the current dataset otherwise false. |
|
| 254 | 256 | */ |
| 255 | 257 | public function is_internal_uri( $uri ) { |
| 256 | 258 | |
@@ -409,7 +411,6 @@ discard block |
||
| 409 | 411 | * |
| 410 | 412 | * @since 3.3.0 |
| 411 | 413 | * |
| 412 | - * @param WP_Post $post Post object. |
|
| 413 | 414 | */ |
| 414 | 415 | public function in_admin_header() { |
| 415 | 416 | |
@@ -470,7 +471,6 @@ discard block |
||
| 470 | 471 | * @since 3.3.0 |
| 471 | 472 | * |
| 472 | 473 | * @param int $post_id The entity post id. |
| 473 | - * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 474 | 474 | * |
| 475 | 475 | * @return int An array representing the rating obj. |
| 476 | 476 | */ |
@@ -620,7 +620,7 @@ discard block |
||
| 620 | 620 | * |
| 621 | 621 | * @param int $score The rating score for a given entity. |
| 622 | 622 | * |
| 623 | - * @return string The input HTML code. |
|
| 623 | + * @return double The input HTML code. |
|
| 624 | 624 | */ |
| 625 | 625 | private function convert_raw_score_to_percentage( $score ) { |
| 626 | 626 | // RATING_MAX : $score = 100 : x |
@@ -7,637 +7,637 @@ |
||
| 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_service The Log service. |
|
| 16 | - */ |
|
| 17 | - private $log_service; |
|
| 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 Schema service. |
|
| 30 | - * |
|
| 31 | - * @since 3.3.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Schema_Service $schema_service The Schema service. |
|
| 34 | - */ |
|
| 35 | - private $schema_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The Notice service. |
|
| 39 | - * |
|
| 40 | - * @since 3.3.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
| 43 | - */ |
|
| 44 | - private $notice_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The entity post type name. |
|
| 48 | - * |
|
| 49 | - * @since 3.1.0 |
|
| 50 | - */ |
|
| 51 | - const TYPE_NAME = 'entity'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Entity rating max. |
|
| 55 | - * |
|
| 56 | - * @since 3.3.0 |
|
| 57 | - */ |
|
| 58 | - const RATING_MAX = 7; |
|
| 10 | + /** |
|
| 11 | + * The Log service. |
|
| 12 | + * |
|
| 13 | + * @since 3.2.0 |
|
| 14 | + * @access private |
|
| 15 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 16 | + */ |
|
| 17 | + private $log_service; |
|
| 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 Schema service. |
|
| 30 | + * |
|
| 31 | + * @since 3.3.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Schema_Service $schema_service The Schema service. |
|
| 34 | + */ |
|
| 35 | + private $schema_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The Notice service. |
|
| 39 | + * |
|
| 40 | + * @since 3.3.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
| 43 | + */ |
|
| 44 | + private $notice_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The entity post type name. |
|
| 48 | + * |
|
| 49 | + * @since 3.1.0 |
|
| 50 | + */ |
|
| 51 | + const TYPE_NAME = 'entity'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Entity rating max. |
|
| 55 | + * |
|
| 56 | + * @since 3.3.0 |
|
| 57 | + */ |
|
| 58 | + const RATING_MAX = 7; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Entity rating score meta key. |
|
| 62 | - * |
|
| 63 | - * @since 3.3.0 |
|
| 64 | - */ |
|
| 65 | - const RATING_RAW_SCORE_META_KEY = '_wl_entity_rating_raw_score'; |
|
| 60 | + /** |
|
| 61 | + * Entity rating score meta key. |
|
| 62 | + * |
|
| 63 | + * @since 3.3.0 |
|
| 64 | + */ |
|
| 65 | + const RATING_RAW_SCORE_META_KEY = '_wl_entity_rating_raw_score'; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Entity rating warnings meta key. |
|
| 69 | - * |
|
| 70 | - * @since 3.3.0 |
|
| 71 | - */ |
|
| 72 | - const RATING_WARNINGS_META_KEY = '_wl_entity_rating_warnings'; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Entity warning has related post identifier. |
|
| 76 | - * |
|
| 77 | - * @since 3.3.0 |
|
| 78 | - */ |
|
| 79 | - const RATING_WARNING_HAS_RELATED_POSTS = 'There are no related posts for the current entity.'; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Entity warning has content post identifier. |
|
| 83 | - * |
|
| 84 | - * @since 3.3.0 |
|
| 85 | - */ |
|
| 86 | - const RATING_WARNING_HAS_CONTENT_POST = 'This entity has not description.'; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Entity warning has related entities identifier. |
|
| 90 | - * |
|
| 91 | - * @since 3.3.0 |
|
| 92 | - */ |
|
| 93 | - const RATING_WARNING_HAS_RELATED_ENTITIES = 'There are no related entities for the current entity.'; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Entity warning is published identifier. |
|
| 97 | - * |
|
| 98 | - * @since 3.3.0 |
|
| 99 | - */ |
|
| 100 | - const RATING_WARNING_IS_PUBLISHED = 'This entity is not published. It will not appear within analysis results.'; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Entity warning has thumbnail identifier. |
|
| 104 | - * |
|
| 105 | - * @since 3.3.0 |
|
| 106 | - */ |
|
| 107 | - const RATING_WARNING_HAS_THUMBNAIL = 'This entity has no featured image yet.'; |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Entity warning has same as identifier. |
|
| 111 | - * |
|
| 112 | - * @since 3.3.0 |
|
| 113 | - */ |
|
| 114 | - const RATING_WARNING_HAS_SAME_AS = 'There are no sameAs configured for this entity.'; |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Entity warning has completed metadata identifier. |
|
| 118 | - * |
|
| 119 | - * @since 3.3.0 |
|
| 120 | - */ |
|
| 121 | - const RATING_WARNING_HAS_COMPLETED_METADATA = 'Schema.org metadata for this entity are not completed.'; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * The alternative label meta key. |
|
| 125 | - * |
|
| 126 | - * @since 3.2.0 |
|
| 127 | - */ |
|
| 128 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * The alternative label input template. |
|
| 132 | - * |
|
| 133 | - * @since 3.2.0 |
|
| 134 | - */ |
|
| 135 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 136 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 67 | + /** |
|
| 68 | + * Entity rating warnings meta key. |
|
| 69 | + * |
|
| 70 | + * @since 3.3.0 |
|
| 71 | + */ |
|
| 72 | + const RATING_WARNINGS_META_KEY = '_wl_entity_rating_warnings'; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Entity warning has related post identifier. |
|
| 76 | + * |
|
| 77 | + * @since 3.3.0 |
|
| 78 | + */ |
|
| 79 | + const RATING_WARNING_HAS_RELATED_POSTS = 'There are no related posts for the current entity.'; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Entity warning has content post identifier. |
|
| 83 | + * |
|
| 84 | + * @since 3.3.0 |
|
| 85 | + */ |
|
| 86 | + const RATING_WARNING_HAS_CONTENT_POST = 'This entity has not description.'; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Entity warning has related entities identifier. |
|
| 90 | + * |
|
| 91 | + * @since 3.3.0 |
|
| 92 | + */ |
|
| 93 | + const RATING_WARNING_HAS_RELATED_ENTITIES = 'There are no related entities for the current entity.'; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Entity warning is published identifier. |
|
| 97 | + * |
|
| 98 | + * @since 3.3.0 |
|
| 99 | + */ |
|
| 100 | + const RATING_WARNING_IS_PUBLISHED = 'This entity is not published. It will not appear within analysis results.'; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Entity warning has thumbnail identifier. |
|
| 104 | + * |
|
| 105 | + * @since 3.3.0 |
|
| 106 | + */ |
|
| 107 | + const RATING_WARNING_HAS_THUMBNAIL = 'This entity has no featured image yet.'; |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Entity warning has same as identifier. |
|
| 111 | + * |
|
| 112 | + * @since 3.3.0 |
|
| 113 | + */ |
|
| 114 | + const RATING_WARNING_HAS_SAME_AS = 'There are no sameAs configured for this entity.'; |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Entity warning has completed metadata identifier. |
|
| 118 | + * |
|
| 119 | + * @since 3.3.0 |
|
| 120 | + */ |
|
| 121 | + const RATING_WARNING_HAS_COMPLETED_METADATA = 'Schema.org metadata for this entity are not completed.'; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * The alternative label meta key. |
|
| 125 | + * |
|
| 126 | + * @since 3.2.0 |
|
| 127 | + */ |
|
| 128 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * The alternative label input template. |
|
| 132 | + * |
|
| 133 | + * @since 3.2.0 |
|
| 134 | + */ |
|
| 135 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 136 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 137 | 137 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 138 | 138 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 139 | 139 | <button class="button wl-delete-button">%s</button> |
| 140 | 140 | </div>'; |
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * A singleton instance of the Entity service. |
|
| 144 | - * |
|
| 145 | - * @since 3.2.0 |
|
| 146 | - * @access private |
|
| 147 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 148 | - */ |
|
| 149 | - private static $instance; |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Create a Wordlift_Entity_Service instance. |
|
| 153 | - * |
|
| 154 | - * @since 3.2.0 |
|
| 155 | - * |
|
| 156 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 157 | - */ |
|
| 158 | - public function __construct( $ui_service, $schema_service, $notice_service ) { |
|
| 159 | - |
|
| 160 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 161 | - |
|
| 162 | - // Set the UI service. |
|
| 163 | - $this->ui_service = $ui_service; |
|
| 164 | - |
|
| 165 | - // Set the Schema service. |
|
| 166 | - $this->schema_service = $schema_service; |
|
| 167 | - |
|
| 168 | - // Set the Schema service. |
|
| 169 | - $this->notice_service = $notice_service; |
|
| 170 | - |
|
| 171 | - // Set the singleton instance. |
|
| 172 | - self::$instance = $this; |
|
| 173 | - |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Get the singleton instance of the Entity service. |
|
| 178 | - * |
|
| 179 | - * @since 3.2.0 |
|
| 180 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 181 | - */ |
|
| 182 | - public static function get_instance() { |
|
| 183 | - |
|
| 184 | - return self::$instance; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Get rating max |
|
| 189 | - * |
|
| 190 | - * @since 3.3.0 |
|
| 191 | - * |
|
| 192 | - * @return int Max rating according to performed checks. |
|
| 193 | - */ |
|
| 194 | - public static function get_rating_max() { |
|
| 195 | - return self::RATING_MAX; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 200 | - * its specific function). |
|
| 201 | - * |
|
| 202 | - * @since 3.1.0 |
|
| 203 | - * |
|
| 204 | - * @return array An array of post IDs. |
|
| 205 | - */ |
|
| 206 | - public function get_all_related_to_last_50_published_posts() { |
|
| 207 | - |
|
| 208 | - // Global timeline. Get entities from the latest posts. |
|
| 209 | - $latest_posts_ids = get_posts( array( |
|
| 210 | - 'numberposts' => 50, |
|
| 211 | - 'fields' => 'ids', //only get post IDs |
|
| 212 | - 'post_type' => 'post', |
|
| 213 | - 'post_status' => 'publish' |
|
| 214 | - ) ); |
|
| 215 | - |
|
| 216 | - if ( empty( $latest_posts_ids ) ) { |
|
| 217 | - // There are no posts. |
|
| 218 | - return array(); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - // Collect entities related to latest posts |
|
| 222 | - $entity_ids = array(); |
|
| 223 | - foreach ( $latest_posts_ids as $id ) { |
|
| 224 | - $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 225 | - 'status' => 'publish' |
|
| 226 | - ) ) ); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - return $entity_ids; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Determines whether a post is an entity or not. |
|
| 234 | - * |
|
| 235 | - * @since 3.1.0 |
|
| 236 | - * |
|
| 237 | - * @param int $post_id A post id. |
|
| 238 | - * |
|
| 239 | - * @return true if the post is an entity otherwise false. |
|
| 240 | - */ |
|
| 241 | - public function is_entity( $post_id ) { |
|
| 242 | - |
|
| 243 | - return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Determines whether a given uri is an internal uri or not. |
|
| 248 | - * |
|
| 249 | - * @since 3.3.2 |
|
| 250 | - * |
|
| 251 | - * @param int $uri An uri. |
|
| 252 | - * |
|
| 253 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 254 | - */ |
|
| 255 | - public function is_internal_uri( $uri ) { |
|
| 256 | - |
|
| 257 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 262 | - * |
|
| 263 | - * @since 3.2.0 |
|
| 264 | - * |
|
| 265 | - * @param string $uri The entity URI. |
|
| 266 | - * |
|
| 267 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 268 | - */ |
|
| 269 | - public function get_entity_post_by_uri( $uri ) { |
|
| 270 | - |
|
| 271 | - $query_args = array( |
|
| 272 | - 'posts_per_page' => 1, |
|
| 273 | - 'post_status' => 'any', |
|
| 274 | - 'post_type' => self::TYPE_NAME, |
|
| 275 | - 'meta_query' => array( |
|
| 276 | - array( |
|
| 277 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 278 | - 'value' => $uri, |
|
| 279 | - 'compare' => '=' |
|
| 280 | - ) |
|
| 281 | - ) |
|
| 282 | - ); |
|
| 283 | - |
|
| 284 | - // Only if the current uri is not an internal uri |
|
| 285 | - // entity search is performed also looking at sameAs values |
|
| 286 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 287 | - if ( !$this->is_internal_uri( $uri ) ) { |
|
| 142 | + /** |
|
| 143 | + * A singleton instance of the Entity service. |
|
| 144 | + * |
|
| 145 | + * @since 3.2.0 |
|
| 146 | + * @access private |
|
| 147 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 148 | + */ |
|
| 149 | + private static $instance; |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Create a Wordlift_Entity_Service instance. |
|
| 153 | + * |
|
| 154 | + * @since 3.2.0 |
|
| 155 | + * |
|
| 156 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 157 | + */ |
|
| 158 | + public function __construct( $ui_service, $schema_service, $notice_service ) { |
|
| 159 | + |
|
| 160 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 161 | + |
|
| 162 | + // Set the UI service. |
|
| 163 | + $this->ui_service = $ui_service; |
|
| 164 | + |
|
| 165 | + // Set the Schema service. |
|
| 166 | + $this->schema_service = $schema_service; |
|
| 167 | + |
|
| 168 | + // Set the Schema service. |
|
| 169 | + $this->notice_service = $notice_service; |
|
| 170 | + |
|
| 171 | + // Set the singleton instance. |
|
| 172 | + self::$instance = $this; |
|
| 173 | + |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Get the singleton instance of the Entity service. |
|
| 178 | + * |
|
| 179 | + * @since 3.2.0 |
|
| 180 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 181 | + */ |
|
| 182 | + public static function get_instance() { |
|
| 183 | + |
|
| 184 | + return self::$instance; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Get rating max |
|
| 189 | + * |
|
| 190 | + * @since 3.3.0 |
|
| 191 | + * |
|
| 192 | + * @return int Max rating according to performed checks. |
|
| 193 | + */ |
|
| 194 | + public static function get_rating_max() { |
|
| 195 | + return self::RATING_MAX; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 200 | + * its specific function). |
|
| 201 | + * |
|
| 202 | + * @since 3.1.0 |
|
| 203 | + * |
|
| 204 | + * @return array An array of post IDs. |
|
| 205 | + */ |
|
| 206 | + public function get_all_related_to_last_50_published_posts() { |
|
| 207 | + |
|
| 208 | + // Global timeline. Get entities from the latest posts. |
|
| 209 | + $latest_posts_ids = get_posts( array( |
|
| 210 | + 'numberposts' => 50, |
|
| 211 | + 'fields' => 'ids', //only get post IDs |
|
| 212 | + 'post_type' => 'post', |
|
| 213 | + 'post_status' => 'publish' |
|
| 214 | + ) ); |
|
| 215 | + |
|
| 216 | + if ( empty( $latest_posts_ids ) ) { |
|
| 217 | + // There are no posts. |
|
| 218 | + return array(); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + // Collect entities related to latest posts |
|
| 222 | + $entity_ids = array(); |
|
| 223 | + foreach ( $latest_posts_ids as $id ) { |
|
| 224 | + $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 225 | + 'status' => 'publish' |
|
| 226 | + ) ) ); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + return $entity_ids; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Determines whether a post is an entity or not. |
|
| 234 | + * |
|
| 235 | + * @since 3.1.0 |
|
| 236 | + * |
|
| 237 | + * @param int $post_id A post id. |
|
| 238 | + * |
|
| 239 | + * @return true if the post is an entity otherwise false. |
|
| 240 | + */ |
|
| 241 | + public function is_entity( $post_id ) { |
|
| 242 | + |
|
| 243 | + return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Determines whether a given uri is an internal uri or not. |
|
| 248 | + * |
|
| 249 | + * @since 3.3.2 |
|
| 250 | + * |
|
| 251 | + * @param int $uri An uri. |
|
| 252 | + * |
|
| 253 | + * @return true if the uri internal to the current dataset otherwise false. |
|
| 254 | + */ |
|
| 255 | + public function is_internal_uri( $uri ) { |
|
| 256 | + |
|
| 257 | + return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 262 | + * |
|
| 263 | + * @since 3.2.0 |
|
| 264 | + * |
|
| 265 | + * @param string $uri The entity URI. |
|
| 266 | + * |
|
| 267 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 268 | + */ |
|
| 269 | + public function get_entity_post_by_uri( $uri ) { |
|
| 270 | + |
|
| 271 | + $query_args = array( |
|
| 272 | + 'posts_per_page' => 1, |
|
| 273 | + 'post_status' => 'any', |
|
| 274 | + 'post_type' => self::TYPE_NAME, |
|
| 275 | + 'meta_query' => array( |
|
| 276 | + array( |
|
| 277 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 278 | + 'value' => $uri, |
|
| 279 | + 'compare' => '=' |
|
| 280 | + ) |
|
| 281 | + ) |
|
| 282 | + ); |
|
| 283 | + |
|
| 284 | + // Only if the current uri is not an internal uri |
|
| 285 | + // entity search is performed also looking at sameAs values |
|
| 286 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 287 | + if ( !$this->is_internal_uri( $uri ) ) { |
|
| 288 | 288 | |
| 289 | - $query_args[ 'meta_query' ][ 'relation' ] = 'OR'; |
|
| 290 | - $query_args[ 'meta_query' ][] = array( |
|
| 291 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 292 | - 'value' => $uri, |
|
| 293 | - 'compare' => '=' |
|
| 294 | - ); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - $query = new WP_Query( $query_args ); |
|
| 298 | - |
|
| 299 | - // Get the matching entity posts. |
|
| 300 | - $posts = $query->get_posts(); |
|
| 301 | - |
|
| 302 | - // Return null if no post is found. |
|
| 303 | - if ( 0 === count( $posts ) ) { |
|
| 304 | - return null; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - // Return the found post. |
|
| 308 | - return $posts[0]; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Fires once a post has been saved. |
|
| 313 | - * |
|
| 314 | - * @since 3.2.0 |
|
| 315 | - * |
|
| 316 | - * @param int $post_id Post ID. |
|
| 317 | - * @param WP_Post $post Post object. |
|
| 318 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 319 | - */ |
|
| 320 | - public function save_post( $post_id, $post, $update ) { |
|
| 321 | - |
|
| 322 | - // If it's not an entity, return. |
|
| 323 | - if ( ! $this->is_entity( $post_id ) ) { |
|
| 324 | - return; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - // Get the alt labels from the request (or empty array). |
|
| 328 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 329 | - |
|
| 330 | - // Set the alternative labels. |
|
| 331 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 332 | - |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Set the alternative labels. |
|
| 337 | - * |
|
| 338 | - * @since 3.2.0 |
|
| 339 | - * |
|
| 340 | - * @param int $post_id The post id. |
|
| 341 | - * @param array $alt_labels An array of labels. |
|
| 342 | - */ |
|
| 343 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 289 | + $query_args[ 'meta_query' ][ 'relation' ] = 'OR'; |
|
| 290 | + $query_args[ 'meta_query' ][] = array( |
|
| 291 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 292 | + 'value' => $uri, |
|
| 293 | + 'compare' => '=' |
|
| 294 | + ); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + $query = new WP_Query( $query_args ); |
|
| 298 | + |
|
| 299 | + // Get the matching entity posts. |
|
| 300 | + $posts = $query->get_posts(); |
|
| 301 | + |
|
| 302 | + // Return null if no post is found. |
|
| 303 | + if ( 0 === count( $posts ) ) { |
|
| 304 | + return null; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + // Return the found post. |
|
| 308 | + return $posts[0]; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Fires once a post has been saved. |
|
| 313 | + * |
|
| 314 | + * @since 3.2.0 |
|
| 315 | + * |
|
| 316 | + * @param int $post_id Post ID. |
|
| 317 | + * @param WP_Post $post Post object. |
|
| 318 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 319 | + */ |
|
| 320 | + public function save_post( $post_id, $post, $update ) { |
|
| 321 | + |
|
| 322 | + // If it's not an entity, return. |
|
| 323 | + if ( ! $this->is_entity( $post_id ) ) { |
|
| 324 | + return; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + // Get the alt labels from the request (or empty array). |
|
| 328 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 329 | + |
|
| 330 | + // Set the alternative labels. |
|
| 331 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 332 | + |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Set the alternative labels. |
|
| 337 | + * |
|
| 338 | + * @since 3.2.0 |
|
| 339 | + * |
|
| 340 | + * @param int $post_id The post id. |
|
| 341 | + * @param array $alt_labels An array of labels. |
|
| 342 | + */ |
|
| 343 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 344 | 344 | |
| 345 | - // Force $alt_labels to be an array |
|
| 346 | - if( !is_array( $alt_labels ) ) { |
|
| 347 | - $alt_labels = array( $alt_labels ); |
|
| 348 | - } |
|
| 345 | + // Force $alt_labels to be an array |
|
| 346 | + if( !is_array( $alt_labels ) ) { |
|
| 347 | + $alt_labels = array( $alt_labels ); |
|
| 348 | + } |
|
| 349 | 349 | |
| 350 | - $this->log_service->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 350 | + $this->log_service->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 351 | 351 | |
| 352 | - // Delete all the existing alternate labels. |
|
| 353 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 352 | + // Delete all the existing alternate labels. |
|
| 353 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 354 | 354 | |
| 355 | - // Set the alternative labels. |
|
| 356 | - foreach ( $alt_labels as $alt_label ) { |
|
| 357 | - if ( ! empty( $alt_label ) ) { |
|
| 358 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Retrieve the alternate labels. |
|
| 366 | - * |
|
| 367 | - * @since 3.2.0 |
|
| 368 | - * |
|
| 369 | - * @param int $post_id Post id. |
|
| 370 | - * |
|
| 371 | - * @return mixed An array of alternative labels. |
|
| 372 | - */ |
|
| 373 | - public function get_alternative_labels( $post_id ) { |
|
| 374 | - |
|
| 375 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 380 | - * |
|
| 381 | - * @since 3.2.0 |
|
| 382 | - * |
|
| 383 | - * @param WP_Post $post Post object. |
|
| 384 | - */ |
|
| 385 | - public function edit_form_before_permalink( $post ) { |
|
| 386 | - |
|
| 387 | - // If it's not an entity, return. |
|
| 388 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 389 | - return; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - // Print the input template. |
|
| 393 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 394 | - |
|
| 395 | - // Print all the currently set alternative labels. |
|
| 396 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 397 | - |
|
| 398 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 399 | - |
|
| 400 | - }; |
|
| 401 | - |
|
| 402 | - // Print the button. |
|
| 403 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 404 | - |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Add admin notices for the current entity depending on the current rating. |
|
| 409 | - * |
|
| 410 | - * @since 3.3.0 |
|
| 411 | - * |
|
| 412 | - * @param WP_Post $post Post object. |
|
| 413 | - */ |
|
| 414 | - public function in_admin_header() { |
|
| 415 | - |
|
| 416 | - // Return safely if get_current_screen() is not defined (yet) |
|
| 417 | - if ( FALSE === function_exists( 'get_current_screen' ) ) { |
|
| 418 | - return; |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - // If you're not in the entity post edit page, return. |
|
| 422 | - if ( self::TYPE_NAME !== get_current_screen()->id ) { |
|
| 423 | - return; |
|
| 424 | - } |
|
| 425 | - // Retrieve the current global post |
|
| 426 | - global $post; |
|
| 427 | - // If it's not an entity, return. |
|
| 428 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 429 | - return; |
|
| 430 | - } |
|
| 431 | - // Retrieve an updated rating for the current entity |
|
| 432 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
| 433 | - // If there is at least 1 warning |
|
| 434 | - if ( isset( $rating[ 'warnings' ] ) && 0 < count( $rating[ 'warnings' ] ) ) { |
|
| 435 | - // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
| 436 | - $this->notice_service->add_suggestion( $rating[ 'warnings' ] ); |
|
| 437 | - } |
|
| 355 | + // Set the alternative labels. |
|
| 356 | + foreach ( $alt_labels as $alt_label ) { |
|
| 357 | + if ( ! empty( $alt_label ) ) { |
|
| 358 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Retrieve the alternate labels. |
|
| 366 | + * |
|
| 367 | + * @since 3.2.0 |
|
| 368 | + * |
|
| 369 | + * @param int $post_id Post id. |
|
| 370 | + * |
|
| 371 | + * @return mixed An array of alternative labels. |
|
| 372 | + */ |
|
| 373 | + public function get_alternative_labels( $post_id ) { |
|
| 374 | + |
|
| 375 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 380 | + * |
|
| 381 | + * @since 3.2.0 |
|
| 382 | + * |
|
| 383 | + * @param WP_Post $post Post object. |
|
| 384 | + */ |
|
| 385 | + public function edit_form_before_permalink( $post ) { |
|
| 386 | + |
|
| 387 | + // If it's not an entity, return. |
|
| 388 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 389 | + return; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + // Print the input template. |
|
| 393 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 394 | + |
|
| 395 | + // Print all the currently set alternative labels. |
|
| 396 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 397 | + |
|
| 398 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 399 | + |
|
| 400 | + }; |
|
| 401 | + |
|
| 402 | + // Print the button. |
|
| 403 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 404 | + |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Add admin notices for the current entity depending on the current rating. |
|
| 409 | + * |
|
| 410 | + * @since 3.3.0 |
|
| 411 | + * |
|
| 412 | + * @param WP_Post $post Post object. |
|
| 413 | + */ |
|
| 414 | + public function in_admin_header() { |
|
| 415 | + |
|
| 416 | + // Return safely if get_current_screen() is not defined (yet) |
|
| 417 | + if ( FALSE === function_exists( 'get_current_screen' ) ) { |
|
| 418 | + return; |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + // If you're not in the entity post edit page, return. |
|
| 422 | + if ( self::TYPE_NAME !== get_current_screen()->id ) { |
|
| 423 | + return; |
|
| 424 | + } |
|
| 425 | + // Retrieve the current global post |
|
| 426 | + global $post; |
|
| 427 | + // If it's not an entity, return. |
|
| 428 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 429 | + return; |
|
| 430 | + } |
|
| 431 | + // Retrieve an updated rating for the current entity |
|
| 432 | + $rating = $this->get_rating_for( $post->ID, true ); |
|
| 433 | + // If there is at least 1 warning |
|
| 434 | + if ( isset( $rating[ 'warnings' ] ) && 0 < count( $rating[ 'warnings' ] ) ) { |
|
| 435 | + // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
| 436 | + $this->notice_service->add_suggestion( $rating[ 'warnings' ] ); |
|
| 437 | + } |
|
| 438 | 438 | |
| 439 | - } |
|
| 439 | + } |
|
| 440 | 440 | |
| 441 | - /** |
|
| 442 | - * Set rating for a given entity |
|
| 443 | - * |
|
| 444 | - * @since 3.3.0 |
|
| 445 | - * |
|
| 446 | - * @param int $post_id The entity post id. |
|
| 447 | - * |
|
| 448 | - * @return int An array representing the rating obj. |
|
| 449 | - */ |
|
| 450 | - public function set_rating_for( $post_id ) { |
|
| 451 | - |
|
| 452 | - // Calculate rating for the given post |
|
| 453 | - $rating = $this->calculate_rating_for( $post_id ); |
|
| 454 | - // Store the rating on db as post meta |
|
| 455 | - // Please notice that RATING_RAW_SCORE_META_KEY |
|
| 456 | - // is saved on a different meta to allow score sorting |
|
| 457 | - // Both meta are managed as unique |
|
| 458 | - // https://codex.wordpress.org/Function_Reference/update_post_meta |
|
| 459 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating[ 'raw_score' ] ); |
|
| 460 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating[ 'warnings' ] ); |
|
| 441 | + /** |
|
| 442 | + * Set rating for a given entity |
|
| 443 | + * |
|
| 444 | + * @since 3.3.0 |
|
| 445 | + * |
|
| 446 | + * @param int $post_id The entity post id. |
|
| 447 | + * |
|
| 448 | + * @return int An array representing the rating obj. |
|
| 449 | + */ |
|
| 450 | + public function set_rating_for( $post_id ) { |
|
| 451 | + |
|
| 452 | + // Calculate rating for the given post |
|
| 453 | + $rating = $this->calculate_rating_for( $post_id ); |
|
| 454 | + // Store the rating on db as post meta |
|
| 455 | + // Please notice that RATING_RAW_SCORE_META_KEY |
|
| 456 | + // is saved on a different meta to allow score sorting |
|
| 457 | + // Both meta are managed as unique |
|
| 458 | + // https://codex.wordpress.org/Function_Reference/update_post_meta |
|
| 459 | + update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating[ 'raw_score' ] ); |
|
| 460 | + update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating[ 'warnings' ] ); |
|
| 461 | 461 | |
| 462 | - $this->log_service->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating[ 'raw_score' ] ) ); |
|
| 462 | + $this->log_service->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating[ 'raw_score' ] ) ); |
|
| 463 | 463 | |
| 464 | - // Finally returns the rating |
|
| 465 | - return $rating; |
|
| 466 | - } |
|
| 467 | - /** |
|
| 468 | - * Get or calculate rating for a given entity |
|
| 469 | - * |
|
| 470 | - * @since 3.3.0 |
|
| 471 | - * |
|
| 472 | - * @param int $post_id The entity post id. |
|
| 473 | - * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 474 | - * |
|
| 475 | - * @return int An array representing the rating obj. |
|
| 476 | - */ |
|
| 477 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 464 | + // Finally returns the rating |
|
| 465 | + return $rating; |
|
| 466 | + } |
|
| 467 | + /** |
|
| 468 | + * Get or calculate rating for a given entity |
|
| 469 | + * |
|
| 470 | + * @since 3.3.0 |
|
| 471 | + * |
|
| 472 | + * @param int $post_id The entity post id. |
|
| 473 | + * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 474 | + * |
|
| 475 | + * @return int An array representing the rating obj. |
|
| 476 | + */ |
|
| 477 | + public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 478 | 478 | |
| 479 | - // If forced reload is required or rating is missing .. |
|
| 480 | - if ( $force_reload ) { |
|
| 481 | - $this->log_service->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 482 | - return $this->set_rating_for( $post_id ); |
|
| 483 | - } |
|
| 479 | + // If forced reload is required or rating is missing .. |
|
| 480 | + if ( $force_reload ) { |
|
| 481 | + $this->log_service->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 482 | + return $this->set_rating_for( $post_id ); |
|
| 483 | + } |
|
| 484 | 484 | |
| 485 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 485 | + $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 486 | 486 | |
| 487 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
| 488 | - $this->log_service->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 489 | - return $this->set_rating_for( $post_id ); |
|
| 490 | - } |
|
| 491 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 487 | + if ( ! is_numeric( $current_raw_score ) ) { |
|
| 488 | + $this->log_service->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 489 | + return $this->set_rating_for( $post_id ); |
|
| 490 | + } |
|
| 491 | + $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 492 | 492 | |
| 493 | - // Finally return score and warnings |
|
| 494 | - return array( |
|
| 495 | - 'raw_score' => $current_raw_score, |
|
| 496 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 497 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 498 | - 'warnings' => $current_warnings, |
|
| 499 | - ); |
|
| 500 | - |
|
| 501 | - } |
|
| 502 | - /** |
|
| 503 | - * Calculate rating for a given entity |
|
| 504 | - * Rating depends from following criteria |
|
| 505 | - * |
|
| 506 | - * 1. Is the current entity related to at least 1 post? |
|
| 507 | - * 2. Is the current entity content post not empty? |
|
| 508 | - * 3. Is the current entity related to at least 1 entity? |
|
| 509 | - * 4. Is the entity published? |
|
| 510 | - * 5. There is a a thumbnail associated to the entity? |
|
| 511 | - * 6. Has the entity a sameas defined? |
|
| 512 | - * 7. Are all schema.org required metadata compiled? |
|
| 513 | - * |
|
| 514 | - * Each positive check means +1 in terms of rating score |
|
| 515 | - * |
|
| 516 | - * @since 3.3.0 |
|
| 517 | - * |
|
| 518 | - * @param int $post_id The entity post id. |
|
| 519 | - * |
|
| 520 | - * @return int An array representing the rating obj. |
|
| 521 | - */ |
|
| 522 | - public function calculate_rating_for( $post_id ) { |
|
| 493 | + // Finally return score and warnings |
|
| 494 | + return array( |
|
| 495 | + 'raw_score' => $current_raw_score, |
|
| 496 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 497 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 498 | + 'warnings' => $current_warnings, |
|
| 499 | + ); |
|
| 500 | + |
|
| 501 | + } |
|
| 502 | + /** |
|
| 503 | + * Calculate rating for a given entity |
|
| 504 | + * Rating depends from following criteria |
|
| 505 | + * |
|
| 506 | + * 1. Is the current entity related to at least 1 post? |
|
| 507 | + * 2. Is the current entity content post not empty? |
|
| 508 | + * 3. Is the current entity related to at least 1 entity? |
|
| 509 | + * 4. Is the entity published? |
|
| 510 | + * 5. There is a a thumbnail associated to the entity? |
|
| 511 | + * 6. Has the entity a sameas defined? |
|
| 512 | + * 7. Are all schema.org required metadata compiled? |
|
| 513 | + * |
|
| 514 | + * Each positive check means +1 in terms of rating score |
|
| 515 | + * |
|
| 516 | + * @since 3.3.0 |
|
| 517 | + * |
|
| 518 | + * @param int $post_id The entity post id. |
|
| 519 | + * |
|
| 520 | + * @return int An array representing the rating obj. |
|
| 521 | + */ |
|
| 522 | + public function calculate_rating_for( $post_id ) { |
|
| 523 | 523 | |
| 524 | - // If it's not an entity, return. |
|
| 525 | - if ( ! $this->is_entity( $post_id ) ) { |
|
| 526 | - return; |
|
| 527 | - } |
|
| 528 | - // Retrieve the post object |
|
| 529 | - $post = get_post( $post_id ); |
|
| 530 | - // Rating value |
|
| 531 | - $score = 0; |
|
| 532 | - // Store warning messages |
|
| 533 | - $warnings = array(); |
|
| 534 | - |
|
| 535 | - // Is the current entity related to at least 1 post? |
|
| 536 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 537 | - $score++ : |
|
| 538 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift' ) ); |
|
| 524 | + // If it's not an entity, return. |
|
| 525 | + if ( ! $this->is_entity( $post_id ) ) { |
|
| 526 | + return; |
|
| 527 | + } |
|
| 528 | + // Retrieve the post object |
|
| 529 | + $post = get_post( $post_id ); |
|
| 530 | + // Rating value |
|
| 531 | + $score = 0; |
|
| 532 | + // Store warning messages |
|
| 533 | + $warnings = array(); |
|
| 534 | + |
|
| 535 | + // Is the current entity related to at least 1 post? |
|
| 536 | + ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 537 | + $score++ : |
|
| 538 | + array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift' ) ); |
|
| 539 | 539 | |
| 540 | - // Is the post content not empty? |
|
| 541 | - ( ! empty( $post->post_content ) ) ? |
|
| 542 | - $score++ : |
|
| 543 | - array_push( $warnings, __( self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift' ) ); |
|
| 540 | + // Is the post content not empty? |
|
| 541 | + ( ! empty( $post->post_content ) ) ? |
|
| 542 | + $score++ : |
|
| 543 | + array_push( $warnings, __( self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift' ) ); |
|
| 544 | 544 | |
| 545 | - // Is the current entity related to at least 1 entity? |
|
| 546 | - // Was the current entity already disambiguated? |
|
| 547 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 548 | - $score++ : |
|
| 549 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift' ) ); |
|
| 545 | + // Is the current entity related to at least 1 entity? |
|
| 546 | + // Was the current entity already disambiguated? |
|
| 547 | + ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 548 | + $score++ : |
|
| 549 | + array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift' ) ); |
|
| 550 | 550 | |
| 551 | - // Is the entity published? |
|
| 552 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 553 | - $score++ : |
|
| 554 | - array_push( $warnings, __( self::RATING_WARNING_IS_PUBLISHED, 'wordlift' ) ); |
|
| 551 | + // Is the entity published? |
|
| 552 | + ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 553 | + $score++ : |
|
| 554 | + array_push( $warnings, __( self::RATING_WARNING_IS_PUBLISHED, 'wordlift' ) ); |
|
| 555 | 555 | |
| 556 | - // Has a thumbnail? |
|
| 557 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
| 558 | - $score++ : |
|
| 559 | - array_push( $warnings, __( self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift' ) ); |
|
| 556 | + // Has a thumbnail? |
|
| 557 | + ( has_post_thumbnail( $post->ID ) ) ? |
|
| 558 | + $score++ : |
|
| 559 | + array_push( $warnings, __( self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift' ) ); |
|
| 560 | 560 | |
| 561 | - // Get all post meta keys for the current post |
|
| 562 | - global $wpdb; |
|
| 563 | - $query = $wpdb->prepare( |
|
| 564 | - "SELECT DISTINCT(meta_key) FROM $wpdb->postmeta WHERE post_id = %d", $post->ID |
|
| 565 | - ); |
|
| 561 | + // Get all post meta keys for the current post |
|
| 562 | + global $wpdb; |
|
| 563 | + $query = $wpdb->prepare( |
|
| 564 | + "SELECT DISTINCT(meta_key) FROM $wpdb->postmeta WHERE post_id = %d", $post->ID |
|
| 565 | + ); |
|
| 566 | 566 | |
| 567 | - // Check intersection between available meta keys |
|
| 568 | - // and expected ones arrays to detect missing values |
|
| 569 | - $available_meta_keys = $wpdb->get_col( $query ); |
|
| 570 | - |
|
| 571 | - // If each expected key is contained in available keys array ... |
|
| 572 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 573 | - $score++ : |
|
| 574 | - array_push( $warnings, __( self::RATING_WARNING_HAS_SAME_AS, 'wordlift' ) ); |
|
| 567 | + // Check intersection between available meta keys |
|
| 568 | + // and expected ones arrays to detect missing values |
|
| 569 | + $available_meta_keys = $wpdb->get_col( $query ); |
|
| 570 | + |
|
| 571 | + // If each expected key is contained in available keys array ... |
|
| 572 | + ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 573 | + $score++ : |
|
| 574 | + array_push( $warnings, __( self::RATING_WARNING_HAS_SAME_AS, 'wordlift' ) ); |
|
| 575 | 575 | |
| 576 | - $schema = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 576 | + $schema = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 577 | 577 | |
| 578 | - $expected_meta_keys = ( null === $schema[ 'custom_fields' ] ) ? |
|
| 579 | - array() : |
|
| 580 | - array_keys( $schema[ 'custom_fields' ] ); |
|
| 578 | + $expected_meta_keys = ( null === $schema[ 'custom_fields' ] ) ? |
|
| 579 | + array() : |
|
| 580 | + array_keys( $schema[ 'custom_fields' ] ); |
|
| 581 | 581 | |
| 582 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 583 | - // If each expected key is contained in available keys array ... |
|
| 584 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 585 | - $score++ : |
|
| 586 | - array_push( $warnings, __( self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift' ) ); |
|
| 582 | + $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 583 | + // If each expected key is contained in available keys array ... |
|
| 584 | + ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 585 | + $score++ : |
|
| 586 | + array_push( $warnings, __( self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift' ) ); |
|
| 587 | 587 | |
| 588 | - // Finally return score and warnings |
|
| 589 | - return array( |
|
| 590 | - 'raw_score' => $score, |
|
| 591 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 592 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 593 | - 'warnings' => $warnings, |
|
| 594 | - ); |
|
| 595 | - |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * Get as rating as input and convert in a traffic-light rating |
|
| 600 | - * |
|
| 601 | - * @since 3.3.0 |
|
| 602 | - * |
|
| 603 | - * @param int $score The rating score for a given entity. |
|
| 604 | - * |
|
| 605 | - * @return string The input HTML code. |
|
| 606 | - */ |
|
| 607 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
| 608 | - // RATING_MAX : $score = 3 : x |
|
| 609 | - // See http://php.net/manual/en/function.round.php |
|
| 610 | - $rating = round( ( $score * 3 ) / self::get_rating_max(), 0, PHP_ROUND_HALF_UP ); |
|
| 611 | - // If rating is 0, return 1, otherwise return rating |
|
| 612 | - return ( 0 == $rating ) ? 1 : $rating; |
|
| 613 | - |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * Get as rating as input and convert in a traffic-light rating |
|
| 618 | - * |
|
| 619 | - * @since 3.3.0 |
|
| 620 | - * |
|
| 621 | - * @param int $score The rating score for a given entity. |
|
| 622 | - * |
|
| 623 | - * @return string The input HTML code. |
|
| 624 | - */ |
|
| 625 | - public function convert_raw_score_to_percentage( $score ) { |
|
| 626 | - // RATING_MAX : $score = 100 : x |
|
| 627 | - return round( ( $score * 100) / self::get_rating_max(), 0, PHP_ROUND_HALF_UP ); |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - /** |
|
| 631 | - * Get the alternative label input HTML code. |
|
| 632 | - * |
|
| 633 | - * @since 3.2.0 |
|
| 634 | - * |
|
| 635 | - * @param string $value The input value. |
|
| 636 | - * |
|
| 637 | - * @return string The input HTML code. |
|
| 638 | - */ |
|
| 639 | - private function get_alternative_label_input( $value = '' ) { |
|
| 640 | - |
|
| 641 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 642 | - } |
|
| 588 | + // Finally return score and warnings |
|
| 589 | + return array( |
|
| 590 | + 'raw_score' => $score, |
|
| 591 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 592 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 593 | + 'warnings' => $warnings, |
|
| 594 | + ); |
|
| 595 | + |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * Get as rating as input and convert in a traffic-light rating |
|
| 600 | + * |
|
| 601 | + * @since 3.3.0 |
|
| 602 | + * |
|
| 603 | + * @param int $score The rating score for a given entity. |
|
| 604 | + * |
|
| 605 | + * @return string The input HTML code. |
|
| 606 | + */ |
|
| 607 | + private function convert_raw_score_to_traffic_light( $score ) { |
|
| 608 | + // RATING_MAX : $score = 3 : x |
|
| 609 | + // See http://php.net/manual/en/function.round.php |
|
| 610 | + $rating = round( ( $score * 3 ) / self::get_rating_max(), 0, PHP_ROUND_HALF_UP ); |
|
| 611 | + // If rating is 0, return 1, otherwise return rating |
|
| 612 | + return ( 0 == $rating ) ? 1 : $rating; |
|
| 613 | + |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + /** |
|
| 617 | + * Get as rating as input and convert in a traffic-light rating |
|
| 618 | + * |
|
| 619 | + * @since 3.3.0 |
|
| 620 | + * |
|
| 621 | + * @param int $score The rating score for a given entity. |
|
| 622 | + * |
|
| 623 | + * @return string The input HTML code. |
|
| 624 | + */ |
|
| 625 | + public function convert_raw_score_to_percentage( $score ) { |
|
| 626 | + // RATING_MAX : $score = 100 : x |
|
| 627 | + return round( ( $score * 100) / self::get_rating_max(), 0, PHP_ROUND_HALF_UP ); |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + /** |
|
| 631 | + * Get the alternative label input HTML code. |
|
| 632 | + * |
|
| 633 | + * @since 3.2.0 |
|
| 634 | + * |
|
| 635 | + * @param string $value The input value. |
|
| 636 | + * |
|
| 637 | + * @return string The input HTML code. |
|
| 638 | + */ |
|
| 639 | + private function get_alternative_label_input( $value = '' ) { |
|
| 640 | + |
|
| 641 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 642 | + } |
|
| 643 | 643 | } |
@@ -155,9 +155,9 @@ discard block |
||
| 155 | 155 | * |
| 156 | 156 | * @param \Wordlift_UI_Service $ui_service The UI service. |
| 157 | 157 | */ |
| 158 | - public function __construct( $ui_service, $schema_service, $notice_service ) { |
|
| 158 | + public function __construct($ui_service, $schema_service, $notice_service) { |
|
| 159 | 159 | |
| 160 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 160 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 161 | 161 | |
| 162 | 162 | // Set the UI service. |
| 163 | 163 | $this->ui_service = $ui_service; |
@@ -206,24 +206,24 @@ discard block |
||
| 206 | 206 | public function get_all_related_to_last_50_published_posts() { |
| 207 | 207 | |
| 208 | 208 | // Global timeline. Get entities from the latest posts. |
| 209 | - $latest_posts_ids = get_posts( array( |
|
| 209 | + $latest_posts_ids = get_posts(array( |
|
| 210 | 210 | 'numberposts' => 50, |
| 211 | 211 | 'fields' => 'ids', //only get post IDs |
| 212 | 212 | 'post_type' => 'post', |
| 213 | 213 | 'post_status' => 'publish' |
| 214 | - ) ); |
|
| 214 | + )); |
|
| 215 | 215 | |
| 216 | - if ( empty( $latest_posts_ids ) ) { |
|
| 216 | + if (empty($latest_posts_ids)) { |
|
| 217 | 217 | // There are no posts. |
| 218 | 218 | return array(); |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | // Collect entities related to latest posts |
| 222 | 222 | $entity_ids = array(); |
| 223 | - foreach ( $latest_posts_ids as $id ) { |
|
| 224 | - $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 223 | + foreach ($latest_posts_ids as $id) { |
|
| 224 | + $entity_ids = array_merge($entity_ids, wl_core_get_related_entity_ids($id, array( |
|
| 225 | 225 | 'status' => 'publish' |
| 226 | - ) ) ); |
|
| 226 | + ))); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | return $entity_ids; |
@@ -238,9 +238,9 @@ discard block |
||
| 238 | 238 | * |
| 239 | 239 | * @return true if the post is an entity otherwise false. |
| 240 | 240 | */ |
| 241 | - public function is_entity( $post_id ) { |
|
| 241 | + public function is_entity($post_id) { |
|
| 242 | 242 | |
| 243 | - return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 243 | + return (self::TYPE_NAME === get_post_type($post_id)); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | /** |
@@ -252,9 +252,9 @@ discard block |
||
| 252 | 252 | * |
| 253 | 253 | * @return true if the uri internal to the current dataset otherwise false. |
| 254 | 254 | */ |
| 255 | - public function is_internal_uri( $uri ) { |
|
| 255 | + public function is_internal_uri($uri) { |
|
| 256 | 256 | |
| 257 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 257 | + return (0 === strrpos($uri, wl_configuration_get_redlink_dataset_uri())); |
|
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | /** |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | * |
| 267 | 267 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 268 | 268 | */ |
| 269 | - public function get_entity_post_by_uri( $uri ) { |
|
| 269 | + public function get_entity_post_by_uri($uri) { |
|
| 270 | 270 | |
| 271 | 271 | $query_args = array( |
| 272 | 272 | 'posts_per_page' => 1, |
@@ -284,23 +284,23 @@ discard block |
||
| 284 | 284 | // Only if the current uri is not an internal uri |
| 285 | 285 | // entity search is performed also looking at sameAs values |
| 286 | 286 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 287 | - if ( !$this->is_internal_uri( $uri ) ) { |
|
| 287 | + if ( ! $this->is_internal_uri($uri)) { |
|
| 288 | 288 | |
| 289 | - $query_args[ 'meta_query' ][ 'relation' ] = 'OR'; |
|
| 290 | - $query_args[ 'meta_query' ][] = array( |
|
| 289 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 290 | + $query_args['meta_query'][] = array( |
|
| 291 | 291 | 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
| 292 | 292 | 'value' => $uri, |
| 293 | 293 | 'compare' => '=' |
| 294 | 294 | ); |
| 295 | 295 | } |
| 296 | 296 | |
| 297 | - $query = new WP_Query( $query_args ); |
|
| 297 | + $query = new WP_Query($query_args); |
|
| 298 | 298 | |
| 299 | 299 | // Get the matching entity posts. |
| 300 | 300 | $posts = $query->get_posts(); |
| 301 | 301 | |
| 302 | 302 | // Return null if no post is found. |
| 303 | - if ( 0 === count( $posts ) ) { |
|
| 303 | + if (0 === count($posts)) { |
|
| 304 | 304 | return null; |
| 305 | 305 | } |
| 306 | 306 | |
@@ -317,18 +317,18 @@ discard block |
||
| 317 | 317 | * @param WP_Post $post Post object. |
| 318 | 318 | * @param bool $update Whether this is an existing post being updated or not. |
| 319 | 319 | */ |
| 320 | - public function save_post( $post_id, $post, $update ) { |
|
| 320 | + public function save_post($post_id, $post, $update) { |
|
| 321 | 321 | |
| 322 | 322 | // If it's not an entity, return. |
| 323 | - if ( ! $this->is_entity( $post_id ) ) { |
|
| 323 | + if ( ! $this->is_entity($post_id)) { |
|
| 324 | 324 | return; |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | // Get the alt labels from the request (or empty array). |
| 328 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 328 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 329 | 329 | |
| 330 | 330 | // Set the alternative labels. |
| 331 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 331 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 332 | 332 | |
| 333 | 333 | } |
| 334 | 334 | |
@@ -340,22 +340,22 @@ discard block |
||
| 340 | 340 | * @param int $post_id The post id. |
| 341 | 341 | * @param array $alt_labels An array of labels. |
| 342 | 342 | */ |
| 343 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 343 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 344 | 344 | |
| 345 | 345 | // Force $alt_labels to be an array |
| 346 | - if( !is_array( $alt_labels ) ) { |
|
| 347 | - $alt_labels = array( $alt_labels ); |
|
| 346 | + if ( ! is_array($alt_labels)) { |
|
| 347 | + $alt_labels = array($alt_labels); |
|
| 348 | 348 | } |
| 349 | 349 | |
| 350 | - $this->log_service->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 350 | + $this->log_service->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 351 | 351 | |
| 352 | 352 | // Delete all the existing alternate labels. |
| 353 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 353 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 354 | 354 | |
| 355 | 355 | // Set the alternative labels. |
| 356 | - foreach ( $alt_labels as $alt_label ) { |
|
| 357 | - if ( ! empty( $alt_label ) ) { |
|
| 358 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 356 | + foreach ($alt_labels as $alt_label) { |
|
| 357 | + if ( ! empty($alt_label)) { |
|
| 358 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | 361 | |
@@ -370,9 +370,9 @@ discard block |
||
| 370 | 370 | * |
| 371 | 371 | * @return mixed An array of alternative labels. |
| 372 | 372 | */ |
| 373 | - public function get_alternative_labels( $post_id ) { |
|
| 373 | + public function get_alternative_labels($post_id) { |
|
| 374 | 374 | |
| 375 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 375 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | /** |
@@ -382,25 +382,25 @@ discard block |
||
| 382 | 382 | * |
| 383 | 383 | * @param WP_Post $post Post object. |
| 384 | 384 | */ |
| 385 | - public function edit_form_before_permalink( $post ) { |
|
| 385 | + public function edit_form_before_permalink($post) { |
|
| 386 | 386 | |
| 387 | 387 | // If it's not an entity, return. |
| 388 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 388 | + if ( ! $this->is_entity($post->ID)) { |
|
| 389 | 389 | return; |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | // Print the input template. |
| 393 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 393 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 394 | 394 | |
| 395 | 395 | // Print all the currently set alternative labels. |
| 396 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 396 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 397 | 397 | |
| 398 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 398 | + echo $this->get_alternative_label_input($alt_label); |
|
| 399 | 399 | |
| 400 | 400 | }; |
| 401 | 401 | |
| 402 | 402 | // Print the button. |
| 403 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 403 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 404 | 404 | |
| 405 | 405 | } |
| 406 | 406 | |
@@ -414,26 +414,26 @@ discard block |
||
| 414 | 414 | public function in_admin_header() { |
| 415 | 415 | |
| 416 | 416 | // Return safely if get_current_screen() is not defined (yet) |
| 417 | - if ( FALSE === function_exists( 'get_current_screen' ) ) { |
|
| 417 | + if (FALSE === function_exists('get_current_screen')) { |
|
| 418 | 418 | return; |
| 419 | 419 | } |
| 420 | 420 | |
| 421 | 421 | // If you're not in the entity post edit page, return. |
| 422 | - if ( self::TYPE_NAME !== get_current_screen()->id ) { |
|
| 422 | + if (self::TYPE_NAME !== get_current_screen()->id) { |
|
| 423 | 423 | return; |
| 424 | 424 | } |
| 425 | 425 | // Retrieve the current global post |
| 426 | 426 | global $post; |
| 427 | 427 | // If it's not an entity, return. |
| 428 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 428 | + if ( ! $this->is_entity($post->ID)) { |
|
| 429 | 429 | return; |
| 430 | 430 | } |
| 431 | 431 | // Retrieve an updated rating for the current entity |
| 432 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
| 432 | + $rating = $this->get_rating_for($post->ID, true); |
|
| 433 | 433 | // If there is at least 1 warning |
| 434 | - if ( isset( $rating[ 'warnings' ] ) && 0 < count( $rating[ 'warnings' ] ) ) { |
|
| 434 | + if (isset($rating['warnings']) && 0 < count($rating['warnings'])) { |
|
| 435 | 435 | // TODO - Pass Wordlift_Notice_Service trough the service constructor |
| 436 | - $this->notice_service->add_suggestion( $rating[ 'warnings' ] ); |
|
| 436 | + $this->notice_service->add_suggestion($rating['warnings']); |
|
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | } |
@@ -447,19 +447,19 @@ discard block |
||
| 447 | 447 | * |
| 448 | 448 | * @return int An array representing the rating obj. |
| 449 | 449 | */ |
| 450 | - public function set_rating_for( $post_id ) { |
|
| 450 | + public function set_rating_for($post_id) { |
|
| 451 | 451 | |
| 452 | 452 | // Calculate rating for the given post |
| 453 | - $rating = $this->calculate_rating_for( $post_id ); |
|
| 453 | + $rating = $this->calculate_rating_for($post_id); |
|
| 454 | 454 | // Store the rating on db as post meta |
| 455 | 455 | // Please notice that RATING_RAW_SCORE_META_KEY |
| 456 | 456 | // is saved on a different meta to allow score sorting |
| 457 | 457 | // Both meta are managed as unique |
| 458 | 458 | // https://codex.wordpress.org/Function_Reference/update_post_meta |
| 459 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating[ 'raw_score' ] ); |
|
| 460 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating[ 'warnings' ] ); |
|
| 459 | + update_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score']); |
|
| 460 | + update_post_meta($post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings']); |
|
| 461 | 461 | |
| 462 | - $this->log_service->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating[ 'raw_score' ] ) ); |
|
| 462 | + $this->log_service->trace(sprintf("Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'])); |
|
| 463 | 463 | |
| 464 | 464 | // Finally returns the rating |
| 465 | 465 | return $rating; |
@@ -474,27 +474,27 @@ discard block |
||
| 474 | 474 | * |
| 475 | 475 | * @return int An array representing the rating obj. |
| 476 | 476 | */ |
| 477 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 477 | + public function get_rating_for($post_id, $force_reload = false) { |
|
| 478 | 478 | |
| 479 | 479 | // If forced reload is required or rating is missing .. |
| 480 | - if ( $force_reload ) { |
|
| 481 | - $this->log_service->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 482 | - return $this->set_rating_for( $post_id ); |
|
| 480 | + if ($force_reload) { |
|
| 481 | + $this->log_service->trace("Force rating reload [ post_id :: $post_id ]"); |
|
| 482 | + return $this->set_rating_for($post_id); |
|
| 483 | 483 | } |
| 484 | 484 | |
| 485 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 485 | + $current_raw_score = get_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, true); |
|
| 486 | 486 | |
| 487 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
| 488 | - $this->log_service->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 489 | - return $this->set_rating_for( $post_id ); |
|
| 487 | + if ( ! is_numeric($current_raw_score)) { |
|
| 488 | + $this->log_service->trace("Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]"); |
|
| 489 | + return $this->set_rating_for($post_id); |
|
| 490 | 490 | } |
| 491 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 491 | + $current_warnings = get_post_meta($post_id, self::RATING_WARNINGS_META_KEY, true); |
|
| 492 | 492 | |
| 493 | 493 | // Finally return score and warnings |
| 494 | 494 | return array( |
| 495 | 495 | 'raw_score' => $current_raw_score, |
| 496 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 497 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 496 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($current_raw_score), |
|
| 497 | + 'percentage_score' => $this->convert_raw_score_to_percentage($current_raw_score), |
|
| 498 | 498 | 'warnings' => $current_warnings, |
| 499 | 499 | ); |
| 500 | 500 | |
@@ -519,44 +519,39 @@ discard block |
||
| 519 | 519 | * |
| 520 | 520 | * @return int An array representing the rating obj. |
| 521 | 521 | */ |
| 522 | - public function calculate_rating_for( $post_id ) { |
|
| 522 | + public function calculate_rating_for($post_id) { |
|
| 523 | 523 | |
| 524 | 524 | // If it's not an entity, return. |
| 525 | - if ( ! $this->is_entity( $post_id ) ) { |
|
| 525 | + if ( ! $this->is_entity($post_id)) { |
|
| 526 | 526 | return; |
| 527 | 527 | } |
| 528 | 528 | // Retrieve the post object |
| 529 | - $post = get_post( $post_id ); |
|
| 529 | + $post = get_post($post_id); |
|
| 530 | 530 | // Rating value |
| 531 | 531 | $score = 0; |
| 532 | 532 | // Store warning messages |
| 533 | 533 | $warnings = array(); |
| 534 | 534 | |
| 535 | 535 | // Is the current entity related to at least 1 post? |
| 536 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 537 | - $score++ : |
|
| 538 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift' ) ); |
|
| 536 | + (0 < count(wl_core_get_related_post_ids($post->ID))) ? |
|
| 537 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift')); |
|
| 539 | 538 | |
| 540 | 539 | // Is the post content not empty? |
| 541 | - ( ! empty( $post->post_content ) ) ? |
|
| 542 | - $score++ : |
|
| 543 | - array_push( $warnings, __( self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift' ) ); |
|
| 540 | + ( ! empty($post->post_content)) ? |
|
| 541 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift')); |
|
| 544 | 542 | |
| 545 | 543 | // Is the current entity related to at least 1 entity? |
| 546 | 544 | // Was the current entity already disambiguated? |
| 547 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 548 | - $score++ : |
|
| 549 | - array_push( $warnings, __( self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift' ) ); |
|
| 545 | + (0 < count(wl_core_get_related_entity_ids($post->ID))) ? |
|
| 546 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift')); |
|
| 550 | 547 | |
| 551 | 548 | // Is the entity published? |
| 552 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 553 | - $score++ : |
|
| 554 | - array_push( $warnings, __( self::RATING_WARNING_IS_PUBLISHED, 'wordlift' ) ); |
|
| 549 | + ('publish' === get_post_status($post->ID)) ? |
|
| 550 | + $score++ : array_push($warnings, __(self::RATING_WARNING_IS_PUBLISHED, 'wordlift')); |
|
| 555 | 551 | |
| 556 | 552 | // Has a thumbnail? |
| 557 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
| 558 | - $score++ : |
|
| 559 | - array_push( $warnings, __( self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift' ) ); |
|
| 553 | + (has_post_thumbnail($post->ID)) ? |
|
| 554 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift')); |
|
| 560 | 555 | |
| 561 | 556 | // Get all post meta keys for the current post |
| 562 | 557 | global $wpdb; |
@@ -566,30 +561,27 @@ discard block |
||
| 566 | 561 | |
| 567 | 562 | // Check intersection between available meta keys |
| 568 | 563 | // and expected ones arrays to detect missing values |
| 569 | - $available_meta_keys = $wpdb->get_col( $query ); |
|
| 564 | + $available_meta_keys = $wpdb->get_col($query); |
|
| 570 | 565 | |
| 571 | 566 | // If each expected key is contained in available keys array ... |
| 572 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 573 | - $score++ : |
|
| 574 | - array_push( $warnings, __( self::RATING_WARNING_HAS_SAME_AS, 'wordlift' ) ); |
|
| 567 | + (in_array(Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys)) ? |
|
| 568 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_SAME_AS, 'wordlift')); |
|
| 575 | 569 | |
| 576 | - $schema = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 570 | + $schema = wl_entity_type_taxonomy_get_type($post_id); |
|
| 577 | 571 | |
| 578 | - $expected_meta_keys = ( null === $schema[ 'custom_fields' ] ) ? |
|
| 579 | - array() : |
|
| 580 | - array_keys( $schema[ 'custom_fields' ] ); |
|
| 572 | + $expected_meta_keys = (null === $schema['custom_fields']) ? |
|
| 573 | + array() : array_keys($schema['custom_fields']); |
|
| 581 | 574 | |
| 582 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 575 | + $intersection = array_intersect($expected_meta_keys, $available_meta_keys); |
|
| 583 | 576 | // If each expected key is contained in available keys array ... |
| 584 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 585 | - $score++ : |
|
| 586 | - array_push( $warnings, __( self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift' ) ); |
|
| 577 | + (count($intersection) === count($expected_meta_keys)) ? |
|
| 578 | + $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift')); |
|
| 587 | 579 | |
| 588 | 580 | // Finally return score and warnings |
| 589 | 581 | return array( |
| 590 | 582 | 'raw_score' => $score, |
| 591 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 592 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 583 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($score), |
|
| 584 | + 'percentage_score' => $this->convert_raw_score_to_percentage($score), |
|
| 593 | 585 | 'warnings' => $warnings, |
| 594 | 586 | ); |
| 595 | 587 | |
@@ -604,12 +596,12 @@ discard block |
||
| 604 | 596 | * |
| 605 | 597 | * @return string The input HTML code. |
| 606 | 598 | */ |
| 607 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
| 599 | + private function convert_raw_score_to_traffic_light($score) { |
|
| 608 | 600 | // RATING_MAX : $score = 3 : x |
| 609 | 601 | // See http://php.net/manual/en/function.round.php |
| 610 | - $rating = round( ( $score * 3 ) / self::get_rating_max(), 0, PHP_ROUND_HALF_UP ); |
|
| 602 | + $rating = round(($score * 3) / self::get_rating_max(), 0, PHP_ROUND_HALF_UP); |
|
| 611 | 603 | // If rating is 0, return 1, otherwise return rating |
| 612 | - return ( 0 == $rating ) ? 1 : $rating; |
|
| 604 | + return (0 == $rating) ? 1 : $rating; |
|
| 613 | 605 | |
| 614 | 606 | } |
| 615 | 607 | |
@@ -622,9 +614,9 @@ discard block |
||
| 622 | 614 | * |
| 623 | 615 | * @return string The input HTML code. |
| 624 | 616 | */ |
| 625 | - public function convert_raw_score_to_percentage( $score ) { |
|
| 617 | + public function convert_raw_score_to_percentage($score) { |
|
| 626 | 618 | // RATING_MAX : $score = 100 : x |
| 627 | - return round( ( $score * 100) / self::get_rating_max(), 0, PHP_ROUND_HALF_UP ); |
|
| 619 | + return round(($score * 100) / self::get_rating_max(), 0, PHP_ROUND_HALF_UP); |
|
| 628 | 620 | } |
| 629 | 621 | |
| 630 | 622 | /** |
@@ -636,8 +628,8 @@ discard block |
||
| 636 | 628 | * |
| 637 | 629 | * @return string The input HTML code. |
| 638 | 630 | */ |
| 639 | - private function get_alternative_label_input( $value = '' ) { |
|
| 631 | + private function get_alternative_label_input($value = '') { |
|
| 640 | 632 | |
| 641 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 633 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 642 | 634 | } |
| 643 | 635 | } |
@@ -3,103 +3,103 @@ discard block |
||
| 3 | 3 | |
| 4 | 4 | class WL_Metabox_Field_uri extends WL_Metabox_Field { |
| 5 | 5 | |
| 6 | - /** |
|
| 7 | - * Only accept URIs or local entity IDs. |
|
| 8 | - * Build new entity if the user inputted a name that is not present in DB. |
|
| 9 | - */ |
|
| 10 | - public function sanitize_data_filter( $value ) { |
|
| 11 | - |
|
| 12 | - if ( empty( $value ) ) { |
|
| 13 | - return null; |
|
| 14 | - } |
|
| 15 | - |
|
| 16 | - // Check that the inserted URI, ID or name does not point to a saved entity. |
|
| 17 | - if ( is_numeric( $value ) ) { |
|
| 18 | - $absent_from_db = is_null( get_post( $value ) ); // search by ID |
|
| 19 | - } else { |
|
| 20 | - $absent_from_db = |
|
| 21 | - is_null( Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $value ) ) && // search by uri |
|
| 22 | - is_null( get_page_by_title( $value, OBJECT, Wordlift_Entity_Service::TYPE_NAME ) ); // search by name |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - // Is it an URI? |
|
| 26 | - $name_is_uri = strpos( $value, 'http' ) === 0; |
|
| 27 | - |
|
| 28 | - // We create a new entity only if the entity is not present in the DB. |
|
| 29 | - // In the case of an external uri, we just save the uri. |
|
| 30 | - if ( $absent_from_db && ! $name_is_uri ) { |
|
| 31 | - |
|
| 32 | - // ...we create a new entity! |
|
| 33 | - $new_entity_id = wp_insert_post( array( |
|
| 34 | - 'post_status' => 'publish', |
|
| 35 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 36 | - 'post_title' => $value |
|
| 37 | - ) ); |
|
| 38 | - $new_entity = get_post( $new_entity_id ); |
|
| 39 | - |
|
| 40 | - $type = 'http://schema.org/' . ( isset( $this->expected_uri_type ) ? $this->expected_uri_type[0] : 'Thing' ); |
|
| 41 | - |
|
| 42 | - wl_set_entity_main_type( $new_entity_id, $type ); |
|
| 43 | - |
|
| 44 | - // Build uri for this entity |
|
| 45 | - $new_uri = wl_build_entity_uri( $new_entity_id ); |
|
| 46 | - wl_set_entity_uri( $new_entity_id, $new_uri ); |
|
| 47 | - |
|
| 48 | - wl_push_entity_post_to_redlink( $new_entity ); |
|
| 49 | - |
|
| 50 | - // Update the value that will be saved as meta |
|
| 51 | - $value = $new_entity_id; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - return $value; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public function html_wrapper_open() { |
|
| 58 | - |
|
| 59 | - // The containing <div> contains info on cardinality and expected types |
|
| 60 | - $html = "<div class='wl-field' data-cardinality='$this->cardinality'"; |
|
| 61 | - |
|
| 62 | - if ( isset( $this->expected_uri_type ) && ! is_null( $this->expected_uri_type ) ) { |
|
| 63 | - |
|
| 64 | - if ( is_array( $this->expected_uri_type ) ) { |
|
| 65 | - $html .= " data-expected-types='" . implode( ',', $this->expected_uri_type ) . "'"; |
|
| 66 | - } else { |
|
| 67 | - $html .= " data-expected-types='$this->expected_uri_type'"; |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $html .= '>'; |
|
| 72 | - |
|
| 73 | - return $html; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - public function html_input( $default_entity_identifier ) { |
|
| 77 | - |
|
| 78 | - // The entity can be referenced as URI or ID. |
|
| 79 | - if ( is_numeric( $default_entity_identifier ) ) { |
|
| 80 | - $entity = get_post( $default_entity_identifier ); |
|
| 81 | - } else { |
|
| 82 | - // It is an URI |
|
| 83 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $default_entity_identifier ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if ( ! is_null( $entity ) ) { |
|
| 87 | - $label = $entity->post_title; |
|
| 88 | - $value = $entity->ID; |
|
| 89 | - } else { |
|
| 90 | - // No ID and no internal uri. Just leave as is. |
|
| 91 | - $label = $default_entity_identifier; |
|
| 92 | - $value = $default_entity_identifier; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /* |
|
| 6 | + /** |
|
| 7 | + * Only accept URIs or local entity IDs. |
|
| 8 | + * Build new entity if the user inputted a name that is not present in DB. |
|
| 9 | + */ |
|
| 10 | + public function sanitize_data_filter( $value ) { |
|
| 11 | + |
|
| 12 | + if ( empty( $value ) ) { |
|
| 13 | + return null; |
|
| 14 | + } |
|
| 15 | + |
|
| 16 | + // Check that the inserted URI, ID or name does not point to a saved entity. |
|
| 17 | + if ( is_numeric( $value ) ) { |
|
| 18 | + $absent_from_db = is_null( get_post( $value ) ); // search by ID |
|
| 19 | + } else { |
|
| 20 | + $absent_from_db = |
|
| 21 | + is_null( Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $value ) ) && // search by uri |
|
| 22 | + is_null( get_page_by_title( $value, OBJECT, Wordlift_Entity_Service::TYPE_NAME ) ); // search by name |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + // Is it an URI? |
|
| 26 | + $name_is_uri = strpos( $value, 'http' ) === 0; |
|
| 27 | + |
|
| 28 | + // We create a new entity only if the entity is not present in the DB. |
|
| 29 | + // In the case of an external uri, we just save the uri. |
|
| 30 | + if ( $absent_from_db && ! $name_is_uri ) { |
|
| 31 | + |
|
| 32 | + // ...we create a new entity! |
|
| 33 | + $new_entity_id = wp_insert_post( array( |
|
| 34 | + 'post_status' => 'publish', |
|
| 35 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 36 | + 'post_title' => $value |
|
| 37 | + ) ); |
|
| 38 | + $new_entity = get_post( $new_entity_id ); |
|
| 39 | + |
|
| 40 | + $type = 'http://schema.org/' . ( isset( $this->expected_uri_type ) ? $this->expected_uri_type[0] : 'Thing' ); |
|
| 41 | + |
|
| 42 | + wl_set_entity_main_type( $new_entity_id, $type ); |
|
| 43 | + |
|
| 44 | + // Build uri for this entity |
|
| 45 | + $new_uri = wl_build_entity_uri( $new_entity_id ); |
|
| 46 | + wl_set_entity_uri( $new_entity_id, $new_uri ); |
|
| 47 | + |
|
| 48 | + wl_push_entity_post_to_redlink( $new_entity ); |
|
| 49 | + |
|
| 50 | + // Update the value that will be saved as meta |
|
| 51 | + $value = $new_entity_id; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + return $value; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public function html_wrapper_open() { |
|
| 58 | + |
|
| 59 | + // The containing <div> contains info on cardinality and expected types |
|
| 60 | + $html = "<div class='wl-field' data-cardinality='$this->cardinality'"; |
|
| 61 | + |
|
| 62 | + if ( isset( $this->expected_uri_type ) && ! is_null( $this->expected_uri_type ) ) { |
|
| 63 | + |
|
| 64 | + if ( is_array( $this->expected_uri_type ) ) { |
|
| 65 | + $html .= " data-expected-types='" . implode( ',', $this->expected_uri_type ) . "'"; |
|
| 66 | + } else { |
|
| 67 | + $html .= " data-expected-types='$this->expected_uri_type'"; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $html .= '>'; |
|
| 72 | + |
|
| 73 | + return $html; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + public function html_input( $default_entity_identifier ) { |
|
| 77 | + |
|
| 78 | + // The entity can be referenced as URI or ID. |
|
| 79 | + if ( is_numeric( $default_entity_identifier ) ) { |
|
| 80 | + $entity = get_post( $default_entity_identifier ); |
|
| 81 | + } else { |
|
| 82 | + // It is an URI |
|
| 83 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $default_entity_identifier ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if ( ! is_null( $entity ) ) { |
|
| 87 | + $label = $entity->post_title; |
|
| 88 | + $value = $entity->ID; |
|
| 89 | + } else { |
|
| 90 | + // No ID and no internal uri. Just leave as is. |
|
| 91 | + $label = $default_entity_identifier; |
|
| 92 | + $value = $default_entity_identifier; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /* |
|
| 96 | 96 | * Write saved value in page |
| 97 | 97 | * The <input> tags host the meta value. |
| 98 | 98 | * The visible <input> has the human readable value (i.e. entity name or uri) |
| 99 | 99 | * and is accompained by an hidden <input> tag, passed to the server, |
| 100 | 100 | * that contains the raw value (i.e. the uri or entity id). |
| 101 | 101 | */ |
| 102 | - $html = <<<EOF |
|
| 102 | + $html = <<<EOF |
|
| 103 | 103 | <div class="wl-input-wrapper wl-autocomplete-wrapper"> |
| 104 | 104 | <input type="text" class="$this->meta_name wl-autocomplete" value="$label" style="width:88%" /> |
| 105 | 105 | <input type="hidden" class="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" /> |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | </div> |
| 109 | 109 | EOF; |
| 110 | 110 | |
| 111 | - return $html; |
|
| 112 | - } |
|
| 111 | + return $html; |
|
| 112 | + } |
|
| 113 | 113 | } |
| 114 | 114 | |
@@ -7,45 +7,45 @@ discard block |
||
| 7 | 7 | * Only accept URIs or local entity IDs. |
| 8 | 8 | * Build new entity if the user inputted a name that is not present in DB. |
| 9 | 9 | */ |
| 10 | - public function sanitize_data_filter( $value ) { |
|
| 10 | + public function sanitize_data_filter($value) { |
|
| 11 | 11 | |
| 12 | - if ( empty( $value ) ) { |
|
| 12 | + if (empty($value)) { |
|
| 13 | 13 | return null; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | // Check that the inserted URI, ID or name does not point to a saved entity. |
| 17 | - if ( is_numeric( $value ) ) { |
|
| 18 | - $absent_from_db = is_null( get_post( $value ) ); // search by ID |
|
| 17 | + if (is_numeric($value)) { |
|
| 18 | + $absent_from_db = is_null(get_post($value)); // search by ID |
|
| 19 | 19 | } else { |
| 20 | 20 | $absent_from_db = |
| 21 | - is_null( Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $value ) ) && // search by uri |
|
| 22 | - is_null( get_page_by_title( $value, OBJECT, Wordlift_Entity_Service::TYPE_NAME ) ); // search by name |
|
| 21 | + is_null(Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($value)) && // search by uri |
|
| 22 | + is_null(get_page_by_title($value, OBJECT, Wordlift_Entity_Service::TYPE_NAME)); // search by name |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | // Is it an URI? |
| 26 | - $name_is_uri = strpos( $value, 'http' ) === 0; |
|
| 26 | + $name_is_uri = strpos($value, 'http') === 0; |
|
| 27 | 27 | |
| 28 | 28 | // We create a new entity only if the entity is not present in the DB. |
| 29 | 29 | // In the case of an external uri, we just save the uri. |
| 30 | - if ( $absent_from_db && ! $name_is_uri ) { |
|
| 30 | + if ($absent_from_db && ! $name_is_uri) { |
|
| 31 | 31 | |
| 32 | 32 | // ...we create a new entity! |
| 33 | - $new_entity_id = wp_insert_post( array( |
|
| 33 | + $new_entity_id = wp_insert_post(array( |
|
| 34 | 34 | 'post_status' => 'publish', |
| 35 | 35 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 36 | 36 | 'post_title' => $value |
| 37 | - ) ); |
|
| 38 | - $new_entity = get_post( $new_entity_id ); |
|
| 37 | + )); |
|
| 38 | + $new_entity = get_post($new_entity_id); |
|
| 39 | 39 | |
| 40 | - $type = 'http://schema.org/' . ( isset( $this->expected_uri_type ) ? $this->expected_uri_type[0] : 'Thing' ); |
|
| 40 | + $type = 'http://schema.org/'.(isset($this->expected_uri_type) ? $this->expected_uri_type[0] : 'Thing'); |
|
| 41 | 41 | |
| 42 | - wl_set_entity_main_type( $new_entity_id, $type ); |
|
| 42 | + wl_set_entity_main_type($new_entity_id, $type); |
|
| 43 | 43 | |
| 44 | 44 | // Build uri for this entity |
| 45 | - $new_uri = wl_build_entity_uri( $new_entity_id ); |
|
| 46 | - wl_set_entity_uri( $new_entity_id, $new_uri ); |
|
| 45 | + $new_uri = wl_build_entity_uri($new_entity_id); |
|
| 46 | + wl_set_entity_uri($new_entity_id, $new_uri); |
|
| 47 | 47 | |
| 48 | - wl_push_entity_post_to_redlink( $new_entity ); |
|
| 48 | + wl_push_entity_post_to_redlink($new_entity); |
|
| 49 | 49 | |
| 50 | 50 | // Update the value that will be saved as meta |
| 51 | 51 | $value = $new_entity_id; |
@@ -59,10 +59,10 @@ discard block |
||
| 59 | 59 | // The containing <div> contains info on cardinality and expected types |
| 60 | 60 | $html = "<div class='wl-field' data-cardinality='$this->cardinality'"; |
| 61 | 61 | |
| 62 | - if ( isset( $this->expected_uri_type ) && ! is_null( $this->expected_uri_type ) ) { |
|
| 62 | + if (isset($this->expected_uri_type) && ! is_null($this->expected_uri_type)) { |
|
| 63 | 63 | |
| 64 | - if ( is_array( $this->expected_uri_type ) ) { |
|
| 65 | - $html .= " data-expected-types='" . implode( ',', $this->expected_uri_type ) . "'"; |
|
| 64 | + if (is_array($this->expected_uri_type)) { |
|
| 65 | + $html .= " data-expected-types='".implode(',', $this->expected_uri_type)."'"; |
|
| 66 | 66 | } else { |
| 67 | 67 | $html .= " data-expected-types='$this->expected_uri_type'"; |
| 68 | 68 | } |
@@ -73,17 +73,17 @@ discard block |
||
| 73 | 73 | return $html; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - public function html_input( $default_entity_identifier ) { |
|
| 76 | + public function html_input($default_entity_identifier) { |
|
| 77 | 77 | |
| 78 | 78 | // The entity can be referenced as URI or ID. |
| 79 | - if ( is_numeric( $default_entity_identifier ) ) { |
|
| 80 | - $entity = get_post( $default_entity_identifier ); |
|
| 79 | + if (is_numeric($default_entity_identifier)) { |
|
| 80 | + $entity = get_post($default_entity_identifier); |
|
| 81 | 81 | } else { |
| 82 | 82 | // It is an URI |
| 83 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $default_entity_identifier ); |
|
| 83 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($default_entity_identifier); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - if ( ! is_null( $entity ) ) { |
|
| 86 | + if ( ! is_null($entity)) { |
|
| 87 | 87 | $label = $entity->post_title; |
| 88 | 88 | $value = $entity->ID; |
| 89 | 89 | } else { |
@@ -14,19 +14,19 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | function wl_linked_data_save_post( $post_id ) { |
| 16 | 16 | |
| 17 | - // If it's not numeric exit from here. |
|
| 18 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 19 | - return; |
|
| 20 | - } |
|
| 17 | + // If it's not numeric exit from here. |
|
| 18 | + if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 19 | + return; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - // unhook this function so it doesn't loop infinitely |
|
| 23 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 22 | + // unhook this function so it doesn't loop infinitely |
|
| 23 | + remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 24 | 24 | |
| 25 | - // raise the *wl_linked_data_save_post* event. |
|
| 26 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 25 | + // raise the *wl_linked_data_save_post* event. |
|
| 26 | + do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 27 | 27 | |
| 28 | - // re-hook this function |
|
| 29 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 28 | + // re-hook this function |
|
| 29 | + add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | add_action( 'save_post', 'wl_linked_data_save_post' ); |
@@ -40,123 +40,123 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | function wl_linked_data_save_post_and_related_entities( $post_id ) { |
| 42 | 42 | |
| 43 | - // Ignore auto-saves |
|
| 44 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 45 | - return; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - // get the current post. |
|
| 49 | - $post = get_post( $post_id ); |
|
| 50 | - |
|
| 51 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 52 | - |
|
| 53 | - // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 54 | - |
|
| 55 | - // Store mapping between tmp new entities uris and real new entities uri |
|
| 56 | - $entities_uri_mapping = array(); |
|
| 57 | - // Store classification box mapping |
|
| 58 | - $entities_predicates_mapping = null; |
|
| 59 | - |
|
| 60 | - // Save the entities coming with POST data. |
|
| 61 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 62 | - |
|
| 63 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 64 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 65 | - wl_write_log( "]" ); |
|
| 66 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 67 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 68 | - wl_write_log( "]" ); |
|
| 69 | - |
|
| 70 | - $entities_via_post = $_POST['wl_entities']; |
|
| 71 | - $boxes_via_post = $_POST['wl_boxes']; |
|
| 72 | - |
|
| 73 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 74 | - // Local entities have a tmp uri with 'local-entity-' |
|
| 75 | - // These uris need to be rewritten here and replaced in the content |
|
| 76 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 77 | - // Build the proper uri |
|
| 78 | - $uri = sprintf( '%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path( $entity['label'] ) ); |
|
| 79 | - // Populate the mapping |
|
| 80 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 81 | - // Override the entity obj |
|
| 82 | - $entities_via_post[ $entity_uri ]['uri'] = $uri; |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // Populate the $entities_predicates_mapping |
|
| 87 | - // Local Redlink uris need to be used here |
|
| 88 | - foreach ( $boxes_via_post as $predicate => $entity_uris ) { |
|
| 89 | - foreach ( $entity_uris as $entity_uri ) { |
|
| 90 | - // Retrieve the entity label needed to build the uri |
|
| 91 | - // If the current uri is an internal uri, then entity uri is preserved: |
|
| 92 | - // the current label could be an alternative label that has not to impact on entity uri |
|
| 93 | - if ( 0 !== strrpos( $entity_uri, wl_configuration_get_redlink_dataset_uri() ) ) { |
|
| 94 | - $label = $entities_via_post[ stripslashes( $entity_uri ) ]['label']; |
|
| 95 | - $uri = sprintf( '%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path( $label ) ); |
|
| 96 | - } else { |
|
| 97 | - $uri = stripslashes( $entity_uri ); |
|
| 98 | - } |
|
| 43 | + // Ignore auto-saves |
|
| 44 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 45 | + return; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + // get the current post. |
|
| 49 | + $post = get_post( $post_id ); |
|
| 50 | + |
|
| 51 | + remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 52 | + |
|
| 53 | + // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 54 | + |
|
| 55 | + // Store mapping between tmp new entities uris and real new entities uri |
|
| 56 | + $entities_uri_mapping = array(); |
|
| 57 | + // Store classification box mapping |
|
| 58 | + $entities_predicates_mapping = null; |
|
| 59 | + |
|
| 60 | + // Save the entities coming with POST data. |
|
| 61 | + if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 62 | + |
|
| 63 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 64 | + wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 65 | + wl_write_log( "]" ); |
|
| 66 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 67 | + wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 68 | + wl_write_log( "]" ); |
|
| 69 | + |
|
| 70 | + $entities_via_post = $_POST['wl_entities']; |
|
| 71 | + $boxes_via_post = $_POST['wl_boxes']; |
|
| 72 | + |
|
| 73 | + foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 74 | + // Local entities have a tmp uri with 'local-entity-' |
|
| 75 | + // These uris need to be rewritten here and replaced in the content |
|
| 76 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 77 | + // Build the proper uri |
|
| 78 | + $uri = sprintf( '%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path( $entity['label'] ) ); |
|
| 79 | + // Populate the mapping |
|
| 80 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 81 | + // Override the entity obj |
|
| 82 | + $entities_via_post[ $entity_uri ]['uri'] = $uri; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // Populate the $entities_predicates_mapping |
|
| 87 | + // Local Redlink uris need to be used here |
|
| 88 | + foreach ( $boxes_via_post as $predicate => $entity_uris ) { |
|
| 89 | + foreach ( $entity_uris as $entity_uri ) { |
|
| 90 | + // Retrieve the entity label needed to build the uri |
|
| 91 | + // If the current uri is an internal uri, then entity uri is preserved: |
|
| 92 | + // the current label could be an alternative label that has not to impact on entity uri |
|
| 93 | + if ( 0 !== strrpos( $entity_uri, wl_configuration_get_redlink_dataset_uri() ) ) { |
|
| 94 | + $label = $entities_via_post[ stripslashes( $entity_uri ) ]['label']; |
|
| 95 | + $uri = sprintf( '%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path( $label ) ); |
|
| 96 | + } else { |
|
| 97 | + $uri = stripslashes( $entity_uri ); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - $entities_predicates_mapping[ $uri ][] = $predicate; |
|
| 100 | + $entities_predicates_mapping[ $uri ][] = $predicate; |
|
| 101 | 101 | |
| 102 | - } |
|
| 103 | - } |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - // Save entities (properties included) and push them to Redlink |
|
| 106 | - wl_save_entities( array_values( $entities_via_post ), $post_id ); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // Replace tmp uris in content post if needed |
|
| 110 | - $updated_post_content = $post->post_content; |
|
| 111 | - // Save each entity and store the post id. |
|
| 112 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 113 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 114 | - } |
|
| 115 | - // Update the post content |
|
| 116 | - wp_update_post( array( |
|
| 117 | - 'ID' => $post->ID, |
|
| 118 | - 'post_content' => $updated_post_content, |
|
| 119 | - ) ); |
|
| 120 | - |
|
| 121 | - // Extract related/referenced entities from text. |
|
| 122 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 123 | - |
|
| 124 | - // Reset previously saved instances |
|
| 125 | - wl_core_delete_relation_instances( $post_id ); |
|
| 126 | - |
|
| 127 | - // Save relation instances |
|
| 128 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 129 | - |
|
| 130 | - if ( $entities_predicates_mapping ) { |
|
| 131 | - |
|
| 132 | - // wl_write_log(" Going to manage relation instances according to the following mapping"); |
|
| 133 | - |
|
| 134 | - // Retrieve the entity uri |
|
| 135 | - $referenced_entity_uri = wl_get_entity_uri( $referenced_entity_id ); |
|
| 136 | - // Retrieve predicates for the current uri |
|
| 137 | - if ( isset( $entities_predicates_mapping[ $referenced_entity_uri ] ) ) { |
|
| 138 | - foreach ( $entities_predicates_mapping[ $referenced_entity_uri ] as $predicate ) { |
|
| 139 | - wl_write_log(" Going to add relation with predicate $predicate"); |
|
| 140 | - wl_core_add_relation_instance( $post_id, $predicate, $referenced_entity_id ); |
|
| 141 | - } |
|
| 142 | - } else { |
|
| 143 | - wl_write_log( "Entity uri $referenced_entity_uri missing in the mapping" ); |
|
| 144 | - wl_write_log( $entities_predicates_mapping ); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - } else { |
|
| 148 | - // Just for unit tests |
|
| 149 | - wl_core_add_relation_instance( $post_id, 'what', $referenced_entity_id ); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // TODO Check if is needed |
|
| 153 | - wl_linked_data_push_to_redlink( $referenced_entity_id ); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - // Push the post to Redlink. |
|
| 157 | - wl_linked_data_push_to_redlink( $post->ID ); |
|
| 158 | - |
|
| 159 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 105 | + // Save entities (properties included) and push them to Redlink |
|
| 106 | + wl_save_entities( array_values( $entities_via_post ), $post_id ); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // Replace tmp uris in content post if needed |
|
| 110 | + $updated_post_content = $post->post_content; |
|
| 111 | + // Save each entity and store the post id. |
|
| 112 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 113 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 114 | + } |
|
| 115 | + // Update the post content |
|
| 116 | + wp_update_post( array( |
|
| 117 | + 'ID' => $post->ID, |
|
| 118 | + 'post_content' => $updated_post_content, |
|
| 119 | + ) ); |
|
| 120 | + |
|
| 121 | + // Extract related/referenced entities from text. |
|
| 122 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 123 | + |
|
| 124 | + // Reset previously saved instances |
|
| 125 | + wl_core_delete_relation_instances( $post_id ); |
|
| 126 | + |
|
| 127 | + // Save relation instances |
|
| 128 | + foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 129 | + |
|
| 130 | + if ( $entities_predicates_mapping ) { |
|
| 131 | + |
|
| 132 | + // wl_write_log(" Going to manage relation instances according to the following mapping"); |
|
| 133 | + |
|
| 134 | + // Retrieve the entity uri |
|
| 135 | + $referenced_entity_uri = wl_get_entity_uri( $referenced_entity_id ); |
|
| 136 | + // Retrieve predicates for the current uri |
|
| 137 | + if ( isset( $entities_predicates_mapping[ $referenced_entity_uri ] ) ) { |
|
| 138 | + foreach ( $entities_predicates_mapping[ $referenced_entity_uri ] as $predicate ) { |
|
| 139 | + wl_write_log(" Going to add relation with predicate $predicate"); |
|
| 140 | + wl_core_add_relation_instance( $post_id, $predicate, $referenced_entity_id ); |
|
| 141 | + } |
|
| 142 | + } else { |
|
| 143 | + wl_write_log( "Entity uri $referenced_entity_uri missing in the mapping" ); |
|
| 144 | + wl_write_log( $entities_predicates_mapping ); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + } else { |
|
| 148 | + // Just for unit tests |
|
| 149 | + wl_core_add_relation_instance( $post_id, 'what', $referenced_entity_id ); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // TODO Check if is needed |
|
| 153 | + wl_linked_data_push_to_redlink( $referenced_entity_id ); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + // Push the post to Redlink. |
|
| 157 | + wl_linked_data_push_to_redlink( $post->ID ); |
|
| 158 | + |
|
| 159 | + add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
@@ -166,14 +166,14 @@ discard block |
||
| 166 | 166 | */ |
| 167 | 167 | function wordlift_save_post_add_default_schema_type( $entity_id ) { |
| 168 | 168 | |
| 169 | - $entity = get_post( $entity_id ); |
|
| 170 | - $entity_type = wl_schema_get_types( $entity_id ); |
|
| 169 | + $entity = get_post( $entity_id ); |
|
| 170 | + $entity_type = wl_schema_get_types( $entity_id ); |
|
| 171 | 171 | |
| 172 | - // Assign type 'Thing' if we are dealing with an entity without type |
|
| 173 | - if ( $entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null( $entity_type ) ) { |
|
| 172 | + // Assign type 'Thing' if we are dealing with an entity without type |
|
| 173 | + if ( $entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null( $entity_type ) ) { |
|
| 174 | 174 | |
| 175 | - wl_schema_set_types( $entity_id, 'Thing' ); |
|
| 176 | - } |
|
| 175 | + wl_schema_set_types( $entity_id, 'Thing' ); |
|
| 176 | + } |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // Priority 1 (default is 10) because we want the default type to be set as soon as possible |
@@ -190,27 +190,27 @@ discard block |
||
| 190 | 190 | */ |
| 191 | 191 | function wl_save_entities( $entities, $related_post_id = null ) { |
| 192 | 192 | |
| 193 | - // wl_write_log( "[ entities count :: " . count( $entities ) . " ][ related post id :: $related_post_id ]" ); |
|
| 193 | + // wl_write_log( "[ entities count :: " . count( $entities ) . " ][ related post id :: $related_post_id ]" ); |
|
| 194 | 194 | |
| 195 | - // Prepare the return array. |
|
| 196 | - $posts = array(); |
|
| 195 | + // Prepare the return array. |
|
| 196 | + $posts = array(); |
|
| 197 | 197 | |
| 198 | - // Save each entity and store the post id. |
|
| 199 | - foreach ( $entities as $entity ) { |
|
| 198 | + // Save each entity and store the post id. |
|
| 199 | + foreach ( $entities as $entity ) { |
|
| 200 | 200 | |
| 201 | - // Update entity data with related post |
|
| 202 | - $entity['related_post_id'] = $related_post_id; |
|
| 201 | + // Update entity data with related post |
|
| 202 | + $entity['related_post_id'] = $related_post_id; |
|
| 203 | 203 | |
| 204 | - // Save the entity. |
|
| 205 | - $entity_post = wl_save_entity( $entity ); |
|
| 204 | + // Save the entity. |
|
| 205 | + $entity_post = wl_save_entity( $entity ); |
|
| 206 | 206 | |
| 207 | - // Store the post in the return array if successful. |
|
| 208 | - if ( null !== $entity_post ) { |
|
| 209 | - array_push( $posts, $entity_post ); |
|
| 210 | - } |
|
| 211 | - } |
|
| 207 | + // Store the post in the return array if successful. |
|
| 208 | + if ( null !== $entity_post ) { |
|
| 209 | + array_push( $posts, $entity_post ); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - return $posts; |
|
| 213 | + return $posts; |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | /** |
@@ -231,176 +231,176 @@ discard block |
||
| 231 | 231 | */ |
| 232 | 232 | function wl_save_entity( $entity_data ) { |
| 233 | 233 | |
| 234 | - $uri = $entity_data['uri']; |
|
| 235 | - $label = $entity_data['label']; |
|
| 236 | - $type_uri = $entity_data['main_type']; |
|
| 237 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 238 | - $description = $entity_data['description']; |
|
| 239 | - $images = isset( $entity_data['image'] ) ? wl_force_to_array( $entity_data['image'] ) : array(); |
|
| 240 | - $same_as = isset( $entity_data['sameas'] ) ? wl_force_to_array( $entity_data['sameas'] ) : array(); |
|
| 241 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 242 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 243 | - |
|
| 244 | - // wl_write_log( "[ uri :: $uri ][ label :: $label ][ type uri :: $type_uri ]" ); |
|
| 245 | - |
|
| 246 | - // Prepare properties of the new entity. |
|
| 247 | - $params = array( |
|
| 248 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 249 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 250 | - 'post_title' => $label, |
|
| 251 | - 'post_content' => $description, |
|
| 252 | - 'post_excerpt' => '' |
|
| 253 | - ); |
|
| 254 | - |
|
| 255 | - // Check whether an entity already exists with the provided URI. |
|
| 256 | - $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 257 | - |
|
| 258 | - if ( null !== $post ) { |
|
| 259 | - // We insert into the params the entity ID, so it will be updated and not inserted. |
|
| 260 | - $params[ 'ID' ] = $post->ID; |
|
| 261 | - // Preserve the current entity status |
|
| 262 | - if ( 'public' === $post->post_status ) { |
|
| 263 | - $params[ 'post_status' ] = $post->post_status; |
|
| 264 | - } |
|
| 265 | - // Preserve the current entity post_content. |
|
| 266 | - $params['post_content'] = $post->post_content; |
|
| 267 | - // Preserve the entity post_title to avoid de-synch between WP and RL |
|
| 268 | - // See: https://github.com/insideout10/wordlift-plugin/issues/221 |
|
| 269 | - $params['post_title'] = $post->post_title; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 273 | - // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 274 | - // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 275 | - // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 276 | - // is created when saving a post. |
|
| 277 | - global $wpseo_metabox, $seo_ultimate; |
|
| 278 | - if ( isset( $wpseo_metabox ) ) { |
|
| 279 | - remove_action( 'wp_insert_post', array( $wpseo_metabox, 'save_postdata' ) ); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - if ( isset( $seo_ultimate ) ) { |
|
| 283 | - remove_action( 'save_post', array( $seo_ultimate, 'save_postmeta_box' ) ); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 287 | - // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 288 | - // to the save_post and restore them after we saved the entity. |
|
| 289 | - // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 290 | - // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 291 | - // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 292 | - global $wp_filter; |
|
| 293 | - $save_post_filters = $wp_filter['save_post']; |
|
| 294 | - $wp_filter['save_post'] = array(); |
|
| 295 | - |
|
| 296 | - // create or update the post. |
|
| 297 | - $post_id = wp_insert_post( $params, true ); |
|
| 298 | - |
|
| 299 | - // Restore all the existing filters. |
|
| 300 | - $wp_filter['save_post'] = $save_post_filters; |
|
| 301 | - |
|
| 302 | - // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 303 | - if ( isset( $wpseo_metabox ) ) { |
|
| 304 | - add_action( 'wp_insert_post', array( $wpseo_metabox, 'save_postdata' ) ); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 308 | - if ( isset( $seo_ultimate ) ) { |
|
| 309 | - add_action( 'save_post', array( $seo_ultimate, 'save_postmeta_box' ), 10, 2 ); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - // TODO: handle errors. |
|
| 313 | - if ( is_wp_error( $post_id ) ) { |
|
| 314 | - wl_write_log( ': error occurred' ); |
|
| 315 | - |
|
| 316 | - // inform an error occurred. |
|
| 317 | - return null; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 321 | - |
|
| 322 | - // Save the entity types. |
|
| 323 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 324 | - |
|
| 325 | - // Get a dataset URI for the entity. |
|
| 326 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 327 | - |
|
| 328 | - // Save the entity URI. |
|
| 329 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 330 | - |
|
| 331 | - // Add the uri to the sameAs data if it's not a local URI. |
|
| 332 | - if ( $wl_uri !== $uri ) { |
|
| 333 | - array_push( $same_as, $uri ); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 337 | - |
|
| 338 | - // Save the sameAs data for the entity. |
|
| 339 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 234 | + $uri = $entity_data['uri']; |
|
| 235 | + $label = $entity_data['label']; |
|
| 236 | + $type_uri = $entity_data['main_type']; |
|
| 237 | + $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 238 | + $description = $entity_data['description']; |
|
| 239 | + $images = isset( $entity_data['image'] ) ? wl_force_to_array( $entity_data['image'] ) : array(); |
|
| 240 | + $same_as = isset( $entity_data['sameas'] ) ? wl_force_to_array( $entity_data['sameas'] ) : array(); |
|
| 241 | + $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 242 | + $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 243 | + |
|
| 244 | + // wl_write_log( "[ uri :: $uri ][ label :: $label ][ type uri :: $type_uri ]" ); |
|
| 245 | + |
|
| 246 | + // Prepare properties of the new entity. |
|
| 247 | + $params = array( |
|
| 248 | + 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 249 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 250 | + 'post_title' => $label, |
|
| 251 | + 'post_content' => $description, |
|
| 252 | + 'post_excerpt' => '' |
|
| 253 | + ); |
|
| 254 | + |
|
| 255 | + // Check whether an entity already exists with the provided URI. |
|
| 256 | + $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 257 | + |
|
| 258 | + if ( null !== $post ) { |
|
| 259 | + // We insert into the params the entity ID, so it will be updated and not inserted. |
|
| 260 | + $params[ 'ID' ] = $post->ID; |
|
| 261 | + // Preserve the current entity status |
|
| 262 | + if ( 'public' === $post->post_status ) { |
|
| 263 | + $params[ 'post_status' ] = $post->post_status; |
|
| 264 | + } |
|
| 265 | + // Preserve the current entity post_content. |
|
| 266 | + $params['post_content'] = $post->post_content; |
|
| 267 | + // Preserve the entity post_title to avoid de-synch between WP and RL |
|
| 268 | + // See: https://github.com/insideout10/wordlift-plugin/issues/221 |
|
| 269 | + $params['post_title'] = $post->post_title; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 273 | + // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 274 | + // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 275 | + // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 276 | + // is created when saving a post. |
|
| 277 | + global $wpseo_metabox, $seo_ultimate; |
|
| 278 | + if ( isset( $wpseo_metabox ) ) { |
|
| 279 | + remove_action( 'wp_insert_post', array( $wpseo_metabox, 'save_postdata' ) ); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + if ( isset( $seo_ultimate ) ) { |
|
| 283 | + remove_action( 'save_post', array( $seo_ultimate, 'save_postmeta_box' ) ); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 287 | + // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 288 | + // to the save_post and restore them after we saved the entity. |
|
| 289 | + // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 290 | + // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 291 | + // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 292 | + global $wp_filter; |
|
| 293 | + $save_post_filters = $wp_filter['save_post']; |
|
| 294 | + $wp_filter['save_post'] = array(); |
|
| 295 | + |
|
| 296 | + // create or update the post. |
|
| 297 | + $post_id = wp_insert_post( $params, true ); |
|
| 298 | + |
|
| 299 | + // Restore all the existing filters. |
|
| 300 | + $wp_filter['save_post'] = $save_post_filters; |
|
| 301 | + |
|
| 302 | + // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 303 | + if ( isset( $wpseo_metabox ) ) { |
|
| 304 | + add_action( 'wp_insert_post', array( $wpseo_metabox, 'save_postdata' ) ); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 308 | + if ( isset( $seo_ultimate ) ) { |
|
| 309 | + add_action( 'save_post', array( $seo_ultimate, 'save_postmeta_box' ), 10, 2 ); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + // TODO: handle errors. |
|
| 313 | + if ( is_wp_error( $post_id ) ) { |
|
| 314 | + wl_write_log( ': error occurred' ); |
|
| 315 | + |
|
| 316 | + // inform an error occurred. |
|
| 317 | + return null; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 321 | + |
|
| 322 | + // Save the entity types. |
|
| 323 | + wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 324 | + |
|
| 325 | + // Get a dataset URI for the entity. |
|
| 326 | + $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 327 | + |
|
| 328 | + // Save the entity URI. |
|
| 329 | + wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 330 | + |
|
| 331 | + // Add the uri to the sameAs data if it's not a local URI. |
|
| 332 | + if ( $wl_uri !== $uri ) { |
|
| 333 | + array_push( $same_as, $uri ); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + $new_uri = wl_get_entity_uri( $post_id ); |
|
| 337 | + |
|
| 338 | + // Save the sameAs data for the entity. |
|
| 339 | + wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 340 | 340 | |
| 341 | - // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 342 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 343 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 344 | - } |
|
| 341 | + // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 342 | + foreach ( $other_properties as $property_name => $property_value ) { |
|
| 343 | + wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 344 | + } |
|
| 345 | 345 | |
| 346 | - // Call hooks. |
|
| 347 | - do_action( 'wl_save_entity', $post_id ); |
|
| 346 | + // Call hooks. |
|
| 347 | + do_action( 'wl_save_entity', $post_id ); |
|
| 348 | 348 | |
| 349 | - wl_write_log( "[ post id :: $post_id ][ uri :: $uri ][ label :: $label ][ wl uri :: $wl_uri ][ types :: " . implode( ',', $entity_types ) . " ][ images count :: " . count( $images ) . " ][ same_as count :: " . count( $same_as ) . " ]" ); |
|
| 349 | + wl_write_log( "[ post id :: $post_id ][ uri :: $uri ][ label :: $label ][ wl uri :: $wl_uri ][ types :: " . implode( ',', $entity_types ) . " ][ images count :: " . count( $images ) . " ][ same_as count :: " . count( $same_as ) . " ]" ); |
|
| 350 | 350 | |
| 351 | - foreach ( $images as $image_remote_url ) { |
|
| 351 | + foreach ( $images as $image_remote_url ) { |
|
| 352 | 352 | |
| 353 | - // Check if image is already present in local DB |
|
| 354 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 355 | - // Do nothing. |
|
| 356 | - continue; |
|
| 357 | - } |
|
| 353 | + // Check if image is already present in local DB |
|
| 354 | + if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 355 | + // Do nothing. |
|
| 356 | + continue; |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | - // Check if there is an existing attachment for this post ID and source URL. |
|
| 360 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 359 | + // Check if there is an existing attachment for this post ID and source URL. |
|
| 360 | + $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 361 | 361 | |
| 362 | - // Skip if an existing image is found. |
|
| 363 | - if ( null !== $existing_image ) { |
|
| 364 | - continue; |
|
| 365 | - } |
|
| 362 | + // Skip if an existing image is found. |
|
| 363 | + if ( null !== $existing_image ) { |
|
| 364 | + continue; |
|
| 365 | + } |
|
| 366 | 366 | |
| 367 | - // Save the image and get the local path. |
|
| 368 | - $image = wl_save_image( $image_remote_url ); |
|
| 367 | + // Save the image and get the local path. |
|
| 368 | + $image = wl_save_image( $image_remote_url ); |
|
| 369 | 369 | |
| 370 | - // Get the local URL. |
|
| 371 | - $filename = $image['path']; |
|
| 372 | - $url = $image['url']; |
|
| 373 | - $content_type = $image['content_type']; |
|
| 370 | + // Get the local URL. |
|
| 371 | + $filename = $image['path']; |
|
| 372 | + $url = $image['url']; |
|
| 373 | + $content_type = $image['content_type']; |
|
| 374 | 374 | |
| 375 | - $attachment = array( |
|
| 376 | - 'guid' => $url, |
|
| 377 | - // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 378 | - 'post_title' => $label, |
|
| 379 | - // Set the title to the post title. |
|
| 380 | - 'post_content' => '', |
|
| 381 | - 'post_status' => 'inherit', |
|
| 382 | - 'post_mime_type' => $content_type |
|
| 383 | - ); |
|
| 375 | + $attachment = array( |
|
| 376 | + 'guid' => $url, |
|
| 377 | + // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 378 | + 'post_title' => $label, |
|
| 379 | + // Set the title to the post title. |
|
| 380 | + 'post_content' => '', |
|
| 381 | + 'post_status' => 'inherit', |
|
| 382 | + 'post_mime_type' => $content_type |
|
| 383 | + ); |
|
| 384 | 384 | |
| 385 | - // Create the attachment in WordPress and generate the related metadata. |
|
| 386 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 385 | + // Create the attachment in WordPress and generate the related metadata. |
|
| 386 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 387 | 387 | |
| 388 | - // Set the source URL for the image. |
|
| 389 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 388 | + // Set the source URL for the image. |
|
| 389 | + wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 390 | 390 | |
| 391 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 392 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 391 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 392 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 393 | 393 | |
| 394 | - // Set it as the featured image. |
|
| 395 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 396 | - } |
|
| 394 | + // Set it as the featured image. |
|
| 395 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 396 | + } |
|
| 397 | 397 | |
| 398 | - // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 399 | - // save the entity in the triple store. |
|
| 400 | - wl_linked_data_push_to_redlink( $post_id ); |
|
| 398 | + // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 399 | + // save the entity in the triple store. |
|
| 400 | + wl_linked_data_push_to_redlink( $post_id ); |
|
| 401 | 401 | |
| 402 | - // finally return the entity post. |
|
| 403 | - return get_post( $post_id ); |
|
| 402 | + // finally return the entity post. |
|
| 403 | + return get_post( $post_id ); |
|
| 404 | 404 | } |
| 405 | 405 | |
| 406 | 406 | /** |
@@ -414,41 +414,41 @@ discard block |
||
| 414 | 414 | */ |
| 415 | 415 | function wl_linked_data_content_get_embedded_entities( $content ) { |
| 416 | 416 | |
| 417 | - // Remove quote escapes. |
|
| 418 | - $content = str_replace( '\\"', '"', $content ); |
|
| 417 | + // Remove quote escapes. |
|
| 418 | + $content = str_replace( '\\"', '"', $content ); |
|
| 419 | 419 | |
| 420 | - // Match all itemid attributes. |
|
| 421 | - $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 420 | + // Match all itemid attributes. |
|
| 421 | + $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 422 | 422 | |
| 423 | - // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 423 | + // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 424 | 424 | |
| 425 | - // Remove the pattern while it is found (match nested annotations). |
|
| 426 | - $matches = array(); |
|
| 425 | + // Remove the pattern while it is found (match nested annotations). |
|
| 426 | + $matches = array(); |
|
| 427 | 427 | |
| 428 | - // In case of errors, return an empty array. |
|
| 429 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 430 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 428 | + // In case of errors, return an empty array. |
|
| 429 | + if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 430 | + wl_write_log( "Found no entities embedded in content" ); |
|
| 431 | 431 | |
| 432 | - return array(); |
|
| 433 | - } |
|
| 432 | + return array(); |
|
| 433 | + } |
|
| 434 | 434 | |
| 435 | 435 | // wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]"); |
| 436 | 436 | |
| 437 | - // Collect the entities. |
|
| 438 | - $entities = array(); |
|
| 439 | - foreach ( $matches[1] as $uri ) { |
|
| 440 | - $uri_d = html_entity_decode( $uri ); |
|
| 441 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 437 | + // Collect the entities. |
|
| 438 | + $entities = array(); |
|
| 439 | + foreach ( $matches[1] as $uri ) { |
|
| 440 | + $uri_d = html_entity_decode( $uri ); |
|
| 441 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 442 | 442 | |
| 443 | - if ( null !== $entity ) { |
|
| 444 | - array_push( $entities, $entity->ID ); |
|
| 445 | - } |
|
| 446 | - } |
|
| 443 | + if ( null !== $entity ) { |
|
| 444 | + array_push( $entities, $entity->ID ); |
|
| 445 | + } |
|
| 446 | + } |
|
| 447 | 447 | |
| 448 | - // $count = sizeof( $entities ); |
|
| 449 | - // wl_write_log( "Found $count entities embedded in content" ); |
|
| 448 | + // $count = sizeof( $entities ); |
|
| 449 | + // wl_write_log( "Found $count entities embedded in content" ); |
|
| 450 | 450 | |
| 451 | - return $entities; |
|
| 451 | + return $entities; |
|
| 452 | 452 | } |
| 453 | 453 | |
| 454 | 454 | /** |
@@ -460,22 +460,22 @@ discard block |
||
| 460 | 460 | */ |
| 461 | 461 | function wl_linked_data_push_to_redlink( $post_id ) { |
| 462 | 462 | |
| 463 | - // Get the post. |
|
| 464 | - $post = get_post( $post_id ); |
|
| 463 | + // Get the post. |
|
| 464 | + $post = get_post( $post_id ); |
|
| 465 | 465 | |
| 466 | - // wl_write_log( "wl_linked_data_push_to_redlink [ post id :: $post_id ][ post type :: $post->post_type ]" ); |
|
| 466 | + // wl_write_log( "wl_linked_data_push_to_redlink [ post id :: $post_id ][ post type :: $post->post_type ]" ); |
|
| 467 | 467 | |
| 468 | - // Call the method on behalf of the post type. |
|
| 469 | - switch ( $post->post_type ) { |
|
| 470 | - case 'entity': |
|
| 471 | - wl_push_entity_post_to_redlink( $post ); |
|
| 472 | - break; |
|
| 473 | - default: |
|
| 474 | - wl_push_post_to_redlink( $post ); |
|
| 475 | - } |
|
| 468 | + // Call the method on behalf of the post type. |
|
| 469 | + switch ( $post->post_type ) { |
|
| 470 | + case 'entity': |
|
| 471 | + wl_push_entity_post_to_redlink( $post ); |
|
| 472 | + break; |
|
| 473 | + default: |
|
| 474 | + wl_push_post_to_redlink( $post ); |
|
| 475 | + } |
|
| 476 | 476 | |
| 477 | - // Reindex the triple store if buffering is turned off. |
|
| 478 | - if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 479 | - wordlift_reindex_triple_store(); |
|
| 480 | - } |
|
| 477 | + // Reindex the triple store if buffering is turned off. |
|
| 478 | + if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 479 | + wordlift_reindex_triple_store(); |
|
| 480 | + } |
|
| 481 | 481 | } |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | * The Linked Data module provides synchronization of local WordPress data with the remote Linked Data store. |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | -require_once( 'wordlift_linked_data_images.php' ); |
|
| 6 | +require_once('wordlift_linked_data_images.php'); |
|
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * Receive events from post saves, and split them according to the post type. |
@@ -12,24 +12,24 @@ discard block |
||
| 12 | 12 | * |
| 13 | 13 | * @param int $post_id The post id. |
| 14 | 14 | */ |
| 15 | -function wl_linked_data_save_post( $post_id ) { |
|
| 15 | +function wl_linked_data_save_post($post_id) { |
|
| 16 | 16 | |
| 17 | 17 | // If it's not numeric exit from here. |
| 18 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 18 | + if ( ! is_numeric($post_id) || is_numeric(wp_is_post_revision($post_id))) { |
|
| 19 | 19 | return; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | // unhook this function so it doesn't loop infinitely |
| 23 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 23 | + remove_action('save_post', 'wl_linked_data_save_post'); |
|
| 24 | 24 | |
| 25 | 25 | // raise the *wl_linked_data_save_post* event. |
| 26 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 26 | + do_action('wl_linked_data_save_post', $post_id); |
|
| 27 | 27 | |
| 28 | 28 | // re-hook this function |
| 29 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 29 | + add_action('save_post', 'wl_linked_data_save_post'); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | -add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 32 | +add_action('save_post', 'wl_linked_data_save_post'); |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * Save the post to the triple store. Also saves the entities locally and on the triple store. |
@@ -38,17 +38,17 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @param int $post_id The post id being saved. |
| 40 | 40 | */ |
| 41 | -function wl_linked_data_save_post_and_related_entities( $post_id ) { |
|
| 41 | +function wl_linked_data_save_post_and_related_entities($post_id) { |
|
| 42 | 42 | |
| 43 | 43 | // Ignore auto-saves |
| 44 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 44 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 45 | 45 | return; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | // get the current post. |
| 49 | - $post = get_post( $post_id ); |
|
| 49 | + $post = get_post($post_id); |
|
| 50 | 50 | |
| 51 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 51 | + remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 52 | 52 | |
| 53 | 53 | // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
| 54 | 54 | |
@@ -58,127 +58,127 @@ discard block |
||
| 58 | 58 | $entities_predicates_mapping = null; |
| 59 | 59 | |
| 60 | 60 | // Save the entities coming with POST data. |
| 61 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 61 | + if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) { |
|
| 62 | 62 | |
| 63 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 64 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 65 | - wl_write_log( "]" ); |
|
| 66 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 67 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 68 | - wl_write_log( "]" ); |
|
| 63 | + wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: "); |
|
| 64 | + wl_write_log(json_encode($_POST['wl_entities'])); |
|
| 65 | + wl_write_log("]"); |
|
| 66 | + wl_write_log("[ post id :: $post_id ][ POST(wl_boxes) :: "); |
|
| 67 | + wl_write_log(json_encode($_POST['wl_boxes'], true)); |
|
| 68 | + wl_write_log("]"); |
|
| 69 | 69 | |
| 70 | 70 | $entities_via_post = $_POST['wl_entities']; |
| 71 | 71 | $boxes_via_post = $_POST['wl_boxes']; |
| 72 | 72 | |
| 73 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 73 | + foreach ($entities_via_post as $entity_uri => $entity) { |
|
| 74 | 74 | // Local entities have a tmp uri with 'local-entity-' |
| 75 | 75 | // These uris need to be rewritten here and replaced in the content |
| 76 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 76 | + if (preg_match('/^local-entity-.+/', $entity_uri) > 0) { |
|
| 77 | 77 | // Build the proper uri |
| 78 | - $uri = sprintf( '%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path( $entity['label'] ) ); |
|
| 78 | + $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($entity['label'])); |
|
| 79 | 79 | // Populate the mapping |
| 80 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 80 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
| 81 | 81 | // Override the entity obj |
| 82 | - $entities_via_post[ $entity_uri ]['uri'] = $uri; |
|
| 82 | + $entities_via_post[$entity_uri]['uri'] = $uri; |
|
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | // Populate the $entities_predicates_mapping |
| 87 | 87 | // Local Redlink uris need to be used here |
| 88 | - foreach ( $boxes_via_post as $predicate => $entity_uris ) { |
|
| 89 | - foreach ( $entity_uris as $entity_uri ) { |
|
| 88 | + foreach ($boxes_via_post as $predicate => $entity_uris) { |
|
| 89 | + foreach ($entity_uris as $entity_uri) { |
|
| 90 | 90 | // Retrieve the entity label needed to build the uri |
| 91 | 91 | // If the current uri is an internal uri, then entity uri is preserved: |
| 92 | 92 | // the current label could be an alternative label that has not to impact on entity uri |
| 93 | - if ( 0 !== strrpos( $entity_uri, wl_configuration_get_redlink_dataset_uri() ) ) { |
|
| 94 | - $label = $entities_via_post[ stripslashes( $entity_uri ) ]['label']; |
|
| 95 | - $uri = sprintf( '%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path( $label ) ); |
|
| 93 | + if (0 !== strrpos($entity_uri, wl_configuration_get_redlink_dataset_uri())) { |
|
| 94 | + $label = $entities_via_post[stripslashes($entity_uri)]['label']; |
|
| 95 | + $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($label)); |
|
| 96 | 96 | } else { |
| 97 | - $uri = stripslashes( $entity_uri ); |
|
| 97 | + $uri = stripslashes($entity_uri); |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - $entities_predicates_mapping[ $uri ][] = $predicate; |
|
| 100 | + $entities_predicates_mapping[$uri][] = $predicate; |
|
| 101 | 101 | |
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | // Save entities (properties included) and push them to Redlink |
| 106 | - wl_save_entities( array_values( $entities_via_post ), $post_id ); |
|
| 106 | + wl_save_entities(array_values($entities_via_post), $post_id); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | // Replace tmp uris in content post if needed |
| 110 | 110 | $updated_post_content = $post->post_content; |
| 111 | 111 | // Save each entity and store the post id. |
| 112 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 113 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 112 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
| 113 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
| 114 | 114 | } |
| 115 | 115 | // Update the post content |
| 116 | - wp_update_post( array( |
|
| 116 | + wp_update_post(array( |
|
| 117 | 117 | 'ID' => $post->ID, |
| 118 | 118 | 'post_content' => $updated_post_content, |
| 119 | - ) ); |
|
| 119 | + )); |
|
| 120 | 120 | |
| 121 | 121 | // Extract related/referenced entities from text. |
| 122 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 122 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content); |
|
| 123 | 123 | |
| 124 | 124 | // Reset previously saved instances |
| 125 | - wl_core_delete_relation_instances( $post_id ); |
|
| 125 | + wl_core_delete_relation_instances($post_id); |
|
| 126 | 126 | |
| 127 | 127 | // Save relation instances |
| 128 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 128 | + foreach (array_unique($disambiguated_entities) as $referenced_entity_id) { |
|
| 129 | 129 | |
| 130 | - if ( $entities_predicates_mapping ) { |
|
| 130 | + if ($entities_predicates_mapping) { |
|
| 131 | 131 | |
| 132 | 132 | // wl_write_log(" Going to manage relation instances according to the following mapping"); |
| 133 | 133 | |
| 134 | 134 | // Retrieve the entity uri |
| 135 | - $referenced_entity_uri = wl_get_entity_uri( $referenced_entity_id ); |
|
| 135 | + $referenced_entity_uri = wl_get_entity_uri($referenced_entity_id); |
|
| 136 | 136 | // Retrieve predicates for the current uri |
| 137 | - if ( isset( $entities_predicates_mapping[ $referenced_entity_uri ] ) ) { |
|
| 138 | - foreach ( $entities_predicates_mapping[ $referenced_entity_uri ] as $predicate ) { |
|
| 137 | + if (isset($entities_predicates_mapping[$referenced_entity_uri])) { |
|
| 138 | + foreach ($entities_predicates_mapping[$referenced_entity_uri] as $predicate) { |
|
| 139 | 139 | wl_write_log(" Going to add relation with predicate $predicate"); |
| 140 | - wl_core_add_relation_instance( $post_id, $predicate, $referenced_entity_id ); |
|
| 140 | + wl_core_add_relation_instance($post_id, $predicate, $referenced_entity_id); |
|
| 141 | 141 | } |
| 142 | 142 | } else { |
| 143 | - wl_write_log( "Entity uri $referenced_entity_uri missing in the mapping" ); |
|
| 144 | - wl_write_log( $entities_predicates_mapping ); |
|
| 143 | + wl_write_log("Entity uri $referenced_entity_uri missing in the mapping"); |
|
| 144 | + wl_write_log($entities_predicates_mapping); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | } else { |
| 148 | 148 | // Just for unit tests |
| 149 | - wl_core_add_relation_instance( $post_id, 'what', $referenced_entity_id ); |
|
| 149 | + wl_core_add_relation_instance($post_id, 'what', $referenced_entity_id); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | // TODO Check if is needed |
| 153 | - wl_linked_data_push_to_redlink( $referenced_entity_id ); |
|
| 153 | + wl_linked_data_push_to_redlink($referenced_entity_id); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | // Push the post to Redlink. |
| 157 | - wl_linked_data_push_to_redlink( $post->ID ); |
|
| 157 | + wl_linked_data_push_to_redlink($post->ID); |
|
| 158 | 158 | |
| 159 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 159 | + add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | -add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 162 | +add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 163 | 163 | |
| 164 | 164 | /** |
| 165 | 165 | * Adds default schema type "Thing" as soon as an entity is created. |
| 166 | 166 | */ |
| 167 | -function wordlift_save_post_add_default_schema_type( $entity_id ) { |
|
| 167 | +function wordlift_save_post_add_default_schema_type($entity_id) { |
|
| 168 | 168 | |
| 169 | - $entity = get_post( $entity_id ); |
|
| 170 | - $entity_type = wl_schema_get_types( $entity_id ); |
|
| 169 | + $entity = get_post($entity_id); |
|
| 170 | + $entity_type = wl_schema_get_types($entity_id); |
|
| 171 | 171 | |
| 172 | 172 | // Assign type 'Thing' if we are dealing with an entity without type |
| 173 | - if ( $entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null( $entity_type ) ) { |
|
| 173 | + if ($entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null($entity_type)) { |
|
| 174 | 174 | |
| 175 | - wl_schema_set_types( $entity_id, 'Thing' ); |
|
| 175 | + wl_schema_set_types($entity_id, 'Thing'); |
|
| 176 | 176 | } |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // Priority 1 (default is 10) because we want the default type to be set as soon as possible |
| 180 | 180 | // Attatched to save_post because *wl_linked_data_save_post* does not always fire |
| 181 | -add_action( 'save_post', 'wordlift_save_post_add_default_schema_type', 1 ); |
|
| 181 | +add_action('save_post', 'wordlift_save_post_add_default_schema_type', 1); |
|
| 182 | 182 | |
| 183 | 183 | /** |
| 184 | 184 | * Save the specified entities to the local storage. |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | * |
| 189 | 189 | * @return array An array of posts. |
| 190 | 190 | */ |
| 191 | -function wl_save_entities( $entities, $related_post_id = null ) { |
|
| 191 | +function wl_save_entities($entities, $related_post_id = null) { |
|
| 192 | 192 | |
| 193 | 193 | // wl_write_log( "[ entities count :: " . count( $entities ) . " ][ related post id :: $related_post_id ]" ); |
| 194 | 194 | |
@@ -196,17 +196,17 @@ discard block |
||
| 196 | 196 | $posts = array(); |
| 197 | 197 | |
| 198 | 198 | // Save each entity and store the post id. |
| 199 | - foreach ( $entities as $entity ) { |
|
| 199 | + foreach ($entities as $entity) { |
|
| 200 | 200 | |
| 201 | 201 | // Update entity data with related post |
| 202 | 202 | $entity['related_post_id'] = $related_post_id; |
| 203 | 203 | |
| 204 | 204 | // Save the entity. |
| 205 | - $entity_post = wl_save_entity( $entity ); |
|
| 205 | + $entity_post = wl_save_entity($entity); |
|
| 206 | 206 | |
| 207 | 207 | // Store the post in the return array if successful. |
| 208 | - if ( null !== $entity_post ) { |
|
| 209 | - array_push( $posts, $entity_post ); |
|
| 208 | + if (null !== $entity_post) { |
|
| 209 | + array_push($posts, $entity_post); |
|
| 210 | 210 | } |
| 211 | 211 | } |
| 212 | 212 | |
@@ -229,23 +229,23 @@ discard block |
||
| 229 | 229 | * |
| 230 | 230 | * @return null|WP_Post A post instance or null in case of failure. |
| 231 | 231 | */ |
| 232 | -function wl_save_entity( $entity_data ) { |
|
| 232 | +function wl_save_entity($entity_data) { |
|
| 233 | 233 | |
| 234 | 234 | $uri = $entity_data['uri']; |
| 235 | 235 | $label = $entity_data['label']; |
| 236 | 236 | $type_uri = $entity_data['main_type']; |
| 237 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 237 | + $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array(); |
|
| 238 | 238 | $description = $entity_data['description']; |
| 239 | - $images = isset( $entity_data['image'] ) ? wl_force_to_array( $entity_data['image'] ) : array(); |
|
| 240 | - $same_as = isset( $entity_data['sameas'] ) ? wl_force_to_array( $entity_data['sameas'] ) : array(); |
|
| 241 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 242 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 239 | + $images = isset($entity_data['image']) ? wl_force_to_array($entity_data['image']) : array(); |
|
| 240 | + $same_as = isset($entity_data['sameas']) ? wl_force_to_array($entity_data['sameas']) : array(); |
|
| 241 | + $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null; |
|
| 242 | + $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array(); |
|
| 243 | 243 | |
| 244 | 244 | // wl_write_log( "[ uri :: $uri ][ label :: $label ][ type uri :: $type_uri ]" ); |
| 245 | 245 | |
| 246 | 246 | // Prepare properties of the new entity. |
| 247 | 247 | $params = array( |
| 248 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 248 | + 'post_status' => (is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft'), |
|
| 249 | 249 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 250 | 250 | 'post_title' => $label, |
| 251 | 251 | 'post_content' => $description, |
@@ -253,14 +253,14 @@ discard block |
||
| 253 | 253 | ); |
| 254 | 254 | |
| 255 | 255 | // Check whether an entity already exists with the provided URI. |
| 256 | - $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 256 | + $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri); |
|
| 257 | 257 | |
| 258 | - if ( null !== $post ) { |
|
| 258 | + if (null !== $post) { |
|
| 259 | 259 | // We insert into the params the entity ID, so it will be updated and not inserted. |
| 260 | - $params[ 'ID' ] = $post->ID; |
|
| 260 | + $params['ID'] = $post->ID; |
|
| 261 | 261 | // Preserve the current entity status |
| 262 | - if ( 'public' === $post->post_status ) { |
|
| 263 | - $params[ 'post_status' ] = $post->post_status; |
|
| 262 | + if ('public' === $post->post_status) { |
|
| 263 | + $params['post_status'] = $post->post_status; |
|
| 264 | 264 | } |
| 265 | 265 | // Preserve the current entity post_content. |
| 266 | 266 | $params['post_content'] = $post->post_content; |
@@ -275,12 +275,12 @@ discard block |
||
| 275 | 275 | // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
| 276 | 276 | // is created when saving a post. |
| 277 | 277 | global $wpseo_metabox, $seo_ultimate; |
| 278 | - if ( isset( $wpseo_metabox ) ) { |
|
| 279 | - remove_action( 'wp_insert_post', array( $wpseo_metabox, 'save_postdata' ) ); |
|
| 278 | + if (isset($wpseo_metabox)) { |
|
| 279 | + remove_action('wp_insert_post', array($wpseo_metabox, 'save_postdata')); |
|
| 280 | 280 | } |
| 281 | 281 | |
| 282 | - if ( isset( $seo_ultimate ) ) { |
|
| 283 | - remove_action( 'save_post', array( $seo_ultimate, 'save_postmeta_box' ) ); |
|
| 282 | + if (isset($seo_ultimate)) { |
|
| 283 | + remove_action('save_post', array($seo_ultimate, 'save_postmeta_box')); |
|
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
@@ -294,78 +294,78 @@ discard block |
||
| 294 | 294 | $wp_filter['save_post'] = array(); |
| 295 | 295 | |
| 296 | 296 | // create or update the post. |
| 297 | - $post_id = wp_insert_post( $params, true ); |
|
| 297 | + $post_id = wp_insert_post($params, true); |
|
| 298 | 298 | |
| 299 | 299 | // Restore all the existing filters. |
| 300 | 300 | $wp_filter['save_post'] = $save_post_filters; |
| 301 | 301 | |
| 302 | 302 | // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
| 303 | - if ( isset( $wpseo_metabox ) ) { |
|
| 304 | - add_action( 'wp_insert_post', array( $wpseo_metabox, 'save_postdata' ) ); |
|
| 303 | + if (isset($wpseo_metabox)) { |
|
| 304 | + add_action('wp_insert_post', array($wpseo_metabox, 'save_postdata')); |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
| 308 | - if ( isset( $seo_ultimate ) ) { |
|
| 309 | - add_action( 'save_post', array( $seo_ultimate, 'save_postmeta_box' ), 10, 2 ); |
|
| 308 | + if (isset($seo_ultimate)) { |
|
| 309 | + add_action('save_post', array($seo_ultimate, 'save_postmeta_box'), 10, 2); |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | // TODO: handle errors. |
| 313 | - if ( is_wp_error( $post_id ) ) { |
|
| 314 | - wl_write_log( ': error occurred' ); |
|
| 313 | + if (is_wp_error($post_id)) { |
|
| 314 | + wl_write_log(': error occurred'); |
|
| 315 | 315 | |
| 316 | 316 | // inform an error occurred. |
| 317 | 317 | return null; |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 320 | + wl_set_entity_main_type($post_id, $type_uri); |
|
| 321 | 321 | |
| 322 | 322 | // Save the entity types. |
| 323 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 323 | + wl_set_entity_rdf_types($post_id, $entity_types); |
|
| 324 | 324 | |
| 325 | 325 | // Get a dataset URI for the entity. |
| 326 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 326 | + $wl_uri = wl_build_entity_uri($post_id); |
|
| 327 | 327 | |
| 328 | 328 | // Save the entity URI. |
| 329 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 329 | + wl_set_entity_uri($post_id, $wl_uri); |
|
| 330 | 330 | |
| 331 | 331 | // Add the uri to the sameAs data if it's not a local URI. |
| 332 | - if ( $wl_uri !== $uri ) { |
|
| 333 | - array_push( $same_as, $uri ); |
|
| 332 | + if ($wl_uri !== $uri) { |
|
| 333 | + array_push($same_as, $uri); |
|
| 334 | 334 | } |
| 335 | 335 | |
| 336 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 336 | + $new_uri = wl_get_entity_uri($post_id); |
|
| 337 | 337 | |
| 338 | 338 | // Save the sameAs data for the entity. |
| 339 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 339 | + wl_schema_set_value($post_id, 'sameAs', $same_as); |
|
| 340 | 340 | |
| 341 | 341 | // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
| 342 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 343 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 342 | + foreach ($other_properties as $property_name => $property_value) { |
|
| 343 | + wl_schema_set_value($post_id, $property_name, $property_value); |
|
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | // Call hooks. |
| 347 | - do_action( 'wl_save_entity', $post_id ); |
|
| 347 | + do_action('wl_save_entity', $post_id); |
|
| 348 | 348 | |
| 349 | - wl_write_log( "[ post id :: $post_id ][ uri :: $uri ][ label :: $label ][ wl uri :: $wl_uri ][ types :: " . implode( ',', $entity_types ) . " ][ images count :: " . count( $images ) . " ][ same_as count :: " . count( $same_as ) . " ]" ); |
|
| 349 | + wl_write_log("[ post id :: $post_id ][ uri :: $uri ][ label :: $label ][ wl uri :: $wl_uri ][ types :: ".implode(',', $entity_types)." ][ images count :: ".count($images)." ][ same_as count :: ".count($same_as)." ]"); |
|
| 350 | 350 | |
| 351 | - foreach ( $images as $image_remote_url ) { |
|
| 351 | + foreach ($images as $image_remote_url) { |
|
| 352 | 352 | |
| 353 | 353 | // Check if image is already present in local DB |
| 354 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 354 | + if (strpos($image_remote_url, site_url()) !== false) { |
|
| 355 | 355 | // Do nothing. |
| 356 | 356 | continue; |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | 359 | // Check if there is an existing attachment for this post ID and source URL. |
| 360 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 360 | + $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url); |
|
| 361 | 361 | |
| 362 | 362 | // Skip if an existing image is found. |
| 363 | - if ( null !== $existing_image ) { |
|
| 363 | + if (null !== $existing_image) { |
|
| 364 | 364 | continue; |
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | // Save the image and get the local path. |
| 368 | - $image = wl_save_image( $image_remote_url ); |
|
| 368 | + $image = wl_save_image($image_remote_url); |
|
| 369 | 369 | |
| 370 | 370 | // Get the local URL. |
| 371 | 371 | $filename = $image['path']; |
@@ -383,24 +383,24 @@ discard block |
||
| 383 | 383 | ); |
| 384 | 384 | |
| 385 | 385 | // Create the attachment in WordPress and generate the related metadata. |
| 386 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 386 | + $attachment_id = wp_insert_attachment($attachment, $filename, $post_id); |
|
| 387 | 387 | |
| 388 | 388 | // Set the source URL for the image. |
| 389 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 389 | + wl_set_source_url($attachment_id, $image_remote_url); |
|
| 390 | 390 | |
| 391 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 392 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 391 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 392 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 393 | 393 | |
| 394 | 394 | // Set it as the featured image. |
| 395 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 395 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 396 | 396 | } |
| 397 | 397 | |
| 398 | 398 | // The entity is pushed to Redlink on save by the function hooked to save_post. |
| 399 | 399 | // save the entity in the triple store. |
| 400 | - wl_linked_data_push_to_redlink( $post_id ); |
|
| 400 | + wl_linked_data_push_to_redlink($post_id); |
|
| 401 | 401 | |
| 402 | 402 | // finally return the entity post. |
| 403 | - return get_post( $post_id ); |
|
| 403 | + return get_post($post_id); |
|
| 404 | 404 | } |
| 405 | 405 | |
| 406 | 406 | /** |
@@ -412,10 +412,10 @@ discard block |
||
| 412 | 412 | * |
| 413 | 413 | * @return array An array of entity posts. |
| 414 | 414 | */ |
| 415 | -function wl_linked_data_content_get_embedded_entities( $content ) { |
|
| 415 | +function wl_linked_data_content_get_embedded_entities($content) { |
|
| 416 | 416 | |
| 417 | 417 | // Remove quote escapes. |
| 418 | - $content = str_replace( '\\"', '"', $content ); |
|
| 418 | + $content = str_replace('\\"', '"', $content); |
|
| 419 | 419 | |
| 420 | 420 | // Match all itemid attributes. |
| 421 | 421 | $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
@@ -426,8 +426,8 @@ discard block |
||
| 426 | 426 | $matches = array(); |
| 427 | 427 | |
| 428 | 428 | // In case of errors, return an empty array. |
| 429 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 430 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 429 | + if (false === preg_match_all($pattern, $content, $matches)) { |
|
| 430 | + wl_write_log("Found no entities embedded in content"); |
|
| 431 | 431 | |
| 432 | 432 | return array(); |
| 433 | 433 | } |
@@ -436,12 +436,12 @@ discard block |
||
| 436 | 436 | |
| 437 | 437 | // Collect the entities. |
| 438 | 438 | $entities = array(); |
| 439 | - foreach ( $matches[1] as $uri ) { |
|
| 440 | - $uri_d = html_entity_decode( $uri ); |
|
| 441 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 439 | + foreach ($matches[1] as $uri) { |
|
| 440 | + $uri_d = html_entity_decode($uri); |
|
| 441 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri_d); |
|
| 442 | 442 | |
| 443 | - if ( null !== $entity ) { |
|
| 444 | - array_push( $entities, $entity->ID ); |
|
| 443 | + if (null !== $entity) { |
|
| 444 | + array_push($entities, $entity->ID); |
|
| 445 | 445 | } |
| 446 | 446 | } |
| 447 | 447 | |
@@ -458,24 +458,24 @@ discard block |
||
| 458 | 458 | * |
| 459 | 459 | * @param int $post_id The post ID. |
| 460 | 460 | */ |
| 461 | -function wl_linked_data_push_to_redlink( $post_id ) { |
|
| 461 | +function wl_linked_data_push_to_redlink($post_id) { |
|
| 462 | 462 | |
| 463 | 463 | // Get the post. |
| 464 | - $post = get_post( $post_id ); |
|
| 464 | + $post = get_post($post_id); |
|
| 465 | 465 | |
| 466 | 466 | // wl_write_log( "wl_linked_data_push_to_redlink [ post id :: $post_id ][ post type :: $post->post_type ]" ); |
| 467 | 467 | |
| 468 | 468 | // Call the method on behalf of the post type. |
| 469 | - switch ( $post->post_type ) { |
|
| 469 | + switch ($post->post_type) { |
|
| 470 | 470 | case 'entity': |
| 471 | - wl_push_entity_post_to_redlink( $post ); |
|
| 471 | + wl_push_entity_post_to_redlink($post); |
|
| 472 | 472 | break; |
| 473 | 473 | default: |
| 474 | - wl_push_post_to_redlink( $post ); |
|
| 474 | + wl_push_post_to_redlink($post); |
|
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | // Reindex the triple store if buffering is turned off. |
| 478 | - if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 478 | + if (false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING) { |
|
| 479 | 479 | wordlift_reindex_triple_store(); |
| 480 | 480 | } |
| 481 | 481 | } |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | // If this file is called directly, abort. |
| 28 | 28 | if ( ! defined( 'WPINC' ) ) { |
| 29 | - die; |
|
| 29 | + die; |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | // Include WordLift constants. |
@@ -47,18 +47,18 @@ discard block |
||
| 47 | 47 | */ |
| 48 | 48 | function wl_write_log( $log ) { |
| 49 | 49 | |
| 50 | - $handler = apply_filters( 'wl_write_log_handler', null ); |
|
| 50 | + $handler = apply_filters( 'wl_write_log_handler', null ); |
|
| 51 | 51 | |
| 52 | - $callers = debug_backtrace(); |
|
| 53 | - $caller_function = $callers[1]['function']; |
|
| 52 | + $callers = debug_backtrace(); |
|
| 53 | + $caller_function = $callers[1]['function']; |
|
| 54 | 54 | |
| 55 | - if ( is_null( $handler ) ) { |
|
| 56 | - wl_write_log_handler( $log, $caller_function ); |
|
| 55 | + if ( is_null( $handler ) ) { |
|
| 56 | + wl_write_log_handler( $log, $caller_function ); |
|
| 57 | 57 | |
| 58 | - return; |
|
| 59 | - } |
|
| 58 | + return; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - call_user_func( $handler, $log, $caller_function ); |
|
| 61 | + call_user_func( $handler, $log, $caller_function ); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
@@ -71,20 +71,20 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | function wl_write_log_handler( $log, $caller = null ) { |
| 73 | 73 | |
| 74 | - global $wl_logger; |
|
| 74 | + global $wl_logger; |
|
| 75 | 75 | |
| 76 | - if ( true === WP_DEBUG ) { |
|
| 76 | + if ( true === WP_DEBUG ) { |
|
| 77 | 77 | |
| 78 | - $message = ( isset( $caller ) ? sprintf( '[%-40.40s] ', $caller ) : '' ) . |
|
| 79 | - ( is_array( $log ) || is_object( $log ) ? print_r( $log, true ) : wl_write_log_hide_key( $log ) ); |
|
| 78 | + $message = ( isset( $caller ) ? sprintf( '[%-40.40s] ', $caller ) : '' ) . |
|
| 79 | + ( is_array( $log ) || is_object( $log ) ? print_r( $log, true ) : wl_write_log_hide_key( $log ) ); |
|
| 80 | 80 | |
| 81 | - if ( isset( $wl_logger ) ) { |
|
| 82 | - $wl_logger->info( $message ); |
|
| 83 | - } else { |
|
| 84 | - error_log( $message ); |
|
| 85 | - } |
|
| 81 | + if ( isset( $wl_logger ) ) { |
|
| 82 | + $wl_logger->info( $message ); |
|
| 83 | + } else { |
|
| 84 | + error_log( $message ); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - } |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | 89 | } |
| 90 | 90 | |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | */ |
| 100 | 100 | function wl_write_log_hide_key( $text ) { |
| 101 | 101 | |
| 102 | - return str_ireplace( wl_configuration_get_key(), '<hidden>', $text ); |
|
| 102 | + return str_ireplace( wl_configuration_get_key(), '<hidden>', $text ); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | /** |
@@ -111,10 +111,10 @@ discard block |
||
| 111 | 111 | */ |
| 112 | 112 | function wl_queue_sparql_update_query( $query ) { |
| 113 | 113 | |
| 114 | - $filename = WL_TEMP_DIR . WL_REQUEST_ID . '.sparql'; |
|
| 115 | - file_put_contents( $filename, $query . "\n", FILE_APPEND ); |
|
| 114 | + $filename = WL_TEMP_DIR . WL_REQUEST_ID . '.sparql'; |
|
| 115 | + file_put_contents( $filename, $query . "\n", FILE_APPEND ); |
|
| 116 | 116 | |
| 117 | - wl_write_log( "wl_queue_sparql_update_query [ filename :: $filename ]" ); |
|
| 117 | + wl_write_log( "wl_queue_sparql_update_query [ filename :: $filename ]" ); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -124,28 +124,28 @@ discard block |
||
| 124 | 124 | */ |
| 125 | 125 | function wl_execute_saved_sparql_update_query( $request_id ) { |
| 126 | 126 | |
| 127 | - $filename = WL_TEMP_DIR . $request_id . '.sparql'; |
|
| 127 | + $filename = WL_TEMP_DIR . $request_id . '.sparql'; |
|
| 128 | 128 | |
| 129 | - // If the file doesn't exist, exit. |
|
| 130 | - if ( ! file_exists( $filename ) ) { |
|
| 131 | - wl_write_log( "wl_execute_saved_sparql_update_query : file doesn't exist [ filename :: $filename ]" ); |
|
| 129 | + // If the file doesn't exist, exit. |
|
| 130 | + if ( ! file_exists( $filename ) ) { |
|
| 131 | + wl_write_log( "wl_execute_saved_sparql_update_query : file doesn't exist [ filename :: $filename ]" ); |
|
| 132 | 132 | |
| 133 | - return; |
|
| 134 | - } |
|
| 133 | + return; |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - wl_write_log( "wl_execute_saved_sparql_update_query [ filename :: $filename ]" ); |
|
| 136 | + wl_write_log( "wl_execute_saved_sparql_update_query [ filename :: $filename ]" ); |
|
| 137 | 137 | |
| 138 | - // Get the query saved in the file. |
|
| 139 | - $query = file_get_contents( $filename ); |
|
| 138 | + // Get the query saved in the file. |
|
| 139 | + $query = file_get_contents( $filename ); |
|
| 140 | 140 | |
| 141 | - // Execute the SPARQL query. |
|
| 142 | - rl_execute_sparql_update_query( $query, false ); |
|
| 141 | + // Execute the SPARQL query. |
|
| 142 | + rl_execute_sparql_update_query( $query, false ); |
|
| 143 | 143 | |
| 144 | - // Reindex the triple store. |
|
| 145 | - wordlift_reindex_triple_store(); |
|
| 144 | + // Reindex the triple store. |
|
| 145 | + wordlift_reindex_triple_store(); |
|
| 146 | 146 | |
| 147 | - // Delete the temporary file. |
|
| 148 | - unlink( $filename ); |
|
| 147 | + // Delete the temporary file. |
|
| 148 | + unlink( $filename ); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | add_action( 'wl_execute_saved_sparql_update_query', 'wl_execute_saved_sparql_update_query', 10, 1 ); |
@@ -155,10 +155,10 @@ discard block |
||
| 155 | 155 | */ |
| 156 | 156 | function wordlift_buttonhooks() { |
| 157 | 157 | |
| 158 | - // Only add hooks when the current user has permissions AND is in Rich Text editor mode |
|
| 159 | - if ( ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) && get_user_option( 'rich_editing' ) ) { |
|
| 160 | - add_filter( 'mce_external_plugins', 'wordlift_register_tinymce_javascript' ); |
|
| 161 | - } |
|
| 158 | + // Only add hooks when the current user has permissions AND is in Rich Text editor mode |
|
| 159 | + if ( ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) && get_user_option( 'rich_editing' ) ) { |
|
| 160 | + add_filter( 'mce_external_plugins', 'wordlift_register_tinymce_javascript' ); |
|
| 161 | + } |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | /** |
@@ -170,10 +170,10 @@ discard block |
||
| 170 | 170 | */ |
| 171 | 171 | function wordlift_register_tinymce_javascript( $plugin_array ) { |
| 172 | 172 | |
| 173 | - // add the wordlift plugin. |
|
| 174 | - $plugin_array['wordlift'] = plugin_dir_url( __FILE__ ) . 'js/wordlift-reloaded.js'; |
|
| 173 | + // add the wordlift plugin. |
|
| 174 | + $plugin_array['wordlift'] = plugin_dir_url( __FILE__ ) . 'js/wordlift-reloaded.js'; |
|
| 175 | 175 | |
| 176 | - return $plugin_array; |
|
| 176 | + return $plugin_array; |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
@@ -181,21 +181,21 @@ discard block |
||
| 181 | 181 | * see http://vip.wordpress.com/documentation/register-additional-html-attributes-for-tinymce-and-wp-kses/ |
| 182 | 182 | */ |
| 183 | 183 | function wordlift_allowed_post_tags() { |
| 184 | - global $allowedposttags; |
|
| 185 | - |
|
| 186 | - $tags = array( 'span' ); |
|
| 187 | - $new_attributes = array( |
|
| 188 | - 'itemscope' => array(), |
|
| 189 | - 'itemtype' => array(), |
|
| 190 | - 'itemprop' => array(), |
|
| 191 | - 'itemid' => array() |
|
| 192 | - ); |
|
| 193 | - |
|
| 194 | - foreach ( $tags as $tag ) { |
|
| 195 | - if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) { |
|
| 196 | - $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes ); |
|
| 197 | - } |
|
| 198 | - } |
|
| 184 | + global $allowedposttags; |
|
| 185 | + |
|
| 186 | + $tags = array( 'span' ); |
|
| 187 | + $new_attributes = array( |
|
| 188 | + 'itemscope' => array(), |
|
| 189 | + 'itemtype' => array(), |
|
| 190 | + 'itemprop' => array(), |
|
| 191 | + 'itemid' => array() |
|
| 192 | + ); |
|
| 193 | + |
|
| 194 | + foreach ( $tags as $tag ) { |
|
| 195 | + if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) { |
|
| 196 | + $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes ); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | // init process for button control |
@@ -209,21 +209,21 @@ discard block |
||
| 209 | 209 | */ |
| 210 | 210 | function wordlift_admin_enqueue_scripts() { |
| 211 | 211 | |
| 212 | - // Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/) |
|
| 213 | - wp_enqueue_script( 'wpdialogs' ); |
|
| 214 | - wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
| 212 | + // Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/) |
|
| 213 | + wp_enqueue_script( 'wpdialogs' ); |
|
| 214 | + wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
| 215 | 215 | |
| 216 | - wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css' ); |
|
| 216 | + wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css' ); |
|
| 217 | 217 | |
| 218 | - wp_enqueue_script( 'jquery-ui-autocomplete' ); |
|
| 219 | - wp_enqueue_script( 'angularjs', plugin_dir_url( __FILE__ ) . 'bower_components/angular/angular.min.js' ); |
|
| 218 | + wp_enqueue_script( 'jquery-ui-autocomplete' ); |
|
| 219 | + wp_enqueue_script( 'angularjs', plugin_dir_url( __FILE__ ) . 'bower_components/angular/angular.min.js' ); |
|
| 220 | 220 | |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | add_action( 'admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts' ); |
| 224 | 224 | |
| 225 | 225 | function wl_enqueue_scripts() { |
| 226 | - wp_enqueue_style( 'wordlift-ui', plugin_dir_url( __FILE__ ) . 'css/wordlift-ui.min.css' ); |
|
| 226 | + wp_enqueue_style( 'wordlift-ui', plugin_dir_url( __FILE__ ) . 'css/wordlift-ui.min.css' ); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | add_action( 'wp_enqueue_scripts', 'wl_enqueue_scripts' ); |
@@ -238,18 +238,18 @@ discard block |
||
| 238 | 238 | */ |
| 239 | 239 | function wordlift_allowed_html( $allowedtags, $context ) { |
| 240 | 240 | |
| 241 | - if ( 'post' !== $context ) { |
|
| 242 | - return $allowedtags; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - return array_merge_recursive( $allowedtags, array( |
|
| 246 | - 'span' => array( |
|
| 247 | - 'itemscope' => true, |
|
| 248 | - 'itemtype' => true, |
|
| 249 | - 'itemid' => true, |
|
| 250 | - 'itemprop' => true |
|
| 251 | - ) |
|
| 252 | - ) ); |
|
| 241 | + if ( 'post' !== $context ) { |
|
| 242 | + return $allowedtags; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + return array_merge_recursive( $allowedtags, array( |
|
| 246 | + 'span' => array( |
|
| 247 | + 'itemscope' => true, |
|
| 248 | + 'itemtype' => true, |
|
| 249 | + 'itemid' => true, |
|
| 250 | + 'itemprop' => true |
|
| 251 | + ) |
|
| 252 | + ) ); |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | add_filter( 'wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2 ); |
@@ -263,15 +263,15 @@ discard block |
||
| 263 | 263 | */ |
| 264 | 264 | function wl_get_coordinates( $post_id ) { |
| 265 | 265 | |
| 266 | - $latitude = wl_schema_get_value( $post_id, 'latitude' ); |
|
| 267 | - $longitude = wl_schema_get_value( $post_id, 'longitude' ); |
|
| 266 | + $latitude = wl_schema_get_value( $post_id, 'latitude' ); |
|
| 267 | + $longitude = wl_schema_get_value( $post_id, 'longitude' ); |
|
| 268 | 268 | |
| 269 | - // DO NOT set latitude/longitude to 0/0 as default values. It's a specific place on the globe: |
|
| 270 | - // "The zero/zero point of this system is located in the Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana." |
|
| 271 | - return array( |
|
| 272 | - 'latitude' => isset( $latitude[0] ) && is_numeric( $latitude[0] ) ? $latitude[0] : '', |
|
| 273 | - 'longitude' => isset( $longitude[0] ) && is_numeric( $longitude[0] ) ? $longitude[0] : '' |
|
| 274 | - ); |
|
| 269 | + // DO NOT set latitude/longitude to 0/0 as default values. It's a specific place on the globe: |
|
| 270 | + // "The zero/zero point of this system is located in the Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana." |
|
| 271 | + return array( |
|
| 272 | + 'latitude' => isset( $latitude[0] ) && is_numeric( $latitude[0] ) ? $latitude[0] : '', |
|
| 273 | + 'longitude' => isset( $longitude[0] ) && is_numeric( $longitude[0] ) ? $longitude[0] : '' |
|
| 274 | + ); |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | /** |
@@ -283,13 +283,13 @@ discard block |
||
| 283 | 283 | */ |
| 284 | 284 | function wl_get_post_modified_time( $post ) { |
| 285 | 285 | |
| 286 | - $date_modified = get_post_modified_time( 'c', true, $post ); |
|
| 286 | + $date_modified = get_post_modified_time( 'c', true, $post ); |
|
| 287 | 287 | |
| 288 | - if ( '-' === substr( $date_modified, 0, 1 ) ) { |
|
| 289 | - return get_the_time( 'c', $post ); |
|
| 290 | - } |
|
| 288 | + if ( '-' === substr( $date_modified, 0, 1 ) ) { |
|
| 289 | + return get_the_time( 'c', $post ); |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | - return $date_modified; |
|
| 292 | + return $date_modified; |
|
| 293 | 293 | } |
| 294 | 294 | |
| 295 | 295 | /** |
@@ -301,34 +301,34 @@ discard block |
||
| 301 | 301 | */ |
| 302 | 302 | function wl_get_image_urls( $post_id ) { |
| 303 | 303 | |
| 304 | - // wl_write_log( "wl_get_image_urls [ post id :: $post_id ]" ); |
|
| 304 | + // wl_write_log( "wl_get_image_urls [ post id :: $post_id ]" ); |
|
| 305 | 305 | |
| 306 | - $images = get_children( array( |
|
| 307 | - 'post_parent' => $post_id, |
|
| 308 | - 'post_type' => 'attachment', |
|
| 309 | - 'post_mime_type' => 'image' |
|
| 310 | - ) ); |
|
| 306 | + $images = get_children( array( |
|
| 307 | + 'post_parent' => $post_id, |
|
| 308 | + 'post_type' => 'attachment', |
|
| 309 | + 'post_mime_type' => 'image' |
|
| 310 | + ) ); |
|
| 311 | 311 | |
| 312 | - // Return an empty array if no image is found. |
|
| 313 | - if ( empty( $images ) ) { |
|
| 314 | - return array(); |
|
| 315 | - } |
|
| 312 | + // Return an empty array if no image is found. |
|
| 313 | + if ( empty( $images ) ) { |
|
| 314 | + return array(); |
|
| 315 | + } |
|
| 316 | 316 | |
| 317 | - // Prepare the return array. |
|
| 318 | - $image_urls = array(); |
|
| 317 | + // Prepare the return array. |
|
| 318 | + $image_urls = array(); |
|
| 319 | 319 | |
| 320 | - // Collect the URLs. |
|
| 321 | - foreach ( $images as $attachment_id => $attachment ) { |
|
| 322 | - $image_url = wp_get_attachment_url( $attachment_id ); |
|
| 323 | - // Ensure the URL isn't collected already. |
|
| 324 | - if ( ! in_array( $image_url, $image_urls ) ) { |
|
| 325 | - array_push( $image_urls, $image_url ); |
|
| 326 | - } |
|
| 327 | - } |
|
| 320 | + // Collect the URLs. |
|
| 321 | + foreach ( $images as $attachment_id => $attachment ) { |
|
| 322 | + $image_url = wp_get_attachment_url( $attachment_id ); |
|
| 323 | + // Ensure the URL isn't collected already. |
|
| 324 | + if ( ! in_array( $image_url, $image_urls ) ) { |
|
| 325 | + array_push( $image_urls, $image_url ); |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - // wl_write_log( "wl_get_image_urls [ post id :: $post_id ][ image urls count :: " . count( $image_urls ) . " ]" ); |
|
| 329 | + // wl_write_log( "wl_get_image_urls [ post id :: $post_id ][ image urls count :: " . count( $image_urls ) . " ]" ); |
|
| 330 | 330 | |
| 331 | - return $image_urls; |
|
| 331 | + return $image_urls; |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | 334 | /** |
@@ -341,19 +341,19 @@ discard block |
||
| 341 | 341 | */ |
| 342 | 342 | function wl_get_sparql_images( $uri, $post_id ) { |
| 343 | 343 | |
| 344 | - $sparql = ''; |
|
| 344 | + $sparql = ''; |
|
| 345 | 345 | |
| 346 | - // Get the escaped URI. |
|
| 347 | - $uri_e = esc_html( $uri ); |
|
| 346 | + // Get the escaped URI. |
|
| 347 | + $uri_e = esc_html( $uri ); |
|
| 348 | 348 | |
| 349 | - // Add SPARQL stmts to write the schema:image. |
|
| 350 | - $image_urls = wl_get_image_urls( $post_id ); |
|
| 351 | - foreach ( $image_urls as $image_url ) { |
|
| 352 | - $image_url_esc = wl_sparql_escape_uri( $image_url ); |
|
| 353 | - $sparql .= " <$uri_e> schema:image <$image_url_esc> . \n"; |
|
| 354 | - } |
|
| 349 | + // Add SPARQL stmts to write the schema:image. |
|
| 350 | + $image_urls = wl_get_image_urls( $post_id ); |
|
| 351 | + foreach ( $image_urls as $image_url ) { |
|
| 352 | + $image_url_esc = wl_sparql_escape_uri( $image_url ); |
|
| 353 | + $sparql .= " <$uri_e> schema:image <$image_url_esc> . \n"; |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | - return $sparql; |
|
| 356 | + return $sparql; |
|
| 357 | 357 | } |
| 358 | 358 | |
| 359 | 359 | /** |
@@ -366,24 +366,24 @@ discard block |
||
| 366 | 366 | */ |
| 367 | 367 | function wl_get_attachment_for_source_url( $parent_post_id, $source_url ) { |
| 368 | 368 | |
| 369 | - // wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" ); |
|
| 369 | + // wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" ); |
|
| 370 | 370 | |
| 371 | - $posts = get_posts( array( |
|
| 372 | - 'post_type' => 'attachment', |
|
| 373 | - 'posts_per_page' => 1, |
|
| 374 | - 'post_status' => 'any', |
|
| 375 | - 'post_parent' => $parent_post_id, |
|
| 376 | - 'meta_key' => 'wl_source_url', |
|
| 377 | - 'meta_value' => $source_url |
|
| 378 | - ) ); |
|
| 371 | + $posts = get_posts( array( |
|
| 372 | + 'post_type' => 'attachment', |
|
| 373 | + 'posts_per_page' => 1, |
|
| 374 | + 'post_status' => 'any', |
|
| 375 | + 'post_parent' => $parent_post_id, |
|
| 376 | + 'meta_key' => 'wl_source_url', |
|
| 377 | + 'meta_value' => $source_url |
|
| 378 | + ) ); |
|
| 379 | 379 | |
| 380 | - // Return the found post. |
|
| 381 | - if ( 1 === count( $posts ) ) { |
|
| 382 | - return $posts[0]; |
|
| 383 | - } |
|
| 380 | + // Return the found post. |
|
| 381 | + if ( 1 === count( $posts ) ) { |
|
| 382 | + return $posts[0]; |
|
| 383 | + } |
|
| 384 | 384 | |
| 385 | - // Return null. |
|
| 386 | - return null; |
|
| 385 | + // Return null. |
|
| 386 | + return null; |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | /** |
@@ -394,8 +394,8 @@ discard block |
||
| 394 | 394 | */ |
| 395 | 395 | function wl_set_source_url( $post_id, $source_url ) { |
| 396 | 396 | |
| 397 | - delete_post_meta( $post_id, 'wl_source_url' ); |
|
| 398 | - add_post_meta( $post_id, 'wl_source_url', $source_url ); |
|
| 397 | + delete_post_meta( $post_id, 'wl_source_url' ); |
|
| 398 | + add_post_meta( $post_id, 'wl_source_url', $source_url ); |
|
| 399 | 399 | } |
| 400 | 400 | |
| 401 | 401 | |
@@ -413,43 +413,43 @@ discard block |
||
| 413 | 413 | */ |
| 414 | 414 | function wl_flush_rewrite_rules_hard( $hard ) { |
| 415 | 415 | |
| 416 | - // Get all published posts. |
|
| 417 | - $posts = get_posts( array( |
|
| 418 | - 'posts_per_page' => - 1, |
|
| 419 | - 'post_type' => 'any', |
|
| 420 | - 'post_status' => 'publish' |
|
| 421 | - ) ); |
|
| 416 | + // Get all published posts. |
|
| 417 | + $posts = get_posts( array( |
|
| 418 | + 'posts_per_page' => - 1, |
|
| 419 | + 'post_type' => 'any', |
|
| 420 | + 'post_status' => 'publish' |
|
| 421 | + ) ); |
|
| 422 | 422 | |
| 423 | - // Holds the delete part of the query. |
|
| 424 | - $delete_query = rl_sparql_prefixes(); |
|
| 425 | - // Holds the insert part of the query. |
|
| 426 | - $insert_query = 'INSERT DATA { '; |
|
| 423 | + // Holds the delete part of the query. |
|
| 424 | + $delete_query = rl_sparql_prefixes(); |
|
| 425 | + // Holds the insert part of the query. |
|
| 426 | + $insert_query = 'INSERT DATA { '; |
|
| 427 | 427 | |
| 428 | - // Cycle in each post to build the query. |
|
| 429 | - foreach ( $posts as $post ) { |
|
| 428 | + // Cycle in each post to build the query. |
|
| 429 | + foreach ( $posts as $post ) { |
|
| 430 | 430 | |
| 431 | - // Ignore revisions. |
|
| 432 | - if ( wp_is_post_revision( $post->ID ) ) { |
|
| 433 | - continue; |
|
| 434 | - } |
|
| 431 | + // Ignore revisions. |
|
| 432 | + if ( wp_is_post_revision( $post->ID ) ) { |
|
| 433 | + continue; |
|
| 434 | + } |
|
| 435 | 435 | |
| 436 | - // Get the entity URI. |
|
| 437 | - $uri = wl_sparql_escape_uri( wl_get_entity_uri( $post->ID ) ); |
|
| 436 | + // Get the entity URI. |
|
| 437 | + $uri = wl_sparql_escape_uri( wl_get_entity_uri( $post->ID ) ); |
|
| 438 | 438 | |
| 439 | - // Get the post URL. |
|
| 440 | - $url = wl_sparql_escape_uri( get_permalink( $post->ID ) ); |
|
| 439 | + // Get the post URL. |
|
| 440 | + $url = wl_sparql_escape_uri( get_permalink( $post->ID ) ); |
|
| 441 | 441 | |
| 442 | - // Prepare the DELETE and INSERT commands. |
|
| 443 | - $delete_query .= "DELETE { <$uri> schema:url ?u . } WHERE { <$uri> schema:url ?u . };\n"; |
|
| 444 | - $insert_query .= " <$uri> schema:url <$url> . \n"; |
|
| 442 | + // Prepare the DELETE and INSERT commands. |
|
| 443 | + $delete_query .= "DELETE { <$uri> schema:url ?u . } WHERE { <$uri> schema:url ?u . };\n"; |
|
| 444 | + $insert_query .= " <$uri> schema:url <$url> . \n"; |
|
| 445 | 445 | |
| 446 | - // wl_write_log( "[ uri :: $uri ][ url :: $url ]" ); |
|
| 447 | - } |
|
| 446 | + // wl_write_log( "[ uri :: $uri ][ url :: $url ]" ); |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | - $insert_query .= ' };'; |
|
| 449 | + $insert_query .= ' };'; |
|
| 450 | 450 | |
| 451 | - // Execute the query. |
|
| 452 | - rl_execute_sparql_update_query( $delete_query . $insert_query ); |
|
| 451 | + // Execute the query. |
|
| 452 | + rl_execute_sparql_update_query( $delete_query . $insert_query ); |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | add_filter( 'flush_rewrite_rules_hard', 'wl_flush_rewrite_rules_hard', 10, 1 ); |
@@ -466,15 +466,15 @@ discard block |
||
| 466 | 466 | */ |
| 467 | 467 | function wl_sanitize_uri_path( $path, $char = '_' ) { |
| 468 | 468 | |
| 469 | - // wl_write_log( "wl_sanitize_uri_path [ path :: $path ][ char :: $char ]" ); |
|
| 469 | + // wl_write_log( "wl_sanitize_uri_path [ path :: $path ][ char :: $char ]" ); |
|
| 470 | 470 | |
| 471 | - // According to RFC2396 (http://www.ietf.org/rfc/rfc2396.txt) these characters are reserved: |
|
| 472 | - // ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | |
|
| 473 | - // "$" | "," |
|
| 474 | - // Plus the ' ' (space). |
|
| 475 | - // TODO: We shall use the same regex used by MediaWiki (http://stackoverflow.com/questions/23114983/mediawiki-wikipedia-url-sanitization-regex) |
|
| 471 | + // According to RFC2396 (http://www.ietf.org/rfc/rfc2396.txt) these characters are reserved: |
|
| 472 | + // ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | |
|
| 473 | + // "$" | "," |
|
| 474 | + // Plus the ' ' (space). |
|
| 475 | + // TODO: We shall use the same regex used by MediaWiki (http://stackoverflow.com/questions/23114983/mediawiki-wikipedia-url-sanitization-regex) |
|
| 476 | 476 | |
| 477 | - return sanitize_title( preg_replace( '/[;\/?:@&=+$,\s]/', $char, stripslashes( $path ) ) ); |
|
| 477 | + return sanitize_title( preg_replace( '/[;\/?:@&=+$,\s]/', $char, stripslashes( $path ) ) ); |
|
| 478 | 478 | } |
| 479 | 479 | |
| 480 | 480 | /** |
@@ -486,11 +486,11 @@ discard block |
||
| 486 | 486 | */ |
| 487 | 487 | function wl_force_to_array( $value ) { |
| 488 | 488 | |
| 489 | - if ( ! is_array( $value ) ) { |
|
| 490 | - return array( $value ); |
|
| 491 | - } |
|
| 489 | + if ( ! is_array( $value ) ) { |
|
| 490 | + return array( $value ); |
|
| 491 | + } |
|
| 492 | 492 | |
| 493 | - return $value; |
|
| 493 | + return $value; |
|
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | /** |
@@ -498,26 +498,26 @@ discard block |
||
| 498 | 498 | */ |
| 499 | 499 | function wl_shutdown() { |
| 500 | 500 | |
| 501 | - // Get the filename to the temporary SPARQL file. |
|
| 502 | - $filename = WL_TEMP_DIR . WL_REQUEST_ID . '.sparql'; |
|
| 501 | + // Get the filename to the temporary SPARQL file. |
|
| 502 | + $filename = WL_TEMP_DIR . WL_REQUEST_ID . '.sparql'; |
|
| 503 | 503 | |
| 504 | - // If WordLift is buffering SPARQL queries, we're admins and a buffer exists, then schedule it. |
|
| 505 | - if ( WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING && is_admin() && file_exists( $filename ) ) { |
|
| 504 | + // If WordLift is buffering SPARQL queries, we're admins and a buffer exists, then schedule it. |
|
| 505 | + if ( WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING && is_admin() && file_exists( $filename ) ) { |
|
| 506 | 506 | |
| 507 | - // The request ID. |
|
| 508 | - $args = array( WL_REQUEST_ID ); |
|
| 507 | + // The request ID. |
|
| 508 | + $args = array( WL_REQUEST_ID ); |
|
| 509 | 509 | |
| 510 | - // Schedule the execution of the SPARQL query with the request ID. |
|
| 511 | - wp_schedule_single_event( time(), 'wl_execute_saved_sparql_update_query', $args ); |
|
| 510 | + // Schedule the execution of the SPARQL query with the request ID. |
|
| 511 | + wp_schedule_single_event( time(), 'wl_execute_saved_sparql_update_query', $args ); |
|
| 512 | 512 | |
| 513 | - // Check that the request is scheduled. |
|
| 514 | - $timestamp = wp_next_scheduled( 'wl_execute_saved_sparql_update_query', $args ); |
|
| 513 | + // Check that the request is scheduled. |
|
| 514 | + $timestamp = wp_next_scheduled( 'wl_execute_saved_sparql_update_query', $args ); |
|
| 515 | 515 | |
| 516 | - // Spawn the cron. |
|
| 517 | - spawn_cron(); |
|
| 516 | + // Spawn the cron. |
|
| 517 | + spawn_cron(); |
|
| 518 | 518 | |
| 519 | - wl_write_log( "wl_shutdown [ request id :: " . WL_REQUEST_ID . " ][ timestamp :: $timestamp ]" ); |
|
| 520 | - } |
|
| 519 | + wl_write_log( "wl_shutdown [ request id :: " . WL_REQUEST_ID . " ][ timestamp :: $timestamp ]" ); |
|
| 520 | + } |
|
| 521 | 521 | } |
| 522 | 522 | |
| 523 | 523 | add_action( 'shutdown', 'wl_shutdown' ); |
@@ -531,45 +531,45 @@ discard block |
||
| 531 | 531 | */ |
| 532 | 532 | function wl_replace_item_id_with_uri( $content ) { |
| 533 | 533 | |
| 534 | - // wl_write_log( "wl_replace_item_id_with_uri" ); |
|
| 534 | + // wl_write_log( "wl_replace_item_id_with_uri" ); |
|
| 535 | 535 | |
| 536 | - // Strip slashes, see https://core.trac.wordpress.org/ticket/21767 |
|
| 537 | - $content = stripslashes( $content ); |
|
| 536 | + // Strip slashes, see https://core.trac.wordpress.org/ticket/21767 |
|
| 537 | + $content = stripslashes( $content ); |
|
| 538 | 538 | |
| 539 | - // If any match are found. |
|
| 540 | - $matches = array(); |
|
| 541 | - if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) { |
|
| 539 | + // If any match are found. |
|
| 540 | + $matches = array(); |
|
| 541 | + if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) { |
|
| 542 | 542 | |
| 543 | - foreach ( $matches as $match ) { |
|
| 543 | + foreach ( $matches as $match ) { |
|
| 544 | 544 | |
| 545 | - // Get the item ID. |
|
| 546 | - $item_id = $match[1]; |
|
| 545 | + // Get the item ID. |
|
| 546 | + $item_id = $match[1]; |
|
| 547 | 547 | |
| 548 | - // Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' . |
|
| 549 | - $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $item_id ); |
|
| 548 | + // Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' . |
|
| 549 | + $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $item_id ); |
|
| 550 | 550 | |
| 551 | - // If no entity is found, continue to the next one. |
|
| 552 | - if ( null === $post ) { |
|
| 553 | - continue; |
|
| 554 | - } |
|
| 551 | + // If no entity is found, continue to the next one. |
|
| 552 | + if ( null === $post ) { |
|
| 553 | + continue; |
|
| 554 | + } |
|
| 555 | 555 | |
| 556 | - // Get the URI for that post. |
|
| 557 | - $uri = wl_get_entity_uri( $post->ID ); |
|
| 556 | + // Get the URI for that post. |
|
| 557 | + $uri = wl_get_entity_uri( $post->ID ); |
|
| 558 | 558 | |
| 559 | - // wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" ); |
|
| 559 | + // wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" ); |
|
| 560 | 560 | |
| 561 | - // If the item ID and the URI differ, replace the item ID with the URI saved in WordPress. |
|
| 562 | - if ( $item_id !== $uri ) { |
|
| 563 | - $uri_e = esc_html( $uri ); |
|
| 564 | - $content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content ); |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - } |
|
| 561 | + // If the item ID and the URI differ, replace the item ID with the URI saved in WordPress. |
|
| 562 | + if ( $item_id !== $uri ) { |
|
| 563 | + $uri_e = esc_html( $uri ); |
|
| 564 | + $content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content ); |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + } |
|
| 568 | 568 | |
| 569 | - // Reapply slashes. |
|
| 570 | - $content = addslashes( $content ); |
|
| 569 | + // Reapply slashes. |
|
| 570 | + $content = addslashes( $content ); |
|
| 571 | 571 | |
| 572 | - return $content; |
|
| 572 | + return $content; |
|
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | add_filter( 'content_save_pre', 'wl_replace_item_id_with_uri', 1, 1 ); |
@@ -648,8 +648,8 @@ discard block |
||
| 648 | 648 | * This action is documented in includes/class-wordlift-activator.php |
| 649 | 649 | */ |
| 650 | 650 | function activate_wordlift() { |
| 651 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php'; |
|
| 652 | - Wordlift_Activator::activate(); |
|
| 651 | + require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php'; |
|
| 652 | + Wordlift_Activator::activate(); |
|
| 653 | 653 | } |
| 654 | 654 | |
| 655 | 655 | /** |
@@ -657,8 +657,8 @@ discard block |
||
| 657 | 657 | * This action is documented in includes/class-wordlift-deactivator.php |
| 658 | 658 | */ |
| 659 | 659 | function deactivate_wordlift() { |
| 660 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php'; |
|
| 661 | - Wordlift_Deactivator::deactivate(); |
|
| 660 | + require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php'; |
|
| 661 | + Wordlift_Deactivator::deactivate(); |
|
| 662 | 662 | } |
| 663 | 663 | |
| 664 | 664 | register_activation_hook( __FILE__, 'activate_wordlift' ); |
@@ -681,8 +681,8 @@ discard block |
||
| 681 | 681 | */ |
| 682 | 682 | function run_wordlift() { |
| 683 | 683 | |
| 684 | - $plugin = new Wordlift(); |
|
| 685 | - $plugin->run(); |
|
| 684 | + $plugin = new Wordlift(); |
|
| 685 | + $plugin->run(); |
|
| 686 | 686 | |
| 687 | 687 | } |
| 688 | 688 | |
@@ -25,16 +25,16 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | 27 | // If this file is called directly, abort. |
| 28 | -if ( ! defined( 'WPINC' ) ) { |
|
| 28 | +if ( ! defined('WPINC')) { |
|
| 29 | 29 | die; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | // Include WordLift constants. |
| 33 | -require_once( 'wordlift_constants.php' ); |
|
| 33 | +require_once('wordlift_constants.php'); |
|
| 34 | 34 | |
| 35 | 35 | // Load modules |
| 36 | -require_once( 'modules/core/wordlift_core.php' ); |
|
| 37 | -require_once( 'modules/configuration/wordlift_configuration.php' ); |
|
| 36 | +require_once('modules/core/wordlift_core.php'); |
|
| 37 | +require_once('modules/configuration/wordlift_configuration.php'); |
|
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * Log to the debug.log file. |
@@ -45,20 +45,20 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @param string|mixed $log The log data. |
| 47 | 47 | */ |
| 48 | -function wl_write_log( $log ) { |
|
| 48 | +function wl_write_log($log) { |
|
| 49 | 49 | |
| 50 | - $handler = apply_filters( 'wl_write_log_handler', null ); |
|
| 50 | + $handler = apply_filters('wl_write_log_handler', null); |
|
| 51 | 51 | |
| 52 | 52 | $callers = debug_backtrace(); |
| 53 | 53 | $caller_function = $callers[1]['function']; |
| 54 | 54 | |
| 55 | - if ( is_null( $handler ) ) { |
|
| 56 | - wl_write_log_handler( $log, $caller_function ); |
|
| 55 | + if (is_null($handler)) { |
|
| 56 | + wl_write_log_handler($log, $caller_function); |
|
| 57 | 57 | |
| 58 | 58 | return; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - call_user_func( $handler, $log, $caller_function ); |
|
| 61 | + call_user_func($handler, $log, $caller_function); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
@@ -69,19 +69,19 @@ discard block |
||
| 69 | 69 | * @param string|array $log The log data. |
| 70 | 70 | * @param string $caller The calling function. |
| 71 | 71 | */ |
| 72 | -function wl_write_log_handler( $log, $caller = null ) { |
|
| 72 | +function wl_write_log_handler($log, $caller = null) { |
|
| 73 | 73 | |
| 74 | 74 | global $wl_logger; |
| 75 | 75 | |
| 76 | - if ( true === WP_DEBUG ) { |
|
| 76 | + if (true === WP_DEBUG) { |
|
| 77 | 77 | |
| 78 | - $message = ( isset( $caller ) ? sprintf( '[%-40.40s] ', $caller ) : '' ) . |
|
| 79 | - ( is_array( $log ) || is_object( $log ) ? print_r( $log, true ) : wl_write_log_hide_key( $log ) ); |
|
| 78 | + $message = (isset($caller) ? sprintf('[%-40.40s] ', $caller) : ''). |
|
| 79 | + (is_array($log) || is_object($log) ? print_r($log, true) : wl_write_log_hide_key($log)); |
|
| 80 | 80 | |
| 81 | - if ( isset( $wl_logger ) ) { |
|
| 82 | - $wl_logger->info( $message ); |
|
| 81 | + if (isset($wl_logger)) { |
|
| 82 | + $wl_logger->info($message); |
|
| 83 | 83 | } else { |
| 84 | - error_log( $message ); |
|
| 84 | + error_log($message); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | } |
@@ -97,9 +97,9 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * @return string A text with the key hidden. |
| 99 | 99 | */ |
| 100 | -function wl_write_log_hide_key( $text ) { |
|
| 100 | +function wl_write_log_hide_key($text) { |
|
| 101 | 101 | |
| 102 | - return str_ireplace( wl_configuration_get_key(), '<hidden>', $text ); |
|
| 102 | + return str_ireplace(wl_configuration_get_key(), '<hidden>', $text); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | /** |
@@ -109,12 +109,12 @@ discard block |
||
| 109 | 109 | * |
| 110 | 110 | * @param string $query A SPARQL query. |
| 111 | 111 | */ |
| 112 | -function wl_queue_sparql_update_query( $query ) { |
|
| 112 | +function wl_queue_sparql_update_query($query) { |
|
| 113 | 113 | |
| 114 | - $filename = WL_TEMP_DIR . WL_REQUEST_ID . '.sparql'; |
|
| 115 | - file_put_contents( $filename, $query . "\n", FILE_APPEND ); |
|
| 114 | + $filename = WL_TEMP_DIR.WL_REQUEST_ID.'.sparql'; |
|
| 115 | + file_put_contents($filename, $query."\n", FILE_APPEND); |
|
| 116 | 116 | |
| 117 | - wl_write_log( "wl_queue_sparql_update_query [ filename :: $filename ]" ); |
|
| 117 | + wl_write_log("wl_queue_sparql_update_query [ filename :: $filename ]"); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -122,33 +122,33 @@ discard block |
||
| 122 | 122 | * |
| 123 | 123 | * @param int $request_id The request ID. |
| 124 | 124 | */ |
| 125 | -function wl_execute_saved_sparql_update_query( $request_id ) { |
|
| 125 | +function wl_execute_saved_sparql_update_query($request_id) { |
|
| 126 | 126 | |
| 127 | - $filename = WL_TEMP_DIR . $request_id . '.sparql'; |
|
| 127 | + $filename = WL_TEMP_DIR.$request_id.'.sparql'; |
|
| 128 | 128 | |
| 129 | 129 | // If the file doesn't exist, exit. |
| 130 | - if ( ! file_exists( $filename ) ) { |
|
| 131 | - wl_write_log( "wl_execute_saved_sparql_update_query : file doesn't exist [ filename :: $filename ]" ); |
|
| 130 | + if ( ! file_exists($filename)) { |
|
| 131 | + wl_write_log("wl_execute_saved_sparql_update_query : file doesn't exist [ filename :: $filename ]"); |
|
| 132 | 132 | |
| 133 | 133 | return; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - wl_write_log( "wl_execute_saved_sparql_update_query [ filename :: $filename ]" ); |
|
| 136 | + wl_write_log("wl_execute_saved_sparql_update_query [ filename :: $filename ]"); |
|
| 137 | 137 | |
| 138 | 138 | // Get the query saved in the file. |
| 139 | - $query = file_get_contents( $filename ); |
|
| 139 | + $query = file_get_contents($filename); |
|
| 140 | 140 | |
| 141 | 141 | // Execute the SPARQL query. |
| 142 | - rl_execute_sparql_update_query( $query, false ); |
|
| 142 | + rl_execute_sparql_update_query($query, false); |
|
| 143 | 143 | |
| 144 | 144 | // Reindex the triple store. |
| 145 | 145 | wordlift_reindex_triple_store(); |
| 146 | 146 | |
| 147 | 147 | // Delete the temporary file. |
| 148 | - unlink( $filename ); |
|
| 148 | + unlink($filename); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | -add_action( 'wl_execute_saved_sparql_update_query', 'wl_execute_saved_sparql_update_query', 10, 1 ); |
|
| 151 | +add_action('wl_execute_saved_sparql_update_query', 'wl_execute_saved_sparql_update_query', 10, 1); |
|
| 152 | 152 | |
| 153 | 153 | /** |
| 154 | 154 | * Add buttons hook for the TinyMCE editor. This method is called by the WP init hook. |
@@ -156,8 +156,8 @@ discard block |
||
| 156 | 156 | function wordlift_buttonhooks() { |
| 157 | 157 | |
| 158 | 158 | // Only add hooks when the current user has permissions AND is in Rich Text editor mode |
| 159 | - if ( ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) && get_user_option( 'rich_editing' ) ) { |
|
| 160 | - add_filter( 'mce_external_plugins', 'wordlift_register_tinymce_javascript' ); |
|
| 159 | + if ((current_user_can('edit_posts') || current_user_can('edit_pages')) && get_user_option('rich_editing')) { |
|
| 160 | + add_filter('mce_external_plugins', 'wordlift_register_tinymce_javascript'); |
|
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | 163 | |
@@ -168,10 +168,10 @@ discard block |
||
| 168 | 168 | * |
| 169 | 169 | * @return array The modified plugins array. |
| 170 | 170 | */ |
| 171 | -function wordlift_register_tinymce_javascript( $plugin_array ) { |
|
| 171 | +function wordlift_register_tinymce_javascript($plugin_array) { |
|
| 172 | 172 | |
| 173 | 173 | // add the wordlift plugin. |
| 174 | - $plugin_array['wordlift'] = plugin_dir_url( __FILE__ ) . 'js/wordlift-reloaded.js'; |
|
| 174 | + $plugin_array['wordlift'] = plugin_dir_url(__FILE__).'js/wordlift-reloaded.js'; |
|
| 175 | 175 | |
| 176 | 176 | return $plugin_array; |
| 177 | 177 | } |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | function wordlift_allowed_post_tags() { |
| 184 | 184 | global $allowedposttags; |
| 185 | 185 | |
| 186 | - $tags = array( 'span' ); |
|
| 186 | + $tags = array('span'); |
|
| 187 | 187 | $new_attributes = array( |
| 188 | 188 | 'itemscope' => array(), |
| 189 | 189 | 'itemtype' => array(), |
@@ -191,17 +191,17 @@ discard block |
||
| 191 | 191 | 'itemid' => array() |
| 192 | 192 | ); |
| 193 | 193 | |
| 194 | - foreach ( $tags as $tag ) { |
|
| 195 | - if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) { |
|
| 196 | - $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes ); |
|
| 194 | + foreach ($tags as $tag) { |
|
| 195 | + if (isset($allowedposttags[$tag]) && is_array($allowedposttags[$tag])) { |
|
| 196 | + $allowedposttags[$tag] = array_merge($allowedposttags[$tag], $new_attributes); |
|
| 197 | 197 | } |
| 198 | 198 | } |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | // init process for button control |
| 202 | -add_action( 'init', 'wordlift_buttonhooks' ); |
|
| 202 | +add_action('init', 'wordlift_buttonhooks'); |
|
| 203 | 203 | // add allowed post tags. |
| 204 | -add_action( 'init', 'wordlift_allowed_post_tags' ); |
|
| 204 | +add_action('init', 'wordlift_allowed_post_tags'); |
|
| 205 | 205 | |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -210,23 +210,23 @@ discard block |
||
| 210 | 210 | function wordlift_admin_enqueue_scripts() { |
| 211 | 211 | |
| 212 | 212 | // Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/) |
| 213 | - wp_enqueue_script( 'wpdialogs' ); |
|
| 214 | - wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
| 213 | + wp_enqueue_script('wpdialogs'); |
|
| 214 | + wp_enqueue_style('wp-jquery-ui-dialog'); |
|
| 215 | 215 | |
| 216 | - wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css' ); |
|
| 216 | + wp_enqueue_style('wordlift-reloaded', plugin_dir_url(__FILE__).'css/wordlift-reloaded.min.css'); |
|
| 217 | 217 | |
| 218 | - wp_enqueue_script( 'jquery-ui-autocomplete' ); |
|
| 219 | - wp_enqueue_script( 'angularjs', plugin_dir_url( __FILE__ ) . 'bower_components/angular/angular.min.js' ); |
|
| 218 | + wp_enqueue_script('jquery-ui-autocomplete'); |
|
| 219 | + wp_enqueue_script('angularjs', plugin_dir_url(__FILE__).'bower_components/angular/angular.min.js'); |
|
| 220 | 220 | |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | -add_action( 'admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts' ); |
|
| 223 | +add_action('admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts'); |
|
| 224 | 224 | |
| 225 | 225 | function wl_enqueue_scripts() { |
| 226 | - wp_enqueue_style( 'wordlift-ui', plugin_dir_url( __FILE__ ) . 'css/wordlift-ui.min.css' ); |
|
| 226 | + wp_enqueue_style('wordlift-ui', plugin_dir_url(__FILE__).'css/wordlift-ui.min.css'); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | -add_action( 'wp_enqueue_scripts', 'wl_enqueue_scripts' ); |
|
| 229 | +add_action('wp_enqueue_scripts', 'wl_enqueue_scripts'); |
|
| 230 | 230 | |
| 231 | 231 | /** |
| 232 | 232 | * Hooked to *wp_kses_allowed_html* filter, adds microdata attributes. |
@@ -236,23 +236,23 @@ discard block |
||
| 236 | 236 | * |
| 237 | 237 | * @return array An array which contains allowed microdata attributes. |
| 238 | 238 | */ |
| 239 | -function wordlift_allowed_html( $allowedtags, $context ) { |
|
| 239 | +function wordlift_allowed_html($allowedtags, $context) { |
|
| 240 | 240 | |
| 241 | - if ( 'post' !== $context ) { |
|
| 241 | + if ('post' !== $context) { |
|
| 242 | 242 | return $allowedtags; |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | - return array_merge_recursive( $allowedtags, array( |
|
| 245 | + return array_merge_recursive($allowedtags, array( |
|
| 246 | 246 | 'span' => array( |
| 247 | 247 | 'itemscope' => true, |
| 248 | 248 | 'itemtype' => true, |
| 249 | 249 | 'itemid' => true, |
| 250 | 250 | 'itemprop' => true |
| 251 | 251 | ) |
| 252 | - ) ); |
|
| 252 | + )); |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | -add_filter( 'wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2 ); |
|
| 255 | +add_filter('wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2); |
|
| 256 | 256 | |
| 257 | 257 | /** |
| 258 | 258 | * Get the coordinates for the specified post ID. |
@@ -261,16 +261,16 @@ discard block |
||
| 261 | 261 | * |
| 262 | 262 | * @return array|null An array of coordinates or null. |
| 263 | 263 | */ |
| 264 | -function wl_get_coordinates( $post_id ) { |
|
| 264 | +function wl_get_coordinates($post_id) { |
|
| 265 | 265 | |
| 266 | - $latitude = wl_schema_get_value( $post_id, 'latitude' ); |
|
| 267 | - $longitude = wl_schema_get_value( $post_id, 'longitude' ); |
|
| 266 | + $latitude = wl_schema_get_value($post_id, 'latitude'); |
|
| 267 | + $longitude = wl_schema_get_value($post_id, 'longitude'); |
|
| 268 | 268 | |
| 269 | 269 | // DO NOT set latitude/longitude to 0/0 as default values. It's a specific place on the globe: |
| 270 | 270 | // "The zero/zero point of this system is located in the Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana." |
| 271 | 271 | return array( |
| 272 | - 'latitude' => isset( $latitude[0] ) && is_numeric( $latitude[0] ) ? $latitude[0] : '', |
|
| 273 | - 'longitude' => isset( $longitude[0] ) && is_numeric( $longitude[0] ) ? $longitude[0] : '' |
|
| 272 | + 'latitude' => isset($latitude[0]) && is_numeric($latitude[0]) ? $latitude[0] : '', |
|
| 273 | + 'longitude' => isset($longitude[0]) && is_numeric($longitude[0]) ? $longitude[0] : '' |
|
| 274 | 274 | ); |
| 275 | 275 | } |
| 276 | 276 | |
@@ -281,12 +281,12 @@ discard block |
||
| 281 | 281 | * |
| 282 | 282 | * @return string A datetime. |
| 283 | 283 | */ |
| 284 | -function wl_get_post_modified_time( $post ) { |
|
| 284 | +function wl_get_post_modified_time($post) { |
|
| 285 | 285 | |
| 286 | - $date_modified = get_post_modified_time( 'c', true, $post ); |
|
| 286 | + $date_modified = get_post_modified_time('c', true, $post); |
|
| 287 | 287 | |
| 288 | - if ( '-' === substr( $date_modified, 0, 1 ) ) { |
|
| 289 | - return get_the_time( 'c', $post ); |
|
| 288 | + if ('-' === substr($date_modified, 0, 1)) { |
|
| 289 | + return get_the_time('c', $post); |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | return $date_modified; |
@@ -299,18 +299,18 @@ discard block |
||
| 299 | 299 | * |
| 300 | 300 | * @return array An array of image URLs. |
| 301 | 301 | */ |
| 302 | -function wl_get_image_urls( $post_id ) { |
|
| 302 | +function wl_get_image_urls($post_id) { |
|
| 303 | 303 | |
| 304 | 304 | // wl_write_log( "wl_get_image_urls [ post id :: $post_id ]" ); |
| 305 | 305 | |
| 306 | - $images = get_children( array( |
|
| 306 | + $images = get_children(array( |
|
| 307 | 307 | 'post_parent' => $post_id, |
| 308 | 308 | 'post_type' => 'attachment', |
| 309 | 309 | 'post_mime_type' => 'image' |
| 310 | - ) ); |
|
| 310 | + )); |
|
| 311 | 311 | |
| 312 | 312 | // Return an empty array if no image is found. |
| 313 | - if ( empty( $images ) ) { |
|
| 313 | + if (empty($images)) { |
|
| 314 | 314 | return array(); |
| 315 | 315 | } |
| 316 | 316 | |
@@ -318,11 +318,11 @@ discard block |
||
| 318 | 318 | $image_urls = array(); |
| 319 | 319 | |
| 320 | 320 | // Collect the URLs. |
| 321 | - foreach ( $images as $attachment_id => $attachment ) { |
|
| 322 | - $image_url = wp_get_attachment_url( $attachment_id ); |
|
| 321 | + foreach ($images as $attachment_id => $attachment) { |
|
| 322 | + $image_url = wp_get_attachment_url($attachment_id); |
|
| 323 | 323 | // Ensure the URL isn't collected already. |
| 324 | - if ( ! in_array( $image_url, $image_urls ) ) { |
|
| 325 | - array_push( $image_urls, $image_url ); |
|
| 324 | + if ( ! in_array($image_url, $image_urls)) { |
|
| 325 | + array_push($image_urls, $image_url); |
|
| 326 | 326 | } |
| 327 | 327 | } |
| 328 | 328 | |
@@ -339,17 +339,17 @@ discard block |
||
| 339 | 339 | * |
| 340 | 340 | * @return string The SPARQL fragment. |
| 341 | 341 | */ |
| 342 | -function wl_get_sparql_images( $uri, $post_id ) { |
|
| 342 | +function wl_get_sparql_images($uri, $post_id) { |
|
| 343 | 343 | |
| 344 | 344 | $sparql = ''; |
| 345 | 345 | |
| 346 | 346 | // Get the escaped URI. |
| 347 | - $uri_e = esc_html( $uri ); |
|
| 347 | + $uri_e = esc_html($uri); |
|
| 348 | 348 | |
| 349 | 349 | // Add SPARQL stmts to write the schema:image. |
| 350 | - $image_urls = wl_get_image_urls( $post_id ); |
|
| 351 | - foreach ( $image_urls as $image_url ) { |
|
| 352 | - $image_url_esc = wl_sparql_escape_uri( $image_url ); |
|
| 350 | + $image_urls = wl_get_image_urls($post_id); |
|
| 351 | + foreach ($image_urls as $image_url) { |
|
| 352 | + $image_url_esc = wl_sparql_escape_uri($image_url); |
|
| 353 | 353 | $sparql .= " <$uri_e> schema:image <$image_url_esc> . \n"; |
| 354 | 354 | } |
| 355 | 355 | |
@@ -364,21 +364,21 @@ discard block |
||
| 364 | 364 | * |
| 365 | 365 | * @return WP_Post|null A post instance or null if not found. |
| 366 | 366 | */ |
| 367 | -function wl_get_attachment_for_source_url( $parent_post_id, $source_url ) { |
|
| 367 | +function wl_get_attachment_for_source_url($parent_post_id, $source_url) { |
|
| 368 | 368 | |
| 369 | 369 | // wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" ); |
| 370 | 370 | |
| 371 | - $posts = get_posts( array( |
|
| 371 | + $posts = get_posts(array( |
|
| 372 | 372 | 'post_type' => 'attachment', |
| 373 | 373 | 'posts_per_page' => 1, |
| 374 | 374 | 'post_status' => 'any', |
| 375 | 375 | 'post_parent' => $parent_post_id, |
| 376 | 376 | 'meta_key' => 'wl_source_url', |
| 377 | 377 | 'meta_value' => $source_url |
| 378 | - ) ); |
|
| 378 | + )); |
|
| 379 | 379 | |
| 380 | 380 | // Return the found post. |
| 381 | - if ( 1 === count( $posts ) ) { |
|
| 381 | + if (1 === count($posts)) { |
|
| 382 | 382 | return $posts[0]; |
| 383 | 383 | } |
| 384 | 384 | |
@@ -392,10 +392,10 @@ discard block |
||
| 392 | 392 | * @param int $post_id The post ID. |
| 393 | 393 | * @param string $source_url The source URL. |
| 394 | 394 | */ |
| 395 | -function wl_set_source_url( $post_id, $source_url ) { |
|
| 395 | +function wl_set_source_url($post_id, $source_url) { |
|
| 396 | 396 | |
| 397 | - delete_post_meta( $post_id, 'wl_source_url' ); |
|
| 398 | - add_post_meta( $post_id, 'wl_source_url', $source_url ); |
|
| 397 | + delete_post_meta($post_id, 'wl_source_url'); |
|
| 398 | + add_post_meta($post_id, 'wl_source_url', $source_url); |
|
| 399 | 399 | } |
| 400 | 400 | |
| 401 | 401 | |
@@ -411,14 +411,14 @@ discard block |
||
| 411 | 411 | * |
| 412 | 412 | * @param bool $hard True if the rewrite involves configuration updates in Apache/IIS. |
| 413 | 413 | */ |
| 414 | -function wl_flush_rewrite_rules_hard( $hard ) { |
|
| 414 | +function wl_flush_rewrite_rules_hard($hard) { |
|
| 415 | 415 | |
| 416 | 416 | // Get all published posts. |
| 417 | - $posts = get_posts( array( |
|
| 418 | - 'posts_per_page' => - 1, |
|
| 417 | + $posts = get_posts(array( |
|
| 418 | + 'posts_per_page' => -1, |
|
| 419 | 419 | 'post_type' => 'any', |
| 420 | 420 | 'post_status' => 'publish' |
| 421 | - ) ); |
|
| 421 | + )); |
|
| 422 | 422 | |
| 423 | 423 | // Holds the delete part of the query. |
| 424 | 424 | $delete_query = rl_sparql_prefixes(); |
@@ -426,18 +426,18 @@ discard block |
||
| 426 | 426 | $insert_query = 'INSERT DATA { '; |
| 427 | 427 | |
| 428 | 428 | // Cycle in each post to build the query. |
| 429 | - foreach ( $posts as $post ) { |
|
| 429 | + foreach ($posts as $post) { |
|
| 430 | 430 | |
| 431 | 431 | // Ignore revisions. |
| 432 | - if ( wp_is_post_revision( $post->ID ) ) { |
|
| 432 | + if (wp_is_post_revision($post->ID)) { |
|
| 433 | 433 | continue; |
| 434 | 434 | } |
| 435 | 435 | |
| 436 | 436 | // Get the entity URI. |
| 437 | - $uri = wl_sparql_escape_uri( wl_get_entity_uri( $post->ID ) ); |
|
| 437 | + $uri = wl_sparql_escape_uri(wl_get_entity_uri($post->ID)); |
|
| 438 | 438 | |
| 439 | 439 | // Get the post URL. |
| 440 | - $url = wl_sparql_escape_uri( get_permalink( $post->ID ) ); |
|
| 440 | + $url = wl_sparql_escape_uri(get_permalink($post->ID)); |
|
| 441 | 441 | |
| 442 | 442 | // Prepare the DELETE and INSERT commands. |
| 443 | 443 | $delete_query .= "DELETE { <$uri> schema:url ?u . } WHERE { <$uri> schema:url ?u . };\n"; |
@@ -449,10 +449,10 @@ discard block |
||
| 449 | 449 | $insert_query .= ' };'; |
| 450 | 450 | |
| 451 | 451 | // Execute the query. |
| 452 | - rl_execute_sparql_update_query( $delete_query . $insert_query ); |
|
| 452 | + rl_execute_sparql_update_query($delete_query.$insert_query); |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | -add_filter( 'flush_rewrite_rules_hard', 'wl_flush_rewrite_rules_hard', 10, 1 ); |
|
| 455 | +add_filter('flush_rewrite_rules_hard', 'wl_flush_rewrite_rules_hard', 10, 1); |
|
| 456 | 456 | |
| 457 | 457 | /** |
| 458 | 458 | * Sanitizes an URI path by replacing the non allowed characters with an underscore. |
@@ -464,7 +464,7 @@ discard block |
||
| 464 | 464 | * |
| 465 | 465 | * @return The sanitized path. |
| 466 | 466 | */ |
| 467 | -function wl_sanitize_uri_path( $path, $char = '_' ) { |
|
| 467 | +function wl_sanitize_uri_path($path, $char = '_') { |
|
| 468 | 468 | |
| 469 | 469 | // wl_write_log( "wl_sanitize_uri_path [ path :: $path ][ char :: $char ]" ); |
| 470 | 470 | |
@@ -474,7 +474,7 @@ discard block |
||
| 474 | 474 | // Plus the ' ' (space). |
| 475 | 475 | // TODO: We shall use the same regex used by MediaWiki (http://stackoverflow.com/questions/23114983/mediawiki-wikipedia-url-sanitization-regex) |
| 476 | 476 | |
| 477 | - return sanitize_title( preg_replace( '/[;\/?:@&=+$,\s]/', $char, stripslashes( $path ) ) ); |
|
| 477 | + return sanitize_title(preg_replace('/[;\/?:@&=+$,\s]/', $char, stripslashes($path))); |
|
| 478 | 478 | } |
| 479 | 479 | |
| 480 | 480 | /** |
@@ -484,10 +484,10 @@ discard block |
||
| 484 | 484 | * |
| 485 | 485 | * @return array Array containing $value (if $value was not an array) |
| 486 | 486 | */ |
| 487 | -function wl_force_to_array( $value ) { |
|
| 487 | +function wl_force_to_array($value) { |
|
| 488 | 488 | |
| 489 | - if ( ! is_array( $value ) ) { |
|
| 490 | - return array( $value ); |
|
| 489 | + if ( ! is_array($value)) { |
|
| 490 | + return array($value); |
|
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | return $value; |
@@ -499,28 +499,28 @@ discard block |
||
| 499 | 499 | function wl_shutdown() { |
| 500 | 500 | |
| 501 | 501 | // Get the filename to the temporary SPARQL file. |
| 502 | - $filename = WL_TEMP_DIR . WL_REQUEST_ID . '.sparql'; |
|
| 502 | + $filename = WL_TEMP_DIR.WL_REQUEST_ID.'.sparql'; |
|
| 503 | 503 | |
| 504 | 504 | // If WordLift is buffering SPARQL queries, we're admins and a buffer exists, then schedule it. |
| 505 | - if ( WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING && is_admin() && file_exists( $filename ) ) { |
|
| 505 | + if (WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING && is_admin() && file_exists($filename)) { |
|
| 506 | 506 | |
| 507 | 507 | // The request ID. |
| 508 | - $args = array( WL_REQUEST_ID ); |
|
| 508 | + $args = array(WL_REQUEST_ID); |
|
| 509 | 509 | |
| 510 | 510 | // Schedule the execution of the SPARQL query with the request ID. |
| 511 | - wp_schedule_single_event( time(), 'wl_execute_saved_sparql_update_query', $args ); |
|
| 511 | + wp_schedule_single_event(time(), 'wl_execute_saved_sparql_update_query', $args); |
|
| 512 | 512 | |
| 513 | 513 | // Check that the request is scheduled. |
| 514 | - $timestamp = wp_next_scheduled( 'wl_execute_saved_sparql_update_query', $args ); |
|
| 514 | + $timestamp = wp_next_scheduled('wl_execute_saved_sparql_update_query', $args); |
|
| 515 | 515 | |
| 516 | 516 | // Spawn the cron. |
| 517 | 517 | spawn_cron(); |
| 518 | 518 | |
| 519 | - wl_write_log( "wl_shutdown [ request id :: " . WL_REQUEST_ID . " ][ timestamp :: $timestamp ]" ); |
|
| 519 | + wl_write_log("wl_shutdown [ request id :: ".WL_REQUEST_ID." ][ timestamp :: $timestamp ]"); |
|
| 520 | 520 | } |
| 521 | 521 | } |
| 522 | 522 | |
| 523 | -add_action( 'shutdown', 'wl_shutdown' ); |
|
| 523 | +add_action('shutdown', 'wl_shutdown'); |
|
| 524 | 524 | |
| 525 | 525 | /** |
| 526 | 526 | * Replaces the *itemid* attributes URIs with the WordLift URIs. |
@@ -529,118 +529,118 @@ discard block |
||
| 529 | 529 | * |
| 530 | 530 | * @return string The updated post content. |
| 531 | 531 | */ |
| 532 | -function wl_replace_item_id_with_uri( $content ) { |
|
| 532 | +function wl_replace_item_id_with_uri($content) { |
|
| 533 | 533 | |
| 534 | 534 | // wl_write_log( "wl_replace_item_id_with_uri" ); |
| 535 | 535 | |
| 536 | 536 | // Strip slashes, see https://core.trac.wordpress.org/ticket/21767 |
| 537 | - $content = stripslashes( $content ); |
|
| 537 | + $content = stripslashes($content); |
|
| 538 | 538 | |
| 539 | 539 | // If any match are found. |
| 540 | 540 | $matches = array(); |
| 541 | - if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) { |
|
| 541 | + if (0 < preg_match_all('/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER)) { |
|
| 542 | 542 | |
| 543 | - foreach ( $matches as $match ) { |
|
| 543 | + foreach ($matches as $match) { |
|
| 544 | 544 | |
| 545 | 545 | // Get the item ID. |
| 546 | 546 | $item_id = $match[1]; |
| 547 | 547 | |
| 548 | 548 | // Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' . |
| 549 | - $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $item_id ); |
|
| 549 | + $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($item_id); |
|
| 550 | 550 | |
| 551 | 551 | // If no entity is found, continue to the next one. |
| 552 | - if ( null === $post ) { |
|
| 552 | + if (null === $post) { |
|
| 553 | 553 | continue; |
| 554 | 554 | } |
| 555 | 555 | |
| 556 | 556 | // Get the URI for that post. |
| 557 | - $uri = wl_get_entity_uri( $post->ID ); |
|
| 557 | + $uri = wl_get_entity_uri($post->ID); |
|
| 558 | 558 | |
| 559 | 559 | // wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" ); |
| 560 | 560 | |
| 561 | 561 | // If the item ID and the URI differ, replace the item ID with the URI saved in WordPress. |
| 562 | - if ( $item_id !== $uri ) { |
|
| 563 | - $uri_e = esc_html( $uri ); |
|
| 564 | - $content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content ); |
|
| 562 | + if ($item_id !== $uri) { |
|
| 563 | + $uri_e = esc_html($uri); |
|
| 564 | + $content = str_replace(" itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content); |
|
| 565 | 565 | } |
| 566 | 566 | } |
| 567 | 567 | } |
| 568 | 568 | |
| 569 | 569 | // Reapply slashes. |
| 570 | - $content = addslashes( $content ); |
|
| 570 | + $content = addslashes($content); |
|
| 571 | 571 | |
| 572 | 572 | return $content; |
| 573 | 573 | } |
| 574 | 574 | |
| 575 | -add_filter( 'content_save_pre', 'wl_replace_item_id_with_uri', 1, 1 ); |
|
| 575 | +add_filter('content_save_pre', 'wl_replace_item_id_with_uri', 1, 1); |
|
| 576 | 576 | |
| 577 | -require_once( 'wordlift_entity_functions.php' ); |
|
| 577 | +require_once('wordlift_entity_functions.php'); |
|
| 578 | 578 | |
| 579 | 579 | // add editor related methods. |
| 580 | -require_once( 'wordlift_editor.php' ); |
|
| 580 | +require_once('wordlift_editor.php'); |
|
| 581 | 581 | |
| 582 | 582 | // add the WordLift entity custom type. |
| 583 | -require_once( 'wordlift_entity_type.php' ); |
|
| 584 | -require_once( 'wordlift_entity_type_taxonomy.php' ); |
|
| 583 | +require_once('wordlift_entity_type.php'); |
|
| 584 | +require_once('wordlift_entity_type_taxonomy.php'); |
|
| 585 | 585 | |
| 586 | 586 | // filters the post content when saving posts. |
| 587 | -require_once( 'wordlift_content_filter.php' ); |
|
| 587 | +require_once('wordlift_content_filter.php'); |
|
| 588 | 588 | // add callbacks on post save to notify data changes from wp to redlink triple store |
| 589 | -require_once( 'wordlift_to_redlink_data_push_callbacks.php' ); |
|
| 589 | +require_once('wordlift_to_redlink_data_push_callbacks.php'); |
|
| 590 | 590 | |
| 591 | 591 | // Load modules |
| 592 | -require_once( 'modules/analyzer/wordlift_analyzer.php' ); |
|
| 593 | -require_once( 'modules/linked_data/wordlift_linked_data.php' ); |
|
| 594 | -require_once( 'modules/prefixes/wordlift_prefixes.php' ); |
|
| 595 | -require_once( 'modules/caching/wordlift_caching.php' ); |
|
| 596 | -require_once( 'modules/redirector/wordlift_redirector.php' ); |
|
| 597 | -require_once( 'modules/freebase_image_proxy/wordlift_freebase_image_proxy.php' ); |
|
| 592 | +require_once('modules/analyzer/wordlift_analyzer.php'); |
|
| 593 | +require_once('modules/linked_data/wordlift_linked_data.php'); |
|
| 594 | +require_once('modules/prefixes/wordlift_prefixes.php'); |
|
| 595 | +require_once('modules/caching/wordlift_caching.php'); |
|
| 596 | +require_once('modules/redirector/wordlift_redirector.php'); |
|
| 597 | +require_once('modules/freebase_image_proxy/wordlift_freebase_image_proxy.php'); |
|
| 598 | 598 | |
| 599 | 599 | // Shortcodes |
| 600 | 600 | |
| 601 | -require_once( 'modules/geo_widget/wordlift_geo_widget.php' ); |
|
| 602 | -require_once( 'shortcodes/wordlift_shortcode_chord.php' ); |
|
| 603 | -require_once( 'shortcodes/wordlift_shortcode_geomap.php' ); |
|
| 604 | -require_once( 'shortcodes/wordlift_shortcode_field.php' ); |
|
| 605 | -require_once( 'shortcodes/wordlift_shortcode_faceted_search.php' ); |
|
| 606 | -require_once( 'shortcodes/wordlift_shortcode_navigator.php' ); |
|
| 607 | -require_once( 'shortcodes/wordlift_shortcode_blog_map.php' ); |
|
| 601 | +require_once('modules/geo_widget/wordlift_geo_widget.php'); |
|
| 602 | +require_once('shortcodes/wordlift_shortcode_chord.php'); |
|
| 603 | +require_once('shortcodes/wordlift_shortcode_geomap.php'); |
|
| 604 | +require_once('shortcodes/wordlift_shortcode_field.php'); |
|
| 605 | +require_once('shortcodes/wordlift_shortcode_faceted_search.php'); |
|
| 606 | +require_once('shortcodes/wordlift_shortcode_navigator.php'); |
|
| 607 | +require_once('shortcodes/wordlift_shortcode_blog_map.php'); |
|
| 608 | 608 | |
| 609 | 609 | // disable In-Depth Articles |
| 610 | 610 | //require_once('wordlift_indepth_articles.php'); |
| 611 | 611 | |
| 612 | -require_once( 'widgets/wordlift_widget_geo.php' ); |
|
| 613 | -require_once( 'widgets/wordlift_widget_chord.php' ); |
|
| 614 | -require_once( 'widgets/wordlift_widget_timeline.php' ); |
|
| 612 | +require_once('widgets/wordlift_widget_geo.php'); |
|
| 613 | +require_once('widgets/wordlift_widget_chord.php'); |
|
| 614 | +require_once('widgets/wordlift_widget_timeline.php'); |
|
| 615 | 615 | |
| 616 | -require_once( 'wordlift_sparql.php' ); |
|
| 617 | -require_once( 'wordlift_redlink.php' ); |
|
| 616 | +require_once('wordlift_sparql.php'); |
|
| 617 | +require_once('wordlift_redlink.php'); |
|
| 618 | 618 | |
| 619 | 619 | // Add admin functions. |
| 620 | 620 | // TODO: find a way to make 'admin' UI tests work. |
| 621 | 621 | //if ( is_admin() ) { |
| 622 | 622 | |
| 623 | -require_once( 'admin/wordlift_admin.php' ); |
|
| 624 | -require_once( 'admin/wordlift_admin_edit_post.php' ); |
|
| 625 | -require_once( 'admin/wordlift_admin_save_post.php' ); |
|
| 623 | +require_once('admin/wordlift_admin.php'); |
|
| 624 | +require_once('admin/wordlift_admin_edit_post.php'); |
|
| 625 | +require_once('admin/wordlift_admin_save_post.php'); |
|
| 626 | 626 | |
| 627 | 627 | // add the entities meta box. |
| 628 | -require_once( 'admin/wordlift_admin_meta_box_entities.php' ); |
|
| 628 | +require_once('admin/wordlift_admin_meta_box_entities.php'); |
|
| 629 | 629 | |
| 630 | 630 | // add the entity creation AJAX. |
| 631 | -require_once( 'admin/wordlift_admin_ajax_related_posts.php' ); |
|
| 631 | +require_once('admin/wordlift_admin_ajax_related_posts.php'); |
|
| 632 | 632 | |
| 633 | 633 | // Load the wl_chord TinyMCE button and configuration dialog. |
| 634 | -require_once( 'admin/wordlift_admin_shortcodes.php' ); |
|
| 634 | +require_once('admin/wordlift_admin_shortcodes.php'); |
|
| 635 | 635 | |
| 636 | 636 | // Provide syncing features. |
| 637 | -require_once( 'admin/wordlift_admin_sync.php' ); |
|
| 637 | +require_once('admin/wordlift_admin_sync.php'); |
|
| 638 | 638 | //} |
| 639 | 639 | |
| 640 | 640 | // load languages. |
| 641 | 641 | // TODO: the following call gives for granted that the plugin is in the wordlift directory, |
| 642 | 642 | // we're currently doing this because wordlift is symbolic linked. |
| 643 | -load_plugin_textdomain( 'wordlift', false, '/wordlift/languages' ); |
|
| 643 | +load_plugin_textdomain('wordlift', false, '/wordlift/languages'); |
|
| 644 | 644 | |
| 645 | 645 | |
| 646 | 646 | /** |
@@ -648,7 +648,7 @@ discard block |
||
| 648 | 648 | * This action is documented in includes/class-wordlift-activator.php |
| 649 | 649 | */ |
| 650 | 650 | function activate_wordlift() { |
| 651 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php'; |
|
| 651 | + require_once plugin_dir_path(__FILE__).'includes/class-wordlift-activator.php'; |
|
| 652 | 652 | Wordlift_Activator::activate(); |
| 653 | 653 | } |
| 654 | 654 | |
@@ -657,18 +657,18 @@ discard block |
||
| 657 | 657 | * This action is documented in includes/class-wordlift-deactivator.php |
| 658 | 658 | */ |
| 659 | 659 | function deactivate_wordlift() { |
| 660 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php'; |
|
| 660 | + require_once plugin_dir_path(__FILE__).'includes/class-wordlift-deactivator.php'; |
|
| 661 | 661 | Wordlift_Deactivator::deactivate(); |
| 662 | 662 | } |
| 663 | 663 | |
| 664 | -register_activation_hook( __FILE__, 'activate_wordlift' ); |
|
| 665 | -register_deactivation_hook( __FILE__, 'deactivate_wordlift' ); |
|
| 664 | +register_activation_hook(__FILE__, 'activate_wordlift'); |
|
| 665 | +register_deactivation_hook(__FILE__, 'deactivate_wordlift'); |
|
| 666 | 666 | |
| 667 | 667 | /** |
| 668 | 668 | * The core plugin class that is used to define internationalization, |
| 669 | 669 | * admin-specific hooks, and public-facing site hooks. |
| 670 | 670 | */ |
| 671 | -require plugin_dir_path( __FILE__ ) . 'includes/class-wordlift.php'; |
|
| 671 | +require plugin_dir_path(__FILE__).'includes/class-wordlift.php'; |
|
| 672 | 672 | |
| 673 | 673 | /** |
| 674 | 674 | * Begins execution of the plugin. |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | * @param string $uri Uri of the entity to search in the post content. |
| 11 | 11 | */ |
| 12 | 12 | function wl_content_embed_build_regex_from_uri( $uri ) { |
| 13 | - return '|<(\\w+)[^<]* itemid=\"' . esc_attr( $uri ) . '\"[^>]*>([^<]*)<\\/\\1>|i'; |
|
| 13 | + return '|<(\\w+)[^<]* itemid=\"' . esc_attr( $uri ) . '\"[^>]*>([^<]*)<\\/\\1>|i'; |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | /** |
@@ -22,8 +22,8 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | function wl_content_embed_microdata( $content ) { |
| 24 | 24 | |
| 25 | - // Apply microdata only to single pages. |
|
| 26 | - /*if ( ! is_single() ) { |
|
| 25 | + // Apply microdata only to single pages. |
|
| 26 | + /*if ( ! is_single() ) { |
|
| 27 | 27 | wl_write_log( "wl_content_embed_microdata : is not single" ); |
| 28 | 28 | |
| 29 | 29 | return $content; |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | global $post; |
| 33 | 33 | */ |
| 34 | - return _wl_content_embed_microdata( get_the_ID(), $content ); |
|
| 34 | + return _wl_content_embed_microdata( get_the_ID(), $content ); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -44,31 +44,31 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | function _wl_content_embed_microdata( $post_id, $content ) { |
| 46 | 46 | |
| 47 | - // If it is an entity, add its own microdata to the content. |
|
| 48 | - if ( get_post_type( $post_id ) == Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 49 | - $own_uri = wl_get_entity_uri( $post_id ); |
|
| 50 | - $content .= '<span itemid="' . $own_uri . '"></span>'; |
|
| 51 | - } |
|
| 47 | + // If it is an entity, add its own microdata to the content. |
|
| 48 | + if ( get_post_type( $post_id ) == Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 49 | + $own_uri = wl_get_entity_uri( $post_id ); |
|
| 50 | + $content .= '<span itemid="' . $own_uri . '"></span>'; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - // Now search in the text entity mentions |
|
| 54 | - $regex = '/<(\\w+)[^<]* itemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 55 | - $matches = array(); |
|
| 53 | + // Now search in the text entity mentions |
|
| 54 | + $regex = '/<(\\w+)[^<]* itemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 55 | + $matches = array(); |
|
| 56 | 56 | |
| 57 | - // Return the content if not item IDs have been found. |
|
| 58 | - if ( false === preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { |
|
| 59 | - return $content; |
|
| 60 | - } |
|
| 57 | + // Return the content if not item IDs have been found. |
|
| 58 | + if ( false === preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { |
|
| 59 | + return $content; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - // TODO: Retrieve here just one time entities type structure to avoid multiple queries. |
|
| 63 | - foreach ( $matches as $match ) { |
|
| 64 | - $item_id = $match[2]; |
|
| 62 | + // TODO: Retrieve here just one time entities type structure to avoid multiple queries. |
|
| 63 | + foreach ( $matches as $match ) { |
|
| 64 | + $item_id = $match[2]; |
|
| 65 | 65 | |
| 66 | - // wl_write_log( "_wl_content_embed_microdata [ item ID :: $item_id ]" ); |
|
| 66 | + // wl_write_log( "_wl_content_embed_microdata [ item ID :: $item_id ]" ); |
|
| 67 | 67 | |
| 68 | - $content = wl_content_embed_item_microdata( $content, $item_id ); |
|
| 69 | - } |
|
| 68 | + $content = wl_content_embed_item_microdata( $content, $item_id ); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - return $content; |
|
| 71 | + return $content; |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -82,68 +82,68 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | function wl_content_embed_item_microdata( $content, $uri, $itemprop = null, $recursion_level = 0 ) { |
| 84 | 84 | |
| 85 | - if ( $recursion_level > wl_config_get_recursion_depth() ) { |
|
| 86 | - wl_write_log( "recursion depth limit exceeded [ level :: $recursion_level ][ max :: " . wl_config_get_recursion_depth() . " ]" ); |
|
| 85 | + if ( $recursion_level > wl_config_get_recursion_depth() ) { |
|
| 86 | + wl_write_log( "recursion depth limit exceeded [ level :: $recursion_level ][ max :: " . wl_config_get_recursion_depth() . " ]" ); |
|
| 87 | 87 | |
| 88 | - return ''; |
|
| 89 | - } |
|
| 88 | + return ''; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 91 | + $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 92 | 92 | |
| 93 | - // Entity not found or not published. Delete <span> tags but leave their content on page. |
|
| 94 | - if ( null === $post || $post->post_status !== 'publish' ) { |
|
| 93 | + // Entity not found or not published. Delete <span> tags but leave their content on page. |
|
| 94 | + if ( null === $post || $post->post_status !== 'publish' ) { |
|
| 95 | 95 | |
| 96 | - // wl_write_log( "wl_content_embed_item_microdata : entity not found or not published [ uri :: $uri ]" ); |
|
| 96 | + // wl_write_log( "wl_content_embed_item_microdata : entity not found or not published [ uri :: $uri ]" ); |
|
| 97 | 97 | |
| 98 | - // Replace the original tagging with the new tagging. |
|
| 99 | - $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 100 | - $content = preg_replace( $regex, '$2', $content ); |
|
| 98 | + // Replace the original tagging with the new tagging. |
|
| 99 | + $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 100 | + $content = preg_replace( $regex, '$2', $content ); |
|
| 101 | 101 | |
| 102 | - return $content; |
|
| 103 | - } |
|
| 102 | + return $content; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - // Get the entity URI and its escaped version for the regex. |
|
| 106 | - $entity_uri = wl_get_entity_uri( $post->ID ); |
|
| 107 | - // Get the main type. |
|
| 108 | - $main_type = wl_entity_type_taxonomy_get_type( $post->ID ); |
|
| 105 | + // Get the entity URI and its escaped version for the regex. |
|
| 106 | + $entity_uri = wl_get_entity_uri( $post->ID ); |
|
| 107 | + // Get the main type. |
|
| 108 | + $main_type = wl_entity_type_taxonomy_get_type( $post->ID ); |
|
| 109 | 109 | |
| 110 | - // Set the item type if available. |
|
| 111 | - $item_type = ( null === $main_type ? '' : ' itemtype="' . esc_attr( $main_type['uri'] ) . '"' ); |
|
| 110 | + // Set the item type if available. |
|
| 111 | + $item_type = ( null === $main_type ? '' : ' itemtype="' . esc_attr( $main_type['uri'] ) . '"' ); |
|
| 112 | 112 | |
| 113 | - // Define attribute itemprop if this entity is nested. |
|
| 114 | - if ( ! is_null( $itemprop ) ) { |
|
| 115 | - $itemprop = ' itemprop="' . $itemprop . '"'; |
|
| 116 | - } |
|
| 113 | + // Define attribute itemprop if this entity is nested. |
|
| 114 | + if ( ! is_null( $itemprop ) ) { |
|
| 115 | + $itemprop = ' itemprop="' . $itemprop . '"'; |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - // Get additional properties (this may imply a recursion of this method on a sub-entity). |
|
| 119 | - $additional_properties = wl_content_embed_compile_microdata_template( $post->ID, $main_type, $recursion_level ); |
|
| 118 | + // Get additional properties (this may imply a recursion of this method on a sub-entity). |
|
| 119 | + $additional_properties = wl_content_embed_compile_microdata_template( $post->ID, $main_type, $recursion_level ); |
|
| 120 | 120 | |
| 121 | - $same_as = ''; |
|
| 122 | - // Get the array of sameAs uris. |
|
| 123 | - $same_as_uris = wl_schema_get_value( $post->ID, 'sameAs' ); |
|
| 124 | - // Prepare the sameAs fragment. |
|
| 125 | - foreach ( $same_as_uris as $same_as_uri ) { |
|
| 126 | - $same_as .= "<link itemprop=\"sameAs\" href=\"$same_as_uri\">"; |
|
| 127 | - } |
|
| 121 | + $same_as = ''; |
|
| 122 | + // Get the array of sameAs uris. |
|
| 123 | + $same_as_uris = wl_schema_get_value( $post->ID, 'sameAs' ); |
|
| 124 | + // Prepare the sameAs fragment. |
|
| 125 | + foreach ( $same_as_uris as $same_as_uri ) { |
|
| 126 | + $same_as .= "<link itemprop=\"sameAs\" href=\"$same_as_uri\">"; |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - // Get the entity URL. |
|
| 130 | - $permalink = get_permalink( $post->ID ); |
|
| 131 | - $url = '<link itemprop="url" href="' . $permalink . '" />'; |
|
| 129 | + // Get the entity URL. |
|
| 130 | + $permalink = get_permalink( $post->ID ); |
|
| 131 | + $url = '<link itemprop="url" href="' . $permalink . '" />'; |
|
| 132 | 132 | |
| 133 | - // Replace the original tagging with the new tagging. |
|
| 134 | - $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 135 | - $content = preg_replace( $regex, |
|
| 136 | - '<$1' . $itemprop . ' itemscope' . $item_type . ' itemid="' . esc_attr( $entity_uri ) . '">' |
|
| 137 | - . $same_as |
|
| 138 | - . $additional_properties |
|
| 139 | - . $url |
|
| 140 | - . '<a class="wl-entity-page-link" href="' . $permalink . '" itemprop="name" content="' . $post->post_title . '">' . ( is_null( $itemprop ) ? '$2' : '' ) . '</a></$1>', //Only print name inside <span> for top-level entities |
|
| 141 | - $content |
|
| 142 | - ); |
|
| 133 | + // Replace the original tagging with the new tagging. |
|
| 134 | + $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 135 | + $content = preg_replace( $regex, |
|
| 136 | + '<$1' . $itemprop . ' itemscope' . $item_type . ' itemid="' . esc_attr( $entity_uri ) . '">' |
|
| 137 | + . $same_as |
|
| 138 | + . $additional_properties |
|
| 139 | + . $url |
|
| 140 | + . '<a class="wl-entity-page-link" href="' . $permalink . '" itemprop="name" content="' . $post->post_title . '">' . ( is_null( $itemprop ) ? '$2' : '' ) . '</a></$1>', //Only print name inside <span> for top-level entities |
|
| 141 | + $content |
|
| 142 | + ); |
|
| 143 | 143 | |
| 144 | - // wl_write_log( "wl_content_embed_item_microdata [ uri :: $uri ][ regex :: $regex ]" ); |
|
| 144 | + // wl_write_log( "wl_content_embed_item_microdata [ uri :: $uri ][ regex :: $regex ]" ); |
|
| 145 | 145 | |
| 146 | - return $content; |
|
| 146 | + return $content; |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | add_filter( 'the_content', 'wl_content_embed_microdata' ); |
@@ -158,93 +158,93 @@ discard block |
||
| 158 | 158 | * @return string The content with embedded microdata. |
| 159 | 159 | */ |
| 160 | 160 | function wl_content_embed_compile_microdata_template( $entity_id, $entity_type, $recursion_level = 0 ) { |
| 161 | - global $wl_logger; |
|
| 162 | - |
|
| 163 | - if ( WP_DEBUG ) { |
|
| 164 | - $wl_logger->trace( "Embedding microdata [ entity id :: $entity_id ][ entity type :: " . var_export( $entity_type, true ) . " ][ recursion level :: $recursion_level ]" ); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $regex = '/{{(.*?)}}/'; |
|
| 168 | - $matches = array(); |
|
| 169 | - |
|
| 170 | - if ( null === $entity_type ) { |
|
| 171 | - return ''; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - $template = $entity_type['microdata_template']; |
|
| 175 | - // Return empty string if template fields have not been found. |
|
| 176 | - if ( false === preg_match_all( $regex, $template, $matches, PREG_SET_ORDER ) ) { |
|
| 177 | - return ''; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - foreach ( $matches as $match ) { |
|
| 181 | - |
|
| 182 | - $placeholder = $match[0]; |
|
| 183 | - $field_name = $match[1]; |
|
| 184 | - |
|
| 185 | - // Get property value. |
|
| 186 | - $meta_collection = wl_schema_get_value( $entity_id, $field_name ); |
|
| 187 | - |
|
| 188 | - // If no value is given, just remove the placeholder from the template |
|
| 189 | - if ( null == $meta_collection ) { |
|
| 190 | - $template = str_replace( $placeholder, '', $template ); |
|
| 191 | - continue; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - // What kind of value is it? |
|
| 195 | - // TODO: Performance issue here: meta type retrieving should be centralized |
|
| 196 | - $expected_type = wl_get_meta_type( $field_name ); |
|
| 197 | - |
|
| 198 | - if ( WP_DEBUG ) { |
|
| 199 | - $wl_logger->trace( "Embedding microdata [ placeholder :: $placeholder ][ field name :: $field_name ][ meta collection :: " . ( is_array( $meta_collection ) ? var_export( $meta_collection, true ) : $meta_collection ) . " ][ expected type :: $expected_type ]" ); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - foreach ( $meta_collection as $field_value ) { |
|
| 203 | - |
|
| 204 | - // Quick and dirty patch for #163: |
|
| 205 | - // - only apply to URIs, i.e. to properties pointing to another post ( $field_value should be a post ID ), |
|
| 206 | - // - check that $field_value is actually a number, |
|
| 207 | - // - check that the referenced post is published. |
|
| 208 | - // OR |
|
| 209 | - // - if the value is empty then we don't display it. |
|
| 210 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI === $expected_type && is_numeric( $field_value ) && 'publish' !== ( $post_status = get_post_status( $field_value ) ) |
|
| 211 | - || empty( $field_value ) |
|
| 212 | - ) { |
|
| 213 | - |
|
| 214 | - if ( WP_DEBUG ) { |
|
| 215 | - $wl_logger->trace( "Microdata refers to a non-published post [ field value :: $field_value ][ post status :: $post_status ]" ); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - // Remove the placeholder. |
|
| 219 | - $template = str_replace( $placeholder, '', $template ); |
|
| 220 | - continue; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI == $expected_type ) { |
|
| 224 | - // If is a numeric value we assume it is an ID referencing for an internal entity. |
|
| 225 | - if ( is_numeric( $field_value ) ) { |
|
| 226 | - // Found id, get uri. |
|
| 227 | - $field_value = wl_get_entity_uri( $field_value ); |
|
| 228 | - } |
|
| 229 | - // Just if the linked entity does exist I can go further with template compiling |
|
| 230 | - $nested_entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $field_value ); |
|
| 231 | - if ( ! is_null( $nested_entity ) ) { |
|
| 232 | - $content = '<span itemid="' . esc_attr( $field_value ) . '">' . $nested_entity->post_title . '</span>'; |
|
| 233 | - $compiled_template = wl_content_embed_item_microdata( $content, $field_value, $field_name, ++ $recursion_level ); |
|
| 234 | - $template = str_replace( $placeholder, $compiled_template, $template ); |
|
| 235 | - } else { |
|
| 236 | - $template = str_replace( $placeholder, '', $template ); |
|
| 237 | - } |
|
| 238 | - continue; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - // Standard condition: field containing a raw value |
|
| 242 | - // For non visible test, schema.org dictates to use the *meta* tag. |
|
| 243 | - // see http://schema.org/docs/gs.html#advanced_missing |
|
| 244 | - $value = '<meta itemprop="' . esc_attr( $field_name ) . '" content="' . esc_attr( $field_value ) . '" />'; |
|
| 245 | - $template = str_replace( $placeholder, $value, $template ); |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - return $template; |
|
| 161 | + global $wl_logger; |
|
| 162 | + |
|
| 163 | + if ( WP_DEBUG ) { |
|
| 164 | + $wl_logger->trace( "Embedding microdata [ entity id :: $entity_id ][ entity type :: " . var_export( $entity_type, true ) . " ][ recursion level :: $recursion_level ]" ); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $regex = '/{{(.*?)}}/'; |
|
| 168 | + $matches = array(); |
|
| 169 | + |
|
| 170 | + if ( null === $entity_type ) { |
|
| 171 | + return ''; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + $template = $entity_type['microdata_template']; |
|
| 175 | + // Return empty string if template fields have not been found. |
|
| 176 | + if ( false === preg_match_all( $regex, $template, $matches, PREG_SET_ORDER ) ) { |
|
| 177 | + return ''; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + foreach ( $matches as $match ) { |
|
| 181 | + |
|
| 182 | + $placeholder = $match[0]; |
|
| 183 | + $field_name = $match[1]; |
|
| 184 | + |
|
| 185 | + // Get property value. |
|
| 186 | + $meta_collection = wl_schema_get_value( $entity_id, $field_name ); |
|
| 187 | + |
|
| 188 | + // If no value is given, just remove the placeholder from the template |
|
| 189 | + if ( null == $meta_collection ) { |
|
| 190 | + $template = str_replace( $placeholder, '', $template ); |
|
| 191 | + continue; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + // What kind of value is it? |
|
| 195 | + // TODO: Performance issue here: meta type retrieving should be centralized |
|
| 196 | + $expected_type = wl_get_meta_type( $field_name ); |
|
| 197 | + |
|
| 198 | + if ( WP_DEBUG ) { |
|
| 199 | + $wl_logger->trace( "Embedding microdata [ placeholder :: $placeholder ][ field name :: $field_name ][ meta collection :: " . ( is_array( $meta_collection ) ? var_export( $meta_collection, true ) : $meta_collection ) . " ][ expected type :: $expected_type ]" ); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + foreach ( $meta_collection as $field_value ) { |
|
| 203 | + |
|
| 204 | + // Quick and dirty patch for #163: |
|
| 205 | + // - only apply to URIs, i.e. to properties pointing to another post ( $field_value should be a post ID ), |
|
| 206 | + // - check that $field_value is actually a number, |
|
| 207 | + // - check that the referenced post is published. |
|
| 208 | + // OR |
|
| 209 | + // - if the value is empty then we don't display it. |
|
| 210 | + if ( Wordlift_Schema_Service::DATA_TYPE_URI === $expected_type && is_numeric( $field_value ) && 'publish' !== ( $post_status = get_post_status( $field_value ) ) |
|
| 211 | + || empty( $field_value ) |
|
| 212 | + ) { |
|
| 213 | + |
|
| 214 | + if ( WP_DEBUG ) { |
|
| 215 | + $wl_logger->trace( "Microdata refers to a non-published post [ field value :: $field_value ][ post status :: $post_status ]" ); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + // Remove the placeholder. |
|
| 219 | + $template = str_replace( $placeholder, '', $template ); |
|
| 220 | + continue; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + if ( Wordlift_Schema_Service::DATA_TYPE_URI == $expected_type ) { |
|
| 224 | + // If is a numeric value we assume it is an ID referencing for an internal entity. |
|
| 225 | + if ( is_numeric( $field_value ) ) { |
|
| 226 | + // Found id, get uri. |
|
| 227 | + $field_value = wl_get_entity_uri( $field_value ); |
|
| 228 | + } |
|
| 229 | + // Just if the linked entity does exist I can go further with template compiling |
|
| 230 | + $nested_entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $field_value ); |
|
| 231 | + if ( ! is_null( $nested_entity ) ) { |
|
| 232 | + $content = '<span itemid="' . esc_attr( $field_value ) . '">' . $nested_entity->post_title . '</span>'; |
|
| 233 | + $compiled_template = wl_content_embed_item_microdata( $content, $field_value, $field_name, ++ $recursion_level ); |
|
| 234 | + $template = str_replace( $placeholder, $compiled_template, $template ); |
|
| 235 | + } else { |
|
| 236 | + $template = str_replace( $placeholder, '', $template ); |
|
| 237 | + } |
|
| 238 | + continue; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + // Standard condition: field containing a raw value |
|
| 242 | + // For non visible test, schema.org dictates to use the *meta* tag. |
|
| 243 | + // see http://schema.org/docs/gs.html#advanced_missing |
|
| 244 | + $value = '<meta itemprop="' . esc_attr( $field_name ) . '" content="' . esc_attr( $field_value ) . '" />'; |
|
| 245 | + $template = str_replace( $placeholder, $value, $template ); |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + return $template; |
|
| 250 | 250 | } |
| 251 | 251 | \ No newline at end of file |
@@ -9,8 +9,8 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @param string $uri Uri of the entity to search in the post content. |
| 11 | 11 | */ |
| 12 | -function wl_content_embed_build_regex_from_uri( $uri ) { |
|
| 13 | - return '|<(\\w+)[^<]* itemid=\"' . esc_attr( $uri ) . '\"[^>]*>([^<]*)<\\/\\1>|i'; |
|
| 12 | +function wl_content_embed_build_regex_from_uri($uri) { |
|
| 13 | + return '|<(\\w+)[^<]* itemid=\"'.esc_attr($uri).'\"[^>]*>([^<]*)<\\/\\1>|i'; |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | /** |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | * |
| 21 | 21 | * @return string The updated post content. |
| 22 | 22 | */ |
| 23 | -function wl_content_embed_microdata( $content ) { |
|
| 23 | +function wl_content_embed_microdata($content) { |
|
| 24 | 24 | |
| 25 | 25 | // Apply microdata only to single pages. |
| 26 | 26 | /*if ( ! is_single() ) { |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | global $post; |
| 33 | 33 | */ |
| 34 | - return _wl_content_embed_microdata( get_the_ID(), $content ); |
|
| 34 | + return _wl_content_embed_microdata(get_the_ID(), $content); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -42,12 +42,12 @@ discard block |
||
| 42 | 42 | * |
| 43 | 43 | * @return string The updated post content. |
| 44 | 44 | */ |
| 45 | -function _wl_content_embed_microdata( $post_id, $content ) { |
|
| 45 | +function _wl_content_embed_microdata($post_id, $content) { |
|
| 46 | 46 | |
| 47 | 47 | // If it is an entity, add its own microdata to the content. |
| 48 | - if ( get_post_type( $post_id ) == Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 49 | - $own_uri = wl_get_entity_uri( $post_id ); |
|
| 50 | - $content .= '<span itemid="' . $own_uri . '"></span>'; |
|
| 48 | + if (get_post_type($post_id) == Wordlift_Entity_Service::TYPE_NAME) { |
|
| 49 | + $own_uri = wl_get_entity_uri($post_id); |
|
| 50 | + $content .= '<span itemid="'.$own_uri.'"></span>'; |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // Now search in the text entity mentions |
@@ -55,17 +55,17 @@ discard block |
||
| 55 | 55 | $matches = array(); |
| 56 | 56 | |
| 57 | 57 | // Return the content if not item IDs have been found. |
| 58 | - if ( false === preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { |
|
| 58 | + if (false === preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) { |
|
| 59 | 59 | return $content; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | // TODO: Retrieve here just one time entities type structure to avoid multiple queries. |
| 63 | - foreach ( $matches as $match ) { |
|
| 63 | + foreach ($matches as $match) { |
|
| 64 | 64 | $item_id = $match[2]; |
| 65 | 65 | |
| 66 | 66 | // wl_write_log( "_wl_content_embed_microdata [ item ID :: $item_id ]" ); |
| 67 | 67 | |
| 68 | - $content = wl_content_embed_item_microdata( $content, $item_id ); |
|
| 68 | + $content = wl_content_embed_item_microdata($content, $item_id); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | return $content; |
@@ -80,64 +80,64 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @return string The content with embedded microdata. |
| 82 | 82 | */ |
| 83 | -function wl_content_embed_item_microdata( $content, $uri, $itemprop = null, $recursion_level = 0 ) { |
|
| 83 | +function wl_content_embed_item_microdata($content, $uri, $itemprop = null, $recursion_level = 0) { |
|
| 84 | 84 | |
| 85 | - if ( $recursion_level > wl_config_get_recursion_depth() ) { |
|
| 86 | - wl_write_log( "recursion depth limit exceeded [ level :: $recursion_level ][ max :: " . wl_config_get_recursion_depth() . " ]" ); |
|
| 85 | + if ($recursion_level > wl_config_get_recursion_depth()) { |
|
| 86 | + wl_write_log("recursion depth limit exceeded [ level :: $recursion_level ][ max :: ".wl_config_get_recursion_depth()." ]"); |
|
| 87 | 87 | |
| 88 | 88 | return ''; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 91 | + $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri); |
|
| 92 | 92 | |
| 93 | 93 | // Entity not found or not published. Delete <span> tags but leave their content on page. |
| 94 | - if ( null === $post || $post->post_status !== 'publish' ) { |
|
| 94 | + if (null === $post || $post->post_status !== 'publish') { |
|
| 95 | 95 | |
| 96 | 96 | // wl_write_log( "wl_content_embed_item_microdata : entity not found or not published [ uri :: $uri ]" ); |
| 97 | 97 | |
| 98 | 98 | // Replace the original tagging with the new tagging. |
| 99 | - $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 100 | - $content = preg_replace( $regex, '$2', $content ); |
|
| 99 | + $regex = wl_content_embed_build_regex_from_uri($uri); |
|
| 100 | + $content = preg_replace($regex, '$2', $content); |
|
| 101 | 101 | |
| 102 | 102 | return $content; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | // Get the entity URI and its escaped version for the regex. |
| 106 | - $entity_uri = wl_get_entity_uri( $post->ID ); |
|
| 106 | + $entity_uri = wl_get_entity_uri($post->ID); |
|
| 107 | 107 | // Get the main type. |
| 108 | - $main_type = wl_entity_type_taxonomy_get_type( $post->ID ); |
|
| 108 | + $main_type = wl_entity_type_taxonomy_get_type($post->ID); |
|
| 109 | 109 | |
| 110 | 110 | // Set the item type if available. |
| 111 | - $item_type = ( null === $main_type ? '' : ' itemtype="' . esc_attr( $main_type['uri'] ) . '"' ); |
|
| 111 | + $item_type = (null === $main_type ? '' : ' itemtype="'.esc_attr($main_type['uri']).'"'); |
|
| 112 | 112 | |
| 113 | 113 | // Define attribute itemprop if this entity is nested. |
| 114 | - if ( ! is_null( $itemprop ) ) { |
|
| 115 | - $itemprop = ' itemprop="' . $itemprop . '"'; |
|
| 114 | + if ( ! is_null($itemprop)) { |
|
| 115 | + $itemprop = ' itemprop="'.$itemprop.'"'; |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | // Get additional properties (this may imply a recursion of this method on a sub-entity). |
| 119 | - $additional_properties = wl_content_embed_compile_microdata_template( $post->ID, $main_type, $recursion_level ); |
|
| 119 | + $additional_properties = wl_content_embed_compile_microdata_template($post->ID, $main_type, $recursion_level); |
|
| 120 | 120 | |
| 121 | 121 | $same_as = ''; |
| 122 | 122 | // Get the array of sameAs uris. |
| 123 | - $same_as_uris = wl_schema_get_value( $post->ID, 'sameAs' ); |
|
| 123 | + $same_as_uris = wl_schema_get_value($post->ID, 'sameAs'); |
|
| 124 | 124 | // Prepare the sameAs fragment. |
| 125 | - foreach ( $same_as_uris as $same_as_uri ) { |
|
| 125 | + foreach ($same_as_uris as $same_as_uri) { |
|
| 126 | 126 | $same_as .= "<link itemprop=\"sameAs\" href=\"$same_as_uri\">"; |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | // Get the entity URL. |
| 130 | - $permalink = get_permalink( $post->ID ); |
|
| 131 | - $url = '<link itemprop="url" href="' . $permalink . '" />'; |
|
| 130 | + $permalink = get_permalink($post->ID); |
|
| 131 | + $url = '<link itemprop="url" href="'.$permalink.'" />'; |
|
| 132 | 132 | |
| 133 | 133 | // Replace the original tagging with the new tagging. |
| 134 | - $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 135 | - $content = preg_replace( $regex, |
|
| 136 | - '<$1' . $itemprop . ' itemscope' . $item_type . ' itemid="' . esc_attr( $entity_uri ) . '">' |
|
| 134 | + $regex = wl_content_embed_build_regex_from_uri($uri); |
|
| 135 | + $content = preg_replace($regex, |
|
| 136 | + '<$1'.$itemprop.' itemscope'.$item_type.' itemid="'.esc_attr($entity_uri).'">' |
|
| 137 | 137 | . $same_as |
| 138 | 138 | . $additional_properties |
| 139 | 139 | . $url |
| 140 | - . '<a class="wl-entity-page-link" href="' . $permalink . '" itemprop="name" content="' . $post->post_title . '">' . ( is_null( $itemprop ) ? '$2' : '' ) . '</a></$1>', //Only print name inside <span> for top-level entities |
|
| 140 | + . '<a class="wl-entity-page-link" href="'.$permalink.'" itemprop="name" content="'.$post->post_title.'">'.(is_null($itemprop) ? '$2' : '').'</a></$1>', //Only print name inside <span> for top-level entities |
|
| 141 | 141 | $content |
| 142 | 142 | ); |
| 143 | 143 | |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | return $content; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | -add_filter( 'the_content', 'wl_content_embed_microdata' ); |
|
| 149 | +add_filter('the_content', 'wl_content_embed_microdata'); |
|
| 150 | 150 | |
| 151 | 151 | /** |
| 152 | 152 | * Fills up the microdata_template with entity's values. |
@@ -157,49 +157,49 @@ discard block |
||
| 157 | 157 | * |
| 158 | 158 | * @return string The content with embedded microdata. |
| 159 | 159 | */ |
| 160 | -function wl_content_embed_compile_microdata_template( $entity_id, $entity_type, $recursion_level = 0 ) { |
|
| 160 | +function wl_content_embed_compile_microdata_template($entity_id, $entity_type, $recursion_level = 0) { |
|
| 161 | 161 | global $wl_logger; |
| 162 | 162 | |
| 163 | - if ( WP_DEBUG ) { |
|
| 164 | - $wl_logger->trace( "Embedding microdata [ entity id :: $entity_id ][ entity type :: " . var_export( $entity_type, true ) . " ][ recursion level :: $recursion_level ]" ); |
|
| 163 | + if (WP_DEBUG) { |
|
| 164 | + $wl_logger->trace("Embedding microdata [ entity id :: $entity_id ][ entity type :: ".var_export($entity_type, true)." ][ recursion level :: $recursion_level ]"); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | $regex = '/{{(.*?)}}/'; |
| 168 | 168 | $matches = array(); |
| 169 | 169 | |
| 170 | - if ( null === $entity_type ) { |
|
| 170 | + if (null === $entity_type) { |
|
| 171 | 171 | return ''; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | $template = $entity_type['microdata_template']; |
| 175 | 175 | // Return empty string if template fields have not been found. |
| 176 | - if ( false === preg_match_all( $regex, $template, $matches, PREG_SET_ORDER ) ) { |
|
| 176 | + if (false === preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) { |
|
| 177 | 177 | return ''; |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | - foreach ( $matches as $match ) { |
|
| 180 | + foreach ($matches as $match) { |
|
| 181 | 181 | |
| 182 | 182 | $placeholder = $match[0]; |
| 183 | 183 | $field_name = $match[1]; |
| 184 | 184 | |
| 185 | 185 | // Get property value. |
| 186 | - $meta_collection = wl_schema_get_value( $entity_id, $field_name ); |
|
| 186 | + $meta_collection = wl_schema_get_value($entity_id, $field_name); |
|
| 187 | 187 | |
| 188 | 188 | // If no value is given, just remove the placeholder from the template |
| 189 | - if ( null == $meta_collection ) { |
|
| 190 | - $template = str_replace( $placeholder, '', $template ); |
|
| 189 | + if (null == $meta_collection) { |
|
| 190 | + $template = str_replace($placeholder, '', $template); |
|
| 191 | 191 | continue; |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | // What kind of value is it? |
| 195 | 195 | // TODO: Performance issue here: meta type retrieving should be centralized |
| 196 | - $expected_type = wl_get_meta_type( $field_name ); |
|
| 196 | + $expected_type = wl_get_meta_type($field_name); |
|
| 197 | 197 | |
| 198 | - if ( WP_DEBUG ) { |
|
| 199 | - $wl_logger->trace( "Embedding microdata [ placeholder :: $placeholder ][ field name :: $field_name ][ meta collection :: " . ( is_array( $meta_collection ) ? var_export( $meta_collection, true ) : $meta_collection ) . " ][ expected type :: $expected_type ]" ); |
|
| 198 | + if (WP_DEBUG) { |
|
| 199 | + $wl_logger->trace("Embedding microdata [ placeholder :: $placeholder ][ field name :: $field_name ][ meta collection :: ".(is_array($meta_collection) ? var_export($meta_collection, true) : $meta_collection)." ][ expected type :: $expected_type ]"); |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - foreach ( $meta_collection as $field_value ) { |
|
| 202 | + foreach ($meta_collection as $field_value) { |
|
| 203 | 203 | |
| 204 | 204 | // Quick and dirty patch for #163: |
| 205 | 205 | // - only apply to URIs, i.e. to properties pointing to another post ( $field_value should be a post ID ), |
@@ -207,33 +207,33 @@ discard block |
||
| 207 | 207 | // - check that the referenced post is published. |
| 208 | 208 | // OR |
| 209 | 209 | // - if the value is empty then we don't display it. |
| 210 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI === $expected_type && is_numeric( $field_value ) && 'publish' !== ( $post_status = get_post_status( $field_value ) ) |
|
| 211 | - || empty( $field_value ) |
|
| 210 | + if (Wordlift_Schema_Service::DATA_TYPE_URI === $expected_type && is_numeric($field_value) && 'publish' !== ($post_status = get_post_status($field_value)) |
|
| 211 | + || empty($field_value) |
|
| 212 | 212 | ) { |
| 213 | 213 | |
| 214 | - if ( WP_DEBUG ) { |
|
| 215 | - $wl_logger->trace( "Microdata refers to a non-published post [ field value :: $field_value ][ post status :: $post_status ]" ); |
|
| 214 | + if (WP_DEBUG) { |
|
| 215 | + $wl_logger->trace("Microdata refers to a non-published post [ field value :: $field_value ][ post status :: $post_status ]"); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | // Remove the placeholder. |
| 219 | - $template = str_replace( $placeholder, '', $template ); |
|
| 219 | + $template = str_replace($placeholder, '', $template); |
|
| 220 | 220 | continue; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI == $expected_type ) { |
|
| 223 | + if (Wordlift_Schema_Service::DATA_TYPE_URI == $expected_type) { |
|
| 224 | 224 | // If is a numeric value we assume it is an ID referencing for an internal entity. |
| 225 | - if ( is_numeric( $field_value ) ) { |
|
| 225 | + if (is_numeric($field_value)) { |
|
| 226 | 226 | // Found id, get uri. |
| 227 | - $field_value = wl_get_entity_uri( $field_value ); |
|
| 227 | + $field_value = wl_get_entity_uri($field_value); |
|
| 228 | 228 | } |
| 229 | 229 | // Just if the linked entity does exist I can go further with template compiling |
| 230 | - $nested_entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $field_value ); |
|
| 231 | - if ( ! is_null( $nested_entity ) ) { |
|
| 232 | - $content = '<span itemid="' . esc_attr( $field_value ) . '">' . $nested_entity->post_title . '</span>'; |
|
| 233 | - $compiled_template = wl_content_embed_item_microdata( $content, $field_value, $field_name, ++ $recursion_level ); |
|
| 234 | - $template = str_replace( $placeholder, $compiled_template, $template ); |
|
| 230 | + $nested_entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($field_value); |
|
| 231 | + if ( ! is_null($nested_entity)) { |
|
| 232 | + $content = '<span itemid="'.esc_attr($field_value).'">'.$nested_entity->post_title.'</span>'; |
|
| 233 | + $compiled_template = wl_content_embed_item_microdata($content, $field_value, $field_name, ++ $recursion_level); |
|
| 234 | + $template = str_replace($placeholder, $compiled_template, $template); |
|
| 235 | 235 | } else { |
| 236 | - $template = str_replace( $placeholder, '', $template ); |
|
| 236 | + $template = str_replace($placeholder, '', $template); |
|
| 237 | 237 | } |
| 238 | 238 | continue; |
| 239 | 239 | } |
@@ -241,8 +241,8 @@ discard block |
||
| 241 | 241 | // Standard condition: field containing a raw value |
| 242 | 242 | // For non visible test, schema.org dictates to use the *meta* tag. |
| 243 | 243 | // see http://schema.org/docs/gs.html#advanced_missing |
| 244 | - $value = '<meta itemprop="' . esc_attr( $field_name ) . '" content="' . esc_attr( $field_value ) . '" />'; |
|
| 245 | - $template = str_replace( $placeholder, $value, $template ); |
|
| 244 | + $value = '<meta itemprop="'.esc_attr($field_name).'" content="'.esc_attr($field_value).'" />'; |
|
| 245 | + $template = str_replace($placeholder, $value, $template); |
|
| 246 | 246 | } |
| 247 | 247 | } |
| 248 | 248 | |
@@ -13,35 +13,35 @@ discard block |
||
| 13 | 13 | function wl_get_entity_post_ids_by_uris( $uris ) { |
| 14 | 14 | |
| 15 | 15 | |
| 16 | - if ( empty( $uris ) ) { |
|
| 17 | - return array(); |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - $query = new WP_Query( array( |
|
| 21 | - 'fields' => 'ids', |
|
| 22 | - 'post_status' => 'any', |
|
| 23 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 24 | - 'meta_query' => array( |
|
| 25 | - 'relation' => 'OR', |
|
| 26 | - array( |
|
| 27 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 28 | - 'value' => $uris, |
|
| 29 | - 'compare' => 'IN' |
|
| 30 | - ), |
|
| 31 | - array( |
|
| 32 | - 'key' => 'entity_url', |
|
| 33 | - 'value' => $uris, |
|
| 34 | - 'compare' => 'IN' |
|
| 35 | - ) |
|
| 36 | - ) |
|
| 37 | - ) |
|
| 38 | - ); |
|
| 39 | - |
|
| 40 | - // Get the matching entity posts. |
|
| 41 | - $posts = $query->get_posts(); |
|
| 42 | - |
|
| 43 | - // Return the array |
|
| 44 | - return $posts; |
|
| 16 | + if ( empty( $uris ) ) { |
|
| 17 | + return array(); |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + $query = new WP_Query( array( |
|
| 21 | + 'fields' => 'ids', |
|
| 22 | + 'post_status' => 'any', |
|
| 23 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 24 | + 'meta_query' => array( |
|
| 25 | + 'relation' => 'OR', |
|
| 26 | + array( |
|
| 27 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 28 | + 'value' => $uris, |
|
| 29 | + 'compare' => 'IN' |
|
| 30 | + ), |
|
| 31 | + array( |
|
| 32 | + 'key' => 'entity_url', |
|
| 33 | + 'value' => $uris, |
|
| 34 | + 'compare' => 'IN' |
|
| 35 | + ) |
|
| 36 | + ) |
|
| 37 | + ) |
|
| 38 | + ); |
|
| 39 | + |
|
| 40 | + // Get the matching entity posts. |
|
| 41 | + $posts = $query->get_posts(); |
|
| 42 | + |
|
| 43 | + // Return the array |
|
| 44 | + return $posts; |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
@@ -56,30 +56,30 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | function wl_build_entity_uri( $post_id ) { |
| 58 | 58 | |
| 59 | - // Get the post. |
|
| 60 | - $post = get_post( $post_id ); |
|
| 59 | + // Get the post. |
|
| 60 | + $post = get_post( $post_id ); |
|
| 61 | 61 | |
| 62 | - if ( null === $post ) { |
|
| 62 | + if ( null === $post ) { |
|
| 63 | 63 | |
| 64 | - wl_write_log( "wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]" ); |
|
| 64 | + wl_write_log( "wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]" ); |
|
| 65 | 65 | |
| 66 | - return null; |
|
| 67 | - } |
|
| 66 | + return null; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - // Create an ID given the title. |
|
| 70 | - $path = wl_sanitize_uri_path( $post->post_title ); |
|
| 69 | + // Create an ID given the title. |
|
| 70 | + $path = wl_sanitize_uri_path( $post->post_title ); |
|
| 71 | 71 | |
| 72 | - // If the path is empty, i.e. there's no title, use the post ID as path. |
|
| 73 | - if ( empty( $path ) ) { |
|
| 74 | - $path = "id/$post->ID"; |
|
| 75 | - } |
|
| 72 | + // If the path is empty, i.e. there's no title, use the post ID as path. |
|
| 73 | + if ( empty( $path ) ) { |
|
| 74 | + $path = "id/$post->ID"; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - // Create the URL (dataset base URI has a trailing slash). |
|
| 78 | - $url = sprintf( '%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), $post->post_type, $path ); |
|
| 77 | + // Create the URL (dataset base URI has a trailing slash). |
|
| 78 | + $url = sprintf( '%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), $post->post_type, $path ); |
|
| 79 | 79 | |
| 80 | - // wl_write_log( "wl_build_entity_uri [ post_id :: $post->ID ][ type :: $post->post_type ][ title :: $post->post_title ][ url :: $url ]" ); |
|
| 80 | + // wl_write_log( "wl_build_entity_uri [ post_id :: $post->ID ][ type :: $post->post_type ][ title :: $post->post_title ][ url :: $url ]" ); |
|
| 81 | 81 | |
| 82 | - return $url; |
|
| 82 | + return $url; |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -94,21 +94,21 @@ discard block |
||
| 94 | 94 | */ |
| 95 | 95 | function wl_get_entity_uri( $post_id ) { |
| 96 | 96 | |
| 97 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 97 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 98 | 98 | |
| 99 | - // If the dataset uri is not properly configured, null is returned |
|
| 100 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 101 | - return null; |
|
| 102 | - } |
|
| 99 | + // If the dataset uri is not properly configured, null is returned |
|
| 100 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 101 | + return null; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - // Set the URI if it isn't set yet. |
|
| 105 | - $post_status = get_post_status( $post_id ); |
|
| 106 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 107 | - $uri = wl_build_entity_uri( $post_id ); // "http://data.redlink.io/$user_id/$dataset_id/post/$post->ID"; |
|
| 108 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 109 | - } |
|
| 104 | + // Set the URI if it isn't set yet. |
|
| 105 | + $post_status = get_post_status( $post_id ); |
|
| 106 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 107 | + $uri = wl_build_entity_uri( $post_id ); // "http://data.redlink.io/$user_id/$dataset_id/post/$post->ID"; |
|
| 108 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - return $uri; |
|
| 111 | + return $uri; |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | /** |
@@ -121,9 +121,9 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | function wl_set_entity_uri( $post_id, $uri ) { |
| 123 | 123 | |
| 124 | - // wl_write_log( "wl_set_entity_uri [ post id :: $post_id ][ uri :: $uri ]" ); |
|
| 124 | + // wl_write_log( "wl_set_entity_uri [ post id :: $post_id ][ uri :: $uri ]" ); |
|
| 125 | 125 | |
| 126 | - return update_post_meta( $post_id, WL_ENTITY_URL_META_NAME, $uri ); |
|
| 126 | + return update_post_meta( $post_id, WL_ENTITY_URL_META_NAME, $uri ); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | * @return array An array of terms. |
| 138 | 138 | */ |
| 139 | 139 | function wl_get_entity_rdf_types( $post_id ) { |
| 140 | - return get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE ); |
|
| 140 | + return get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE ); |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | /** |
@@ -148,21 +148,21 @@ discard block |
||
| 148 | 148 | */ |
| 149 | 149 | function wl_set_entity_rdf_types( $post_id, $type_uris = array() ) { |
| 150 | 150 | |
| 151 | - // Avoid errors because of null values. |
|
| 152 | - if ( is_null( $type_uris ) ) { |
|
| 153 | - $type_uris = array(); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - // Ensure there are no duplicates. |
|
| 157 | - $type_uris = array_unique( $type_uris ); |
|
| 158 | - |
|
| 159 | - delete_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE ); |
|
| 160 | - foreach ( $type_uris as $type_uri ) { |
|
| 161 | - if ( empty( $type_uri ) ) { |
|
| 162 | - continue; |
|
| 163 | - } |
|
| 164 | - add_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri ); |
|
| 165 | - } |
|
| 151 | + // Avoid errors because of null values. |
|
| 152 | + if ( is_null( $type_uris ) ) { |
|
| 153 | + $type_uris = array(); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + // Ensure there are no duplicates. |
|
| 157 | + $type_uris = array_unique( $type_uris ); |
|
| 158 | + |
|
| 159 | + delete_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE ); |
|
| 160 | + foreach ( $type_uris as $type_uri ) { |
|
| 161 | + if ( empty( $type_uri ) ) { |
|
| 162 | + continue; |
|
| 163 | + } |
|
| 164 | + add_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri ); |
|
| 165 | + } |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -175,33 +175,33 @@ discard block |
||
| 175 | 175 | */ |
| 176 | 176 | function wl_get_meta_type( $property_name ) { |
| 177 | 177 | |
| 178 | - // Property name must be defined. |
|
| 179 | - if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 180 | - return null; |
|
| 181 | - } |
|
| 178 | + // Property name must be defined. |
|
| 179 | + if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 180 | + return null; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - // store eventual schema name in different variable |
|
| 184 | - $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 183 | + // store eventual schema name in different variable |
|
| 184 | + $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 185 | 185 | |
| 186 | - // Loop over custom_fields |
|
| 187 | - $entity_terms = wl_entity_taxonomy_get_custom_fields(); |
|
| 186 | + // Loop over custom_fields |
|
| 187 | + $entity_terms = wl_entity_taxonomy_get_custom_fields(); |
|
| 188 | 188 | |
| 189 | - foreach ( $entity_terms as $term ) { |
|
| 189 | + foreach ( $entity_terms as $term ) { |
|
| 190 | 190 | |
| 191 | - foreach ( $term as $wl_constant => $field ) { |
|
| 191 | + foreach ( $term as $wl_constant => $field ) { |
|
| 192 | 192 | |
| 193 | - // Is this the predicate we are searching for? |
|
| 194 | - if ( isset( $field['type'] ) ) { |
|
| 195 | - $found_predicate = isset( $field['predicate'] ) && ( $field['predicate'] == $property_schema_name ); |
|
| 196 | - $found_constant = ( $wl_constant == $property_name ); |
|
| 197 | - if ( $found_predicate || $found_constant ) { |
|
| 198 | - return $field['type']; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - } |
|
| 193 | + // Is this the predicate we are searching for? |
|
| 194 | + if ( isset( $field['type'] ) ) { |
|
| 195 | + $found_predicate = isset( $field['predicate'] ) && ( $field['predicate'] == $property_schema_name ); |
|
| 196 | + $found_constant = ( $wl_constant == $property_name ); |
|
| 197 | + if ( $found_predicate || $found_constant ) { |
|
| 198 | + return $field['type']; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - return null; |
|
| 204 | + return null; |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -214,31 +214,31 @@ discard block |
||
| 214 | 214 | */ |
| 215 | 215 | function wl_get_meta_constraints( $property_name ) { |
| 216 | 216 | |
| 217 | - // Property name must be defined. |
|
| 218 | - if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 219 | - return null; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - // store eventual schema name in different variable |
|
| 223 | - $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 224 | - |
|
| 225 | - // Get WL taxonomy mapping. |
|
| 226 | - $types = wl_entity_taxonomy_get_custom_fields(); |
|
| 227 | - |
|
| 228 | - // Loop over types |
|
| 229 | - foreach ( $types as $type ) { |
|
| 230 | - // Loop over custom fields of this type |
|
| 231 | - foreach ( $type as $property => $field ) { |
|
| 232 | - if ( isset( $field['constraints'] ) && ! empty( $field['constraints'] ) ) { |
|
| 233 | - // Is this the property we are searhing for? |
|
| 234 | - if ( ( $property == $property_name ) || ( $field['predicate'] == $property_schema_name ) ) { |
|
| 235 | - return $field['constraints']; |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - return null; |
|
| 217 | + // Property name must be defined. |
|
| 218 | + if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 219 | + return null; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + // store eventual schema name in different variable |
|
| 223 | + $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 224 | + |
|
| 225 | + // Get WL taxonomy mapping. |
|
| 226 | + $types = wl_entity_taxonomy_get_custom_fields(); |
|
| 227 | + |
|
| 228 | + // Loop over types |
|
| 229 | + foreach ( $types as $type ) { |
|
| 230 | + // Loop over custom fields of this type |
|
| 231 | + foreach ( $type as $property => $field ) { |
|
| 232 | + if ( isset( $field['constraints'] ) && ! empty( $field['constraints'] ) ) { |
|
| 233 | + // Is this the property we are searhing for? |
|
| 234 | + if ( ( $property == $property_name ) || ( $field['predicate'] == $property_schema_name ) ) { |
|
| 235 | + return $field['constraints']; |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + return null; |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -250,30 +250,30 @@ discard block |
||
| 250 | 250 | */ |
| 251 | 251 | function wl_entity_taxonomy_get_custom_fields( $entity_id = null ) { |
| 252 | 252 | |
| 253 | - if ( is_null( $entity_id ) ) { |
|
| 253 | + if ( is_null( $entity_id ) ) { |
|
| 254 | 254 | |
| 255 | - // Return all custom fields. |
|
| 256 | - // Get taxonomy terms |
|
| 257 | - $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( 'hide_empty' => 0 ) ); |
|
| 255 | + // Return all custom fields. |
|
| 256 | + // Get taxonomy terms |
|
| 257 | + $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( 'hide_empty' => 0 ) ); |
|
| 258 | 258 | |
| 259 | - if ( is_wp_error( $terms ) ) { |
|
| 260 | - return null; |
|
| 261 | - } |
|
| 259 | + if ( is_wp_error( $terms ) ) { |
|
| 260 | + return null; |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - $custom_fields = array(); |
|
| 264 | - foreach ( $terms as $term ) { |
|
| 265 | - // Get custom_fields |
|
| 266 | - $term_options = Wordlift_Schema_Service::get_instance()->get_schema( $term->slug ); |
|
| 267 | - $custom_fields[ $term_options['uri'] ] = $term_options['custom_fields']; |
|
| 268 | - } |
|
| 263 | + $custom_fields = array(); |
|
| 264 | + foreach ( $terms as $term ) { |
|
| 265 | + // Get custom_fields |
|
| 266 | + $term_options = Wordlift_Schema_Service::get_instance()->get_schema( $term->slug ); |
|
| 267 | + $custom_fields[ $term_options['uri'] ] = $term_options['custom_fields']; |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - return $custom_fields; |
|
| 270 | + return $custom_fields; |
|
| 271 | 271 | |
| 272 | - } |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - // Return custom fields for this specific entity's type. |
|
| 275 | - $type = wl_entity_type_taxonomy_get_type( $entity_id ); |
|
| 274 | + // Return custom fields for this specific entity's type. |
|
| 275 | + $type = wl_entity_type_taxonomy_get_type( $entity_id ); |
|
| 276 | 276 | |
| 277 | - return $type['custom_fields']; |
|
| 277 | + return $type['custom_fields']; |
|
| 278 | 278 | |
| 279 | 279 | } |
@@ -10,14 +10,14 @@ discard block |
||
| 10 | 10 | * |
| 11 | 11 | * @return array A WP_Post instance or null if not found. |
| 12 | 12 | */ |
| 13 | -function wl_get_entity_post_ids_by_uris( $uris ) { |
|
| 13 | +function wl_get_entity_post_ids_by_uris($uris) { |
|
| 14 | 14 | |
| 15 | 15 | |
| 16 | - if ( empty( $uris ) ) { |
|
| 16 | + if (empty($uris)) { |
|
| 17 | 17 | return array(); |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - $query = new WP_Query( array( |
|
| 20 | + $query = new WP_Query(array( |
|
| 21 | 21 | 'fields' => 'ids', |
| 22 | 22 | 'post_status' => 'any', |
| 23 | 23 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
@@ -54,28 +54,28 @@ discard block |
||
| 54 | 54 | * |
| 55 | 55 | * @return string The URI of the entity |
| 56 | 56 | */ |
| 57 | -function wl_build_entity_uri( $post_id ) { |
|
| 57 | +function wl_build_entity_uri($post_id) { |
|
| 58 | 58 | |
| 59 | 59 | // Get the post. |
| 60 | - $post = get_post( $post_id ); |
|
| 60 | + $post = get_post($post_id); |
|
| 61 | 61 | |
| 62 | - if ( null === $post ) { |
|
| 62 | + if (null === $post) { |
|
| 63 | 63 | |
| 64 | - wl_write_log( "wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]" ); |
|
| 64 | + wl_write_log("wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]"); |
|
| 65 | 65 | |
| 66 | 66 | return null; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | // Create an ID given the title. |
| 70 | - $path = wl_sanitize_uri_path( $post->post_title ); |
|
| 70 | + $path = wl_sanitize_uri_path($post->post_title); |
|
| 71 | 71 | |
| 72 | 72 | // If the path is empty, i.e. there's no title, use the post ID as path. |
| 73 | - if ( empty( $path ) ) { |
|
| 73 | + if (empty($path)) { |
|
| 74 | 74 | $path = "id/$post->ID"; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | // Create the URL (dataset base URI has a trailing slash). |
| 78 | - $url = sprintf( '%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), $post->post_type, $path ); |
|
| 78 | + $url = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), $post->post_type, $path); |
|
| 79 | 79 | |
| 80 | 80 | // wl_write_log( "wl_build_entity_uri [ post_id :: $post->ID ][ type :: $post->post_type ][ title :: $post->post_title ][ url :: $url ]" ); |
| 81 | 81 | |
@@ -92,20 +92,20 @@ discard block |
||
| 92 | 92 | * |
| 93 | 93 | * @return string|null The URI of the entity or null if not configured. |
| 94 | 94 | */ |
| 95 | -function wl_get_entity_uri( $post_id ) { |
|
| 95 | +function wl_get_entity_uri($post_id) { |
|
| 96 | 96 | |
| 97 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 97 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 98 | 98 | |
| 99 | 99 | // If the dataset uri is not properly configured, null is returned |
| 100 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 100 | + if ('' === wl_configuration_get_redlink_dataset_uri()) { |
|
| 101 | 101 | return null; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | // Set the URI if it isn't set yet. |
| 105 | - $post_status = get_post_status( $post_id ); |
|
| 106 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 107 | - $uri = wl_build_entity_uri( $post_id ); // "http://data.redlink.io/$user_id/$dataset_id/post/$post->ID"; |
|
| 108 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 105 | + $post_status = get_post_status($post_id); |
|
| 106 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 107 | + $uri = wl_build_entity_uri($post_id); // "http://data.redlink.io/$user_id/$dataset_id/post/$post->ID"; |
|
| 108 | + wl_set_entity_uri($post_id, $uri); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | return $uri; |
@@ -119,11 +119,11 @@ discard block |
||
| 119 | 119 | * |
| 120 | 120 | * @return bool True if successful, otherwise false. |
| 121 | 121 | */ |
| 122 | -function wl_set_entity_uri( $post_id, $uri ) { |
|
| 122 | +function wl_set_entity_uri($post_id, $uri) { |
|
| 123 | 123 | |
| 124 | 124 | // wl_write_log( "wl_set_entity_uri [ post id :: $post_id ][ uri :: $uri ]" ); |
| 125 | 125 | |
| 126 | - return update_post_meta( $post_id, WL_ENTITY_URL_META_NAME, $uri ); |
|
| 126 | + return update_post_meta($post_id, WL_ENTITY_URL_META_NAME, $uri); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | |
@@ -136,8 +136,8 @@ discard block |
||
| 136 | 136 | * |
| 137 | 137 | * @return array An array of terms. |
| 138 | 138 | */ |
| 139 | -function wl_get_entity_rdf_types( $post_id ) { |
|
| 140 | - return get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE ); |
|
| 139 | +function wl_get_entity_rdf_types($post_id) { |
|
| 140 | + return get_post_meta($post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE); |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | /** |
@@ -146,22 +146,22 @@ discard block |
||
| 146 | 146 | * @param int $post_id The entity post ID. |
| 147 | 147 | * @param array $type_uris An array of type URIs. |
| 148 | 148 | */ |
| 149 | -function wl_set_entity_rdf_types( $post_id, $type_uris = array() ) { |
|
| 149 | +function wl_set_entity_rdf_types($post_id, $type_uris = array()) { |
|
| 150 | 150 | |
| 151 | 151 | // Avoid errors because of null values. |
| 152 | - if ( is_null( $type_uris ) ) { |
|
| 152 | + if (is_null($type_uris)) { |
|
| 153 | 153 | $type_uris = array(); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | // Ensure there are no duplicates. |
| 157 | - $type_uris = array_unique( $type_uris ); |
|
| 157 | + $type_uris = array_unique($type_uris); |
|
| 158 | 158 | |
| 159 | - delete_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE ); |
|
| 160 | - foreach ( $type_uris as $type_uri ) { |
|
| 161 | - if ( empty( $type_uri ) ) { |
|
| 159 | + delete_post_meta($post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE); |
|
| 160 | + foreach ($type_uris as $type_uri) { |
|
| 161 | + if (empty($type_uri)) { |
|
| 162 | 162 | continue; |
| 163 | 163 | } |
| 164 | - add_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri ); |
|
| 164 | + add_post_meta($post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri); |
|
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | |
@@ -173,28 +173,28 @@ discard block |
||
| 173 | 173 | * |
| 174 | 174 | * @return array containing type(s) or null (in case of error or no types). |
| 175 | 175 | */ |
| 176 | -function wl_get_meta_type( $property_name ) { |
|
| 176 | +function wl_get_meta_type($property_name) { |
|
| 177 | 177 | |
| 178 | 178 | // Property name must be defined. |
| 179 | - if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 179 | + if ( ! isset($property_name) || is_null($property_name)) { |
|
| 180 | 180 | return null; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | // store eventual schema name in different variable |
| 184 | - $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 184 | + $property_schema_name = wl_build_full_schema_uri_from_schema_slug($property_name); |
|
| 185 | 185 | |
| 186 | 186 | // Loop over custom_fields |
| 187 | 187 | $entity_terms = wl_entity_taxonomy_get_custom_fields(); |
| 188 | 188 | |
| 189 | - foreach ( $entity_terms as $term ) { |
|
| 189 | + foreach ($entity_terms as $term) { |
|
| 190 | 190 | |
| 191 | - foreach ( $term as $wl_constant => $field ) { |
|
| 191 | + foreach ($term as $wl_constant => $field) { |
|
| 192 | 192 | |
| 193 | 193 | // Is this the predicate we are searching for? |
| 194 | - if ( isset( $field['type'] ) ) { |
|
| 195 | - $found_predicate = isset( $field['predicate'] ) && ( $field['predicate'] == $property_schema_name ); |
|
| 196 | - $found_constant = ( $wl_constant == $property_name ); |
|
| 197 | - if ( $found_predicate || $found_constant ) { |
|
| 194 | + if (isset($field['type'])) { |
|
| 195 | + $found_predicate = isset($field['predicate']) && ($field['predicate'] == $property_schema_name); |
|
| 196 | + $found_constant = ($wl_constant == $property_name); |
|
| 197 | + if ($found_predicate || $found_constant) { |
|
| 198 | 198 | return $field['type']; |
| 199 | 199 | } |
| 200 | 200 | } |
@@ -212,26 +212,26 @@ discard block |
||
| 212 | 212 | * |
| 213 | 213 | * @return array containing constraint(s) or null (in case of error or no constraint). |
| 214 | 214 | */ |
| 215 | -function wl_get_meta_constraints( $property_name ) { |
|
| 215 | +function wl_get_meta_constraints($property_name) { |
|
| 216 | 216 | |
| 217 | 217 | // Property name must be defined. |
| 218 | - if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 218 | + if ( ! isset($property_name) || is_null($property_name)) { |
|
| 219 | 219 | return null; |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | // store eventual schema name in different variable |
| 223 | - $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 223 | + $property_schema_name = wl_build_full_schema_uri_from_schema_slug($property_name); |
|
| 224 | 224 | |
| 225 | 225 | // Get WL taxonomy mapping. |
| 226 | 226 | $types = wl_entity_taxonomy_get_custom_fields(); |
| 227 | 227 | |
| 228 | 228 | // Loop over types |
| 229 | - foreach ( $types as $type ) { |
|
| 229 | + foreach ($types as $type) { |
|
| 230 | 230 | // Loop over custom fields of this type |
| 231 | - foreach ( $type as $property => $field ) { |
|
| 232 | - if ( isset( $field['constraints'] ) && ! empty( $field['constraints'] ) ) { |
|
| 231 | + foreach ($type as $property => $field) { |
|
| 232 | + if (isset($field['constraints']) && ! empty($field['constraints'])) { |
|
| 233 | 233 | // Is this the property we are searhing for? |
| 234 | - if ( ( $property == $property_name ) || ( $field['predicate'] == $property_schema_name ) ) { |
|
| 234 | + if (($property == $property_name) || ($field['predicate'] == $property_schema_name)) { |
|
| 235 | 235 | return $field['constraints']; |
| 236 | 236 | } |
| 237 | 237 | } |
@@ -248,23 +248,23 @@ discard block |
||
| 248 | 248 | * |
| 249 | 249 | * @return array|null if $entity_id was specified, return custom_fields for that entity's type. Otherwise returns all custom_fields |
| 250 | 250 | */ |
| 251 | -function wl_entity_taxonomy_get_custom_fields( $entity_id = null ) { |
|
| 251 | +function wl_entity_taxonomy_get_custom_fields($entity_id = null) { |
|
| 252 | 252 | |
| 253 | - if ( is_null( $entity_id ) ) { |
|
| 253 | + if (is_null($entity_id)) { |
|
| 254 | 254 | |
| 255 | 255 | // Return all custom fields. |
| 256 | 256 | // Get taxonomy terms |
| 257 | - $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( 'hide_empty' => 0 ) ); |
|
| 257 | + $terms = get_terms(Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array('hide_empty' => 0)); |
|
| 258 | 258 | |
| 259 | - if ( is_wp_error( $terms ) ) { |
|
| 259 | + if (is_wp_error($terms)) { |
|
| 260 | 260 | return null; |
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | $custom_fields = array(); |
| 264 | - foreach ( $terms as $term ) { |
|
| 264 | + foreach ($terms as $term) { |
|
| 265 | 265 | // Get custom_fields |
| 266 | - $term_options = Wordlift_Schema_Service::get_instance()->get_schema( $term->slug ); |
|
| 267 | - $custom_fields[ $term_options['uri'] ] = $term_options['custom_fields']; |
|
| 266 | + $term_options = Wordlift_Schema_Service::get_instance()->get_schema($term->slug); |
|
| 267 | + $custom_fields[$term_options['uri']] = $term_options['custom_fields']; |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | return $custom_fields; |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | // Return custom fields for this specific entity's type. |
| 275 | - $type = wl_entity_type_taxonomy_get_type( $entity_id ); |
|
| 275 | + $type = wl_entity_type_taxonomy_get_type($entity_id); |
|
| 276 | 276 | |
| 277 | 277 | return $type['custom_fields']; |
| 278 | 278 | |
@@ -5,99 +5,99 @@ discard block |
||
| 5 | 5 | */ |
| 6 | 6 | function wl_core_install_entity_type_data() { |
| 7 | 7 | |
| 8 | - // global $wl_logger; |
|
| 9 | - // $wl_logger->trace( 'Installing entity type data...' ); |
|
| 10 | - |
|
| 11 | - // Ensure the custom type and the taxonomy are registered. |
|
| 12 | - wl_entity_type_register(); |
|
| 13 | - wl_entity_type_taxonomy_register(); |
|
| 14 | - |
|
| 15 | - // Set the taxonomy data. |
|
| 16 | - // Note: parent types must be defined before child types. |
|
| 17 | - $terms = array( |
|
| 18 | - 'thing' => array( |
|
| 19 | - 'label' => 'Thing', |
|
| 20 | - 'description' => 'A generic thing (something that doesn\'t fit in the previous definitions.', |
|
| 21 | - ), |
|
| 22 | - 'creative-work' => array( |
|
| 23 | - 'label' => 'CreativeWork', |
|
| 24 | - 'description' => 'A creative work (or a Music Album).', |
|
| 25 | - 'parents' => array( 'thing' ), // give term slug as parent |
|
| 26 | - ), |
|
| 27 | - 'event' => array( |
|
| 28 | - 'label' => 'Event', |
|
| 29 | - 'description' => 'An event.', |
|
| 30 | - 'parents' => array( 'thing' ), |
|
| 31 | - ), |
|
| 32 | - 'organization' => array( |
|
| 33 | - 'label' => 'Organization', |
|
| 34 | - 'description' => 'An organization, including a government or a newspaper.', |
|
| 35 | - 'parents' => array( 'thing' ), |
|
| 36 | - ), |
|
| 37 | - 'person' => array( |
|
| 38 | - 'label' => 'Person', |
|
| 39 | - 'description' => 'A person (or a music artist).', |
|
| 40 | - 'parents' => array( 'thing' ), |
|
| 41 | - ), |
|
| 42 | - 'place' => array( |
|
| 43 | - 'label' => 'Place', |
|
| 44 | - 'description' => 'A place.', |
|
| 45 | - 'parents' => array( 'thing' ), |
|
| 46 | - ), |
|
| 47 | - 'localbusiness' => array( |
|
| 48 | - 'label' => 'LocalBusiness', |
|
| 49 | - 'description' => 'A local business.', |
|
| 50 | - 'parents' => array( 'place', 'organization' ), |
|
| 51 | - ), |
|
| 52 | - ); |
|
| 53 | - |
|
| 54 | - foreach ( $terms as $slug => $term ) { |
|
| 55 | - |
|
| 56 | - // Create the term if it does not exist, then get its ID |
|
| 57 | - $term_id = term_exists( $slug, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 58 | - |
|
| 59 | - if ( $term_id == 0 || is_null( $term_id ) ) { |
|
| 60 | - $result = wp_insert_term( $slug, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 61 | - } else { |
|
| 62 | - $term_id = $term_id['term_id']; |
|
| 63 | - $result = get_term( $term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, ARRAY_A ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // Check for errors. |
|
| 67 | - if ( is_wp_error( $result ) ) { |
|
| 68 | - wl_write_log( 'wl_install_entity_type_data [ ' . $result->get_error_message() . ' ]' ); |
|
| 69 | - continue; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // Check if 'parent' corresponds to an actual term and get its ID. |
|
| 73 | - if ( ! isset( $term['parents'] ) ) { |
|
| 74 | - $term['parents'] = array(); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - $parent_ids = array(); |
|
| 78 | - foreach ( $term['parents'] as $parent_slug ) { |
|
| 79 | - $parent_id = get_term_by( 'slug', $parent_slug, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 80 | - $parent_ids[] = intval( $parent_id->term_id ); // Note: int casting is suggested by Codex: http://codex.wordpress.org/Function_Reference/get_term_by |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // Define a parent in the WP taxonomy style (not important for WL) |
|
| 84 | - if ( empty( $parent_ids ) ) { |
|
| 85 | - // No parent |
|
| 86 | - $parent_id = 0; |
|
| 87 | - } else { |
|
| 88 | - // Get first parent |
|
| 89 | - $parent_id = $parent_ids[0]; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // Update term with description, slug and parent |
|
| 93 | - wp_update_term( $result['term_id'], Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 94 | - 'name' => $term['label'], |
|
| 95 | - 'slug' => $slug, |
|
| 96 | - 'description' => $term['description'], |
|
| 97 | - 'parent' => $parent_id // We give to WP taxonomy just one parent. TODO: see if can give more than one |
|
| 98 | - ) ); |
|
| 99 | - |
|
| 100 | - } |
|
| 8 | + // global $wl_logger; |
|
| 9 | + // $wl_logger->trace( 'Installing entity type data...' ); |
|
| 10 | + |
|
| 11 | + // Ensure the custom type and the taxonomy are registered. |
|
| 12 | + wl_entity_type_register(); |
|
| 13 | + wl_entity_type_taxonomy_register(); |
|
| 14 | + |
|
| 15 | + // Set the taxonomy data. |
|
| 16 | + // Note: parent types must be defined before child types. |
|
| 17 | + $terms = array( |
|
| 18 | + 'thing' => array( |
|
| 19 | + 'label' => 'Thing', |
|
| 20 | + 'description' => 'A generic thing (something that doesn\'t fit in the previous definitions.', |
|
| 21 | + ), |
|
| 22 | + 'creative-work' => array( |
|
| 23 | + 'label' => 'CreativeWork', |
|
| 24 | + 'description' => 'A creative work (or a Music Album).', |
|
| 25 | + 'parents' => array( 'thing' ), // give term slug as parent |
|
| 26 | + ), |
|
| 27 | + 'event' => array( |
|
| 28 | + 'label' => 'Event', |
|
| 29 | + 'description' => 'An event.', |
|
| 30 | + 'parents' => array( 'thing' ), |
|
| 31 | + ), |
|
| 32 | + 'organization' => array( |
|
| 33 | + 'label' => 'Organization', |
|
| 34 | + 'description' => 'An organization, including a government or a newspaper.', |
|
| 35 | + 'parents' => array( 'thing' ), |
|
| 36 | + ), |
|
| 37 | + 'person' => array( |
|
| 38 | + 'label' => 'Person', |
|
| 39 | + 'description' => 'A person (or a music artist).', |
|
| 40 | + 'parents' => array( 'thing' ), |
|
| 41 | + ), |
|
| 42 | + 'place' => array( |
|
| 43 | + 'label' => 'Place', |
|
| 44 | + 'description' => 'A place.', |
|
| 45 | + 'parents' => array( 'thing' ), |
|
| 46 | + ), |
|
| 47 | + 'localbusiness' => array( |
|
| 48 | + 'label' => 'LocalBusiness', |
|
| 49 | + 'description' => 'A local business.', |
|
| 50 | + 'parents' => array( 'place', 'organization' ), |
|
| 51 | + ), |
|
| 52 | + ); |
|
| 53 | + |
|
| 54 | + foreach ( $terms as $slug => $term ) { |
|
| 55 | + |
|
| 56 | + // Create the term if it does not exist, then get its ID |
|
| 57 | + $term_id = term_exists( $slug, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 58 | + |
|
| 59 | + if ( $term_id == 0 || is_null( $term_id ) ) { |
|
| 60 | + $result = wp_insert_term( $slug, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 61 | + } else { |
|
| 62 | + $term_id = $term_id['term_id']; |
|
| 63 | + $result = get_term( $term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, ARRAY_A ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // Check for errors. |
|
| 67 | + if ( is_wp_error( $result ) ) { |
|
| 68 | + wl_write_log( 'wl_install_entity_type_data [ ' . $result->get_error_message() . ' ]' ); |
|
| 69 | + continue; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // Check if 'parent' corresponds to an actual term and get its ID. |
|
| 73 | + if ( ! isset( $term['parents'] ) ) { |
|
| 74 | + $term['parents'] = array(); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + $parent_ids = array(); |
|
| 78 | + foreach ( $term['parents'] as $parent_slug ) { |
|
| 79 | + $parent_id = get_term_by( 'slug', $parent_slug, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 80 | + $parent_ids[] = intval( $parent_id->term_id ); // Note: int casting is suggested by Codex: http://codex.wordpress.org/Function_Reference/get_term_by |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // Define a parent in the WP taxonomy style (not important for WL) |
|
| 84 | + if ( empty( $parent_ids ) ) { |
|
| 85 | + // No parent |
|
| 86 | + $parent_id = 0; |
|
| 87 | + } else { |
|
| 88 | + // Get first parent |
|
| 89 | + $parent_id = $parent_ids[0]; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // Update term with description, slug and parent |
|
| 93 | + wp_update_term( $result['term_id'], Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 94 | + 'name' => $term['label'], |
|
| 95 | + 'slug' => $slug, |
|
| 96 | + 'description' => $term['description'], |
|
| 97 | + 'parent' => $parent_id // We give to WP taxonomy just one parent. TODO: see if can give more than one |
|
| 98 | + ) ); |
|
| 99 | + |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | 102 | } |
| 103 | 103 | |
@@ -106,16 +106,16 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | function wl_core_install_create_relation_instance_table() { |
| 108 | 108 | |
| 109 | - global $wpdb; |
|
| 110 | - // global $wl_db_version; |
|
| 111 | - $installed_version = get_option( "wl_db_version" ); |
|
| 109 | + global $wpdb; |
|
| 110 | + // global $wl_db_version; |
|
| 111 | + $installed_version = get_option( "wl_db_version" ); |
|
| 112 | 112 | |
| 113 | - if ( $installed_version != WL_DB_VERSION ) { |
|
| 114 | - $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
| 115 | - $charset_collate = $wpdb->get_charset_collate(); |
|
| 113 | + if ( $installed_version != WL_DB_VERSION ) { |
|
| 114 | + $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
| 115 | + $charset_collate = $wpdb->get_charset_collate(); |
|
| 116 | 116 | |
| 117 | - // Sql statement for the relation instances custom table |
|
| 118 | - $sql = <<<EOF |
|
| 117 | + // Sql statement for the relation instances custom table |
|
| 118 | + $sql = <<<EOF |
|
| 119 | 119 | CREATE TABLE $table_name ( |
| 120 | 120 | id int(11) NOT NULL AUTO_INCREMENT, |
| 121 | 121 | subject_id int(11) NOT NULL, |
@@ -127,14 +127,14 @@ discard block |
||
| 127 | 127 | ) $charset_collate; |
| 128 | 128 | EOF; |
| 129 | 129 | |
| 130 | - // @see: https://codex.wordpress.org/Creating_Tables_with_Plugins |
|
| 131 | - require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
|
| 132 | - $results = dbDelta( $sql ); |
|
| 130 | + // @see: https://codex.wordpress.org/Creating_Tables_with_Plugins |
|
| 131 | + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
|
| 132 | + $results = dbDelta( $sql ); |
|
| 133 | 133 | |
| 134 | - wl_write_log( $results ); |
|
| 134 | + wl_write_log( $results ); |
|
| 135 | 135 | |
| 136 | - update_option( "wl_db_version", WL_DB_VERSION ); |
|
| 137 | - } |
|
| 136 | + update_option( "wl_db_version", WL_DB_VERSION ); |
|
| 137 | + } |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
@@ -142,15 +142,15 @@ discard block |
||
| 142 | 142 | */ |
| 143 | 143 | function wl_core_install() { |
| 144 | 144 | |
| 145 | - // Create a blank application key if there is none |
|
| 146 | - $key = wl_configuration_get_key(); |
|
| 147 | - if ( empty( $key ) ) { |
|
| 148 | - wl_configuration_set_key( '' ); |
|
| 149 | - } |
|
| 145 | + // Create a blank application key if there is none |
|
| 146 | + $key = wl_configuration_get_key(); |
|
| 147 | + if ( empty( $key ) ) { |
|
| 148 | + wl_configuration_set_key( '' ); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - wl_core_install_entity_type_data(); |
|
| 152 | - wl_core_install_create_relation_instance_table(); |
|
| 153 | - flush_rewrite_rules(); |
|
| 151 | + wl_core_install_entity_type_data(); |
|
| 152 | + wl_core_install_create_relation_instance_table(); |
|
| 153 | + flush_rewrite_rules(); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | // Installation Hook |
@@ -158,9 +158,9 @@ discard block |
||
| 158 | 158 | |
| 159 | 159 | // Check db status on automated plugins updates |
| 160 | 160 | function wl_core_update_db_check() { |
| 161 | - if ( get_site_option( 'wl_db_version' ) != WL_DB_VERSION ) { |
|
| 162 | - wl_core_install_create_relation_instance_table(); |
|
| 163 | - } |
|
| 161 | + if ( get_site_option( 'wl_db_version' ) != WL_DB_VERSION ) { |
|
| 162 | + wl_core_install_create_relation_instance_table(); |
|
| 163 | + } |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | add_action( 'plugins_loaded', 'wl_core_update_db_check' ); |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | * Calculate the avarage entities rating |
| 199 | 199 | * @since 3.4.0 |
| 200 | 200 | * |
| 201 | - * @return int Avarage entities rating. |
|
| 201 | + * @return double Avarage entities rating. |
|
| 202 | 202 | */ |
| 203 | 203 | public function avarage_entities_rating() { |
| 204 | 204 | |
@@ -254,6 +254,9 @@ discard block |
||
| 254 | 254 | return (int) FALSE; |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | + /** |
|
| 258 | + * @param string $query |
|
| 259 | + */ |
|
| 257 | 260 | private function rl_sparql_select( $query ) { |
| 258 | 261 | |
| 259 | 262 | // Prepare the SPARQL statement by prepending the default namespaces. |
@@ -266,6 +269,9 @@ discard block |
||
| 266 | 269 | return wp_remote_get( $url, $args ); |
| 267 | 270 | } |
| 268 | 271 | |
| 272 | + /** |
|
| 273 | + * @param string $param |
|
| 274 | + */ |
|
| 269 | 275 | private function render_stat_param( $param ) { |
| 270 | 276 | |
| 271 | 277 | return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
@@ -14,83 +14,83 @@ 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'; |
|
| 17 | + /** |
|
| 18 | + * Transient Name |
|
| 19 | + * |
|
| 20 | + * @since 3.4.0 |
|
| 21 | + */ |
|
| 22 | + const TRANSIENT_NAME = 'wl_dashboard_stats'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Transient Expiration (in seconds) |
|
| 26 | - * |
|
| 27 | - * @since 3.4.0 |
|
| 28 | - */ |
|
| 29 | - const TRANSIENT_EXPIRATION = 86400; |
|
| 24 | + /** |
|
| 25 | + * Transient Expiration (in seconds) |
|
| 26 | + * |
|
| 27 | + * @since 3.4.0 |
|
| 28 | + */ |
|
| 29 | + const TRANSIENT_EXPIRATION = 86400; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The Entity service. |
|
| 33 | - * |
|
| 34 | - * @since 3.4.0 |
|
| 35 | - * @access private |
|
| 36 | - * @var \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 37 | - */ |
|
| 38 | - private $entity_service; |
|
| 31 | + /** |
|
| 32 | + * The Entity service. |
|
| 33 | + * |
|
| 34 | + * @since 3.4.0 |
|
| 35 | + * @access private |
|
| 36 | + * @var \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 37 | + */ |
|
| 38 | + private $entity_service; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Create a Wordlift_Entity_List_Service. |
|
| 42 | - * |
|
| 43 | - * @since 3.4.0 |
|
| 44 | - * |
|
| 45 | - * @param \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 46 | - */ |
|
| 47 | - public function __construct( $entity_service ) { |
|
| 40 | + /** |
|
| 41 | + * Create a Wordlift_Entity_List_Service. |
|
| 42 | + * |
|
| 43 | + * @since 3.4.0 |
|
| 44 | + * |
|
| 45 | + * @param \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 46 | + */ |
|
| 47 | + public function __construct( $entity_service ) { |
|
| 48 | 48 | |
| 49 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Dashboard_Service' ); |
|
| 49 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Dashboard_Service' ); |
|
| 50 | 50 | |
| 51 | - $this->entity_service = $entity_service; |
|
| 51 | + $this->entity_service = $entity_service; |
|
| 52 | 52 | |
| 53 | - } |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Return stats layout |
|
| 57 | - * |
|
| 58 | - * @since 3.4.0 |
|
| 59 | - * |
|
| 60 | - * @return string Dashboard widget html markup |
|
| 61 | - */ |
|
| 62 | - public function dashboard_widget_callback( $post ) { |
|
| 55 | + /** |
|
| 56 | + * Return stats layout |
|
| 57 | + * |
|
| 58 | + * @since 3.4.0 |
|
| 59 | + * |
|
| 60 | + * @return string Dashboard widget html markup |
|
| 61 | + */ |
|
| 62 | + public function dashboard_widget_callback( $post ) { |
|
| 63 | 63 | |
| 64 | - $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 65 | - $enriched_posts_caption = sprintf( wp_kses( |
|
| 66 | - __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 67 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 68 | - $this->render_stat_param( 'annotated_posts_percentage' ), |
|
| 69 | - esc_url( admin_url( 'edit.php' ) ), |
|
| 70 | - $this->render_stat_param( 'posts' ), |
|
| 71 | - $this->render_stat_param( 'annotated_posts' ) |
|
| 72 | - ); |
|
| 64 | + $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 65 | + $enriched_posts_caption = sprintf( wp_kses( |
|
| 66 | + __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 67 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 68 | + $this->render_stat_param( 'annotated_posts_percentage' ), |
|
| 69 | + esc_url( admin_url( 'edit.php' ) ), |
|
| 70 | + $this->render_stat_param( 'posts' ), |
|
| 71 | + $this->render_stat_param( 'annotated_posts' ) |
|
| 72 | + ); |
|
| 73 | 73 | |
| 74 | - $rating_title = __( 'avarage entity rating', 'wordlift' ); |
|
| 75 | - $rating_caption = sprintf( wp_kses( |
|
| 76 | - __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an avarage rating of %3$s.', 'wordlift' ), |
|
| 77 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 78 | - $this->render_stat_param( 'entities' ), |
|
| 79 | - esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 80 | - $this->render_stat_param( 'rating' ) |
|
| 81 | - ); |
|
| 74 | + $rating_title = __( 'avarage entity rating', 'wordlift' ); |
|
| 75 | + $rating_caption = sprintf( wp_kses( |
|
| 76 | + __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an avarage rating of %3$s.', 'wordlift' ), |
|
| 77 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 78 | + $this->render_stat_param( 'entities' ), |
|
| 79 | + esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 80 | + $this->render_stat_param( 'rating' ) |
|
| 81 | + ); |
|
| 82 | 82 | |
| 83 | - $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 84 | - $graph_caption = sprintf( wp_kses( |
|
| 85 | - __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 86 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 87 | - $this->render_stat_param( 'wikidata' ), |
|
| 88 | - esc_url( 'https://www.wikidata.org/' ) |
|
| 89 | - ); |
|
| 83 | + $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 84 | + $graph_caption = sprintf( wp_kses( |
|
| 85 | + __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 86 | + array( 'a' => array( 'href' => array() ) ) ), |
|
| 87 | + $this->render_stat_param( 'wikidata' ), |
|
| 88 | + esc_url( 'https://www.wikidata.org/' ) |
|
| 89 | + ); |
|
| 90 | 90 | |
| 91 | - $triples_label = __( 'triples', 'wordlift' ); |
|
| 91 | + $triples_label = __( 'triples', 'wordlift' ); |
|
| 92 | 92 | |
| 93 | - echo <<<EOF |
|
| 93 | + echo <<<EOF |
|
| 94 | 94 | <div id="wl-dashboard-widget-inner-wrapper"> |
| 95 | 95 | <div class="wl-stat-card"> |
| 96 | 96 | <div class="wl-stat-graph-wrapper"> |
@@ -119,156 +119,156 @@ discard block |
||
| 119 | 119 | </div> |
| 120 | 120 | EOF; |
| 121 | 121 | |
| 122 | - } |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * Return stats |
|
| 126 | - * |
|
| 127 | - * @since 3.4.0 |
|
| 128 | - * |
|
| 129 | - * @return string markup |
|
| 130 | - */ |
|
| 131 | - public function add_dashboard_widgets() { |
|
| 132 | - wp_add_dashboard_widget('wordlift-dashboard-widget', 'WordLift Dashboard', array( $this, 'dashboard_widget_callback' ) ); |
|
| 133 | - } |
|
| 124 | + /** |
|
| 125 | + * Return stats |
|
| 126 | + * |
|
| 127 | + * @since 3.4.0 |
|
| 128 | + * |
|
| 129 | + * @return string markup |
|
| 130 | + */ |
|
| 131 | + public function add_dashboard_widgets() { |
|
| 132 | + wp_add_dashboard_widget('wordlift-dashboard-widget', 'WordLift Dashboard', array( $this, 'dashboard_widget_callback' ) ); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Return stats |
|
| 137 | - * @uses https://codex.wordpress.org/Function_Reference/set_transient |
|
| 138 | - * |
|
| 139 | - * @since 3.4.0 |
|
| 140 | - * |
|
| 141 | - * @return string JSON obj with all available stats. |
|
| 142 | - */ |
|
| 143 | - public function ajax_get_stats() { |
|
| 135 | + /** |
|
| 136 | + * Return stats |
|
| 137 | + * @uses https://codex.wordpress.org/Function_Reference/set_transient |
|
| 138 | + * |
|
| 139 | + * @since 3.4.0 |
|
| 140 | + * |
|
| 141 | + * @return string JSON obj with all available stats. |
|
| 142 | + */ |
|
| 143 | + public function ajax_get_stats() { |
|
| 144 | 144 | |
| 145 | - // Try to retrieve the transient |
|
| 146 | - $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 145 | + // Try to retrieve the transient |
|
| 146 | + $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 147 | 147 | |
| 148 | - if ( !$stats ) { |
|
| 149 | - // Calculate stats |
|
| 150 | - $stats = array( |
|
| 151 | - 'entities' => $this->count_entities(), |
|
| 152 | - 'posts' => $this->count_posts(), |
|
| 153 | - 'annotated_posts' => $this->count_annotated_posts(), |
|
| 154 | - 'triples' => $this->count_triples(), |
|
| 155 | - 'rating' => $this->avarage_entities_rating(), |
|
| 156 | - ); |
|
| 157 | - // Cache stats results trough transient |
|
| 158 | - set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 159 | - } |
|
| 160 | - // Return stats as json object |
|
| 161 | - wl_core_send_json( $stats ); |
|
| 162 | - } |
|
| 148 | + if ( !$stats ) { |
|
| 149 | + // Calculate stats |
|
| 150 | + $stats = array( |
|
| 151 | + 'entities' => $this->count_entities(), |
|
| 152 | + 'posts' => $this->count_posts(), |
|
| 153 | + 'annotated_posts' => $this->count_annotated_posts(), |
|
| 154 | + 'triples' => $this->count_triples(), |
|
| 155 | + 'rating' => $this->avarage_entities_rating(), |
|
| 156 | + ); |
|
| 157 | + // Cache stats results trough transient |
|
| 158 | + set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 159 | + } |
|
| 160 | + // Return stats as json object |
|
| 161 | + wl_core_send_json( $stats ); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Calculate total number of published posts |
|
| 166 | - * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 167 | - * @since 3.4.0 |
|
| 168 | - * |
|
| 169 | - * @return int Total number of published posts. |
|
| 170 | - */ |
|
| 171 | - public function count_posts() { |
|
| 164 | + /** |
|
| 165 | + * Calculate total number of published posts |
|
| 166 | + * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 167 | + * @since 3.4.0 |
|
| 168 | + * |
|
| 169 | + * @return int Total number of published posts. |
|
| 170 | + */ |
|
| 171 | + public function count_posts() { |
|
| 172 | 172 | |
| 173 | - return (int) wp_count_posts()->publish; |
|
| 174 | - } |
|
| 173 | + return (int) wp_count_posts()->publish; |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * Calculate total number of annotated published posts |
|
| 178 | - * @since 3.4.0 |
|
| 179 | - * |
|
| 180 | - * @return int Total number of annotated published posts. |
|
| 181 | - */ |
|
| 182 | - public function count_annotated_posts() { |
|
| 176 | + /** |
|
| 177 | + * Calculate total number of annotated published posts |
|
| 178 | + * @since 3.4.0 |
|
| 179 | + * |
|
| 180 | + * @return int Total number of annotated published posts. |
|
| 181 | + */ |
|
| 182 | + public function count_annotated_posts() { |
|
| 183 | 183 | |
| 184 | - // Prepare interaction with db |
|
| 185 | - global $wpdb; |
|
| 186 | - // Retrieve Wordlift relation instances table name |
|
| 187 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 188 | - // Calculate sql statement |
|
| 189 | - $sql_statement = <<<EOF |
|
| 184 | + // Prepare interaction with db |
|
| 185 | + global $wpdb; |
|
| 186 | + // Retrieve Wordlift relation instances table name |
|
| 187 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 188 | + // Calculate sql statement |
|
| 189 | + $sql_statement = <<<EOF |
|
| 190 | 190 | SELECT COUNT(*) 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'; |
| 191 | 191 | EOF; |
| 192 | - // Perform the query |
|
| 193 | - return (int) $wpdb->get_var( $sql_statement ); |
|
| 192 | + // Perform the query |
|
| 193 | + return (int) $wpdb->get_var( $sql_statement ); |
|
| 194 | 194 | |
| 195 | - } |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - /** |
|
| 198 | - * Calculate the avarage entities rating |
|
| 199 | - * @since 3.4.0 |
|
| 200 | - * |
|
| 201 | - * @return int Avarage entities rating. |
|
| 202 | - */ |
|
| 203 | - public function avarage_entities_rating() { |
|
| 197 | + /** |
|
| 198 | + * Calculate the avarage entities rating |
|
| 199 | + * @since 3.4.0 |
|
| 200 | + * |
|
| 201 | + * @return int Avarage entities rating. |
|
| 202 | + */ |
|
| 203 | + public function avarage_entities_rating() { |
|
| 204 | 204 | |
| 205 | - // Prepare interaction with db |
|
| 206 | - global $wpdb; |
|
| 207 | - $query = $wpdb->prepare( |
|
| 208 | - "SELECT AVG(meta_value) FROM $wpdb->postmeta where meta_key = %s", |
|
| 209 | - Wordlift_Entity_Service::RATING_RAW_SCORE_META_KEY |
|
| 210 | - ); |
|
| 211 | - // Perform the query |
|
| 212 | - return $this->entity_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 213 | - } |
|
| 205 | + // Prepare interaction with db |
|
| 206 | + global $wpdb; |
|
| 207 | + $query = $wpdb->prepare( |
|
| 208 | + "SELECT AVG(meta_value) FROM $wpdb->postmeta where meta_key = %s", |
|
| 209 | + Wordlift_Entity_Service::RATING_RAW_SCORE_META_KEY |
|
| 210 | + ); |
|
| 211 | + // Perform the query |
|
| 212 | + return $this->entity_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * Calculate total number of published entities |
|
| 217 | - * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 218 | - * @since 3.4.0 |
|
| 219 | - * |
|
| 220 | - * @return int Total number of posts. |
|
| 221 | - */ |
|
| 222 | - public function count_entities() { |
|
| 215 | + /** |
|
| 216 | + * Calculate total number of published entities |
|
| 217 | + * @uses https://codex.wordpress.org/it:Riferimento_funzioni/wp_count_posts |
|
| 218 | + * @since 3.4.0 |
|
| 219 | + * |
|
| 220 | + * @return int Total number of posts. |
|
| 221 | + */ |
|
| 222 | + public function count_entities() { |
|
| 223 | 223 | |
| 224 | - return (int) wp_count_posts( Wordlift_Entity_Service::TYPE_NAME )->publish; |
|
| 225 | - } |
|
| 224 | + return (int) wp_count_posts( Wordlift_Entity_Service::TYPE_NAME )->publish; |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - /** |
|
| 228 | - * Calculate total number of published rdf triples |
|
| 229 | - * @since 3.4.0 |
|
| 230 | - * |
|
| 231 | - * @return int Total number of triples. |
|
| 232 | - */ |
|
| 233 | - public function count_triples() { |
|
| 227 | + /** |
|
| 228 | + * Calculate total number of published rdf triples |
|
| 229 | + * @since 3.4.0 |
|
| 230 | + * |
|
| 231 | + * @return int Total number of triples. |
|
| 232 | + */ |
|
| 233 | + public function count_triples() { |
|
| 234 | 234 | |
| 235 | - // Set the SPARQL query. |
|
| 236 | - $sparql = 'SELECT (COUNT(*) AS ?no) { ?s ?p ?o }'; |
|
| 237 | - // Send the request. |
|
| 238 | - $response = $this->rl_sparql_select( $sparql ); |
|
| 235 | + // Set the SPARQL query. |
|
| 236 | + $sparql = 'SELECT (COUNT(*) AS ?no) { ?s ?p ?o }'; |
|
| 237 | + // Send the request. |
|
| 238 | + $response = $this->rl_sparql_select( $sparql ); |
|
| 239 | 239 | |
| 240 | - // Return the error in case of failure. |
|
| 241 | - if ( is_wp_error( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 242 | - return (int) FALSE; |
|
| 243 | - } |
|
| 240 | + // Return the error in case of failure. |
|
| 241 | + if ( is_wp_error( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 242 | + return (int) FALSE; |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - // Get the body. |
|
| 246 | - $body = $response['body']; |
|
| 247 | - // Get the values. |
|
| 248 | - $matches = array(); |
|
| 249 | - if ( 1 === preg_match( '/(\d+)/im', $body, $matches ) && 2 === count( $matches ) ) { |
|
| 250 | - // Return the counts. |
|
| 251 | - return (int) $matches[1]; |
|
| 252 | - } |
|
| 245 | + // Get the body. |
|
| 246 | + $body = $response['body']; |
|
| 247 | + // Get the values. |
|
| 248 | + $matches = array(); |
|
| 249 | + if ( 1 === preg_match( '/(\d+)/im', $body, $matches ) && 2 === count( $matches ) ) { |
|
| 250 | + // Return the counts. |
|
| 251 | + return (int) $matches[1]; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - return (int) FALSE; |
|
| 255 | - } |
|
| 254 | + return (int) FALSE; |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - private function rl_sparql_select( $query ) { |
|
| 257 | + private function rl_sparql_select( $query ) { |
|
| 258 | 258 | |
| 259 | - // Prepare the SPARQL statement by prepending the default namespaces. |
|
| 260 | - $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 261 | - // Get the SPARQL SELECT URL. |
|
| 262 | - $url = wl_configuration_get_query_select_url( 'csv' ) . urlencode( $sparql ); |
|
| 263 | - // Prepare the request. |
|
| 264 | - $args = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 259 | + // Prepare the SPARQL statement by prepending the default namespaces. |
|
| 260 | + $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 261 | + // Get the SPARQL SELECT URL. |
|
| 262 | + $url = wl_configuration_get_query_select_url( 'csv' ) . urlencode( $sparql ); |
|
| 263 | + // Prepare the request. |
|
| 264 | + $args = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 265 | 265 | |
| 266 | - return wp_remote_get( $url, $args ); |
|
| 267 | - } |
|
| 266 | + return wp_remote_get( $url, $args ); |
|
| 267 | + } |
|
| 268 | 268 | |
| 269 | - private function render_stat_param( $param ) { |
|
| 269 | + private function render_stat_param( $param ) { |
|
| 270 | 270 | |
| 271 | - return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
|
| 272 | - } |
|
| 271 | + return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | 274 | } |
@@ -44,9 +44,9 @@ discard block |
||
| 44 | 44 | * |
| 45 | 45 | * @param \Wordlift_Entity_Service $entity_service The Entity service. |
| 46 | 46 | */ |
| 47 | - public function __construct( $entity_service ) { |
|
| 47 | + public function __construct($entity_service) { |
|
| 48 | 48 | |
| 49 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Dashboard_Service' ); |
|
| 49 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Dashboard_Service'); |
|
| 50 | 50 | |
| 51 | 51 | $this->entity_service = $entity_service; |
| 52 | 52 | |
@@ -59,36 +59,36 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @return string Dashboard widget html markup |
| 61 | 61 | */ |
| 62 | - public function dashboard_widget_callback( $post ) { |
|
| 62 | + public function dashboard_widget_callback($post) { |
|
| 63 | 63 | |
| 64 | - $enriched_posts_title = __( 'enriched posts', 'wordlift' ); |
|
| 65 | - $enriched_posts_caption = sprintf( wp_kses( |
|
| 66 | - __( '%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift' ), |
|
| 67 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 68 | - $this->render_stat_param( 'annotated_posts_percentage' ), |
|
| 69 | - esc_url( admin_url( 'edit.php' ) ), |
|
| 70 | - $this->render_stat_param( 'posts' ), |
|
| 71 | - $this->render_stat_param( 'annotated_posts' ) |
|
| 64 | + $enriched_posts_title = __('enriched posts', 'wordlift'); |
|
| 65 | + $enriched_posts_caption = sprintf(wp_kses( |
|
| 66 | + __('%1$s, of your <a href="%2$s">posts</a> are annotated. This means %3$s annotated posts on %4$s.', 'wordlift'), |
|
| 67 | + array('a' => array('href' => array())) ), |
|
| 68 | + $this->render_stat_param('annotated_posts_percentage'), |
|
| 69 | + esc_url(admin_url('edit.php')), |
|
| 70 | + $this->render_stat_param('posts'), |
|
| 71 | + $this->render_stat_param('annotated_posts') |
|
| 72 | 72 | ); |
| 73 | 73 | |
| 74 | - $rating_title = __( 'avarage entity rating', 'wordlift' ); |
|
| 75 | - $rating_caption = sprintf( wp_kses( |
|
| 76 | - __( 'You have %1$s entities in your <a href="%2$s">vocabulary</a> with an avarage rating of %3$s.', 'wordlift' ), |
|
| 77 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 78 | - $this->render_stat_param( 'entities' ), |
|
| 79 | - esc_url( admin_url( 'edit.php?post_type=entity' ) ), |
|
| 80 | - $this->render_stat_param( 'rating' ) |
|
| 74 | + $rating_title = __('avarage entity rating', 'wordlift'); |
|
| 75 | + $rating_caption = sprintf(wp_kses( |
|
| 76 | + __('You have %1$s entities in your <a href="%2$s">vocabulary</a> with an avarage rating of %3$s.', 'wordlift'), |
|
| 77 | + array('a' => array('href' => array())) ), |
|
| 78 | + $this->render_stat_param('entities'), |
|
| 79 | + esc_url(admin_url('edit.php?post_type=entity')), |
|
| 80 | + $this->render_stat_param('rating') |
|
| 81 | 81 | ); |
| 82 | 82 | |
| 83 | - $graph_title = __( 'triples in your graph', 'wordlift' ); |
|
| 84 | - $graph_caption = sprintf( wp_kses( |
|
| 85 | - __( 'Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift' ), |
|
| 86 | - array( 'a' => array( 'href' => array() ) ) ), |
|
| 87 | - $this->render_stat_param( 'wikidata' ), |
|
| 88 | - esc_url( 'https://www.wikidata.org/' ) |
|
| 83 | + $graph_title = __('triples in your graph', 'wordlift'); |
|
| 84 | + $graph_caption = sprintf(wp_kses( |
|
| 85 | + __('Your graphs size corresponds to %1$s of <a href="%2$s">Wikidata</a>.', 'wordlift'), |
|
| 86 | + array('a' => array('href' => array())) ), |
|
| 87 | + $this->render_stat_param('wikidata'), |
|
| 88 | + esc_url('https://www.wikidata.org/') |
|
| 89 | 89 | ); |
| 90 | 90 | |
| 91 | - $triples_label = __( 'triples', 'wordlift' ); |
|
| 91 | + $triples_label = __('triples', 'wordlift'); |
|
| 92 | 92 | |
| 93 | 93 | echo <<<EOF |
| 94 | 94 | <div id="wl-dashboard-widget-inner-wrapper"> |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | * @return string markup |
| 130 | 130 | */ |
| 131 | 131 | public function add_dashboard_widgets() { |
| 132 | - wp_add_dashboard_widget('wordlift-dashboard-widget', 'WordLift Dashboard', array( $this, 'dashboard_widget_callback' ) ); |
|
| 132 | + wp_add_dashboard_widget('wordlift-dashboard-widget', 'WordLift Dashboard', array($this, 'dashboard_widget_callback')); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | /** |
@@ -143,9 +143,9 @@ discard block |
||
| 143 | 143 | public function ajax_get_stats() { |
| 144 | 144 | |
| 145 | 145 | // Try to retrieve the transient |
| 146 | - $stats = get_transient( self::TRANSIENT_NAME ); |
|
| 146 | + $stats = get_transient(self::TRANSIENT_NAME); |
|
| 147 | 147 | |
| 148 | - if ( !$stats ) { |
|
| 148 | + if ( ! $stats) { |
|
| 149 | 149 | // Calculate stats |
| 150 | 150 | $stats = array( |
| 151 | 151 | 'entities' => $this->count_entities(), |
@@ -155,10 +155,10 @@ discard block |
||
| 155 | 155 | 'rating' => $this->avarage_entities_rating(), |
| 156 | 156 | ); |
| 157 | 157 | // Cache stats results trough transient |
| 158 | - set_transient( self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION ); |
|
| 158 | + set_transient(self::TRANSIENT_NAME, $stats, self::TRANSIENT_EXPIRATION); |
|
| 159 | 159 | } |
| 160 | 160 | // Return stats as json object |
| 161 | - wl_core_send_json( $stats ); |
|
| 161 | + wl_core_send_json($stats); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | /** |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | SELECT COUNT(*) 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'; |
| 191 | 191 | EOF; |
| 192 | 192 | // Perform the query |
| 193 | - return (int) $wpdb->get_var( $sql_statement ); |
|
| 193 | + return (int) $wpdb->get_var($sql_statement); |
|
| 194 | 194 | |
| 195 | 195 | } |
| 196 | 196 | |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | Wordlift_Entity_Service::RATING_RAW_SCORE_META_KEY |
| 210 | 210 | ); |
| 211 | 211 | // Perform the query |
| 212 | - return $this->entity_service->convert_raw_score_to_percentage( $wpdb->get_var( $query ) ); |
|
| 212 | + return $this->entity_service->convert_raw_score_to_percentage($wpdb->get_var($query)); |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | /** |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | */ |
| 222 | 222 | public function count_entities() { |
| 223 | 223 | |
| 224 | - return (int) wp_count_posts( Wordlift_Entity_Service::TYPE_NAME )->publish; |
|
| 224 | + return (int) wp_count_posts(Wordlift_Entity_Service::TYPE_NAME)->publish; |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | /** |
@@ -235,10 +235,10 @@ discard block |
||
| 235 | 235 | // Set the SPARQL query. |
| 236 | 236 | $sparql = 'SELECT (COUNT(*) AS ?no) { ?s ?p ?o }'; |
| 237 | 237 | // Send the request. |
| 238 | - $response = $this->rl_sparql_select( $sparql ); |
|
| 238 | + $response = $this->rl_sparql_select($sparql); |
|
| 239 | 239 | |
| 240 | 240 | // Return the error in case of failure. |
| 241 | - if ( is_wp_error( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 241 | + if (is_wp_error($response) || 200 !== (int) $response['response']['code']) { |
|
| 242 | 242 | return (int) FALSE; |
| 243 | 243 | } |
| 244 | 244 | |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | $body = $response['body']; |
| 247 | 247 | // Get the values. |
| 248 | 248 | $matches = array(); |
| 249 | - if ( 1 === preg_match( '/(\d+)/im', $body, $matches ) && 2 === count( $matches ) ) { |
|
| 249 | + if (1 === preg_match('/(\d+)/im', $body, $matches) && 2 === count($matches)) { |
|
| 250 | 250 | // Return the counts. |
| 251 | 251 | return (int) $matches[1]; |
| 252 | 252 | } |
@@ -254,21 +254,21 @@ discard block |
||
| 254 | 254 | return (int) FALSE; |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - private function rl_sparql_select( $query ) { |
|
| 257 | + private function rl_sparql_select($query) { |
|
| 258 | 258 | |
| 259 | 259 | // Prepare the SPARQL statement by prepending the default namespaces. |
| 260 | - $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 260 | + $sparql = rl_sparql_prefixes()."\n".$query; |
|
| 261 | 261 | // Get the SPARQL SELECT URL. |
| 262 | - $url = wl_configuration_get_query_select_url( 'csv' ) . urlencode( $sparql ); |
|
| 262 | + $url = wl_configuration_get_query_select_url('csv').urlencode($sparql); |
|
| 263 | 263 | // Prepare the request. |
| 264 | - $args = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 264 | + $args = unserialize(WL_REDLINK_API_HTTP_OPTIONS); |
|
| 265 | 265 | |
| 266 | - return wp_remote_get( $url, $args ); |
|
| 266 | + return wp_remote_get($url, $args); |
|
| 267 | 267 | } |
| 268 | 268 | |
| 269 | - private function render_stat_param( $param ) { |
|
| 269 | + private function render_stat_param($param) { |
|
| 270 | 270 | |
| 271 | - return '<span id="wl-dashboard-widget-' . $param . '" class="wl-stat-value"></span>'; |
|
| 271 | + return '<span id="wl-dashboard-widget-'.$param.'" class="wl-stat-value"></span>'; |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | } |