@@ -18,59 +18,59 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Batch_Analysis_Request_Async_Task extends Wordlift_Async_Task { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The protected $action property should be set to the action to which you |
|
| 23 | - * wish to attach the asynchronous task. For example, if you want to spin |
|
| 24 | - * off an asynchronous task whenever a post gets saved, you would set this |
|
| 25 | - * to save_post. |
|
| 26 | - * |
|
| 27 | - * @since 3.14.2 |
|
| 28 | - * @access protected |
|
| 29 | - * @var string $action The action to which you wish to attach the |
|
| 30 | - * asynchronous task. |
|
| 31 | - */ |
|
| 32 | - protected $action = 'wl_batch_analysis_request'; |
|
| 21 | + /** |
|
| 22 | + * The protected $action property should be set to the action to which you |
|
| 23 | + * wish to attach the asynchronous task. For example, if you want to spin |
|
| 24 | + * off an asynchronous task whenever a post gets saved, you would set this |
|
| 25 | + * to save_post. |
|
| 26 | + * |
|
| 27 | + * @since 3.14.2 |
|
| 28 | + * @access protected |
|
| 29 | + * @var string $action The action to which you wish to attach the |
|
| 30 | + * asynchronous task. |
|
| 31 | + */ |
|
| 32 | + protected $action = 'wl_batch_analysis_request'; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * A {@link Wordlift_Log_Service} instance. |
|
| 36 | - * |
|
| 37 | - * @since 3.14.2 |
|
| 38 | - * @access private |
|
| 39 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 40 | - */ |
|
| 41 | - private $log; |
|
| 34 | + /** |
|
| 35 | + * A {@link Wordlift_Log_Service} instance. |
|
| 36 | + * |
|
| 37 | + * @since 3.14.2 |
|
| 38 | + * @access private |
|
| 39 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 40 | + */ |
|
| 41 | + private $log; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Create a {@link Wordlift_Sparql_Query_Async_Task} instance. |
|
| 45 | - * |
|
| 46 | - * @since 3.14.2 |
|
| 47 | - * |
|
| 48 | - * @param int $auth_level The authentication level to use (see above) |
|
| 49 | - */ |
|
| 50 | - public function __construct( $auth_level = self::BOTH ) { |
|
| 51 | - parent::__construct( $auth_level ); |
|
| 43 | + /** |
|
| 44 | + * Create a {@link Wordlift_Sparql_Query_Async_Task} instance. |
|
| 45 | + * |
|
| 46 | + * @since 3.14.2 |
|
| 47 | + * |
|
| 48 | + * @param int $auth_level The authentication level to use (see above) |
|
| 49 | + */ |
|
| 50 | + public function __construct( $auth_level = self::BOTH ) { |
|
| 51 | + parent::__construct( $auth_level ); |
|
| 52 | 52 | |
| 53 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Batch_Analysis_Request_Async_Task' ); |
|
| 53 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Batch_Analysis_Request_Async_Task' ); |
|
| 54 | 54 | |
| 55 | - } |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @inheritdoc |
|
| 59 | - */ |
|
| 60 | - protected function prepare_data( $data ) { |
|
| 57 | + /** |
|
| 58 | + * @inheritdoc |
|
| 59 | + */ |
|
| 60 | + protected function prepare_data( $data ) { |
|
| 61 | 61 | |
| 62 | - // Return the link setting. |
|
| 63 | - return array(); |
|
| 64 | - } |
|
| 62 | + // Return the link setting. |
|
| 63 | + return array(); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @inheritdoc |
|
| 68 | - */ |
|
| 69 | - protected function run_action() { |
|
| 66 | + /** |
|
| 67 | + * @inheritdoc |
|
| 68 | + */ |
|
| 69 | + protected function run_action() { |
|
| 70 | 70 | |
| 71 | - // Run the asynchronous action. |
|
| 72 | - do_action( "wl_async_$this->action" ); |
|
| 71 | + // Run the asynchronous action. |
|
| 72 | + do_action( "wl_async_$this->action" ); |
|
| 73 | 73 | |
| 74 | - } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | 76 | } |
@@ -47,17 +47,17 @@ discard block |
||
| 47 | 47 | * |
| 48 | 48 | * @param int $auth_level The authentication level to use (see above) |
| 49 | 49 | */ |
| 50 | - public function __construct( $auth_level = self::BOTH ) { |
|
| 51 | - parent::__construct( $auth_level ); |
|
| 50 | + public function __construct($auth_level = self::BOTH) { |
|
| 51 | + parent::__construct($auth_level); |
|
| 52 | 52 | |
| 53 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Batch_Analysis_Request_Async_Task' ); |
|
| 53 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Batch_Analysis_Request_Async_Task'); |
|
| 54 | 54 | |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * @inheritdoc |
| 59 | 59 | */ |
| 60 | - protected function prepare_data( $data ) { |
|
| 60 | + protected function prepare_data($data) { |
|
| 61 | 61 | |
| 62 | 62 | // Return the link setting. |
| 63 | 63 | return array(); |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | protected function run_action() { |
| 70 | 70 | |
| 71 | 71 | // Run the asynchronous action. |
| 72 | - do_action( "wl_async_$this->action" ); |
|
| 72 | + do_action("wl_async_$this->action"); |
|
| 73 | 73 | |
| 74 | 74 | } |
| 75 | 75 | |
@@ -8,13 +8,13 @@ discard block |
||
| 8 | 8 | * @return string The updated list of styles, including the custom style provided by WordLift. |
| 9 | 9 | */ |
| 10 | 10 | function wordlift_mce_css( $mce_css ) { |
| 11 | - if ( ! empty( $mce_css ) ) { |
|
| 12 | - $mce_css .= ','; |
|
| 13 | - } |
|
| 11 | + if ( ! empty( $mce_css ) ) { |
|
| 12 | + $mce_css .= ','; |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - $mce_css .= plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css'; |
|
| 15 | + $mce_css .= plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css'; |
|
| 16 | 16 | |
| 17 | - return $mce_css; |
|
| 17 | + return $mce_css; |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | // hook the TinyMCE custom styles function. |
@@ -29,13 +29,13 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | function wordlift_filter_tiny_mce_before_init( $options ) { |
| 31 | 31 | |
| 32 | - if ( ! isset( $options['extended_valid_elements'] ) ) { |
|
| 33 | - $options['extended_valid_elements'] = ''; |
|
| 34 | - } |
|
| 32 | + if ( ! isset( $options['extended_valid_elements'] ) ) { |
|
| 33 | + $options['extended_valid_elements'] = ''; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - $options['extended_valid_elements'] .= ",span[*]"; |
|
| 36 | + $options['extended_valid_elements'] .= ",span[*]"; |
|
| 37 | 37 | |
| 38 | - return $options; |
|
| 38 | + return $options; |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | add_filter( 'tiny_mce_before_init', 'wordlift_filter_tiny_mce_before_init' ); |
@@ -7,18 +7,18 @@ discard block |
||
| 7 | 7 | * |
| 8 | 8 | * @return string The updated list of styles, including the custom style provided by WordLift. |
| 9 | 9 | */ |
| 10 | -function wordlift_mce_css( $mce_css ) { |
|
| 11 | - if ( ! empty( $mce_css ) ) { |
|
| 10 | +function wordlift_mce_css($mce_css) { |
|
| 11 | + if ( ! empty($mce_css)) { |
|
| 12 | 12 | $mce_css .= ','; |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | - $mce_css .= plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css'; |
|
| 15 | + $mce_css .= plugin_dir_url(__FILE__).'css/wordlift-reloaded.min.css'; |
|
| 16 | 16 | |
| 17 | 17 | return $mce_css; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | // hook the TinyMCE custom styles function. |
| 21 | -add_filter( 'mce_css', 'wordlift_mce_css' ); |
|
| 21 | +add_filter('mce_css', 'wordlift_mce_css'); |
|
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * Set TinyMCE options, in particular enable microdata tagging. |
@@ -27,9 +27,9 @@ discard block |
||
| 27 | 27 | * |
| 28 | 28 | * @return mixed |
| 29 | 29 | */ |
| 30 | -function wordlift_filter_tiny_mce_before_init( $options ) { |
|
| 30 | +function wordlift_filter_tiny_mce_before_init($options) { |
|
| 31 | 31 | |
| 32 | - if ( ! isset( $options['extended_valid_elements'] ) ) { |
|
| 32 | + if ( ! isset($options['extended_valid_elements'])) { |
|
| 33 | 33 | $options['extended_valid_elements'] = ''; |
| 34 | 34 | } |
| 35 | 35 | |
@@ -38,4 +38,4 @@ discard block |
||
| 38 | 38 | return $options; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | -add_filter( 'tiny_mce_before_init', 'wordlift_filter_tiny_mce_before_init' ); |
|
| 41 | +add_filter('tiny_mce_before_init', 'wordlift_filter_tiny_mce_before_init'); |
|
@@ -27,26 +27,26 @@ discard block |
||
| 27 | 27 | * |
| 28 | 28 | * @return int|false Attachment ID on success, false on failure |
| 29 | 29 | */ |
| 30 | - public function get_attachment_id( $url ) { |
|
| 30 | + public function get_attachment_id($url) { |
|
| 31 | 31 | |
| 32 | 32 | // Get the upload directory data, we need the base URL to check whether |
| 33 | 33 | // the URL we received is within WP. |
| 34 | 34 | $dir = wp_upload_dir(); |
| 35 | 35 | |
| 36 | 36 | // If the image is not inside WP's base url, return. |
| 37 | - if ( false === strpos( $url, $dir['baseurl'] . '/' ) ) { |
|
| 37 | + if (false === strpos($url, $dir['baseurl'].'/')) { |
|
| 38 | 38 | return false; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | // Get the filename, the extension is kept. |
| 42 | - $filename = basename( $url ); |
|
| 42 | + $filename = basename($url); |
|
| 43 | 43 | |
| 44 | 44 | // The following query is CPU-intensive, we need to review it. |
| 45 | 45 | // |
| 46 | 46 | // See https://github.com/insideout10/wordlift-plugin/issues/689. |
| 47 | 47 | // |
| 48 | 48 | // Query for attachments with the specified filename. |
| 49 | - $query = new WP_Query( array( |
|
| 49 | + $query = new WP_Query(array( |
|
| 50 | 50 | 'post_type' => 'attachment', |
| 51 | 51 | 'post_status' => 'inherit', |
| 52 | 52 | 'fields' => 'ids', |
@@ -57,21 +57,21 @@ discard block |
||
| 57 | 57 | 'key' => '_wp_attachment_metadata', |
| 58 | 58 | ), |
| 59 | 59 | ), |
| 60 | - ) ); |
|
| 60 | + )); |
|
| 61 | 61 | |
| 62 | 62 | // If there are no posts, return. |
| 63 | - if ( $query->have_posts() ) { |
|
| 64 | - foreach ( $query->posts as $attachment_id ) { |
|
| 63 | + if ($query->have_posts()) { |
|
| 64 | + foreach ($query->posts as $attachment_id) { |
|
| 65 | 65 | |
| 66 | 66 | // Get the attachment metadata, we need the filename. |
| 67 | - $metadata = wp_get_attachment_metadata( $attachment_id ); |
|
| 68 | - $original_filename = basename( $metadata['file'] ); |
|
| 67 | + $metadata = wp_get_attachment_metadata($attachment_id); |
|
| 68 | + $original_filename = basename($metadata['file']); |
|
| 69 | 69 | |
| 70 | 70 | // Get the cropped filenames, or an empty array in case there are no files. |
| 71 | - $sizes_filenames = isset( $metadata['sizes'] ) ? wp_list_pluck( $metadata['sizes'], 'file' ) : array(); |
|
| 71 | + $sizes_filenames = isset($metadata['sizes']) ? wp_list_pluck($metadata['sizes'], 'file') : array(); |
|
| 72 | 72 | |
| 73 | 73 | // If the provided filename matches the attachment filename (or one of its resized images), return the id. |
| 74 | - if ( $original_filename === $filename || in_array( $filename, $sizes_filenames ) ) { |
|
| 74 | + if ($original_filename === $filename || in_array($filename, $sizes_filenames)) { |
|
| 75 | 75 | return $attachment_id; |
| 76 | 76 | } |
| 77 | 77 | } |
@@ -90,23 +90,23 @@ discard block |
||
| 90 | 90 | * |
| 91 | 91 | * @return array An array of attachment ids. |
| 92 | 92 | */ |
| 93 | - public function get_image_embeds( $content ) { |
|
| 93 | + public function get_image_embeds($content) { |
|
| 94 | 94 | |
| 95 | 95 | // Go over all the images included in the post content, check if they are |
| 96 | 96 | // in the DB, and if so include them. |
| 97 | 97 | $images = array(); |
| 98 | - if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
| 98 | + if (false === preg_match_all('#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images)) { |
|
| 99 | 99 | return array(); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | // Map the image URLs to attachment ids. |
| 103 | 103 | $that = $this; |
| 104 | - $ids = array_map( function ( $url ) use ( $that ) { |
|
| 105 | - return $that->get_attachment_id( $url ); |
|
| 106 | - }, $images[1] ); |
|
| 104 | + $ids = array_map(function($url) use ($that) { |
|
| 105 | + return $that->get_attachment_id($url); |
|
| 106 | + }, $images[1]); |
|
| 107 | 107 | |
| 108 | 108 | // Filter out not found ids (i.e. id is false). |
| 109 | - return array_filter( $ids, function ( $item ) { |
|
| 109 | + return array_filter($ids, function($item) { |
|
| 110 | 110 | return false !== $item; |
| 111 | 111 | } ); |
| 112 | 112 | } |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | * |
| 121 | 121 | * @return array An array of attachment ids. |
| 122 | 122 | */ |
| 123 | - public function get_gallery( $post ) { |
|
| 123 | + public function get_gallery($post) { |
|
| 124 | 124 | |
| 125 | 125 | // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
| 126 | 126 | // checked at the moment. |
@@ -132,38 +132,38 @@ discard block |
||
| 132 | 132 | // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
| 133 | 133 | $pattern = get_shortcode_regex(); |
| 134 | 134 | |
| 135 | - if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
| 136 | - && array_key_exists( 2, $matches ) |
|
| 137 | - && in_array( 'gallery', $matches[2] ) |
|
| 135 | + if (preg_match_all('/'.$pattern.'/s', $post->post_content, $matches) |
|
| 136 | + && array_key_exists(2, $matches) |
|
| 137 | + && in_array('gallery', $matches[2]) |
|
| 138 | 138 | ) { |
| 139 | 139 | |
| 140 | - $keys = array_keys( $matches[2], 'gallery' ); |
|
| 140 | + $keys = array_keys($matches[2], 'gallery'); |
|
| 141 | 141 | |
| 142 | - foreach ( $keys as $key ) { |
|
| 143 | - $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
| 142 | + foreach ($keys as $key) { |
|
| 143 | + $atts = shortcode_parse_atts($matches[3][$key]); |
|
| 144 | 144 | |
| 145 | - if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
| 145 | + if (is_array($atts) && array_key_exists('ids', $atts)) { |
|
| 146 | 146 | // gallery images insert explicitly by their ids. |
| 147 | 147 | |
| 148 | - foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
| 148 | + foreach (explode(',', $atts['ids']) as $attachment_id) { |
|
| 149 | 149 | // Since we do not check for actual image existence |
| 150 | 150 | // when generating the json content, check it now. |
| 151 | - if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
| 152 | - $ids[ $attachment_id ] = true; |
|
| 151 | + if (wp_get_attachment_image_src($attachment_id, 'full')) { |
|
| 152 | + $ids[$attachment_id] = true; |
|
| 153 | 153 | } |
| 154 | 154 | } |
| 155 | 155 | } else { |
| 156 | 156 | // gallery shortcode with no ids uses all the images |
| 157 | 157 | // attached to the post. |
| 158 | - $images = get_attached_media( 'image', $post->ID ); |
|
| 159 | - foreach ( $images as $attachment ) { |
|
| 160 | - $ids[ $attachment->ID ] = true; |
|
| 158 | + $images = get_attached_media('image', $post->ID); |
|
| 159 | + foreach ($images as $attachment) { |
|
| 160 | + $ids[$attachment->ID] = true; |
|
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | 163 | } |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - return array_keys( $ids ); |
|
| 166 | + return array_keys($ids); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | } |
@@ -16,186 +16,186 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Attachment_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The singleton instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.20.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Attachment_Service The singleton instance. |
|
| 25 | - */ |
|
| 26 | - private static $instance; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Create a {@link Wordlift_Attachment_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.20.0 |
|
| 32 | - */ |
|
| 33 | - public function __construct() { |
|
| 34 | - |
|
| 35 | - self::$instance = $this; |
|
| 36 | - |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Get the singleton instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.20.0 |
|
| 43 | - * |
|
| 44 | - * @return \Wordlift_Attachment_Service The singleton instance. |
|
| 45 | - */ |
|
| 46 | - public static function get_instance() { |
|
| 47 | - |
|
| 48 | - return self::$instance; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Get an attachment ID given a URL. |
|
| 53 | - * |
|
| 54 | - * Inspired from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
|
| 55 | - * |
|
| 56 | - * @since 3.10.0 |
|
| 57 | - * |
|
| 58 | - * @param string $url The attachment URL. |
|
| 59 | - * |
|
| 60 | - * @return int|false Attachment ID on success, false on failure |
|
| 61 | - */ |
|
| 62 | - public function get_attachment_id( $url ) { |
|
| 63 | - |
|
| 64 | - // Get the upload directory data, we need the base URL to check whether |
|
| 65 | - // the URL we received is within WP. |
|
| 66 | - $dir = wp_upload_dir(); |
|
| 67 | - |
|
| 68 | - // If the image is not inside WP's base url, return. |
|
| 69 | - if ( false === strpos( $url, $dir['baseurl'] . '/' ) ) { |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - // Get the filename, the extension is kept. |
|
| 74 | - $filename = basename( $url ); |
|
| 75 | - |
|
| 76 | - // The following query is CPU-intensive, we need to review it. |
|
| 77 | - // |
|
| 78 | - // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 79 | - // |
|
| 80 | - // Query for attachments with the specified filename. |
|
| 81 | - $query = new WP_Query( array( |
|
| 82 | - 'post_type' => 'attachment', |
|
| 83 | - 'post_status' => 'inherit', |
|
| 84 | - 'fields' => 'ids', |
|
| 85 | - 'meta_query' => array( |
|
| 86 | - array( |
|
| 87 | - 'value' => $filename, |
|
| 88 | - 'compare' => 'LIKE', |
|
| 89 | - 'key' => '_wp_attachment_metadata', |
|
| 90 | - ), |
|
| 91 | - ), |
|
| 92 | - ) ); |
|
| 93 | - |
|
| 94 | - // If there are no posts, return. |
|
| 95 | - if ( $query->have_posts() ) { |
|
| 96 | - foreach ( $query->posts as $attachment_id ) { |
|
| 97 | - |
|
| 98 | - // Get the attachment metadata, we need the filename. |
|
| 99 | - $metadata = wp_get_attachment_metadata( $attachment_id ); |
|
| 100 | - $original_filename = basename( $metadata['file'] ); |
|
| 101 | - |
|
| 102 | - // Get the cropped filenames, or an empty array in case there are no files. |
|
| 103 | - $sizes_filenames = isset( $metadata['sizes'] ) ? wp_list_pluck( $metadata['sizes'], 'file' ) : array(); |
|
| 104 | - |
|
| 105 | - // If the provided filename matches the attachment filename (or one of its resized images), return the id. |
|
| 106 | - if ( $original_filename === $filename || in_array( $filename, $sizes_filenames ) ) { |
|
| 107 | - return $attachment_id; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // If we got here, we couldn't find any attachment. |
|
| 113 | - return false; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Get images embedded in the post content. |
|
| 118 | - * |
|
| 119 | - * @since 3.10.0 |
|
| 120 | - * |
|
| 121 | - * @param string $content The post content. |
|
| 122 | - * |
|
| 123 | - * @return array An array of attachment ids. |
|
| 124 | - */ |
|
| 125 | - public function get_image_embeds( $content ) { |
|
| 126 | - |
|
| 127 | - // Go over all the images included in the post content, check if they are |
|
| 128 | - // in the DB, and if so include them. |
|
| 129 | - $images = array(); |
|
| 130 | - if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
| 131 | - return array(); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Map the image URLs to attachment ids. |
|
| 135 | - $that = $this; |
|
| 136 | - $ids = array_map( function ( $url ) use ( $that ) { |
|
| 137 | - return $that->get_attachment_id( $url ); |
|
| 138 | - }, $images[1] ); |
|
| 139 | - |
|
| 140 | - // Filter out not found ids (i.e. id is false). |
|
| 141 | - return array_filter( $ids, function ( $item ) { |
|
| 142 | - return false !== $item; |
|
| 143 | - } ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get images linked via the `gallery` shortcode. |
|
| 148 | - * |
|
| 149 | - * @since 3.10.0 |
|
| 150 | - * |
|
| 151 | - * @param \WP_Post $post A {@link WP_Post} instance. |
|
| 152 | - * |
|
| 153 | - * @return array An array of attachment ids. |
|
| 154 | - */ |
|
| 155 | - public function get_gallery( $post ) { |
|
| 156 | - |
|
| 157 | - // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
|
| 158 | - // checked at the moment. |
|
| 159 | - |
|
| 160 | - // Prepare the return value. |
|
| 161 | - $ids = array(); |
|
| 162 | - |
|
| 163 | - // As the above for images in galleries. |
|
| 164 | - // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
|
| 165 | - $pattern = get_shortcode_regex(); |
|
| 166 | - |
|
| 167 | - if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
| 168 | - && array_key_exists( 2, $matches ) |
|
| 169 | - && in_array( 'gallery', $matches[2] ) |
|
| 170 | - ) { |
|
| 171 | - |
|
| 172 | - $keys = array_keys( $matches[2], 'gallery' ); |
|
| 173 | - |
|
| 174 | - foreach ( $keys as $key ) { |
|
| 175 | - $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
| 176 | - |
|
| 177 | - if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
| 178 | - // gallery images insert explicitly by their ids. |
|
| 179 | - |
|
| 180 | - foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
| 181 | - // Since we do not check for actual image existence |
|
| 182 | - // when generating the json content, check it now. |
|
| 183 | - if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
| 184 | - $ids[ $attachment_id ] = true; |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - } else { |
|
| 188 | - // gallery shortcode with no ids uses all the images |
|
| 189 | - // attached to the post. |
|
| 190 | - $images = get_attached_media( 'image', $post->ID ); |
|
| 191 | - foreach ( $images as $attachment ) { |
|
| 192 | - $ids[ $attachment->ID ] = true; |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return array_keys( $ids ); |
|
| 199 | - } |
|
| 19 | + /** |
|
| 20 | + * The singleton instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.20.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Attachment_Service The singleton instance. |
|
| 25 | + */ |
|
| 26 | + private static $instance; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Create a {@link Wordlift_Attachment_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.20.0 |
|
| 32 | + */ |
|
| 33 | + public function __construct() { |
|
| 34 | + |
|
| 35 | + self::$instance = $this; |
|
| 36 | + |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Get the singleton instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.20.0 |
|
| 43 | + * |
|
| 44 | + * @return \Wordlift_Attachment_Service The singleton instance. |
|
| 45 | + */ |
|
| 46 | + public static function get_instance() { |
|
| 47 | + |
|
| 48 | + return self::$instance; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Get an attachment ID given a URL. |
|
| 53 | + * |
|
| 54 | + * Inspired from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
|
| 55 | + * |
|
| 56 | + * @since 3.10.0 |
|
| 57 | + * |
|
| 58 | + * @param string $url The attachment URL. |
|
| 59 | + * |
|
| 60 | + * @return int|false Attachment ID on success, false on failure |
|
| 61 | + */ |
|
| 62 | + public function get_attachment_id( $url ) { |
|
| 63 | + |
|
| 64 | + // Get the upload directory data, we need the base URL to check whether |
|
| 65 | + // the URL we received is within WP. |
|
| 66 | + $dir = wp_upload_dir(); |
|
| 67 | + |
|
| 68 | + // If the image is not inside WP's base url, return. |
|
| 69 | + if ( false === strpos( $url, $dir['baseurl'] . '/' ) ) { |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + // Get the filename, the extension is kept. |
|
| 74 | + $filename = basename( $url ); |
|
| 75 | + |
|
| 76 | + // The following query is CPU-intensive, we need to review it. |
|
| 77 | + // |
|
| 78 | + // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 79 | + // |
|
| 80 | + // Query for attachments with the specified filename. |
|
| 81 | + $query = new WP_Query( array( |
|
| 82 | + 'post_type' => 'attachment', |
|
| 83 | + 'post_status' => 'inherit', |
|
| 84 | + 'fields' => 'ids', |
|
| 85 | + 'meta_query' => array( |
|
| 86 | + array( |
|
| 87 | + 'value' => $filename, |
|
| 88 | + 'compare' => 'LIKE', |
|
| 89 | + 'key' => '_wp_attachment_metadata', |
|
| 90 | + ), |
|
| 91 | + ), |
|
| 92 | + ) ); |
|
| 93 | + |
|
| 94 | + // If there are no posts, return. |
|
| 95 | + if ( $query->have_posts() ) { |
|
| 96 | + foreach ( $query->posts as $attachment_id ) { |
|
| 97 | + |
|
| 98 | + // Get the attachment metadata, we need the filename. |
|
| 99 | + $metadata = wp_get_attachment_metadata( $attachment_id ); |
|
| 100 | + $original_filename = basename( $metadata['file'] ); |
|
| 101 | + |
|
| 102 | + // Get the cropped filenames, or an empty array in case there are no files. |
|
| 103 | + $sizes_filenames = isset( $metadata['sizes'] ) ? wp_list_pluck( $metadata['sizes'], 'file' ) : array(); |
|
| 104 | + |
|
| 105 | + // If the provided filename matches the attachment filename (or one of its resized images), return the id. |
|
| 106 | + if ( $original_filename === $filename || in_array( $filename, $sizes_filenames ) ) { |
|
| 107 | + return $attachment_id; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // If we got here, we couldn't find any attachment. |
|
| 113 | + return false; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Get images embedded in the post content. |
|
| 118 | + * |
|
| 119 | + * @since 3.10.0 |
|
| 120 | + * |
|
| 121 | + * @param string $content The post content. |
|
| 122 | + * |
|
| 123 | + * @return array An array of attachment ids. |
|
| 124 | + */ |
|
| 125 | + public function get_image_embeds( $content ) { |
|
| 126 | + |
|
| 127 | + // Go over all the images included in the post content, check if they are |
|
| 128 | + // in the DB, and if so include them. |
|
| 129 | + $images = array(); |
|
| 130 | + if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
| 131 | + return array(); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Map the image URLs to attachment ids. |
|
| 135 | + $that = $this; |
|
| 136 | + $ids = array_map( function ( $url ) use ( $that ) { |
|
| 137 | + return $that->get_attachment_id( $url ); |
|
| 138 | + }, $images[1] ); |
|
| 139 | + |
|
| 140 | + // Filter out not found ids (i.e. id is false). |
|
| 141 | + return array_filter( $ids, function ( $item ) { |
|
| 142 | + return false !== $item; |
|
| 143 | + } ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get images linked via the `gallery` shortcode. |
|
| 148 | + * |
|
| 149 | + * @since 3.10.0 |
|
| 150 | + * |
|
| 151 | + * @param \WP_Post $post A {@link WP_Post} instance. |
|
| 152 | + * |
|
| 153 | + * @return array An array of attachment ids. |
|
| 154 | + */ |
|
| 155 | + public function get_gallery( $post ) { |
|
| 156 | + |
|
| 157 | + // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
|
| 158 | + // checked at the moment. |
|
| 159 | + |
|
| 160 | + // Prepare the return value. |
|
| 161 | + $ids = array(); |
|
| 162 | + |
|
| 163 | + // As the above for images in galleries. |
|
| 164 | + // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
|
| 165 | + $pattern = get_shortcode_regex(); |
|
| 166 | + |
|
| 167 | + if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
| 168 | + && array_key_exists( 2, $matches ) |
|
| 169 | + && in_array( 'gallery', $matches[2] ) |
|
| 170 | + ) { |
|
| 171 | + |
|
| 172 | + $keys = array_keys( $matches[2], 'gallery' ); |
|
| 173 | + |
|
| 174 | + foreach ( $keys as $key ) { |
|
| 175 | + $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
| 176 | + |
|
| 177 | + if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
| 178 | + // gallery images insert explicitly by their ids. |
|
| 179 | + |
|
| 180 | + foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
| 181 | + // Since we do not check for actual image existence |
|
| 182 | + // when generating the json content, check it now. |
|
| 183 | + if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
| 184 | + $ids[ $attachment_id ] = true; |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + } else { |
|
| 188 | + // gallery shortcode with no ids uses all the images |
|
| 189 | + // attached to the post. |
|
| 190 | + $images = get_attached_media( 'image', $post->ID ); |
|
| 191 | + foreach ( $images as $attachment ) { |
|
| 192 | + $ids[ $attachment->ID ] = true; |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return array_keys( $ids ); |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | 201 | } |
@@ -19,188 +19,188 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Import_Service { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * A {@link Wordlift_Log_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @since 3.6.0 |
|
| 26 | - * @access private |
|
| 27 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 28 | - */ |
|
| 29 | - private $log; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 33 | - * |
|
| 34 | - * @since 3.6.0 |
|
| 35 | - * @access private |
|
| 36 | - * @var \Wordlift_Entity_Post_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 37 | - */ |
|
| 38 | - private $entity_type_service; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The entity service instance. |
|
| 42 | - * |
|
| 43 | - * @since 3.6.0 |
|
| 44 | - * @access private |
|
| 45 | - * @var \Wordlift_Entity_Service $entity_service The entity service instance. |
|
| 46 | - */ |
|
| 47 | - private $entity_service; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * The schema service instance. |
|
| 51 | - * |
|
| 52 | - * @since 3.6.0 |
|
| 53 | - * @access private |
|
| 54 | - * @var \Wordlift_Schema_Service $schema_service The schema service instance. |
|
| 55 | - */ |
|
| 56 | - private $schema_service; |
|
| 57 | - |
|
| 58 | - private $sparql_service; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The dataset URI for this WordPress web site. |
|
| 62 | - * |
|
| 63 | - * @since 3.6.0 |
|
| 64 | - * @access private |
|
| 65 | - * @var string $dataset_uri The dataset URI for this WordPress web site. |
|
| 66 | - */ |
|
| 67 | - private $dataset_uri; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Create a Wordlift_Import_Service instance. |
|
| 71 | - * |
|
| 72 | - * @since 3.6.0 |
|
| 73 | - * |
|
| 74 | - * @param \Wordlift_Entity_Post_Type_Service $entity_type_service |
|
| 75 | - * @param \Wordlift_Entity_Service $entity_service |
|
| 76 | - * @param \Wordlift_Schema_Service $schema_service |
|
| 77 | - * @param \Wordlift_Sparql_Service $sparql_service |
|
| 78 | - * @param string $dataset_uri |
|
| 79 | - */ |
|
| 80 | - public function __construct( $entity_type_service, $entity_service, $schema_service, $sparql_service, $dataset_uri ) { |
|
| 81 | - |
|
| 82 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Import_Service' ); |
|
| 83 | - |
|
| 84 | - $this->entity_type_service = $entity_type_service; |
|
| 85 | - $this->entity_service = $entity_service; |
|
| 86 | - $this->schema_service = $schema_service; |
|
| 87 | - $this->sparql_service = $sparql_service; |
|
| 88 | - $this->dataset_uri = $dataset_uri; |
|
| 89 | - |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Handle the `wp_import_post_meta` filter by checking the `entity_url` meta. |
|
| 94 | - * If the `entity_url` meta value is not within the WP web site dataset URI, |
|
| 95 | - * it is changed into an `entity_same_as` meta key. |
|
| 96 | - * |
|
| 97 | - * @since 3.6.0 |
|
| 98 | - * |
|
| 99 | - * @param array $postmeta An array of indexed post meta. |
|
| 100 | - * @param int $post_id The post ID being imported. |
|
| 101 | - * @param array $post An array of post properties. |
|
| 102 | - * |
|
| 103 | - * @return array An array of indexed post meta. |
|
| 104 | - */ |
|
| 105 | - public function wp_import_post_meta( $postmeta, $post_id, $post ) { |
|
| 106 | - |
|
| 107 | - // If we're not dealing with entity posts, return the original post meta. |
|
| 108 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 109 | - return $postmeta; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // Get a reference to the entity URL meta. |
|
| 113 | - $entity_url_meta = null; |
|
| 114 | - |
|
| 115 | - foreach ( $postmeta as &$meta ) { |
|
| 116 | - if ( 'entity_url' === $meta['key'] ) { |
|
| 117 | - $entity_url_meta = &$meta; |
|
| 118 | - break; |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // If the entity URI is within the dataset URI, we don't change anything. |
|
| 123 | - if ( null === $entity_url_meta || 0 === strpos( $entity_url_meta['value'], $this->dataset_uri ) ) { |
|
| 124 | - return $postmeta; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - // Since the entity URL doesn't belong to this WP install, as the dataset |
|
| 128 | - // URI doesn't match the start of the entity URL, we turn the entity URL |
|
| 129 | - // meta to an entity sameAs. $entity_url_meta is a reference so it should |
|
| 130 | - // update the item in the postmeta array directly. |
|
| 131 | - $entity_url_meta['key'] = 'entity_same_as'; |
|
| 132 | - |
|
| 133 | - return $postmeta; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Catch post meta updates. |
|
| 138 | - * |
|
| 139 | - * @since 3.6.0 |
|
| 140 | - * |
|
| 141 | - * @param int $mid The meta ID after successful update. |
|
| 142 | - * @param int $object_id Object ID. |
|
| 143 | - * @param string $meta_key Meta key. |
|
| 144 | - * @param mixed $meta_value Meta value. |
|
| 145 | - */ |
|
| 146 | - public function added_post_meta( $mid, $object_id, $meta_key, $meta_value ) { |
|
| 147 | - |
|
| 148 | - // Get the entity URI. |
|
| 149 | - $s = $this->entity_service->get_uri( $object_id ); |
|
| 150 | - |
|
| 151 | - // Get the field with the specified meta key. Return if the field is not defined. |
|
| 152 | - if ( null === ( $field = $this->schema_service->get_field( $meta_key ) ) ) { |
|
| 153 | - return; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - // Get the field predicate. |
|
| 157 | - $p = $field['predicate']; |
|
| 158 | - |
|
| 159 | - // Format the object value according to the field type. |
|
| 160 | - $o = Wordlift_Sparql_Service::format( $meta_value, $field['type'] ); |
|
| 161 | - |
|
| 162 | - // Create the statement. |
|
| 163 | - $stmt = sprintf( 'INSERT DATA { <%s> <%s> %s };', Wordlift_Sparql_Service::escape_uri( $s ), Wordlift_Sparql_Service::escape_uri( $p ), $o ); |
|
| 164 | - |
|
| 165 | - // Finally queue the statement. |
|
| 166 | - $this->sparql_service->execute( $stmt ); |
|
| 167 | - |
|
| 168 | - // Removed. |
|
| 169 | - // |
|
| 170 | - // See https://github.com/insideout10/wordlift-plugin/issues/686. |
|
| 171 | - // |
|
| 172 | - // Clear up the cache to avoid memory errors. |
|
| 173 | - // wp_cache_flush(); |
|
| 174 | - |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * When an import is running, hook the {@link added_post_meta} function in order |
|
| 179 | - * to insert metadata from an import in Linked Data. |
|
| 180 | - * |
|
| 181 | - * @since 3.6.0 |
|
| 182 | - */ |
|
| 183 | - public function import_start() { |
|
| 184 | - |
|
| 185 | - add_action( 'added_post_meta', array( |
|
| 186 | - $this, |
|
| 187 | - 'added_post_meta', |
|
| 188 | - ), 10, 4 ); |
|
| 189 | - |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * When an import ends, remove the hook previously added by {@link import_start}. |
|
| 194 | - * |
|
| 195 | - * @since 3.6.0 |
|
| 196 | - */ |
|
| 197 | - public function import_end() { |
|
| 198 | - |
|
| 199 | - remove_action( 'added_post_meta', array( |
|
| 200 | - $this, |
|
| 201 | - 'added_post_meta', |
|
| 202 | - ), 10 ); |
|
| 203 | - |
|
| 204 | - } |
|
| 22 | + /** |
|
| 23 | + * A {@link Wordlift_Log_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @since 3.6.0 |
|
| 26 | + * @access private |
|
| 27 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 28 | + */ |
|
| 29 | + private $log; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 33 | + * |
|
| 34 | + * @since 3.6.0 |
|
| 35 | + * @access private |
|
| 36 | + * @var \Wordlift_Entity_Post_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 37 | + */ |
|
| 38 | + private $entity_type_service; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The entity service instance. |
|
| 42 | + * |
|
| 43 | + * @since 3.6.0 |
|
| 44 | + * @access private |
|
| 45 | + * @var \Wordlift_Entity_Service $entity_service The entity service instance. |
|
| 46 | + */ |
|
| 47 | + private $entity_service; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * The schema service instance. |
|
| 51 | + * |
|
| 52 | + * @since 3.6.0 |
|
| 53 | + * @access private |
|
| 54 | + * @var \Wordlift_Schema_Service $schema_service The schema service instance. |
|
| 55 | + */ |
|
| 56 | + private $schema_service; |
|
| 57 | + |
|
| 58 | + private $sparql_service; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The dataset URI for this WordPress web site. |
|
| 62 | + * |
|
| 63 | + * @since 3.6.0 |
|
| 64 | + * @access private |
|
| 65 | + * @var string $dataset_uri The dataset URI for this WordPress web site. |
|
| 66 | + */ |
|
| 67 | + private $dataset_uri; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Create a Wordlift_Import_Service instance. |
|
| 71 | + * |
|
| 72 | + * @since 3.6.0 |
|
| 73 | + * |
|
| 74 | + * @param \Wordlift_Entity_Post_Type_Service $entity_type_service |
|
| 75 | + * @param \Wordlift_Entity_Service $entity_service |
|
| 76 | + * @param \Wordlift_Schema_Service $schema_service |
|
| 77 | + * @param \Wordlift_Sparql_Service $sparql_service |
|
| 78 | + * @param string $dataset_uri |
|
| 79 | + */ |
|
| 80 | + public function __construct( $entity_type_service, $entity_service, $schema_service, $sparql_service, $dataset_uri ) { |
|
| 81 | + |
|
| 82 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Import_Service' ); |
|
| 83 | + |
|
| 84 | + $this->entity_type_service = $entity_type_service; |
|
| 85 | + $this->entity_service = $entity_service; |
|
| 86 | + $this->schema_service = $schema_service; |
|
| 87 | + $this->sparql_service = $sparql_service; |
|
| 88 | + $this->dataset_uri = $dataset_uri; |
|
| 89 | + |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Handle the `wp_import_post_meta` filter by checking the `entity_url` meta. |
|
| 94 | + * If the `entity_url` meta value is not within the WP web site dataset URI, |
|
| 95 | + * it is changed into an `entity_same_as` meta key. |
|
| 96 | + * |
|
| 97 | + * @since 3.6.0 |
|
| 98 | + * |
|
| 99 | + * @param array $postmeta An array of indexed post meta. |
|
| 100 | + * @param int $post_id The post ID being imported. |
|
| 101 | + * @param array $post An array of post properties. |
|
| 102 | + * |
|
| 103 | + * @return array An array of indexed post meta. |
|
| 104 | + */ |
|
| 105 | + public function wp_import_post_meta( $postmeta, $post_id, $post ) { |
|
| 106 | + |
|
| 107 | + // If we're not dealing with entity posts, return the original post meta. |
|
| 108 | + if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 109 | + return $postmeta; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // Get a reference to the entity URL meta. |
|
| 113 | + $entity_url_meta = null; |
|
| 114 | + |
|
| 115 | + foreach ( $postmeta as &$meta ) { |
|
| 116 | + if ( 'entity_url' === $meta['key'] ) { |
|
| 117 | + $entity_url_meta = &$meta; |
|
| 118 | + break; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // If the entity URI is within the dataset URI, we don't change anything. |
|
| 123 | + if ( null === $entity_url_meta || 0 === strpos( $entity_url_meta['value'], $this->dataset_uri ) ) { |
|
| 124 | + return $postmeta; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + // Since the entity URL doesn't belong to this WP install, as the dataset |
|
| 128 | + // URI doesn't match the start of the entity URL, we turn the entity URL |
|
| 129 | + // meta to an entity sameAs. $entity_url_meta is a reference so it should |
|
| 130 | + // update the item in the postmeta array directly. |
|
| 131 | + $entity_url_meta['key'] = 'entity_same_as'; |
|
| 132 | + |
|
| 133 | + return $postmeta; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Catch post meta updates. |
|
| 138 | + * |
|
| 139 | + * @since 3.6.0 |
|
| 140 | + * |
|
| 141 | + * @param int $mid The meta ID after successful update. |
|
| 142 | + * @param int $object_id Object ID. |
|
| 143 | + * @param string $meta_key Meta key. |
|
| 144 | + * @param mixed $meta_value Meta value. |
|
| 145 | + */ |
|
| 146 | + public function added_post_meta( $mid, $object_id, $meta_key, $meta_value ) { |
|
| 147 | + |
|
| 148 | + // Get the entity URI. |
|
| 149 | + $s = $this->entity_service->get_uri( $object_id ); |
|
| 150 | + |
|
| 151 | + // Get the field with the specified meta key. Return if the field is not defined. |
|
| 152 | + if ( null === ( $field = $this->schema_service->get_field( $meta_key ) ) ) { |
|
| 153 | + return; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + // Get the field predicate. |
|
| 157 | + $p = $field['predicate']; |
|
| 158 | + |
|
| 159 | + // Format the object value according to the field type. |
|
| 160 | + $o = Wordlift_Sparql_Service::format( $meta_value, $field['type'] ); |
|
| 161 | + |
|
| 162 | + // Create the statement. |
|
| 163 | + $stmt = sprintf( 'INSERT DATA { <%s> <%s> %s };', Wordlift_Sparql_Service::escape_uri( $s ), Wordlift_Sparql_Service::escape_uri( $p ), $o ); |
|
| 164 | + |
|
| 165 | + // Finally queue the statement. |
|
| 166 | + $this->sparql_service->execute( $stmt ); |
|
| 167 | + |
|
| 168 | + // Removed. |
|
| 169 | + // |
|
| 170 | + // See https://github.com/insideout10/wordlift-plugin/issues/686. |
|
| 171 | + // |
|
| 172 | + // Clear up the cache to avoid memory errors. |
|
| 173 | + // wp_cache_flush(); |
|
| 174 | + |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * When an import is running, hook the {@link added_post_meta} function in order |
|
| 179 | + * to insert metadata from an import in Linked Data. |
|
| 180 | + * |
|
| 181 | + * @since 3.6.0 |
|
| 182 | + */ |
|
| 183 | + public function import_start() { |
|
| 184 | + |
|
| 185 | + add_action( 'added_post_meta', array( |
|
| 186 | + $this, |
|
| 187 | + 'added_post_meta', |
|
| 188 | + ), 10, 4 ); |
|
| 189 | + |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * When an import ends, remove the hook previously added by {@link import_start}. |
|
| 194 | + * |
|
| 195 | + * @since 3.6.0 |
|
| 196 | + */ |
|
| 197 | + public function import_end() { |
|
| 198 | + |
|
| 199 | + remove_action( 'added_post_meta', array( |
|
| 200 | + $this, |
|
| 201 | + 'added_post_meta', |
|
| 202 | + ), 10 ); |
|
| 203 | + |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | 206 | } |
@@ -77,9 +77,9 @@ discard block |
||
| 77 | 77 | * @param \Wordlift_Sparql_Service $sparql_service |
| 78 | 78 | * @param string $dataset_uri |
| 79 | 79 | */ |
| 80 | - public function __construct( $entity_type_service, $entity_service, $schema_service, $sparql_service, $dataset_uri ) { |
|
| 80 | + public function __construct($entity_type_service, $entity_service, $schema_service, $sparql_service, $dataset_uri) { |
|
| 81 | 81 | |
| 82 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Import_Service' ); |
|
| 82 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Import_Service'); |
|
| 83 | 83 | |
| 84 | 84 | $this->entity_type_service = $entity_type_service; |
| 85 | 85 | $this->entity_service = $entity_service; |
@@ -102,25 +102,25 @@ discard block |
||
| 102 | 102 | * |
| 103 | 103 | * @return array An array of indexed post meta. |
| 104 | 104 | */ |
| 105 | - public function wp_import_post_meta( $postmeta, $post_id, $post ) { |
|
| 105 | + public function wp_import_post_meta($postmeta, $post_id, $post) { |
|
| 106 | 106 | |
| 107 | 107 | // If we're not dealing with entity posts, return the original post meta. |
| 108 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 108 | + if ( ! $this->entity_service->is_entity($post_id)) { |
|
| 109 | 109 | return $postmeta; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // Get a reference to the entity URL meta. |
| 113 | 113 | $entity_url_meta = null; |
| 114 | 114 | |
| 115 | - foreach ( $postmeta as &$meta ) { |
|
| 116 | - if ( 'entity_url' === $meta['key'] ) { |
|
| 115 | + foreach ($postmeta as &$meta) { |
|
| 116 | + if ('entity_url' === $meta['key']) { |
|
| 117 | 117 | $entity_url_meta = &$meta; |
| 118 | 118 | break; |
| 119 | 119 | } |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | // If the entity URI is within the dataset URI, we don't change anything. |
| 123 | - if ( null === $entity_url_meta || 0 === strpos( $entity_url_meta['value'], $this->dataset_uri ) ) { |
|
| 123 | + if (null === $entity_url_meta || 0 === strpos($entity_url_meta['value'], $this->dataset_uri)) { |
|
| 124 | 124 | return $postmeta; |
| 125 | 125 | } |
| 126 | 126 | |
@@ -143,13 +143,13 @@ discard block |
||
| 143 | 143 | * @param string $meta_key Meta key. |
| 144 | 144 | * @param mixed $meta_value Meta value. |
| 145 | 145 | */ |
| 146 | - public function added_post_meta( $mid, $object_id, $meta_key, $meta_value ) { |
|
| 146 | + public function added_post_meta($mid, $object_id, $meta_key, $meta_value) { |
|
| 147 | 147 | |
| 148 | 148 | // Get the entity URI. |
| 149 | - $s = $this->entity_service->get_uri( $object_id ); |
|
| 149 | + $s = $this->entity_service->get_uri($object_id); |
|
| 150 | 150 | |
| 151 | 151 | // Get the field with the specified meta key. Return if the field is not defined. |
| 152 | - if ( null === ( $field = $this->schema_service->get_field( $meta_key ) ) ) { |
|
| 152 | + if (null === ($field = $this->schema_service->get_field($meta_key))) { |
|
| 153 | 153 | return; |
| 154 | 154 | } |
| 155 | 155 | |
@@ -157,13 +157,13 @@ discard block |
||
| 157 | 157 | $p = $field['predicate']; |
| 158 | 158 | |
| 159 | 159 | // Format the object value according to the field type. |
| 160 | - $o = Wordlift_Sparql_Service::format( $meta_value, $field['type'] ); |
|
| 160 | + $o = Wordlift_Sparql_Service::format($meta_value, $field['type']); |
|
| 161 | 161 | |
| 162 | 162 | // Create the statement. |
| 163 | - $stmt = sprintf( 'INSERT DATA { <%s> <%s> %s };', Wordlift_Sparql_Service::escape_uri( $s ), Wordlift_Sparql_Service::escape_uri( $p ), $o ); |
|
| 163 | + $stmt = sprintf('INSERT DATA { <%s> <%s> %s };', Wordlift_Sparql_Service::escape_uri($s), Wordlift_Sparql_Service::escape_uri($p), $o); |
|
| 164 | 164 | |
| 165 | 165 | // Finally queue the statement. |
| 166 | - $this->sparql_service->execute( $stmt ); |
|
| 166 | + $this->sparql_service->execute($stmt); |
|
| 167 | 167 | |
| 168 | 168 | // Removed. |
| 169 | 169 | // |
@@ -182,10 +182,10 @@ discard block |
||
| 182 | 182 | */ |
| 183 | 183 | public function import_start() { |
| 184 | 184 | |
| 185 | - add_action( 'added_post_meta', array( |
|
| 185 | + add_action('added_post_meta', array( |
|
| 186 | 186 | $this, |
| 187 | 187 | 'added_post_meta', |
| 188 | - ), 10, 4 ); |
|
| 188 | + ), 10, 4); |
|
| 189 | 189 | |
| 190 | 190 | } |
| 191 | 191 | |
@@ -196,10 +196,10 @@ discard block |
||
| 196 | 196 | */ |
| 197 | 197 | public function import_end() { |
| 198 | 198 | |
| 199 | - remove_action( 'added_post_meta', array( |
|
| 199 | + remove_action('added_post_meta', array( |
|
| 200 | 200 | $this, |
| 201 | 201 | 'added_post_meta', |
| 202 | - ), 10 ); |
|
| 202 | + ), 10); |
|
| 203 | 203 | |
| 204 | 204 | } |
| 205 | 205 | |
@@ -16,58 +16,58 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Postid_To_Jsonld_Converter implements Wordlift_Post_Converter { |
| 18 | 18 | |
| 19 | - private $entity_service; |
|
| 20 | - private $entity_post_to_jsonld_converter; |
|
| 21 | - private $post_to_jsonld_converter; |
|
| 19 | + private $entity_service; |
|
| 20 | + private $entity_post_to_jsonld_converter; |
|
| 21 | + private $post_to_jsonld_converter; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * A {@link Wordlift_Log_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.16.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $log; |
|
| 23 | + /** |
|
| 24 | + * A {@link Wordlift_Log_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.16.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $log; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Wordlift_Entity_To_Jsonld_Converter constructor. |
|
| 34 | - * |
|
| 35 | - * @since 3.8.0 |
|
| 36 | - * |
|
| 37 | - * @param \Wordlift_Entity_Service $entity_service |
|
| 38 | - * @param \Wordlift_Entity_Post_To_Jsonld_Converter $entity_post_to_jsonld_converter |
|
| 39 | - * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
| 40 | - */ |
|
| 41 | - public function __construct( $entity_service, $entity_post_to_jsonld_converter, $post_to_jsonld_converter ) { |
|
| 32 | + /** |
|
| 33 | + * Wordlift_Entity_To_Jsonld_Converter constructor. |
|
| 34 | + * |
|
| 35 | + * @since 3.8.0 |
|
| 36 | + * |
|
| 37 | + * @param \Wordlift_Entity_Service $entity_service |
|
| 38 | + * @param \Wordlift_Entity_Post_To_Jsonld_Converter $entity_post_to_jsonld_converter |
|
| 39 | + * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
| 40 | + */ |
|
| 41 | + public function __construct( $entity_service, $entity_post_to_jsonld_converter, $post_to_jsonld_converter ) { |
|
| 42 | 42 | |
| 43 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 43 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 44 | 44 | |
| 45 | - $this->entity_service = $entity_service; |
|
| 46 | - $this->entity_post_to_jsonld_converter = $entity_post_to_jsonld_converter; |
|
| 47 | - $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
| 45 | + $this->entity_service = $entity_service; |
|
| 46 | + $this->entity_post_to_jsonld_converter = $entity_post_to_jsonld_converter; |
|
| 47 | + $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
| 48 | 48 | |
| 49 | - } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Convert a post to JSON-LD. |
|
| 53 | - * |
|
| 54 | - * @since 3.8.0 |
|
| 55 | - * |
|
| 56 | - * @param string $id A post id (post or entity). |
|
| 57 | - * |
|
| 58 | - * @param array $references |
|
| 59 | - * |
|
| 60 | - * @return array|NULL A JSON-LD array representation of the post with the provided id, or NULL if not found. |
|
| 61 | - */ |
|
| 62 | - public function convert( $id, &$references = array() ) { |
|
| 51 | + /** |
|
| 52 | + * Convert a post to JSON-LD. |
|
| 53 | + * |
|
| 54 | + * @since 3.8.0 |
|
| 55 | + * |
|
| 56 | + * @param string $id A post id (post or entity). |
|
| 57 | + * |
|
| 58 | + * @param array $references |
|
| 59 | + * |
|
| 60 | + * @return array|NULL A JSON-LD array representation of the post with the provided id, or NULL if not found. |
|
| 61 | + */ |
|
| 62 | + public function convert( $id, &$references = array() ) { |
|
| 63 | 63 | |
| 64 | - $this->log->trace( "Converting post $id..." ); |
|
| 64 | + $this->log->trace( "Converting post $id..." ); |
|
| 65 | 65 | |
| 66 | - return $this->entity_service->is_entity( $id ) |
|
| 67 | - // Entity. |
|
| 68 | - ? $this->entity_post_to_jsonld_converter->convert( $id, $references ) |
|
| 69 | - // Post/Page. |
|
| 70 | - : $this->post_to_jsonld_converter->convert( $id, $references ); |
|
| 71 | - } |
|
| 66 | + return $this->entity_service->is_entity( $id ) |
|
| 67 | + // Entity. |
|
| 68 | + ? $this->entity_post_to_jsonld_converter->convert( $id, $references ) |
|
| 69 | + // Post/Page. |
|
| 70 | + : $this->post_to_jsonld_converter->convert( $id, $references ); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | } |
@@ -38,9 +38,9 @@ discard block |
||
| 38 | 38 | * @param \Wordlift_Entity_Post_To_Jsonld_Converter $entity_post_to_jsonld_converter |
| 39 | 39 | * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
| 40 | 40 | */ |
| 41 | - public function __construct( $entity_service, $entity_post_to_jsonld_converter, $post_to_jsonld_converter ) { |
|
| 41 | + public function __construct($entity_service, $entity_post_to_jsonld_converter, $post_to_jsonld_converter) { |
|
| 42 | 42 | |
| 43 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 43 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 44 | 44 | |
| 45 | 45 | $this->entity_service = $entity_service; |
| 46 | 46 | $this->entity_post_to_jsonld_converter = $entity_post_to_jsonld_converter; |
@@ -59,15 +59,15 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @return array|NULL A JSON-LD array representation of the post with the provided id, or NULL if not found. |
| 61 | 61 | */ |
| 62 | - public function convert( $id, &$references = array() ) { |
|
| 62 | + public function convert($id, &$references = array()) { |
|
| 63 | 63 | |
| 64 | - $this->log->trace( "Converting post $id..." ); |
|
| 64 | + $this->log->trace("Converting post $id..."); |
|
| 65 | 65 | |
| 66 | - return $this->entity_service->is_entity( $id ) |
|
| 66 | + return $this->entity_service->is_entity($id) |
|
| 67 | 67 | // Entity. |
| 68 | - ? $this->entity_post_to_jsonld_converter->convert( $id, $references ) |
|
| 68 | + ? $this->entity_post_to_jsonld_converter->convert($id, $references) |
|
| 69 | 69 | // Post/Page. |
| 70 | - : $this->post_to_jsonld_converter->convert( $id, $references ); |
|
| 70 | + : $this->post_to_jsonld_converter->convert($id, $references); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function __construct() { |
| 54 | 54 | |
| 55 | - self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 55 | + self::$log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 56 | 56 | |
| 57 | 57 | global $wpdb; |
| 58 | 58 | |
@@ -93,24 +93,24 @@ discard block |
||
| 93 | 93 | * |
| 94 | 94 | * @return array|object|null Database query results |
| 95 | 95 | */ |
| 96 | - public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null ) { |
|
| 96 | + public function get_article_subjects($object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null) { |
|
| 97 | 97 | global $wpdb; |
| 98 | 98 | |
| 99 | 99 | // The output fields. |
| 100 | - $actual_fields = self::fields( $fields ); |
|
| 100 | + $actual_fields = self::fields($fields); |
|
| 101 | 101 | |
| 102 | - self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 102 | + self::$log->trace('Getting article subjects for object '.implode(', ', (array) $object_id).'...'); |
|
| 103 | 103 | |
| 104 | - $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 104 | + $objects = $this->article_id_to_entity_id($object_id); |
|
| 105 | 105 | |
| 106 | 106 | // If there are no related objects, return an empty array. |
| 107 | - if ( empty( $objects ) ) { |
|
| 108 | - self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 107 | + if (empty($objects)) { |
|
| 108 | + self::$log->debug('No entities found for object '.implode(', ', (array) $object_id).'.'); |
|
| 109 | 109 | |
| 110 | 110 | return array(); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 113 | + self::$log->debug(count($objects).' entity id(s) found for object '.implode(', ', (array) $object_id).'.'); |
|
| 114 | 114 | |
| 115 | 115 | $sql = |
| 116 | 116 | " |
@@ -120,19 +120,19 @@ discard block |
||
| 120 | 120 | ON p.id = r.subject_id |
| 121 | 121 | " |
| 122 | 122 | // Add the status clause. |
| 123 | - . self::and_status( $status ) |
|
| 123 | + . self::and_status($status) |
|
| 124 | 124 | . self::inner_join_is_article() |
| 125 | - . self::where_object_id( $objects ) |
|
| 125 | + . self::where_object_id($objects) |
|
| 126 | 126 | // Since `object_id` can be an article ID we need to exclude it from |
| 127 | 127 | // the results. |
| 128 | - . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 129 | - . self::and_article_in( $include ) |
|
| 128 | + . self::and_article_not_in(array_merge($excludes, (array) $object_id)) |
|
| 129 | + . self::and_article_in($include) |
|
| 130 | 130 | . self::and_post_type_in() |
| 131 | - . self::and_predicate( $predicate ) |
|
| 132 | - . self::limit( $limit ); |
|
| 131 | + . self::and_predicate($predicate) |
|
| 132 | + . self::limit($limit); |
|
| 133 | 133 | |
| 134 | 134 | |
| 135 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 135 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | return " AND p.post_type IN ( '" |
| 148 | 148 | . implode( |
| 149 | 149 | "','", |
| 150 | - array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) |
|
| 150 | + array_map('esc_sql', Wordlift_Entity_Service::valid_entity_post_types()) |
|
| 151 | 151 | ) |
| 152 | 152 | . "' )"; |
| 153 | 153 | } |
@@ -161,9 +161,9 @@ discard block |
||
| 161 | 161 | * |
| 162 | 162 | * @return string The limit clause (empty if no limit has been specified). |
| 163 | 163 | */ |
| 164 | - private static function limit( $limit = null ) { |
|
| 164 | + private static function limit($limit = null) { |
|
| 165 | 165 | |
| 166 | - if ( null === $limit ) { |
|
| 166 | + if (null === $limit) { |
|
| 167 | 167 | return ''; |
| 168 | 168 | } |
| 169 | 169 | |
@@ -180,19 +180,19 @@ discard block |
||
| 180 | 180 | * |
| 181 | 181 | * @return array An array of entities' ids. |
| 182 | 182 | */ |
| 183 | - private function article_id_to_entity_id( $object_id ) { |
|
| 183 | + private function article_id_to_entity_id($object_id) { |
|
| 184 | 184 | |
| 185 | 185 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 186 | 186 | |
| 187 | 187 | $relation_service = $this; |
| 188 | 188 | |
| 189 | - return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 190 | - if ( $entity_service->is_entity( $item ) ) { |
|
| 191 | - return array_merge( $carry, (array) $item ); |
|
| 189 | + return array_reduce((array) $object_id, function($carry, $item) use ($entity_service, $relation_service) { |
|
| 190 | + if ($entity_service->is_entity($item)) { |
|
| 191 | + return array_merge($carry, (array) $item); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | - return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 195 | - }, array() ); |
|
| 194 | + return array_merge($carry, $relation_service->get_objects($item, 'ids')); |
|
| 195 | + }, array()); |
|
| 196 | 196 | |
| 197 | 197 | } |
| 198 | 198 | |
@@ -205,15 +205,15 @@ discard block |
||
| 205 | 205 | * |
| 206 | 206 | * @return string The WHERE clause. |
| 207 | 207 | */ |
| 208 | - private static function where_object_id( $object_id ) { |
|
| 208 | + private static function where_object_id($object_id) { |
|
| 209 | 209 | |
| 210 | - if ( empty( $object_id ) ) { |
|
| 211 | - self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
|
| 210 | + if (empty($object_id)) { |
|
| 211 | + self::$log->warn(sprintf("%s `where_object_id` called with empty `object_id`.", var_export(debug_backtrace(false, 3), true))); |
|
| 212 | 212 | |
| 213 | 213 | return ' WHERE 1 = 1'; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 216 | + return ' WHERE r.object_id IN ( '.implode(',', wp_parse_id_list((array) $object_id)).' )'; |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | /** |
@@ -225,9 +225,9 @@ discard block |
||
| 225 | 225 | * |
| 226 | 226 | * @return string The exclude clause. |
| 227 | 227 | */ |
| 228 | - private static function and_article_not_in( $exclude ) { |
|
| 228 | + private static function and_article_not_in($exclude) { |
|
| 229 | 229 | |
| 230 | - return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 230 | + return ' AND NOT p.ID IN ( '.implode(',', wp_parse_id_list((array) $exclude)).' )'; |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | /** |
@@ -240,13 +240,13 @@ discard block |
||
| 240 | 240 | * @return string An empty string if $include is null otherwise the include |
| 241 | 241 | * clause. |
| 242 | 242 | */ |
| 243 | - private static function and_article_in( $include = null ) { |
|
| 243 | + private static function and_article_in($include = null) { |
|
| 244 | 244 | |
| 245 | - if ( null === $include ) { |
|
| 245 | + if (null === $include) { |
|
| 246 | 246 | return ''; |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 249 | + return ' AND p.ID IN ( '.implode(',', wp_parse_id_list((array) $include)).' )'; |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | /** |
@@ -261,11 +261,11 @@ discard block |
||
| 261 | 261 | * |
| 262 | 262 | * @return array|object|null Database query results |
| 263 | 263 | */ |
| 264 | - public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 264 | + public function get_non_article_subjects($object_id, $fields = '*', $status = null) { |
|
| 265 | 265 | global $wpdb; |
| 266 | 266 | |
| 267 | 267 | // The output fields. |
| 268 | - $actual_fields = self::fields( $fields ); |
|
| 268 | + $actual_fields = self::fields($fields); |
|
| 269 | 269 | |
| 270 | 270 | $sql = $wpdb->prepare( |
| 271 | 271 | " |
@@ -275,7 +275,7 @@ discard block |
||
| 275 | 275 | ON p.id = r.subject_id |
| 276 | 276 | " |
| 277 | 277 | // Add the status clause. |
| 278 | - . self::and_status( $status ) |
|
| 278 | + . self::and_status($status) |
|
| 279 | 279 | . self::inner_join_is_not_article() |
| 280 | 280 | . " WHERE r.object_id = %d " |
| 281 | 281 | . self::and_post_type_in() |
@@ -283,7 +283,7 @@ discard block |
||
| 283 | 283 | $object_id |
| 284 | 284 | ); |
| 285 | 285 | |
| 286 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 286 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | /** |
@@ -299,11 +299,11 @@ discard block |
||
| 299 | 299 | * |
| 300 | 300 | * @return array|object|null Database query results |
| 301 | 301 | */ |
| 302 | - public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 302 | + public function get_objects($subject_id, $fields = '*', $predicate = null, $status = null) { |
|
| 303 | 303 | global $wpdb; |
| 304 | 304 | |
| 305 | 305 | // The output fields. |
| 306 | - $actual_fields = self::fields( $fields ); |
|
| 306 | + $actual_fields = self::fields($fields); |
|
| 307 | 307 | |
| 308 | 308 | $sql = $wpdb->prepare( |
| 309 | 309 | " |
@@ -313,16 +313,16 @@ discard block |
||
| 313 | 313 | ON p.id = r.object_id |
| 314 | 314 | " |
| 315 | 315 | // Add the status clause. |
| 316 | - . self::and_status( $status ) |
|
| 316 | + . self::and_status($status) |
|
| 317 | 317 | . self::inner_join_is_not_article() |
| 318 | 318 | . " WHERE r.subject_id = %d " |
| 319 | 319 | . self::and_post_type_in() |
| 320 | - . self::and_predicate( $predicate ) |
|
| 320 | + . self::and_predicate($predicate) |
|
| 321 | 321 | , |
| 322 | 322 | $subject_id |
| 323 | 323 | ); |
| 324 | 324 | |
| 325 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 325 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); |
|
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | /** |
@@ -334,13 +334,13 @@ discard block |
||
| 334 | 334 | * |
| 335 | 335 | * @return string An empty string if $status is null, otherwise the status clause. |
| 336 | 336 | */ |
| 337 | - private static function and_status( $status = null ) { |
|
| 337 | + private static function and_status($status = null) { |
|
| 338 | 338 | |
| 339 | - if ( null === $status ) { |
|
| 339 | + if (null === $status) { |
|
| 340 | 340 | return ''; |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | - return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 343 | + return " AND p.post_status IN ('".implode("', '", array_map('esc_sql', (array) $status))."')"; |
|
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | /** |
@@ -353,13 +353,13 @@ discard block |
||
| 353 | 353 | * @return string An empty string if $predicate is null otherwise the predicate |
| 354 | 354 | * clause. |
| 355 | 355 | */ |
| 356 | - private static function and_predicate( $predicate = null ) { |
|
| 356 | + private static function and_predicate($predicate = null) { |
|
| 357 | 357 | |
| 358 | - if ( null === $predicate ) { |
|
| 358 | + if (null === $predicate) { |
|
| 359 | 359 | return ''; |
| 360 | 360 | } |
| 361 | 361 | |
| 362 | - return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 362 | + return " AND r.predicate IN ('".implode("', '", array_map('esc_sql', (array) $predicate))."')"; |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
@@ -371,7 +371,7 @@ discard block |
||
| 371 | 371 | * |
| 372 | 372 | * @return string The `id` field if `ids` otherwise `*`. |
| 373 | 373 | */ |
| 374 | - private static function fields( $fields = '*' ) { |
|
| 374 | + private static function fields($fields = '*') { |
|
| 375 | 375 | |
| 376 | 376 | // The output fields. |
| 377 | 377 | return 'ids' === $fields ? 'id' : '*'; |
@@ -18,376 +18,376 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Relation_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The singleton instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Relation_Service $instance The singleton instance. |
|
| 27 | - */ |
|
| 28 | - private static $instance; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The relation table name in MySQL, set during instantiation. |
|
| 32 | - * |
|
| 33 | - * @since 3.15.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var string $relation_table The relation table name. |
|
| 36 | - */ |
|
| 37 | - private $relation_table; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * A {@link Wordlift_Log_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.15.3 |
|
| 43 | - * |
|
| 44 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 45 | - */ |
|
| 46 | - private static $log; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Create a {@link Wordlift_Relation_Service} instance. |
|
| 50 | - * |
|
| 51 | - * @since 3.15.0 |
|
| 52 | - */ |
|
| 53 | - public function __construct() { |
|
| 54 | - |
|
| 55 | - self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 56 | - |
|
| 57 | - global $wpdb; |
|
| 58 | - |
|
| 59 | - // The relations table. |
|
| 60 | - $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
|
| 61 | - |
|
| 62 | - self::$instance = $this; |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get the singleton instance. |
|
| 68 | - * |
|
| 69 | - * @since 3.15.0 |
|
| 70 | - * @access public |
|
| 71 | - * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service} |
|
| 72 | - * singleton instance. |
|
| 73 | - */ |
|
| 74 | - public static function get_instance() { |
|
| 75 | - |
|
| 76 | - return self::$instance; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Get the articles referencing the specified entity {@link WP_Post}. |
|
| 81 | - * |
|
| 82 | - * @since 3.15.0 |
|
| 83 | - * |
|
| 84 | - * @param int|array $object_id The entity {@link WP_Post}'s id. |
|
| 85 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 86 | - * '*' to return all fields, by default '*'. |
|
| 87 | - * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 88 | - * @param null|string $status The status, by default all. |
|
| 89 | - * @param array $excludes An array of ids to exclude from the results. |
|
| 90 | - * @param null|int $limit The maximum number of results, by default |
|
| 91 | - * no limit. |
|
| 92 | - * @param null|array $include The {@link WP_Post}s' ids to include. |
|
| 93 | - * |
|
| 94 | - * @return array|object|null Database query results |
|
| 95 | - */ |
|
| 96 | - public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null ) { |
|
| 97 | - global $wpdb; |
|
| 98 | - |
|
| 99 | - // The output fields. |
|
| 100 | - $actual_fields = self::fields( $fields ); |
|
| 101 | - |
|
| 102 | - self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 103 | - |
|
| 104 | - $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 105 | - |
|
| 106 | - // If there are no related objects, return an empty array. |
|
| 107 | - if ( empty( $objects ) ) { |
|
| 108 | - self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 109 | - |
|
| 110 | - return array(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 114 | - |
|
| 115 | - $sql = |
|
| 116 | - " |
|
| 21 | + /** |
|
| 22 | + * The singleton instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Relation_Service $instance The singleton instance. |
|
| 27 | + */ |
|
| 28 | + private static $instance; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The relation table name in MySQL, set during instantiation. |
|
| 32 | + * |
|
| 33 | + * @since 3.15.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var string $relation_table The relation table name. |
|
| 36 | + */ |
|
| 37 | + private $relation_table; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * A {@link Wordlift_Log_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.15.3 |
|
| 43 | + * |
|
| 44 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 45 | + */ |
|
| 46 | + private static $log; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Create a {@link Wordlift_Relation_Service} instance. |
|
| 50 | + * |
|
| 51 | + * @since 3.15.0 |
|
| 52 | + */ |
|
| 53 | + public function __construct() { |
|
| 54 | + |
|
| 55 | + self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 56 | + |
|
| 57 | + global $wpdb; |
|
| 58 | + |
|
| 59 | + // The relations table. |
|
| 60 | + $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
|
| 61 | + |
|
| 62 | + self::$instance = $this; |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get the singleton instance. |
|
| 68 | + * |
|
| 69 | + * @since 3.15.0 |
|
| 70 | + * @access public |
|
| 71 | + * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service} |
|
| 72 | + * singleton instance. |
|
| 73 | + */ |
|
| 74 | + public static function get_instance() { |
|
| 75 | + |
|
| 76 | + return self::$instance; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Get the articles referencing the specified entity {@link WP_Post}. |
|
| 81 | + * |
|
| 82 | + * @since 3.15.0 |
|
| 83 | + * |
|
| 84 | + * @param int|array $object_id The entity {@link WP_Post}'s id. |
|
| 85 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 86 | + * '*' to return all fields, by default '*'. |
|
| 87 | + * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 88 | + * @param null|string $status The status, by default all. |
|
| 89 | + * @param array $excludes An array of ids to exclude from the results. |
|
| 90 | + * @param null|int $limit The maximum number of results, by default |
|
| 91 | + * no limit. |
|
| 92 | + * @param null|array $include The {@link WP_Post}s' ids to include. |
|
| 93 | + * |
|
| 94 | + * @return array|object|null Database query results |
|
| 95 | + */ |
|
| 96 | + public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null ) { |
|
| 97 | + global $wpdb; |
|
| 98 | + |
|
| 99 | + // The output fields. |
|
| 100 | + $actual_fields = self::fields( $fields ); |
|
| 101 | + |
|
| 102 | + self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 103 | + |
|
| 104 | + $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 105 | + |
|
| 106 | + // If there are no related objects, return an empty array. |
|
| 107 | + if ( empty( $objects ) ) { |
|
| 108 | + self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 109 | + |
|
| 110 | + return array(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 114 | + |
|
| 115 | + $sql = |
|
| 116 | + " |
|
| 117 | 117 | SELECT DISTINCT p.$actual_fields |
| 118 | 118 | FROM {$this->relation_table} r |
| 119 | 119 | INNER JOIN $wpdb->posts p |
| 120 | 120 | ON p.id = r.subject_id |
| 121 | 121 | " |
| 122 | - // Add the status clause. |
|
| 123 | - . self::and_status( $status ) |
|
| 124 | - . self::inner_join_is_article() |
|
| 125 | - . self::where_object_id( $objects ) |
|
| 126 | - // Since `object_id` can be an article ID we need to exclude it from |
|
| 127 | - // the results. |
|
| 128 | - . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 129 | - . self::and_article_in( $include ) |
|
| 130 | - . self::and_post_type_in() |
|
| 131 | - . self::and_predicate( $predicate ) |
|
| 132 | - . self::limit( $limit ); |
|
| 133 | - |
|
| 134 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * The `post_type IN` clause. |
|
| 139 | - * |
|
| 140 | - * @since 3.15.3 |
|
| 141 | - * |
|
| 142 | - * @return string The `post_type IN` clause. |
|
| 143 | - */ |
|
| 144 | - private static function and_post_type_in() { |
|
| 145 | - |
|
| 146 | - return " AND p.post_type IN ( '" |
|
| 147 | - . implode( |
|
| 148 | - "','", |
|
| 149 | - array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) |
|
| 150 | - ) |
|
| 151 | - . "' )"; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Add the limit clause if specified. |
|
| 156 | - * |
|
| 157 | - * @since 3.15.0 |
|
| 158 | - * |
|
| 159 | - * @param null|int $limit The maximum number of results. |
|
| 160 | - * |
|
| 161 | - * @return string The limit clause (empty if no limit has been specified). |
|
| 162 | - */ |
|
| 163 | - private static function limit( $limit = null ) { |
|
| 164 | - |
|
| 165 | - if ( null === $limit ) { |
|
| 166 | - return ''; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return "LIMIT $limit"; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Map the provided ids into entities (i.e. return the id if it's an entity |
|
| 174 | - * or get the entities if it's a post). |
|
| 175 | - * |
|
| 176 | - * @since 3.15.0 |
|
| 177 | - * |
|
| 178 | - * @param int|array $object_id An array of posts/entities' ids. |
|
| 179 | - * |
|
| 180 | - * @return array An array of entities' ids. |
|
| 181 | - */ |
|
| 182 | - private function article_id_to_entity_id( $object_id ) { |
|
| 183 | - |
|
| 184 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 185 | - |
|
| 186 | - $relation_service = $this; |
|
| 187 | - |
|
| 188 | - return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 189 | - if ( $entity_service->is_entity( $item ) ) { |
|
| 190 | - return array_merge( $carry, (array) $item ); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 194 | - }, array() ); |
|
| 195 | - |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Add the WHERE clause. |
|
| 200 | - * |
|
| 201 | - * @since 3.15.0 |
|
| 202 | - * |
|
| 203 | - * @param int|array $object_id An array of {@link WP_Post}s' ids. |
|
| 204 | - * |
|
| 205 | - * @return string The WHERE clause. |
|
| 206 | - */ |
|
| 207 | - private static function where_object_id( $object_id ) { |
|
| 208 | - |
|
| 209 | - if ( empty( $object_id ) ) { |
|
| 210 | - self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
|
| 211 | - |
|
| 212 | - return ' WHERE 1 = 1'; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Add the exclude clause. |
|
| 220 | - * |
|
| 221 | - * @since 3.15.0 |
|
| 222 | - * |
|
| 223 | - * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
|
| 224 | - * |
|
| 225 | - * @return string The exclude clause. |
|
| 226 | - */ |
|
| 227 | - private static function and_article_not_in( $exclude ) { |
|
| 228 | - |
|
| 229 | - return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Add the include clause. |
|
| 234 | - * |
|
| 235 | - * @since 3.15.0 |
|
| 236 | - * |
|
| 237 | - * @param null|int|array $include An array of {@link WP_Post}s' ids. |
|
| 238 | - * |
|
| 239 | - * @return string An empty string if $include is null otherwise the include |
|
| 240 | - * clause. |
|
| 241 | - */ |
|
| 242 | - private static function and_article_in( $include = null ) { |
|
| 243 | - |
|
| 244 | - if ( null === $include ) { |
|
| 245 | - return ''; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
|
| 253 | - * |
|
| 254 | - * @since 3.15.0 |
|
| 255 | - * |
|
| 256 | - * @param int $object_id The object {@link WP_Post}'s id. |
|
| 257 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 258 | - * '*' to return all fields, by default '*'. |
|
| 259 | - * @param null|string $status The status, by default all. |
|
| 260 | - * |
|
| 261 | - * @return array|object|null Database query results |
|
| 262 | - */ |
|
| 263 | - public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 264 | - global $wpdb; |
|
| 265 | - |
|
| 266 | - // The output fields. |
|
| 267 | - $actual_fields = self::fields( $fields ); |
|
| 268 | - |
|
| 269 | - $sql = $wpdb->prepare( |
|
| 270 | - " |
|
| 122 | + // Add the status clause. |
|
| 123 | + . self::and_status( $status ) |
|
| 124 | + . self::inner_join_is_article() |
|
| 125 | + . self::where_object_id( $objects ) |
|
| 126 | + // Since `object_id` can be an article ID we need to exclude it from |
|
| 127 | + // the results. |
|
| 128 | + . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 129 | + . self::and_article_in( $include ) |
|
| 130 | + . self::and_post_type_in() |
|
| 131 | + . self::and_predicate( $predicate ) |
|
| 132 | + . self::limit( $limit ); |
|
| 133 | + |
|
| 134 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * The `post_type IN` clause. |
|
| 139 | + * |
|
| 140 | + * @since 3.15.3 |
|
| 141 | + * |
|
| 142 | + * @return string The `post_type IN` clause. |
|
| 143 | + */ |
|
| 144 | + private static function and_post_type_in() { |
|
| 145 | + |
|
| 146 | + return " AND p.post_type IN ( '" |
|
| 147 | + . implode( |
|
| 148 | + "','", |
|
| 149 | + array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) |
|
| 150 | + ) |
|
| 151 | + . "' )"; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Add the limit clause if specified. |
|
| 156 | + * |
|
| 157 | + * @since 3.15.0 |
|
| 158 | + * |
|
| 159 | + * @param null|int $limit The maximum number of results. |
|
| 160 | + * |
|
| 161 | + * @return string The limit clause (empty if no limit has been specified). |
|
| 162 | + */ |
|
| 163 | + private static function limit( $limit = null ) { |
|
| 164 | + |
|
| 165 | + if ( null === $limit ) { |
|
| 166 | + return ''; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return "LIMIT $limit"; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Map the provided ids into entities (i.e. return the id if it's an entity |
|
| 174 | + * or get the entities if it's a post). |
|
| 175 | + * |
|
| 176 | + * @since 3.15.0 |
|
| 177 | + * |
|
| 178 | + * @param int|array $object_id An array of posts/entities' ids. |
|
| 179 | + * |
|
| 180 | + * @return array An array of entities' ids. |
|
| 181 | + */ |
|
| 182 | + private function article_id_to_entity_id( $object_id ) { |
|
| 183 | + |
|
| 184 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 185 | + |
|
| 186 | + $relation_service = $this; |
|
| 187 | + |
|
| 188 | + return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 189 | + if ( $entity_service->is_entity( $item ) ) { |
|
| 190 | + return array_merge( $carry, (array) $item ); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 194 | + }, array() ); |
|
| 195 | + |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Add the WHERE clause. |
|
| 200 | + * |
|
| 201 | + * @since 3.15.0 |
|
| 202 | + * |
|
| 203 | + * @param int|array $object_id An array of {@link WP_Post}s' ids. |
|
| 204 | + * |
|
| 205 | + * @return string The WHERE clause. |
|
| 206 | + */ |
|
| 207 | + private static function where_object_id( $object_id ) { |
|
| 208 | + |
|
| 209 | + if ( empty( $object_id ) ) { |
|
| 210 | + self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
|
| 211 | + |
|
| 212 | + return ' WHERE 1 = 1'; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Add the exclude clause. |
|
| 220 | + * |
|
| 221 | + * @since 3.15.0 |
|
| 222 | + * |
|
| 223 | + * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
|
| 224 | + * |
|
| 225 | + * @return string The exclude clause. |
|
| 226 | + */ |
|
| 227 | + private static function and_article_not_in( $exclude ) { |
|
| 228 | + |
|
| 229 | + return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Add the include clause. |
|
| 234 | + * |
|
| 235 | + * @since 3.15.0 |
|
| 236 | + * |
|
| 237 | + * @param null|int|array $include An array of {@link WP_Post}s' ids. |
|
| 238 | + * |
|
| 239 | + * @return string An empty string if $include is null otherwise the include |
|
| 240 | + * clause. |
|
| 241 | + */ |
|
| 242 | + private static function and_article_in( $include = null ) { |
|
| 243 | + |
|
| 244 | + if ( null === $include ) { |
|
| 245 | + return ''; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
|
| 253 | + * |
|
| 254 | + * @since 3.15.0 |
|
| 255 | + * |
|
| 256 | + * @param int $object_id The object {@link WP_Post}'s id. |
|
| 257 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 258 | + * '*' to return all fields, by default '*'. |
|
| 259 | + * @param null|string $status The status, by default all. |
|
| 260 | + * |
|
| 261 | + * @return array|object|null Database query results |
|
| 262 | + */ |
|
| 263 | + public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 264 | + global $wpdb; |
|
| 265 | + |
|
| 266 | + // The output fields. |
|
| 267 | + $actual_fields = self::fields( $fields ); |
|
| 268 | + |
|
| 269 | + $sql = $wpdb->prepare( |
|
| 270 | + " |
|
| 271 | 271 | SELECT p.$actual_fields |
| 272 | 272 | FROM {$this->relation_table} r |
| 273 | 273 | INNER JOIN $wpdb->posts p |
| 274 | 274 | ON p.id = r.subject_id |
| 275 | 275 | " |
| 276 | - // Add the status clause. |
|
| 277 | - . self::and_status( $status ) |
|
| 278 | - . self::inner_join_is_not_article() |
|
| 279 | - . " WHERE r.object_id = %d " |
|
| 280 | - . self::and_post_type_in() |
|
| 281 | - , |
|
| 282 | - $object_id |
|
| 283 | - ); |
|
| 284 | - |
|
| 285 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Get the entities referenced by the specified {@link WP_Post}. |
|
| 290 | - * |
|
| 291 | - * @since 3.15.0 |
|
| 292 | - * |
|
| 293 | - * @param int $subject_id The {@link WP_Post}'s id. |
|
| 294 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 295 | - * '*' to return all fields, by default '*'. |
|
| 296 | - * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 297 | - * @param null|string $status The status, by default all. |
|
| 298 | - * |
|
| 299 | - * @return array|object|null Database query results |
|
| 300 | - */ |
|
| 301 | - public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 302 | - global $wpdb; |
|
| 303 | - |
|
| 304 | - // The output fields. |
|
| 305 | - $actual_fields = self::fields( $fields ); |
|
| 306 | - |
|
| 307 | - $sql = $wpdb->prepare( |
|
| 308 | - " |
|
| 276 | + // Add the status clause. |
|
| 277 | + . self::and_status( $status ) |
|
| 278 | + . self::inner_join_is_not_article() |
|
| 279 | + . " WHERE r.object_id = %d " |
|
| 280 | + . self::and_post_type_in() |
|
| 281 | + , |
|
| 282 | + $object_id |
|
| 283 | + ); |
|
| 284 | + |
|
| 285 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Get the entities referenced by the specified {@link WP_Post}. |
|
| 290 | + * |
|
| 291 | + * @since 3.15.0 |
|
| 292 | + * |
|
| 293 | + * @param int $subject_id The {@link WP_Post}'s id. |
|
| 294 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 295 | + * '*' to return all fields, by default '*'. |
|
| 296 | + * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 297 | + * @param null|string $status The status, by default all. |
|
| 298 | + * |
|
| 299 | + * @return array|object|null Database query results |
|
| 300 | + */ |
|
| 301 | + public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 302 | + global $wpdb; |
|
| 303 | + |
|
| 304 | + // The output fields. |
|
| 305 | + $actual_fields = self::fields( $fields ); |
|
| 306 | + |
|
| 307 | + $sql = $wpdb->prepare( |
|
| 308 | + " |
|
| 309 | 309 | SELECT p.$actual_fields |
| 310 | 310 | FROM {$this->relation_table} r |
| 311 | 311 | INNER JOIN $wpdb->posts p |
| 312 | 312 | ON p.id = r.object_id |
| 313 | 313 | " |
| 314 | - // Add the status clause. |
|
| 315 | - . self::and_status( $status ) |
|
| 316 | - . self::inner_join_is_not_article() |
|
| 317 | - . " WHERE r.subject_id = %d " |
|
| 318 | - . self::and_post_type_in() |
|
| 319 | - . self::and_predicate( $predicate ) |
|
| 320 | - , |
|
| 321 | - $subject_id |
|
| 322 | - ); |
|
| 323 | - |
|
| 324 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Add the `post_status` clause. |
|
| 329 | - * |
|
| 330 | - * @since 3.15.0 |
|
| 331 | - * |
|
| 332 | - * @param null|string|array $status The status values. |
|
| 333 | - * |
|
| 334 | - * @return string An empty string if $status is null, otherwise the status clause. |
|
| 335 | - */ |
|
| 336 | - private static function and_status( $status = null ) { |
|
| 337 | - |
|
| 338 | - if ( null === $status ) { |
|
| 339 | - return ''; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Add the `predicate` clause. |
|
| 347 | - * |
|
| 348 | - * @since 3.15.0 |
|
| 349 | - * |
|
| 350 | - * @param null|string|array $predicate An array of predicates. |
|
| 351 | - * |
|
| 352 | - * @return string An empty string if $predicate is null otherwise the predicate |
|
| 353 | - * clause. |
|
| 354 | - */ |
|
| 355 | - private static function and_predicate( $predicate = null ) { |
|
| 356 | - |
|
| 357 | - if ( null === $predicate ) { |
|
| 358 | - return ''; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * The select fields. |
|
| 366 | - * |
|
| 367 | - * @since 3.15.0 |
|
| 368 | - * |
|
| 369 | - * @param string $fields Either 'ids' or '*', by default '*'. |
|
| 370 | - * |
|
| 371 | - * @return string The `id` field if `ids` otherwise `*`. |
|
| 372 | - */ |
|
| 373 | - private static function fields( $fields = '*' ) { |
|
| 374 | - |
|
| 375 | - // The output fields. |
|
| 376 | - return 'ids' === $fields ? 'id' : '*'; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * The inner join clause for articles. |
|
| 381 | - * |
|
| 382 | - * @since 3.15.0 |
|
| 383 | - * |
|
| 384 | - * @return string The articles inner join clause. |
|
| 385 | - */ |
|
| 386 | - private static function inner_join_is_article() { |
|
| 387 | - global $wpdb; |
|
| 388 | - |
|
| 389 | - return $wpdb->prepare( |
|
| 390 | - " |
|
| 314 | + // Add the status clause. |
|
| 315 | + . self::and_status( $status ) |
|
| 316 | + . self::inner_join_is_not_article() |
|
| 317 | + . " WHERE r.subject_id = %d " |
|
| 318 | + . self::and_post_type_in() |
|
| 319 | + . self::and_predicate( $predicate ) |
|
| 320 | + , |
|
| 321 | + $subject_id |
|
| 322 | + ); |
|
| 323 | + |
|
| 324 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Add the `post_status` clause. |
|
| 329 | + * |
|
| 330 | + * @since 3.15.0 |
|
| 331 | + * |
|
| 332 | + * @param null|string|array $status The status values. |
|
| 333 | + * |
|
| 334 | + * @return string An empty string if $status is null, otherwise the status clause. |
|
| 335 | + */ |
|
| 336 | + private static function and_status( $status = null ) { |
|
| 337 | + |
|
| 338 | + if ( null === $status ) { |
|
| 339 | + return ''; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Add the `predicate` clause. |
|
| 347 | + * |
|
| 348 | + * @since 3.15.0 |
|
| 349 | + * |
|
| 350 | + * @param null|string|array $predicate An array of predicates. |
|
| 351 | + * |
|
| 352 | + * @return string An empty string if $predicate is null otherwise the predicate |
|
| 353 | + * clause. |
|
| 354 | + */ |
|
| 355 | + private static function and_predicate( $predicate = null ) { |
|
| 356 | + |
|
| 357 | + if ( null === $predicate ) { |
|
| 358 | + return ''; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * The select fields. |
|
| 366 | + * |
|
| 367 | + * @since 3.15.0 |
|
| 368 | + * |
|
| 369 | + * @param string $fields Either 'ids' or '*', by default '*'. |
|
| 370 | + * |
|
| 371 | + * @return string The `id` field if `ids` otherwise `*`. |
|
| 372 | + */ |
|
| 373 | + private static function fields( $fields = '*' ) { |
|
| 374 | + |
|
| 375 | + // The output fields. |
|
| 376 | + return 'ids' === $fields ? 'id' : '*'; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * The inner join clause for articles. |
|
| 381 | + * |
|
| 382 | + * @since 3.15.0 |
|
| 383 | + * |
|
| 384 | + * @return string The articles inner join clause. |
|
| 385 | + */ |
|
| 386 | + private static function inner_join_is_article() { |
|
| 387 | + global $wpdb; |
|
| 388 | + |
|
| 389 | + return $wpdb->prepare( |
|
| 390 | + " |
|
| 391 | 391 | INNER JOIN $wpdb->term_relationships tr |
| 392 | 392 | ON p.id = tr.object_id |
| 393 | 393 | INNER JOIN $wpdb->term_taxonomy tt |
@@ -397,23 +397,23 @@ discard block |
||
| 397 | 397 | ON t.term_id = tt.term_id |
| 398 | 398 | AND t.slug = %s |
| 399 | 399 | ", |
| 400 | - 'wl_entity_type', |
|
| 401 | - 'article' |
|
| 402 | - ); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * The inner join clause for non-articles. |
|
| 407 | - * |
|
| 408 | - * @since 3.15.0 |
|
| 409 | - * |
|
| 410 | - * @return string The non-articles inner join clause. |
|
| 411 | - */ |
|
| 412 | - private static function inner_join_is_not_article() { |
|
| 413 | - global $wpdb; |
|
| 414 | - |
|
| 415 | - return $wpdb->prepare( |
|
| 416 | - " |
|
| 400 | + 'wl_entity_type', |
|
| 401 | + 'article' |
|
| 402 | + ); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * The inner join clause for non-articles. |
|
| 407 | + * |
|
| 408 | + * @since 3.15.0 |
|
| 409 | + * |
|
| 410 | + * @return string The non-articles inner join clause. |
|
| 411 | + */ |
|
| 412 | + private static function inner_join_is_not_article() { |
|
| 413 | + global $wpdb; |
|
| 414 | + |
|
| 415 | + return $wpdb->prepare( |
|
| 416 | + " |
|
| 417 | 417 | INNER JOIN $wpdb->term_relationships tr |
| 418 | 418 | ON p.id = tr.object_id |
| 419 | 419 | INNER JOIN $wpdb->term_taxonomy tt |
@@ -423,29 +423,29 @@ discard block |
||
| 423 | 423 | ON t.term_id = tt.term_id |
| 424 | 424 | AND NOT t.slug = %s |
| 425 | 425 | ", |
| 426 | - 'wl_entity_type', |
|
| 427 | - 'article' |
|
| 428 | - ); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Find all the subject IDs and their referenced/related object IDs. The |
|
| 433 | - * object IDs are returned as comma separated IDs in the `object_ids` key. |
|
| 434 | - * |
|
| 435 | - * @since 3.18.0 |
|
| 436 | - * @return mixed Database query results |
|
| 437 | - */ |
|
| 438 | - public function find_all_grouped_by_subject_id() { |
|
| 439 | - global $wpdb; |
|
| 440 | - |
|
| 441 | - return $wpdb->get_results( |
|
| 442 | - " |
|
| 426 | + 'wl_entity_type', |
|
| 427 | + 'article' |
|
| 428 | + ); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Find all the subject IDs and their referenced/related object IDs. The |
|
| 433 | + * object IDs are returned as comma separated IDs in the `object_ids` key. |
|
| 434 | + * |
|
| 435 | + * @since 3.18.0 |
|
| 436 | + * @return mixed Database query results |
|
| 437 | + */ |
|
| 438 | + public function find_all_grouped_by_subject_id() { |
|
| 439 | + global $wpdb; |
|
| 440 | + |
|
| 441 | + return $wpdb->get_results( |
|
| 442 | + " |
|
| 443 | 443 | SELECT subject_id, GROUP_CONCAT( DISTINCT object_id ORDER BY object_id SEPARATOR ',' ) AS object_ids |
| 444 | 444 | FROM $this->relation_table |
| 445 | 445 | GROUP BY subject_id |
| 446 | 446 | " |
| 447 | - ); |
|
| 447 | + ); |
|
| 448 | 448 | |
| 449 | - } |
|
| 449 | + } |
|
| 450 | 450 | |
| 451 | 451 | } |
@@ -18,17 +18,17 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | interface Wordlift_Post_Converter { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Convert the specified post id. |
|
| 23 | - * |
|
| 24 | - * @since 3.16.0 $references argument added. |
|
| 25 | - * @since 3.10.0 |
|
| 26 | - * |
|
| 27 | - * @param int $post_id The post id. |
|
| 28 | - * @param array $references An array of posts referenced by the specified post. |
|
| 29 | - * |
|
| 30 | - * @return mixed The conversion result. |
|
| 31 | - */ |
|
| 32 | - public function convert( $post_id, &$references = array() ); |
|
| 21 | + /** |
|
| 22 | + * Convert the specified post id. |
|
| 23 | + * |
|
| 24 | + * @since 3.16.0 $references argument added. |
|
| 25 | + * @since 3.10.0 |
|
| 26 | + * |
|
| 27 | + * @param int $post_id The post id. |
|
| 28 | + * @param array $references An array of posts referenced by the specified post. |
|
| 29 | + * |
|
| 30 | + * @return mixed The conversion result. |
|
| 31 | + */ |
|
| 32 | + public function convert( $post_id, &$references = array() ); |
|
| 33 | 33 | |
| 34 | 34 | } |
@@ -29,6 +29,6 @@ |
||
| 29 | 29 | * |
| 30 | 30 | * @return mixed The conversion result. |
| 31 | 31 | */ |
| 32 | - public function convert( $post_id, &$references = array() ); |
|
| 32 | + public function convert($post_id, &$references = array()); |
|
| 33 | 33 | |
| 34 | 34 | } |
@@ -17,98 +17,98 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Sample_Data_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * An array of sample data. |
|
| 22 | - * |
|
| 23 | - * @since 3.12.0 |
|
| 24 | - * @var array $samples An array of sample data. |
|
| 25 | - */ |
|
| 26 | - private $samples = array( |
|
| 27 | - array( |
|
| 28 | - 'post' => array( |
|
| 29 | - 'post_name' => 'praesent_imperdiet_odio_sed_lectus_vulputate_finibus', |
|
| 30 | - 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
| 31 | - 'post_content' => 'Praesent imperdiet odio sed lectus vulputate finibus. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.', |
|
| 32 | - 'post_type' => 'entity', |
|
| 33 | - 'post_status' => 'publish', |
|
| 34 | - ), |
|
| 35 | - 'entity_type_uri' => 'http://schema.org/Event', |
|
| 36 | - ), |
|
| 37 | - array( |
|
| 38 | - 'post' => array( |
|
| 39 | - 'post_name' => 'nullam_tempor_lectus_sit_amet_tincidunt_euismod', |
|
| 40 | - 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
| 41 | - 'post_content' => 'Nullam tempor lectus sit amet tincidunt euismod. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.', |
|
| 42 | - 'post_type' => 'entity', |
|
| 43 | - 'post_status' => 'publish', |
|
| 44 | - ), |
|
| 45 | - 'entity_type_uri' => 'http://schema.org/Place', |
|
| 46 | - ), |
|
| 47 | - array( |
|
| 48 | - 'post' => array( |
|
| 49 | - 'post_name' => 'praesent_luctus_tincidunt_odio_quis_aliquam', |
|
| 50 | - 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
| 51 | - 'post_content' => 'Praesent luctus tincidunt odio quis aliquam. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.', |
|
| 52 | - 'post_type' => 'entity', |
|
| 53 | - 'post_status' => 'publish', |
|
| 54 | - ), |
|
| 55 | - 'entity_type_uri' => 'http://schema.org/Organization', |
|
| 56 | - ), |
|
| 57 | - array( |
|
| 58 | - 'post' => array( |
|
| 59 | - 'post_name' => 'lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit', |
|
| 60 | - 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
| 61 | - 'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', |
|
| 62 | - 'post_type' => 'entity', |
|
| 63 | - 'post_status' => 'publish', |
|
| 64 | - ), |
|
| 65 | - 'entity_type_uri' => 'http://schema.org/CreativeWork', |
|
| 66 | - ), |
|
| 67 | - array( |
|
| 68 | - 'post' => |
|
| 69 | - array( |
|
| 70 | - 'post_name' => 'post_1', |
|
| 71 | - 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
| 72 | - 'post_content' => '<span><span id="urn:enhancement-da554278-9522-2d83-76ad-8129d2292cb3" class="textannotation disambiguated wl-event" itemid="{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus">Praesent imperdiet odio sed lectus vulputate finibus</span>. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.</span>', |
|
| 73 | - 'post_type' => 'post', |
|
| 74 | - 'post_status' => 'publish', |
|
| 75 | - ), |
|
| 76 | - ), |
|
| 77 | - array( |
|
| 78 | - 'post' => |
|
| 79 | - array( |
|
| 80 | - 'post_name' => 'post_2', |
|
| 81 | - 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
| 82 | - 'post_content' => '<span><span id="urn:local-text-annotation-p8i5o4279ex3rsbwqkrx9z5mh1ox91ae" class="textannotation disambiguated wl-place" itemid="{dataset-uri}/entity/nullam_tempor_lectus_sit_amet_tincidunt_euismod">Nullam tempor lectus sit amet tincidunt euismod</span>. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</span>', |
|
| 83 | - 'post_type' => 'post', |
|
| 84 | - 'post_status' => 'publish', |
|
| 85 | - ), |
|
| 86 | - ), |
|
| 87 | - array( |
|
| 88 | - 'post' => |
|
| 89 | - array( |
|
| 90 | - 'post_name' => 'post_3', |
|
| 91 | - 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
| 92 | - 'post_content' => '<span><span id="urn:enhancement-b3487a20-4696-b6d9-6c55-842445f5c263" class="textannotation disambiguated wl-organization" itemid="{dataset-uri}/entity/praesent_luctus_tincidunt_odio_quis_aliquam">Praesent luctus tincidunt odio quis aliquam</span>. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.</span>', |
|
| 93 | - 'post_type' => 'post', |
|
| 94 | - 'post_status' => 'publish', |
|
| 95 | - ), |
|
| 96 | - ), |
|
| 97 | - array( |
|
| 98 | - 'post' => |
|
| 99 | - array( |
|
| 100 | - 'post_name' => 'post_4', |
|
| 101 | - 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
| 102 | - 'post_content' => '<span><span id="urn:enhancement-4edc3bde-d275-22f9-8d50-0b707596b292" class="textannotation disambiguated wl-thing" itemid="{dataset-uri}/entity/lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit">Lorem ipsum dolor sit amet, consectetur adipiscing elit</span>. Proin rutrum ultrices nulla ut elementum. Nunc nec lacus tortor. Curabitur bibendum imperdiet luctus. Vivamus a faucibus dolor. Donec blandit malesuada risus. Vestibulum volutpat ut tellus sed tincidunt. Sed id tincidunt velit. Integer sed felis id libero fringilla molestie vitae id orci. Ut vel purus ullamcorper, feugiat tortor non, iaculis neque. Vivamus vitae vehicula sem. Mauris fermentum, metus id vestibulum sodales, lorem lacus efficitur ante, non vestibulum ligula ligula a turpis. Vivamus quis scelerisque massa.</span>', |
|
| 103 | - 'post_type' => 'post', |
|
| 104 | - 'post_status' => 'publish', |
|
| 105 | - ), |
|
| 106 | - ), |
|
| 107 | - array( |
|
| 108 | - 'post' => array( |
|
| 109 | - 'post_name' => 'post_5', |
|
| 110 | - 'post_title' => 'Lorem ipsum', |
|
| 111 | - 'post_content' => " |
|
| 20 | + /** |
|
| 21 | + * An array of sample data. |
|
| 22 | + * |
|
| 23 | + * @since 3.12.0 |
|
| 24 | + * @var array $samples An array of sample data. |
|
| 25 | + */ |
|
| 26 | + private $samples = array( |
|
| 27 | + array( |
|
| 28 | + 'post' => array( |
|
| 29 | + 'post_name' => 'praesent_imperdiet_odio_sed_lectus_vulputate_finibus', |
|
| 30 | + 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
| 31 | + 'post_content' => 'Praesent imperdiet odio sed lectus vulputate finibus. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.', |
|
| 32 | + 'post_type' => 'entity', |
|
| 33 | + 'post_status' => 'publish', |
|
| 34 | + ), |
|
| 35 | + 'entity_type_uri' => 'http://schema.org/Event', |
|
| 36 | + ), |
|
| 37 | + array( |
|
| 38 | + 'post' => array( |
|
| 39 | + 'post_name' => 'nullam_tempor_lectus_sit_amet_tincidunt_euismod', |
|
| 40 | + 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
| 41 | + 'post_content' => 'Nullam tempor lectus sit amet tincidunt euismod. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.', |
|
| 42 | + 'post_type' => 'entity', |
|
| 43 | + 'post_status' => 'publish', |
|
| 44 | + ), |
|
| 45 | + 'entity_type_uri' => 'http://schema.org/Place', |
|
| 46 | + ), |
|
| 47 | + array( |
|
| 48 | + 'post' => array( |
|
| 49 | + 'post_name' => 'praesent_luctus_tincidunt_odio_quis_aliquam', |
|
| 50 | + 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
| 51 | + 'post_content' => 'Praesent luctus tincidunt odio quis aliquam. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.', |
|
| 52 | + 'post_type' => 'entity', |
|
| 53 | + 'post_status' => 'publish', |
|
| 54 | + ), |
|
| 55 | + 'entity_type_uri' => 'http://schema.org/Organization', |
|
| 56 | + ), |
|
| 57 | + array( |
|
| 58 | + 'post' => array( |
|
| 59 | + 'post_name' => 'lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit', |
|
| 60 | + 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
| 61 | + 'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', |
|
| 62 | + 'post_type' => 'entity', |
|
| 63 | + 'post_status' => 'publish', |
|
| 64 | + ), |
|
| 65 | + 'entity_type_uri' => 'http://schema.org/CreativeWork', |
|
| 66 | + ), |
|
| 67 | + array( |
|
| 68 | + 'post' => |
|
| 69 | + array( |
|
| 70 | + 'post_name' => 'post_1', |
|
| 71 | + 'post_title' => 'Praesent imperdiet odio sed lectus vulputate finibus', |
|
| 72 | + 'post_content' => '<span><span id="urn:enhancement-da554278-9522-2d83-76ad-8129d2292cb3" class="textannotation disambiguated wl-event" itemid="{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus">Praesent imperdiet odio sed lectus vulputate finibus</span>. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus.</span>', |
|
| 73 | + 'post_type' => 'post', |
|
| 74 | + 'post_status' => 'publish', |
|
| 75 | + ), |
|
| 76 | + ), |
|
| 77 | + array( |
|
| 78 | + 'post' => |
|
| 79 | + array( |
|
| 80 | + 'post_name' => 'post_2', |
|
| 81 | + 'post_title' => 'Nullam tempor lectus sit amet tincidunt euismod', |
|
| 82 | + 'post_content' => '<span><span id="urn:local-text-annotation-p8i5o4279ex3rsbwqkrx9z5mh1ox91ae" class="textannotation disambiguated wl-place" itemid="{dataset-uri}/entity/nullam_tempor_lectus_sit_amet_tincidunt_euismod">Nullam tempor lectus sit amet tincidunt euismod</span>. Nunc posuere libero augue, eu pretium erat interdum id. Vivamus aliquam dui in mauris tempor, vitae vestibulum odio aliquet. Proin quis bibendum diam, nec tempus dui. Pellentesque sit amet justo vitae urna ornare volutpat quis consectetur nisl. Sed hendrerit purus et magna varius, sodales tincidunt velit finibus. Donec malesuada faucibus mattis. Morbi viverra sagittis justo nec luctus. Nullam et justo sed nisi fringilla rutrum sit amet a urna. Integer elementum, risus in condimentum rhoncus, nisi velit cursus tellus, sed sagittis ante tellus hendrerit ante. Donec et semper libero, vitae imperdiet ligula. Donec eleifend iaculis nisi sed mollis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin faucibus magna ac lectus tempor iaculis quis in nisi. Mauris ac nibh lacinia, ultrices erat quis, rhoncus lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</span>', |
|
| 83 | + 'post_type' => 'post', |
|
| 84 | + 'post_status' => 'publish', |
|
| 85 | + ), |
|
| 86 | + ), |
|
| 87 | + array( |
|
| 88 | + 'post' => |
|
| 89 | + array( |
|
| 90 | + 'post_name' => 'post_3', |
|
| 91 | + 'post_title' => 'Praesent luctus tincidunt odio quis aliquam', |
|
| 92 | + 'post_content' => '<span><span id="urn:enhancement-b3487a20-4696-b6d9-6c55-842445f5c263" class="textannotation disambiguated wl-organization" itemid="{dataset-uri}/entity/praesent_luctus_tincidunt_odio_quis_aliquam">Praesent luctus tincidunt odio quis aliquam</span>. Ut pellentesque odio nec turpis placerat, at rhoncus mauris elementum. Proin vehicula lectus a dolor bibendum, ut pretium lacus volutpat. Integer luctus enim sed odio dapibus tempus. Fusce elementum purus in diam dictum, sit amet ultricies leo molestie. Etiam id nunc tincidunt sapien tristique interdum ac at purus. Nulla eget laoreet turpis. Nullam id cursus nulla.</span>', |
|
| 93 | + 'post_type' => 'post', |
|
| 94 | + 'post_status' => 'publish', |
|
| 95 | + ), |
|
| 96 | + ), |
|
| 97 | + array( |
|
| 98 | + 'post' => |
|
| 99 | + array( |
|
| 100 | + 'post_name' => 'post_4', |
|
| 101 | + 'post_title' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', |
|
| 102 | + 'post_content' => '<span><span id="urn:enhancement-4edc3bde-d275-22f9-8d50-0b707596b292" class="textannotation disambiguated wl-thing" itemid="{dataset-uri}/entity/lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit">Lorem ipsum dolor sit amet, consectetur adipiscing elit</span>. Proin rutrum ultrices nulla ut elementum. Nunc nec lacus tortor. Curabitur bibendum imperdiet luctus. Vivamus a faucibus dolor. Donec blandit malesuada risus. Vestibulum volutpat ut tellus sed tincidunt. Sed id tincidunt velit. Integer sed felis id libero fringilla molestie vitae id orci. Ut vel purus ullamcorper, feugiat tortor non, iaculis neque. Vivamus vitae vehicula sem. Mauris fermentum, metus id vestibulum sodales, lorem lacus efficitur ante, non vestibulum ligula ligula a turpis. Vivamus quis scelerisque massa.</span>', |
|
| 103 | + 'post_type' => 'post', |
|
| 104 | + 'post_status' => 'publish', |
|
| 105 | + ), |
|
| 106 | + ), |
|
| 107 | + array( |
|
| 108 | + 'post' => array( |
|
| 109 | + 'post_name' => 'post_5', |
|
| 110 | + 'post_title' => 'Lorem ipsum', |
|
| 111 | + 'post_content' => " |
|
| 112 | 112 | <span id=\"urn:enhancement-28cb4112-64cf-bd49-ef97-a2ee54727de7\" class=\"textannotation disambiguated wl-thing\" itemid=\"{dataset-uri}/entity/lorem_ipsum_dolor_sit_amet__consectetur_adipiscing_elit\">Lorem ipsum</span> dolor sit amet, consectetur adipiscing elit. Proin rutrum ultrices nulla ut elementum. Nunc nec lacus tortor. Curabitur bibendum imperdiet luctus. Vivamus a faucibus dolor. Donec blandit malesuada risus. Vestibulum volutpat ut tellus sed tincidunt. Sed id tincidunt velit. Integer sed felis id libero fringilla molestie vitae id orci. Ut vel purus ullamcorper, feugiat tortor non, iaculis neque. Vivamus vitae vehicula sem. Mauris fermentum, metus id vestibulum sodales, lorem lacus efficitur ante, non vestibulum ligula ligula a turpis. Vivamus quis scelerisque massa. |
| 113 | 113 | |
| 114 | 114 | [wl_navigator] |
@@ -123,237 +123,237 @@ discard block |
||
| 123 | 123 | |
| 124 | 124 | <span id=\"urn:local-text-annotation-v0kqdtx685n6cg9jrfvl67amkhm28hxh\" class=\"textannotation disambiguated wl-event\" itemid=\"{dataset-uri}/entity/praesent_imperdiet_odio_sed_lectus_vulputate_finibus\">Praesent imperdiet odio sed lectus vulputate finibus</span>. Donec placerat ex arcu, eget fermentum metus ullamcorper vitae. Cras interdum libero a tellus sagittis, sed ultricies sapien tincidunt. Aliquam sit amet vehicula sem. Mauris neque nisl, pellentesque ut molestie id, laoreet nec tortor. Sed tempus ornare est, nec dapibus enim ornare eu. Cras risus ligula, blandit ut faucibus ut, vulputate id ipsum. In vel purus at orci hendrerit cursus. Aliquam interdum lorem id dui maximus volutpat. Vestibulum mi velit, efficitur nec neque eu, posuere porta risus. |
| 125 | 125 | ", |
| 126 | - 'post_type' => 'post', |
|
| 127 | - 'post_status' => 'publish', |
|
| 128 | - ), |
|
| 129 | - ), |
|
| 130 | - ); |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 134 | - * |
|
| 135 | - * @since 3.12.0 |
|
| 136 | - * @access private |
|
| 137 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 138 | - */ |
|
| 139 | - private $entity_type_service; |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 143 | - * |
|
| 144 | - * @since 3.12.0 |
|
| 145 | - * @access private |
|
| 146 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 147 | - */ |
|
| 148 | - private $configuration_service; |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * The {@link Wordlift_User_Service} instance. |
|
| 152 | - * |
|
| 153 | - * @since 3.16.0 |
|
| 154 | - * @access private |
|
| 155 | - * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 156 | - */ |
|
| 157 | - private $user_service; |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Create a {@link Wordlift_Sample_Data_Service} instance. |
|
| 161 | - * |
|
| 162 | - * @since 3.12.0 |
|
| 163 | - * |
|
| 164 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 165 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 166 | - * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 167 | - */ |
|
| 168 | - function __construct( $entity_type_service, $configuration_service, $user_service ) { |
|
| 169 | - |
|
| 170 | - $this->entity_type_service = $entity_type_service; |
|
| 171 | - $this->configuration_service = $configuration_service; |
|
| 172 | - $this->user_service = $user_service; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Create sample data in this WordPress instance. |
|
| 177 | - * |
|
| 178 | - * @since 3.12.0 |
|
| 179 | - */ |
|
| 180 | - function create() { |
|
| 181 | - |
|
| 182 | - // Get the source image path. |
|
| 183 | - $source = plugin_dir_path( dirname( __FILE__ ) ) . 'images/rome.png'; |
|
| 184 | - |
|
| 185 | - // Create an attachment with the local file. |
|
| 186 | - $attachment_id = $this->create_attachment_from_local_file( $source ); |
|
| 187 | - |
|
| 188 | - // Add a flag to signal the attachment is sample data and allow easy delete |
|
| 189 | - // afterwards. |
|
| 190 | - add_post_meta( $attachment_id, '_wl_sample_data', 1, true ); |
|
| 191 | - |
|
| 192 | - // Get the dataset URI, used for replacements in the `post_content`. |
|
| 193 | - $dataset_uri = $this->configuration_service->get_dataset_uri(); |
|
| 194 | - |
|
| 195 | - // Create the author and get its id. |
|
| 196 | - $author_id = $this->create_author(); |
|
| 197 | - |
|
| 198 | - // Create 4 entities. |
|
| 199 | - // Create 4 posts referencing each one entity. |
|
| 200 | - // Create 1 post referencing all the entities. |
|
| 201 | - foreach ( $this->samples as $sample ) { |
|
| 202 | - |
|
| 203 | - // Get the post data. |
|
| 204 | - $post = array_replace_recursive( $sample['post'], array( |
|
| 205 | - 'post_content' => str_replace( '{dataset-uri}', $dataset_uri, $sample['post']['post_content'] ), |
|
| 206 | - ) ); |
|
| 207 | - |
|
| 208 | - // Set the author. |
|
| 209 | - $post['post_author'] = $author_id; |
|
| 210 | - |
|
| 211 | - // Insert the post. |
|
| 212 | - $post_id = wp_insert_post( $post ); |
|
| 213 | - |
|
| 214 | - // Add a flag to signal the post is sample data and allow easy delete |
|
| 215 | - // afterwards. |
|
| 216 | - add_post_meta( $post_id, '_wl_sample_data', 1, true ); |
|
| 217 | - |
|
| 218 | - // Set the post thumbnail. |
|
| 219 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 220 | - |
|
| 221 | - // If the `entity_type_uri` property is set, set it on the post. |
|
| 222 | - if ( isset( $sample['entity_type_uri'] ) ) { |
|
| 223 | - $this->entity_type_service->set( $post_id, $sample['entity_type_uri'] ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Create an author to bind to posts. |
|
| 233 | - * |
|
| 234 | - * @since 3.16.0 |
|
| 235 | - * |
|
| 236 | - * @return int The {@link WP_User}'s id. |
|
| 237 | - */ |
|
| 238 | - private function create_author() { |
|
| 239 | - |
|
| 240 | - $user_id = wp_create_user( 'wl-sample-data', wp_generate_password() ); |
|
| 241 | - $author_post_id = wp_insert_post( array( |
|
| 242 | - 'post_type' => 'entity', |
|
| 243 | - 'post_title' => 'WordLift Sample Data Person', |
|
| 244 | - ) ); |
|
| 245 | - // Add a flag to signal the attachment is sample data and allow easy delete |
|
| 246 | - // afterwards. |
|
| 247 | - add_post_meta( $author_post_id, '_wl_sample_data', 1, true ); |
|
| 248 | - |
|
| 249 | - $this->entity_type_service->set( $author_post_id, 'http://schema.org/Person' ); |
|
| 250 | - $this->user_service->set_entity( $user_id, $author_post_id ); |
|
| 251 | - |
|
| 252 | - return $user_id; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Remove the sample data from this WordPress instance. |
|
| 257 | - * |
|
| 258 | - * @since 3.12.0 |
|
| 259 | - */ |
|
| 260 | - function delete() { |
|
| 261 | - |
|
| 262 | - $this->delete_by_type( 'post' ); |
|
| 263 | - $this->delete_by_type( 'entity' ); |
|
| 264 | - $this->delete_by_type( 'attachment' ); |
|
| 265 | - |
|
| 266 | - // Get and delete the user. |
|
| 267 | - $user = get_user_by( 'login', 'wl-sample-data' ); |
|
| 268 | - wp_delete_user( $user->ID ); |
|
| 269 | - |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Remove the sample data of the specified type (e.g. `post`, `entity`, `attachment`) |
|
| 274 | - * from the local WordPress instance. |
|
| 275 | - * |
|
| 276 | - * @since 3.12.0 |
|
| 277 | - * |
|
| 278 | - * @param string $type WordPress {@link WP_Post}'s type, e.g. `post`, `entity`, `attachment`. |
|
| 279 | - */ |
|
| 280 | - private function delete_by_type( $type ) { |
|
| 281 | - |
|
| 282 | - $posts = get_posts( array( |
|
| 283 | - 'meta_key' => '_wl_sample_data', |
|
| 284 | - 'meta_value' => 1, |
|
| 285 | - 'post_status' => 'any', |
|
| 286 | - 'post_type' => $type, |
|
| 287 | - ) ); |
|
| 288 | - |
|
| 289 | - foreach ( $posts as $post ) { |
|
| 290 | - wp_delete_post( $post->ID, true ); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Create a WordPress' attachment using the specified file. |
|
| 297 | - * |
|
| 298 | - * @since 3.12.0 |
|
| 299 | - * |
|
| 300 | - * @param string $source The source file path. |
|
| 301 | - * |
|
| 302 | - * @return int WordPress' attachment's id. |
|
| 303 | - */ |
|
| 304 | - private function create_attachment_from_local_file( $source ) { |
|
| 305 | - |
|
| 306 | - // Get the path to the upload directory. |
|
| 307 | - $upload_dir = wp_upload_dir(); |
|
| 308 | - $upload_path = $upload_dir['path']; |
|
| 309 | - |
|
| 310 | - // Get the destination image path. |
|
| 311 | - $destination = $upload_path . '/wl-sample-data.png'; |
|
| 312 | - |
|
| 313 | - // Copy the source file to the destination. |
|
| 314 | - @copy( $source, $destination ); |
|
| 315 | - |
|
| 316 | - return $this->create_attachment( $destination ); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Create a WordPress attachment using the specified file in the upload folder. |
|
| 321 | - * |
|
| 322 | - * @see https://codex.wordpress.org/Function_Reference/wp_insert_attachment |
|
| 323 | - * |
|
| 324 | - * @since 3.12.0 |
|
| 325 | - * |
|
| 326 | - * @param string $filename The image filename. |
|
| 327 | - * |
|
| 328 | - * @return int The attachment id. |
|
| 329 | - */ |
|
| 330 | - private function create_attachment( $filename ) { |
|
| 331 | - |
|
| 332 | - // Check the type of file. We'll use this as the 'post_mime_type'. |
|
| 333 | - $filetype = wp_check_filetype( basename( $filename ), null ); |
|
| 334 | - |
|
| 335 | - // Get the path to the upload directory. |
|
| 336 | - $wp_upload_dir = wp_upload_dir(); |
|
| 337 | - |
|
| 338 | - // Prepare an array of post data for the attachment. |
|
| 339 | - $attachment = array( |
|
| 340 | - 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), |
|
| 341 | - 'post_mime_type' => $filetype['type'], |
|
| 342 | - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
| 343 | - 'post_content' => '', |
|
| 344 | - 'post_status' => 'inherit', |
|
| 345 | - ); |
|
| 346 | - |
|
| 347 | - // Insert the attachment. |
|
| 348 | - $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
| 349 | - |
|
| 350 | - // Generate the metadata for the attachment, and update the database record. |
|
| 351 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 352 | - |
|
| 353 | - // Update the attachment metadata. |
|
| 354 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 355 | - |
|
| 356 | - return $attachment_id; |
|
| 357 | - } |
|
| 126 | + 'post_type' => 'post', |
|
| 127 | + 'post_status' => 'publish', |
|
| 128 | + ), |
|
| 129 | + ), |
|
| 130 | + ); |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 134 | + * |
|
| 135 | + * @since 3.12.0 |
|
| 136 | + * @access private |
|
| 137 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 138 | + */ |
|
| 139 | + private $entity_type_service; |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 143 | + * |
|
| 144 | + * @since 3.12.0 |
|
| 145 | + * @access private |
|
| 146 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 147 | + */ |
|
| 148 | + private $configuration_service; |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * The {@link Wordlift_User_Service} instance. |
|
| 152 | + * |
|
| 153 | + * @since 3.16.0 |
|
| 154 | + * @access private |
|
| 155 | + * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 156 | + */ |
|
| 157 | + private $user_service; |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Create a {@link Wordlift_Sample_Data_Service} instance. |
|
| 161 | + * |
|
| 162 | + * @since 3.12.0 |
|
| 163 | + * |
|
| 164 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 165 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 166 | + * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 167 | + */ |
|
| 168 | + function __construct( $entity_type_service, $configuration_service, $user_service ) { |
|
| 169 | + |
|
| 170 | + $this->entity_type_service = $entity_type_service; |
|
| 171 | + $this->configuration_service = $configuration_service; |
|
| 172 | + $this->user_service = $user_service; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Create sample data in this WordPress instance. |
|
| 177 | + * |
|
| 178 | + * @since 3.12.0 |
|
| 179 | + */ |
|
| 180 | + function create() { |
|
| 181 | + |
|
| 182 | + // Get the source image path. |
|
| 183 | + $source = plugin_dir_path( dirname( __FILE__ ) ) . 'images/rome.png'; |
|
| 184 | + |
|
| 185 | + // Create an attachment with the local file. |
|
| 186 | + $attachment_id = $this->create_attachment_from_local_file( $source ); |
|
| 187 | + |
|
| 188 | + // Add a flag to signal the attachment is sample data and allow easy delete |
|
| 189 | + // afterwards. |
|
| 190 | + add_post_meta( $attachment_id, '_wl_sample_data', 1, true ); |
|
| 191 | + |
|
| 192 | + // Get the dataset URI, used for replacements in the `post_content`. |
|
| 193 | + $dataset_uri = $this->configuration_service->get_dataset_uri(); |
|
| 194 | + |
|
| 195 | + // Create the author and get its id. |
|
| 196 | + $author_id = $this->create_author(); |
|
| 197 | + |
|
| 198 | + // Create 4 entities. |
|
| 199 | + // Create 4 posts referencing each one entity. |
|
| 200 | + // Create 1 post referencing all the entities. |
|
| 201 | + foreach ( $this->samples as $sample ) { |
|
| 202 | + |
|
| 203 | + // Get the post data. |
|
| 204 | + $post = array_replace_recursive( $sample['post'], array( |
|
| 205 | + 'post_content' => str_replace( '{dataset-uri}', $dataset_uri, $sample['post']['post_content'] ), |
|
| 206 | + ) ); |
|
| 207 | + |
|
| 208 | + // Set the author. |
|
| 209 | + $post['post_author'] = $author_id; |
|
| 210 | + |
|
| 211 | + // Insert the post. |
|
| 212 | + $post_id = wp_insert_post( $post ); |
|
| 213 | + |
|
| 214 | + // Add a flag to signal the post is sample data and allow easy delete |
|
| 215 | + // afterwards. |
|
| 216 | + add_post_meta( $post_id, '_wl_sample_data', 1, true ); |
|
| 217 | + |
|
| 218 | + // Set the post thumbnail. |
|
| 219 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 220 | + |
|
| 221 | + // If the `entity_type_uri` property is set, set it on the post. |
|
| 222 | + if ( isset( $sample['entity_type_uri'] ) ) { |
|
| 223 | + $this->entity_type_service->set( $post_id, $sample['entity_type_uri'] ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Create an author to bind to posts. |
|
| 233 | + * |
|
| 234 | + * @since 3.16.0 |
|
| 235 | + * |
|
| 236 | + * @return int The {@link WP_User}'s id. |
|
| 237 | + */ |
|
| 238 | + private function create_author() { |
|
| 239 | + |
|
| 240 | + $user_id = wp_create_user( 'wl-sample-data', wp_generate_password() ); |
|
| 241 | + $author_post_id = wp_insert_post( array( |
|
| 242 | + 'post_type' => 'entity', |
|
| 243 | + 'post_title' => 'WordLift Sample Data Person', |
|
| 244 | + ) ); |
|
| 245 | + // Add a flag to signal the attachment is sample data and allow easy delete |
|
| 246 | + // afterwards. |
|
| 247 | + add_post_meta( $author_post_id, '_wl_sample_data', 1, true ); |
|
| 248 | + |
|
| 249 | + $this->entity_type_service->set( $author_post_id, 'http://schema.org/Person' ); |
|
| 250 | + $this->user_service->set_entity( $user_id, $author_post_id ); |
|
| 251 | + |
|
| 252 | + return $user_id; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Remove the sample data from this WordPress instance. |
|
| 257 | + * |
|
| 258 | + * @since 3.12.0 |
|
| 259 | + */ |
|
| 260 | + function delete() { |
|
| 261 | + |
|
| 262 | + $this->delete_by_type( 'post' ); |
|
| 263 | + $this->delete_by_type( 'entity' ); |
|
| 264 | + $this->delete_by_type( 'attachment' ); |
|
| 265 | + |
|
| 266 | + // Get and delete the user. |
|
| 267 | + $user = get_user_by( 'login', 'wl-sample-data' ); |
|
| 268 | + wp_delete_user( $user->ID ); |
|
| 269 | + |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Remove the sample data of the specified type (e.g. `post`, `entity`, `attachment`) |
|
| 274 | + * from the local WordPress instance. |
|
| 275 | + * |
|
| 276 | + * @since 3.12.0 |
|
| 277 | + * |
|
| 278 | + * @param string $type WordPress {@link WP_Post}'s type, e.g. `post`, `entity`, `attachment`. |
|
| 279 | + */ |
|
| 280 | + private function delete_by_type( $type ) { |
|
| 281 | + |
|
| 282 | + $posts = get_posts( array( |
|
| 283 | + 'meta_key' => '_wl_sample_data', |
|
| 284 | + 'meta_value' => 1, |
|
| 285 | + 'post_status' => 'any', |
|
| 286 | + 'post_type' => $type, |
|
| 287 | + ) ); |
|
| 288 | + |
|
| 289 | + foreach ( $posts as $post ) { |
|
| 290 | + wp_delete_post( $post->ID, true ); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Create a WordPress' attachment using the specified file. |
|
| 297 | + * |
|
| 298 | + * @since 3.12.0 |
|
| 299 | + * |
|
| 300 | + * @param string $source The source file path. |
|
| 301 | + * |
|
| 302 | + * @return int WordPress' attachment's id. |
|
| 303 | + */ |
|
| 304 | + private function create_attachment_from_local_file( $source ) { |
|
| 305 | + |
|
| 306 | + // Get the path to the upload directory. |
|
| 307 | + $upload_dir = wp_upload_dir(); |
|
| 308 | + $upload_path = $upload_dir['path']; |
|
| 309 | + |
|
| 310 | + // Get the destination image path. |
|
| 311 | + $destination = $upload_path . '/wl-sample-data.png'; |
|
| 312 | + |
|
| 313 | + // Copy the source file to the destination. |
|
| 314 | + @copy( $source, $destination ); |
|
| 315 | + |
|
| 316 | + return $this->create_attachment( $destination ); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Create a WordPress attachment using the specified file in the upload folder. |
|
| 321 | + * |
|
| 322 | + * @see https://codex.wordpress.org/Function_Reference/wp_insert_attachment |
|
| 323 | + * |
|
| 324 | + * @since 3.12.0 |
|
| 325 | + * |
|
| 326 | + * @param string $filename The image filename. |
|
| 327 | + * |
|
| 328 | + * @return int The attachment id. |
|
| 329 | + */ |
|
| 330 | + private function create_attachment( $filename ) { |
|
| 331 | + |
|
| 332 | + // Check the type of file. We'll use this as the 'post_mime_type'. |
|
| 333 | + $filetype = wp_check_filetype( basename( $filename ), null ); |
|
| 334 | + |
|
| 335 | + // Get the path to the upload directory. |
|
| 336 | + $wp_upload_dir = wp_upload_dir(); |
|
| 337 | + |
|
| 338 | + // Prepare an array of post data for the attachment. |
|
| 339 | + $attachment = array( |
|
| 340 | + 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), |
|
| 341 | + 'post_mime_type' => $filetype['type'], |
|
| 342 | + 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
| 343 | + 'post_content' => '', |
|
| 344 | + 'post_status' => 'inherit', |
|
| 345 | + ); |
|
| 346 | + |
|
| 347 | + // Insert the attachment. |
|
| 348 | + $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
| 349 | + |
|
| 350 | + // Generate the metadata for the attachment, and update the database record. |
|
| 351 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 352 | + |
|
| 353 | + // Update the attachment metadata. |
|
| 354 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 355 | + |
|
| 356 | + return $attachment_id; |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | 359 | } |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 166 | 166 | * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
| 167 | 167 | */ |
| 168 | - function __construct( $entity_type_service, $configuration_service, $user_service ) { |
|
| 168 | + function __construct($entity_type_service, $configuration_service, $user_service) { |
|
| 169 | 169 | |
| 170 | 170 | $this->entity_type_service = $entity_type_service; |
| 171 | 171 | $this->configuration_service = $configuration_service; |
@@ -180,14 +180,14 @@ discard block |
||
| 180 | 180 | function create() { |
| 181 | 181 | |
| 182 | 182 | // Get the source image path. |
| 183 | - $source = plugin_dir_path( dirname( __FILE__ ) ) . 'images/rome.png'; |
|
| 183 | + $source = plugin_dir_path(dirname(__FILE__)).'images/rome.png'; |
|
| 184 | 184 | |
| 185 | 185 | // Create an attachment with the local file. |
| 186 | - $attachment_id = $this->create_attachment_from_local_file( $source ); |
|
| 186 | + $attachment_id = $this->create_attachment_from_local_file($source); |
|
| 187 | 187 | |
| 188 | 188 | // Add a flag to signal the attachment is sample data and allow easy delete |
| 189 | 189 | // afterwards. |
| 190 | - add_post_meta( $attachment_id, '_wl_sample_data', 1, true ); |
|
| 190 | + add_post_meta($attachment_id, '_wl_sample_data', 1, true); |
|
| 191 | 191 | |
| 192 | 192 | // Get the dataset URI, used for replacements in the `post_content`. |
| 193 | 193 | $dataset_uri = $this->configuration_service->get_dataset_uri(); |
@@ -198,29 +198,29 @@ discard block |
||
| 198 | 198 | // Create 4 entities. |
| 199 | 199 | // Create 4 posts referencing each one entity. |
| 200 | 200 | // Create 1 post referencing all the entities. |
| 201 | - foreach ( $this->samples as $sample ) { |
|
| 201 | + foreach ($this->samples as $sample) { |
|
| 202 | 202 | |
| 203 | 203 | // Get the post data. |
| 204 | - $post = array_replace_recursive( $sample['post'], array( |
|
| 205 | - 'post_content' => str_replace( '{dataset-uri}', $dataset_uri, $sample['post']['post_content'] ), |
|
| 206 | - ) ); |
|
| 204 | + $post = array_replace_recursive($sample['post'], array( |
|
| 205 | + 'post_content' => str_replace('{dataset-uri}', $dataset_uri, $sample['post']['post_content']), |
|
| 206 | + )); |
|
| 207 | 207 | |
| 208 | 208 | // Set the author. |
| 209 | 209 | $post['post_author'] = $author_id; |
| 210 | 210 | |
| 211 | 211 | // Insert the post. |
| 212 | - $post_id = wp_insert_post( $post ); |
|
| 212 | + $post_id = wp_insert_post($post); |
|
| 213 | 213 | |
| 214 | 214 | // Add a flag to signal the post is sample data and allow easy delete |
| 215 | 215 | // afterwards. |
| 216 | - add_post_meta( $post_id, '_wl_sample_data', 1, true ); |
|
| 216 | + add_post_meta($post_id, '_wl_sample_data', 1, true); |
|
| 217 | 217 | |
| 218 | 218 | // Set the post thumbnail. |
| 219 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 219 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 220 | 220 | |
| 221 | 221 | // If the `entity_type_uri` property is set, set it on the post. |
| 222 | - if ( isset( $sample['entity_type_uri'] ) ) { |
|
| 223 | - $this->entity_type_service->set( $post_id, $sample['entity_type_uri'] ); |
|
| 222 | + if (isset($sample['entity_type_uri'])) { |
|
| 223 | + $this->entity_type_service->set($post_id, $sample['entity_type_uri']); |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | } |
@@ -237,17 +237,17 @@ discard block |
||
| 237 | 237 | */ |
| 238 | 238 | private function create_author() { |
| 239 | 239 | |
| 240 | - $user_id = wp_create_user( 'wl-sample-data', wp_generate_password() ); |
|
| 241 | - $author_post_id = wp_insert_post( array( |
|
| 240 | + $user_id = wp_create_user('wl-sample-data', wp_generate_password()); |
|
| 241 | + $author_post_id = wp_insert_post(array( |
|
| 242 | 242 | 'post_type' => 'entity', |
| 243 | 243 | 'post_title' => 'WordLift Sample Data Person', |
| 244 | - ) ); |
|
| 244 | + )); |
|
| 245 | 245 | // Add a flag to signal the attachment is sample data and allow easy delete |
| 246 | 246 | // afterwards. |
| 247 | - add_post_meta( $author_post_id, '_wl_sample_data', 1, true ); |
|
| 247 | + add_post_meta($author_post_id, '_wl_sample_data', 1, true); |
|
| 248 | 248 | |
| 249 | - $this->entity_type_service->set( $author_post_id, 'http://schema.org/Person' ); |
|
| 250 | - $this->user_service->set_entity( $user_id, $author_post_id ); |
|
| 249 | + $this->entity_type_service->set($author_post_id, 'http://schema.org/Person'); |
|
| 250 | + $this->user_service->set_entity($user_id, $author_post_id); |
|
| 251 | 251 | |
| 252 | 252 | return $user_id; |
| 253 | 253 | } |
@@ -259,13 +259,13 @@ discard block |
||
| 259 | 259 | */ |
| 260 | 260 | function delete() { |
| 261 | 261 | |
| 262 | - $this->delete_by_type( 'post' ); |
|
| 263 | - $this->delete_by_type( 'entity' ); |
|
| 264 | - $this->delete_by_type( 'attachment' ); |
|
| 262 | + $this->delete_by_type('post'); |
|
| 263 | + $this->delete_by_type('entity'); |
|
| 264 | + $this->delete_by_type('attachment'); |
|
| 265 | 265 | |
| 266 | 266 | // Get and delete the user. |
| 267 | - $user = get_user_by( 'login', 'wl-sample-data' ); |
|
| 268 | - wp_delete_user( $user->ID ); |
|
| 267 | + $user = get_user_by('login', 'wl-sample-data'); |
|
| 268 | + wp_delete_user($user->ID); |
|
| 269 | 269 | |
| 270 | 270 | } |
| 271 | 271 | |
@@ -277,17 +277,17 @@ discard block |
||
| 277 | 277 | * |
| 278 | 278 | * @param string $type WordPress {@link WP_Post}'s type, e.g. `post`, `entity`, `attachment`. |
| 279 | 279 | */ |
| 280 | - private function delete_by_type( $type ) { |
|
| 280 | + private function delete_by_type($type) { |
|
| 281 | 281 | |
| 282 | - $posts = get_posts( array( |
|
| 282 | + $posts = get_posts(array( |
|
| 283 | 283 | 'meta_key' => '_wl_sample_data', |
| 284 | 284 | 'meta_value' => 1, |
| 285 | 285 | 'post_status' => 'any', |
| 286 | 286 | 'post_type' => $type, |
| 287 | - ) ); |
|
| 287 | + )); |
|
| 288 | 288 | |
| 289 | - foreach ( $posts as $post ) { |
|
| 290 | - wp_delete_post( $post->ID, true ); |
|
| 289 | + foreach ($posts as $post) { |
|
| 290 | + wp_delete_post($post->ID, true); |
|
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | } |
@@ -301,19 +301,19 @@ discard block |
||
| 301 | 301 | * |
| 302 | 302 | * @return int WordPress' attachment's id. |
| 303 | 303 | */ |
| 304 | - private function create_attachment_from_local_file( $source ) { |
|
| 304 | + private function create_attachment_from_local_file($source) { |
|
| 305 | 305 | |
| 306 | 306 | // Get the path to the upload directory. |
| 307 | 307 | $upload_dir = wp_upload_dir(); |
| 308 | 308 | $upload_path = $upload_dir['path']; |
| 309 | 309 | |
| 310 | 310 | // Get the destination image path. |
| 311 | - $destination = $upload_path . '/wl-sample-data.png'; |
|
| 311 | + $destination = $upload_path.'/wl-sample-data.png'; |
|
| 312 | 312 | |
| 313 | 313 | // Copy the source file to the destination. |
| 314 | - @copy( $source, $destination ); |
|
| 314 | + @copy($source, $destination); |
|
| 315 | 315 | |
| 316 | - return $this->create_attachment( $destination ); |
|
| 316 | + return $this->create_attachment($destination); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | /** |
@@ -327,31 +327,31 @@ discard block |
||
| 327 | 327 | * |
| 328 | 328 | * @return int The attachment id. |
| 329 | 329 | */ |
| 330 | - private function create_attachment( $filename ) { |
|
| 330 | + private function create_attachment($filename) { |
|
| 331 | 331 | |
| 332 | 332 | // Check the type of file. We'll use this as the 'post_mime_type'. |
| 333 | - $filetype = wp_check_filetype( basename( $filename ), null ); |
|
| 333 | + $filetype = wp_check_filetype(basename($filename), null); |
|
| 334 | 334 | |
| 335 | 335 | // Get the path to the upload directory. |
| 336 | 336 | $wp_upload_dir = wp_upload_dir(); |
| 337 | 337 | |
| 338 | 338 | // Prepare an array of post data for the attachment. |
| 339 | 339 | $attachment = array( |
| 340 | - 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), |
|
| 340 | + 'guid' => $wp_upload_dir['url'].'/'.basename($filename), |
|
| 341 | 341 | 'post_mime_type' => $filetype['type'], |
| 342 | - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
| 342 | + 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), |
|
| 343 | 343 | 'post_content' => '', |
| 344 | 344 | 'post_status' => 'inherit', |
| 345 | 345 | ); |
| 346 | 346 | |
| 347 | 347 | // Insert the attachment. |
| 348 | - $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
| 348 | + $attachment_id = wp_insert_attachment($attachment, $filename); |
|
| 349 | 349 | |
| 350 | 350 | // Generate the metadata for the attachment, and update the database record. |
| 351 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 351 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 352 | 352 | |
| 353 | 353 | // Update the attachment metadata. |
| 354 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 354 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 355 | 355 | |
| 356 | 356 | return $attachment_id; |
| 357 | 357 | } |
@@ -31,17 +31,17 @@ discard block |
||
| 31 | 31 | public static function activate() { |
| 32 | 32 | |
| 33 | 33 | // Do not let the plugin be activate on WordPress versions before 4.4. |
| 34 | - $version = get_bloginfo( 'version' ); |
|
| 35 | - if ( version_compare( $version, '4.4', '<' ) ) { |
|
| 36 | - die( esc_html__( 'The WordLift plugin requires WordPress version 4.4 or above.', 'wordlift' ) ); |
|
| 34 | + $version = get_bloginfo('version'); |
|
| 35 | + if (version_compare($version, '4.4', '<')) { |
|
| 36 | + die(esc_html__('The WordLift plugin requires WordPress version 4.4 or above.', 'wordlift')); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | $configuration_service = Wordlift_Configuration_Service::get_instance(); |
| 40 | 40 | |
| 41 | 41 | // Create a blank application key if there is none. |
| 42 | 42 | $key = $configuration_service->get_key(); |
| 43 | - if ( empty( $key ) ) { |
|
| 44 | - $configuration_service->set_key( '' ); |
|
| 43 | + if (empty($key)) { |
|
| 44 | + $configuration_service->set_key(''); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | // Intentionally go through the whole upgrade procedure to be DRY. |
@@ -51,8 +51,8 @@ discard block |
||
| 51 | 51 | |
| 52 | 52 | // If WordLift's key is not configured, set `_wl_activation_redirect` transient. We won't redirect here, because we can't give |
| 53 | 53 | // for granted that we're in a browser admin session. |
| 54 | - if ( '' === $configuration_service->get_key() ) { |
|
| 55 | - set_transient( '_wl_activation_redirect', true, 30 ); |
|
| 54 | + if ('' === $configuration_service->get_key()) { |
|
| 55 | + set_transient('_wl_activation_redirect', true, 30); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | // Clear caches. |
@@ -21,43 +21,43 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Activator { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Short Description. (use period) |
|
| 26 | - * |
|
| 27 | - * Long Description. |
|
| 28 | - * |
|
| 29 | - * @since 1.0.0 |
|
| 30 | - */ |
|
| 31 | - public static function activate() { |
|
| 32 | - |
|
| 33 | - // Do not let the plugin be activate on WordPress versions before 4.4. |
|
| 34 | - $version = get_bloginfo( 'version' ); |
|
| 35 | - if ( version_compare( $version, '4.4', '<' ) ) { |
|
| 36 | - die( esc_html__( 'The WordLift plugin requires WordPress version 4.4 or above.', 'wordlift' ) ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - $configuration_service = Wordlift_Configuration_Service::get_instance(); |
|
| 40 | - |
|
| 41 | - // Create a blank application key if there is none. |
|
| 42 | - $key = $configuration_service->get_key(); |
|
| 43 | - if ( empty( $key ) ) { |
|
| 44 | - $configuration_service->set_key( '' ); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - // Intentionally go through the whole upgrade procedure to be DRY. |
|
| 48 | - // The following function is called also from `init` so it's not necessary |
|
| 49 | - // here. |
|
| 50 | - // wl_core_update_db_check. |
|
| 51 | - |
|
| 52 | - // If WordLift's key is not configured, set `_wl_activation_redirect` transient. We won't redirect here, because we can't give |
|
| 53 | - // for granted that we're in a browser admin session. |
|
| 54 | - if ( '' === $configuration_service->get_key() ) { |
|
| 55 | - set_transient( '_wl_activation_redirect', true, 30 ); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - // Clear caches. |
|
| 59 | - Wordlift_File_Cache_Service::flush_all(); |
|
| 60 | - |
|
| 61 | - } |
|
| 24 | + /** |
|
| 25 | + * Short Description. (use period) |
|
| 26 | + * |
|
| 27 | + * Long Description. |
|
| 28 | + * |
|
| 29 | + * @since 1.0.0 |
|
| 30 | + */ |
|
| 31 | + public static function activate() { |
|
| 32 | + |
|
| 33 | + // Do not let the plugin be activate on WordPress versions before 4.4. |
|
| 34 | + $version = get_bloginfo( 'version' ); |
|
| 35 | + if ( version_compare( $version, '4.4', '<' ) ) { |
|
| 36 | + die( esc_html__( 'The WordLift plugin requires WordPress version 4.4 or above.', 'wordlift' ) ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + $configuration_service = Wordlift_Configuration_Service::get_instance(); |
|
| 40 | + |
|
| 41 | + // Create a blank application key if there is none. |
|
| 42 | + $key = $configuration_service->get_key(); |
|
| 43 | + if ( empty( $key ) ) { |
|
| 44 | + $configuration_service->set_key( '' ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + // Intentionally go through the whole upgrade procedure to be DRY. |
|
| 48 | + // The following function is called also from `init` so it's not necessary |
|
| 49 | + // here. |
|
| 50 | + // wl_core_update_db_check. |
|
| 51 | + |
|
| 52 | + // If WordLift's key is not configured, set `_wl_activation_redirect` transient. We won't redirect here, because we can't give |
|
| 53 | + // for granted that we're in a browser admin session. |
|
| 54 | + if ( '' === $configuration_service->get_key() ) { |
|
| 55 | + set_transient( '_wl_activation_redirect', true, 30 ); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + // Clear caches. |
|
| 59 | + Wordlift_File_Cache_Service::flush_all(); |
|
| 60 | + |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | 63 | } |