@@ -8,143 +8,143 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Wordlift_Remote_Image_Service { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Save the image with the specified URL locally. |
|
| 13 | - * |
|
| 14 | - * @param string $url The image remote URL. |
|
| 15 | - * |
|
| 16 | - * @return array|false|WP_Error An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
| 17 | - * url, *content_type*: the image content type) or false on error. |
|
| 18 | - * @since 3.18.0 |
|
| 19 | - * @since 3.23.4 the function may return a WP_Error. |
|
| 20 | - */ |
|
| 21 | - public static function save_from_url( $url ) { |
|
| 22 | - |
|
| 23 | - // Required for REST API calls |
|
| 24 | - if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - // Load `WP_Filesystem`. |
|
| 29 | - WP_Filesystem(); |
|
| 30 | - global $wp_filesystem; |
|
| 31 | - |
|
| 32 | - // Parse the url. |
|
| 33 | - $parts = wp_parse_url( $url ); |
|
| 34 | - |
|
| 35 | - // Get the bare filename (filename w/o the extension). |
|
| 36 | - $basename = str_replace( DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 37 | - pathinfo( $parts['path'], PATHINFO_FILENAME ) |
|
| 38 | - ) ); |
|
| 39 | - |
|
| 40 | - // Get the base dir. |
|
| 41 | - $wp_upload_dir = wp_upload_dir(); |
|
| 42 | - |
|
| 43 | - // Set the upload directory and URL. |
|
| 44 | - $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 45 | - $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 46 | - |
|
| 47 | - // Get the full path to the local filename. |
|
| 48 | - $image_full_path = $upload_dir . '/' . $basename; |
|
| 49 | - $image_full_url = $upload_url . '/' . $basename; |
|
| 50 | - |
|
| 51 | - // Create custom directory and bail on failure. |
|
| 52 | - if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 53 | - Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 54 | - ->warn( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 55 | - |
|
| 56 | - return new WP_Error( 0, "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 57 | - }; |
|
| 58 | - |
|
| 59 | - $response = self::get_response( $url ); |
|
| 60 | - |
|
| 61 | - // Bail if the response is not set. |
|
| 62 | - if ( false === $response ) { |
|
| 63 | - Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 64 | - ->warn( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 65 | - |
|
| 66 | - return new WP_Error( 0, "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - // Get the content type of response. |
|
| 70 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 71 | - |
|
| 72 | - // Get the file extension. |
|
| 73 | - $extension = self::get_extension_from_content_type( $content_type ); |
|
| 74 | - |
|
| 75 | - // Bail if the content type is not supported. |
|
| 76 | - if ( empty( $extension ) ) { |
|
| 77 | - return new WP_Error( 0, 'Unsupported content type.' ); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // Complete the local filename. |
|
| 81 | - $image_full_path .= $extension; |
|
| 82 | - $image_full_url .= $extension; |
|
| 83 | - |
|
| 84 | - // Store the data locally. |
|
| 85 | - $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 86 | - |
|
| 87 | - // Return the path. |
|
| 88 | - return array( |
|
| 89 | - 'path' => $image_full_path, |
|
| 90 | - 'url' => $image_full_url, |
|
| 91 | - 'content_type' => $content_type, |
|
| 92 | - ); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Returns the file extension using the content type. |
|
| 97 | - * |
|
| 98 | - * @param string $content_type File content type. |
|
| 99 | - * |
|
| 100 | - * @return string|bool The file extension on success and |
|
| 101 | - * false on fail or if the content type is not supported. |
|
| 102 | - * @since 3.18.0 |
|
| 103 | - * |
|
| 104 | - */ |
|
| 105 | - private static function get_extension_from_content_type( $content_type ) { |
|
| 106 | - |
|
| 107 | - // Return the extension if match. |
|
| 108 | - switch ( $content_type ) { |
|
| 109 | - case 'image/jpeg': |
|
| 110 | - case 'image/jpg': |
|
| 111 | - return '.jpg'; |
|
| 112 | - case 'image/gif': |
|
| 113 | - return '.gif'; |
|
| 114 | - case 'image/png': |
|
| 115 | - return '.png'; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - // Otherwise return false. |
|
| 119 | - return false; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Retrieve the response from url and sets the response. |
|
| 124 | - * |
|
| 125 | - * @param string $url The url to retrieve. |
|
| 126 | - * |
|
| 127 | - * @return false|array True on success and false on failure. |
|
| 128 | - * @since 3.18.0 |
|
| 129 | - * |
|
| 130 | - */ |
|
| 131 | - private static function get_response( $url ) { |
|
| 132 | - // Request the remote file. |
|
| 133 | - $response = wp_remote_get( $url ); |
|
| 134 | - |
|
| 135 | - // Bail out if the response is not ok. |
|
| 136 | - if ( |
|
| 137 | - is_wp_error( $response ) |
|
| 138 | - || 200 !== (int) $response['response']['code'] |
|
| 139 | - || ! isset( $response['body'] ) |
|
| 140 | - ) { |
|
| 141 | - wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 142 | - |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // Set the response. |
|
| 147 | - return $response; |
|
| 148 | - } |
|
| 11 | + /** |
|
| 12 | + * Save the image with the specified URL locally. |
|
| 13 | + * |
|
| 14 | + * @param string $url The image remote URL. |
|
| 15 | + * |
|
| 16 | + * @return array|false|WP_Error An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
| 17 | + * url, *content_type*: the image content type) or false on error. |
|
| 18 | + * @since 3.18.0 |
|
| 19 | + * @since 3.23.4 the function may return a WP_Error. |
|
| 20 | + */ |
|
| 21 | + public static function save_from_url( $url ) { |
|
| 22 | + |
|
| 23 | + // Required for REST API calls |
|
| 24 | + if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | + require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + // Load `WP_Filesystem`. |
|
| 29 | + WP_Filesystem(); |
|
| 30 | + global $wp_filesystem; |
|
| 31 | + |
|
| 32 | + // Parse the url. |
|
| 33 | + $parts = wp_parse_url( $url ); |
|
| 34 | + |
|
| 35 | + // Get the bare filename (filename w/o the extension). |
|
| 36 | + $basename = str_replace( DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 37 | + pathinfo( $parts['path'], PATHINFO_FILENAME ) |
|
| 38 | + ) ); |
|
| 39 | + |
|
| 40 | + // Get the base dir. |
|
| 41 | + $wp_upload_dir = wp_upload_dir(); |
|
| 42 | + |
|
| 43 | + // Set the upload directory and URL. |
|
| 44 | + $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 45 | + $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 46 | + |
|
| 47 | + // Get the full path to the local filename. |
|
| 48 | + $image_full_path = $upload_dir . '/' . $basename; |
|
| 49 | + $image_full_url = $upload_url . '/' . $basename; |
|
| 50 | + |
|
| 51 | + // Create custom directory and bail on failure. |
|
| 52 | + if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 53 | + Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 54 | + ->warn( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 55 | + |
|
| 56 | + return new WP_Error( 0, "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 57 | + }; |
|
| 58 | + |
|
| 59 | + $response = self::get_response( $url ); |
|
| 60 | + |
|
| 61 | + // Bail if the response is not set. |
|
| 62 | + if ( false === $response ) { |
|
| 63 | + Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 64 | + ->warn( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 65 | + |
|
| 66 | + return new WP_Error( 0, "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + // Get the content type of response. |
|
| 70 | + $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 71 | + |
|
| 72 | + // Get the file extension. |
|
| 73 | + $extension = self::get_extension_from_content_type( $content_type ); |
|
| 74 | + |
|
| 75 | + // Bail if the content type is not supported. |
|
| 76 | + if ( empty( $extension ) ) { |
|
| 77 | + return new WP_Error( 0, 'Unsupported content type.' ); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // Complete the local filename. |
|
| 81 | + $image_full_path .= $extension; |
|
| 82 | + $image_full_url .= $extension; |
|
| 83 | + |
|
| 84 | + // Store the data locally. |
|
| 85 | + $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 86 | + |
|
| 87 | + // Return the path. |
|
| 88 | + return array( |
|
| 89 | + 'path' => $image_full_path, |
|
| 90 | + 'url' => $image_full_url, |
|
| 91 | + 'content_type' => $content_type, |
|
| 92 | + ); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Returns the file extension using the content type. |
|
| 97 | + * |
|
| 98 | + * @param string $content_type File content type. |
|
| 99 | + * |
|
| 100 | + * @return string|bool The file extension on success and |
|
| 101 | + * false on fail or if the content type is not supported. |
|
| 102 | + * @since 3.18.0 |
|
| 103 | + * |
|
| 104 | + */ |
|
| 105 | + private static function get_extension_from_content_type( $content_type ) { |
|
| 106 | + |
|
| 107 | + // Return the extension if match. |
|
| 108 | + switch ( $content_type ) { |
|
| 109 | + case 'image/jpeg': |
|
| 110 | + case 'image/jpg': |
|
| 111 | + return '.jpg'; |
|
| 112 | + case 'image/gif': |
|
| 113 | + return '.gif'; |
|
| 114 | + case 'image/png': |
|
| 115 | + return '.png'; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + // Otherwise return false. |
|
| 119 | + return false; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Retrieve the response from url and sets the response. |
|
| 124 | + * |
|
| 125 | + * @param string $url The url to retrieve. |
|
| 126 | + * |
|
| 127 | + * @return false|array True on success and false on failure. |
|
| 128 | + * @since 3.18.0 |
|
| 129 | + * |
|
| 130 | + */ |
|
| 131 | + private static function get_response( $url ) { |
|
| 132 | + // Request the remote file. |
|
| 133 | + $response = wp_remote_get( $url ); |
|
| 134 | + |
|
| 135 | + // Bail out if the response is not ok. |
|
| 136 | + if ( |
|
| 137 | + is_wp_error( $response ) |
|
| 138 | + || 200 !== (int) $response['response']['code'] |
|
| 139 | + || ! isset( $response['body'] ) |
|
| 140 | + ) { |
|
| 141 | + wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 142 | + |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // Set the response. |
|
| 147 | + return $response; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | 150 | } |
@@ -18,11 +18,11 @@ discard block |
||
| 18 | 18 | * @since 3.18.0 |
| 19 | 19 | * @since 3.23.4 the function may return a WP_Error. |
| 20 | 20 | */ |
| 21 | - public static function save_from_url( $url ) { |
|
| 21 | + public static function save_from_url($url) { |
|
| 22 | 22 | |
| 23 | 23 | // Required for REST API calls |
| 24 | - if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 24 | + if ( ! function_exists('WP_Filesystem')) { |
|
| 25 | + require_once(ABSPATH.'wp-admin/includes/file.php'); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | // Load `WP_Filesystem`. |
@@ -30,51 +30,51 @@ discard block |
||
| 30 | 30 | global $wp_filesystem; |
| 31 | 31 | |
| 32 | 32 | // Parse the url. |
| 33 | - $parts = wp_parse_url( $url ); |
|
| 33 | + $parts = wp_parse_url($url); |
|
| 34 | 34 | |
| 35 | 35 | // Get the bare filename (filename w/o the extension). |
| 36 | - $basename = str_replace( DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 37 | - pathinfo( $parts['path'], PATHINFO_FILENAME ) |
|
| 38 | - ) ); |
|
| 36 | + $basename = str_replace(DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 37 | + pathinfo($parts['path'], PATHINFO_FILENAME) |
|
| 38 | + )); |
|
| 39 | 39 | |
| 40 | 40 | // Get the base dir. |
| 41 | 41 | $wp_upload_dir = wp_upload_dir(); |
| 42 | 42 | |
| 43 | 43 | // Set the upload directory and URL. |
| 44 | - $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 45 | - $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 44 | + $upload_dir = $wp_upload_dir['basedir'].'/wl'.$wp_upload_dir['subdir']; |
|
| 45 | + $upload_url = $wp_upload_dir['baseurl'].'/wl'.$wp_upload_dir['subdir']; |
|
| 46 | 46 | |
| 47 | 47 | // Get the full path to the local filename. |
| 48 | - $image_full_path = $upload_dir . '/' . $basename; |
|
| 49 | - $image_full_url = $upload_url . '/' . $basename; |
|
| 48 | + $image_full_path = $upload_dir.'/'.$basename; |
|
| 49 | + $image_full_url = $upload_url.'/'.$basename; |
|
| 50 | 50 | |
| 51 | 51 | // Create custom directory and bail on failure. |
| 52 | - if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 53 | - Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 54 | - ->warn( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 52 | + if ( ! wp_mkdir_p($upload_dir)) { |
|
| 53 | + Wordlift_Log_Service::get_logger('Wordlift_Remote_Image_Service') |
|
| 54 | + ->warn("save_image_from_url : failed creating upload dir $upload_dir \n"); |
|
| 55 | 55 | |
| 56 | - return new WP_Error( 0, "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 56 | + return new WP_Error(0, "save_image_from_url : failed creating upload dir $upload_dir \n"); |
|
| 57 | 57 | }; |
| 58 | 58 | |
| 59 | - $response = self::get_response( $url ); |
|
| 59 | + $response = self::get_response($url); |
|
| 60 | 60 | |
| 61 | 61 | // Bail if the response is not set. |
| 62 | - if ( false === $response ) { |
|
| 63 | - Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' ) |
|
| 64 | - ->warn( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 62 | + if (false === $response) { |
|
| 63 | + Wordlift_Log_Service::get_logger('Wordlift_Remote_Image_Service') |
|
| 64 | + ->warn("save_image_from_url : failed to fetch the response from: $url \n"); |
|
| 65 | 65 | |
| 66 | - return new WP_Error( 0, "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 66 | + return new WP_Error(0, "save_image_from_url : failed to fetch the response from: $url \n"); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | // Get the content type of response. |
| 70 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 70 | + $content_type = wp_remote_retrieve_header($response, 'content-type'); |
|
| 71 | 71 | |
| 72 | 72 | // Get the file extension. |
| 73 | - $extension = self::get_extension_from_content_type( $content_type ); |
|
| 73 | + $extension = self::get_extension_from_content_type($content_type); |
|
| 74 | 74 | |
| 75 | 75 | // Bail if the content type is not supported. |
| 76 | - if ( empty( $extension ) ) { |
|
| 77 | - return new WP_Error( 0, 'Unsupported content type.' ); |
|
| 76 | + if (empty($extension)) { |
|
| 77 | + return new WP_Error(0, 'Unsupported content type.'); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | // Complete the local filename. |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | $image_full_url .= $extension; |
| 83 | 83 | |
| 84 | 84 | // Store the data locally. |
| 85 | - $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 85 | + $wp_filesystem->put_contents($image_full_path, wp_remote_retrieve_body($response)); |
|
| 86 | 86 | |
| 87 | 87 | // Return the path. |
| 88 | 88 | return array( |
@@ -102,10 +102,10 @@ discard block |
||
| 102 | 102 | * @since 3.18.0 |
| 103 | 103 | * |
| 104 | 104 | */ |
| 105 | - private static function get_extension_from_content_type( $content_type ) { |
|
| 105 | + private static function get_extension_from_content_type($content_type) { |
|
| 106 | 106 | |
| 107 | 107 | // Return the extension if match. |
| 108 | - switch ( $content_type ) { |
|
| 108 | + switch ($content_type) { |
|
| 109 | 109 | case 'image/jpeg': |
| 110 | 110 | case 'image/jpg': |
| 111 | 111 | return '.jpg'; |
@@ -128,17 +128,17 @@ discard block |
||
| 128 | 128 | * @since 3.18.0 |
| 129 | 129 | * |
| 130 | 130 | */ |
| 131 | - private static function get_response( $url ) { |
|
| 131 | + private static function get_response($url) { |
|
| 132 | 132 | // Request the remote file. |
| 133 | - $response = wp_remote_get( $url ); |
|
| 133 | + $response = wp_remote_get($url); |
|
| 134 | 134 | |
| 135 | 135 | // Bail out if the response is not ok. |
| 136 | 136 | if ( |
| 137 | - is_wp_error( $response ) |
|
| 137 | + is_wp_error($response) |
|
| 138 | 138 | || 200 !== (int) $response['response']['code'] |
| 139 | - || ! isset( $response['body'] ) |
|
| 139 | + || ! isset($response['body']) |
|
| 140 | 140 | ) { |
| 141 | - wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 141 | + wl_write_log("save_image_from_url : error fetching image $url \n"); |
|
| 142 | 142 | |
| 143 | 143 | return false; |
| 144 | 144 | } |
@@ -13,65 +13,65 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Debug_Service { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * The {@link Wordlift_Entity_Service} instance. |
|
| 18 | - * |
|
| 19 | - * @since 3.7.2 |
|
| 20 | - * @access private |
|
| 21 | - * @var Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 22 | - */ |
|
| 23 | - private $entity_service; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * A {@link Wordlift_Uri_Service} instance. |
|
| 27 | - * |
|
| 28 | - * @since 3.10.0 |
|
| 29 | - * @access private |
|
| 30 | - * @var \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
| 31 | - */ |
|
| 32 | - private $uri_service; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Wordlift_Debug_Service constructor. |
|
| 36 | - * |
|
| 37 | - * @since 3.7.2 |
|
| 38 | - * |
|
| 39 | - * @param Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 40 | - * @param \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
| 41 | - */ |
|
| 42 | - public function __construct( $entity_service, $uri_service ) { |
|
| 43 | - |
|
| 44 | - $this->entity_service = $entity_service; |
|
| 45 | - $this->uri_service = $uri_service; |
|
| 46 | - |
|
| 47 | - add_action( 'wp_ajax_wl_dump_uri', array( $this, 'dump_uri' ) ); |
|
| 48 | - |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - public function dump_uri() { |
|
| 52 | - |
|
| 53 | - if ( ! isset( $_GET['id'] ) ) { |
|
| 54 | - wp_send_json_error( 'id not set' ); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $post_id = $_GET['id']; |
|
| 58 | - |
|
| 59 | - $post = get_post( $post_id ); |
|
| 60 | - |
|
| 61 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 62 | - $build_uri = $this->uri_service->build_uri( $post->post_title, $post->post_type ); |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - var_dump( $uri ); |
|
| 66 | - |
|
| 67 | - wp_send_json_success( array( |
|
| 68 | - 'uri' => $uri, |
|
| 69 | - 'post_title' => sprintf( '%s (%s)', $post->post_title, mb_detect_encoding( $post->post_title ) ), |
|
| 70 | - 'post_title_ascii' => mb_convert_encoding( $post->post_title, 'ASCII' ), |
|
| 71 | - 'build_uri' => $build_uri, |
|
| 72 | - 'build_uri_convert' => mb_convert_encoding( $build_uri, 'ASCII' ), |
|
| 73 | - ) ); |
|
| 74 | - |
|
| 75 | - } |
|
| 16 | + /** |
|
| 17 | + * The {@link Wordlift_Entity_Service} instance. |
|
| 18 | + * |
|
| 19 | + * @since 3.7.2 |
|
| 20 | + * @access private |
|
| 21 | + * @var Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 22 | + */ |
|
| 23 | + private $entity_service; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * A {@link Wordlift_Uri_Service} instance. |
|
| 27 | + * |
|
| 28 | + * @since 3.10.0 |
|
| 29 | + * @access private |
|
| 30 | + * @var \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
| 31 | + */ |
|
| 32 | + private $uri_service; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Wordlift_Debug_Service constructor. |
|
| 36 | + * |
|
| 37 | + * @since 3.7.2 |
|
| 38 | + * |
|
| 39 | + * @param Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 40 | + * @param \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
| 41 | + */ |
|
| 42 | + public function __construct( $entity_service, $uri_service ) { |
|
| 43 | + |
|
| 44 | + $this->entity_service = $entity_service; |
|
| 45 | + $this->uri_service = $uri_service; |
|
| 46 | + |
|
| 47 | + add_action( 'wp_ajax_wl_dump_uri', array( $this, 'dump_uri' ) ); |
|
| 48 | + |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + public function dump_uri() { |
|
| 52 | + |
|
| 53 | + if ( ! isset( $_GET['id'] ) ) { |
|
| 54 | + wp_send_json_error( 'id not set' ); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $post_id = $_GET['id']; |
|
| 58 | + |
|
| 59 | + $post = get_post( $post_id ); |
|
| 60 | + |
|
| 61 | + $uri = $this->entity_service->get_uri( $post_id ); |
|
| 62 | + $build_uri = $this->uri_service->build_uri( $post->post_title, $post->post_type ); |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + var_dump( $uri ); |
|
| 66 | + |
|
| 67 | + wp_send_json_success( array( |
|
| 68 | + 'uri' => $uri, |
|
| 69 | + 'post_title' => sprintf( '%s (%s)', $post->post_title, mb_detect_encoding( $post->post_title ) ), |
|
| 70 | + 'post_title_ascii' => mb_convert_encoding( $post->post_title, 'ASCII' ), |
|
| 71 | + 'build_uri' => $build_uri, |
|
| 72 | + 'build_uri_convert' => mb_convert_encoding( $build_uri, 'ASCII' ), |
|
| 73 | + ) ); |
|
| 74 | + |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | } |
| 78 | 78 | \ No newline at end of file |
@@ -39,38 +39,38 @@ |
||
| 39 | 39 | * @param Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
| 40 | 40 | * @param \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
| 41 | 41 | */ |
| 42 | - public function __construct( $entity_service, $uri_service ) { |
|
| 42 | + public function __construct($entity_service, $uri_service) { |
|
| 43 | 43 | |
| 44 | 44 | $this->entity_service = $entity_service; |
| 45 | 45 | $this->uri_service = $uri_service; |
| 46 | 46 | |
| 47 | - add_action( 'wp_ajax_wl_dump_uri', array( $this, 'dump_uri' ) ); |
|
| 47 | + add_action('wp_ajax_wl_dump_uri', array($this, 'dump_uri')); |
|
| 48 | 48 | |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | public function dump_uri() { |
| 52 | 52 | |
| 53 | - if ( ! isset( $_GET['id'] ) ) { |
|
| 54 | - wp_send_json_error( 'id not set' ); |
|
| 53 | + if ( ! isset($_GET['id'])) { |
|
| 54 | + wp_send_json_error('id not set'); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | $post_id = $_GET['id']; |
| 58 | 58 | |
| 59 | - $post = get_post( $post_id ); |
|
| 59 | + $post = get_post($post_id); |
|
| 60 | 60 | |
| 61 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 62 | - $build_uri = $this->uri_service->build_uri( $post->post_title, $post->post_type ); |
|
| 61 | + $uri = $this->entity_service->get_uri($post_id); |
|
| 62 | + $build_uri = $this->uri_service->build_uri($post->post_title, $post->post_type); |
|
| 63 | 63 | |
| 64 | 64 | |
| 65 | - var_dump( $uri ); |
|
| 65 | + var_dump($uri); |
|
| 66 | 66 | |
| 67 | - wp_send_json_success( array( |
|
| 67 | + wp_send_json_success(array( |
|
| 68 | 68 | 'uri' => $uri, |
| 69 | - 'post_title' => sprintf( '%s (%s)', $post->post_title, mb_detect_encoding( $post->post_title ) ), |
|
| 70 | - 'post_title_ascii' => mb_convert_encoding( $post->post_title, 'ASCII' ), |
|
| 69 | + 'post_title' => sprintf('%s (%s)', $post->post_title, mb_detect_encoding($post->post_title)), |
|
| 70 | + 'post_title_ascii' => mb_convert_encoding($post->post_title, 'ASCII'), |
|
| 71 | 71 | 'build_uri' => $build_uri, |
| 72 | - 'build_uri_convert' => mb_convert_encoding( $build_uri, 'ASCII' ), |
|
| 73 | - ) ); |
|
| 72 | + 'build_uri_convert' => mb_convert_encoding($build_uri, 'ASCII'), |
|
| 73 | + )); |
|
| 74 | 74 | |
| 75 | 75 | } |
| 76 | 76 | |
@@ -16,45 +16,45 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | function wl_linked_data_save_post( $post_id ) { |
| 18 | 18 | |
| 19 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 19 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 20 | 20 | |
| 21 | - $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 21 | + $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 22 | 22 | |
| 23 | - // If it's not numeric exit from here. |
|
| 24 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 25 | - $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 23 | + // If it's not numeric exit from here. |
|
| 24 | + if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 25 | + $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 26 | 26 | |
| 27 | - return; |
|
| 28 | - } |
|
| 27 | + return; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - // Get the post type and check whether it supports the editor. |
|
| 31 | - // |
|
| 32 | - // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
|
| 33 | - $post_type = get_post_type( $post_id ); |
|
| 34 | - /** |
|
| 35 | - * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
|
| 36 | - * |
|
| 37 | - * @since 3.19.4 |
|
| 38 | - * |
|
| 39 | - * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
| 40 | - */ |
|
| 41 | - $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 30 | + // Get the post type and check whether it supports the editor. |
|
| 31 | + // |
|
| 32 | + // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
|
| 33 | + $post_type = get_post_type( $post_id ); |
|
| 34 | + /** |
|
| 35 | + * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
|
| 36 | + * |
|
| 37 | + * @since 3.19.4 |
|
| 38 | + * |
|
| 39 | + * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
| 40 | + */ |
|
| 41 | + $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 42 | 42 | |
| 43 | - // Bail out if it's not an entity. |
|
| 44 | - if ( ! $is_editor_supported ) { |
|
| 45 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 43 | + // Bail out if it's not an entity. |
|
| 44 | + if ( ! $is_editor_supported ) { |
|
| 45 | + $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 46 | 46 | |
| 47 | - return; |
|
| 48 | - } |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - // Unhook this function so it doesn't loop infinitely. |
|
| 51 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 50 | + // Unhook this function so it doesn't loop infinitely. |
|
| 51 | + remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 52 | 52 | |
| 53 | - // raise the *wl_linked_data_save_post* event. |
|
| 54 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 53 | + // raise the *wl_linked_data_save_post* event. |
|
| 54 | + do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 55 | 55 | |
| 56 | - // Re-hook this function. |
|
| 57 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 56 | + // Re-hook this function. |
|
| 57 | + add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | add_action( 'save_post', 'wl_linked_data_save_post' ); |
@@ -176,156 +176,156 @@ discard block |
||
| 176 | 176 | */ |
| 177 | 177 | function wl_linked_data_save_post_and_related_entities( $post_id ) { |
| 178 | 178 | |
| 179 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 179 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 180 | 180 | |
| 181 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 181 | + $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 182 | 182 | |
| 183 | - // Ignore auto-saves |
|
| 184 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 185 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 183 | + // Ignore auto-saves |
|
| 184 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 185 | + $log->trace( 'Doing autosave, skipping...' ); |
|
| 186 | 186 | |
| 187 | - return; |
|
| 188 | - } |
|
| 187 | + return; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - // get the current post. |
|
| 191 | - $post = get_post( $post_id ); |
|
| 190 | + // get the current post. |
|
| 191 | + $post = get_post( $post_id ); |
|
| 192 | 192 | |
| 193 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 193 | + remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 194 | 194 | |
| 195 | - // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 195 | + // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 196 | 196 | |
| 197 | - // Get the entity service instance. |
|
| 198 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 197 | + // Get the entity service instance. |
|
| 198 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 199 | 199 | |
| 200 | - // Store mapping between tmp new entities uris and real new entities uri |
|
| 201 | - $entities_uri_mapping = array(); |
|
| 200 | + // Store mapping between tmp new entities uris and real new entities uri |
|
| 201 | + $entities_uri_mapping = array(); |
|
| 202 | 202 | |
| 203 | - // Save the entities coming with POST data. |
|
| 204 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 203 | + // Save the entities coming with POST data. |
|
| 204 | + if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 205 | 205 | |
| 206 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 207 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 208 | - wl_write_log( "]" ); |
|
| 209 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 210 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 211 | - wl_write_log( "]" ); |
|
| 206 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 207 | + wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 208 | + wl_write_log( "]" ); |
|
| 209 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 210 | + wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 211 | + wl_write_log( "]" ); |
|
| 212 | 212 | |
| 213 | - $entities_via_post = $_POST['wl_entities']; |
|
| 214 | - $boxes_via_post = $_POST['wl_boxes']; |
|
| 213 | + $entities_via_post = $_POST['wl_entities']; |
|
| 214 | + $boxes_via_post = $_POST['wl_boxes']; |
|
| 215 | 215 | |
| 216 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 216 | + foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 217 | 217 | |
| 218 | - // Only if the current entity is created from scratch let's avoid to |
|
| 219 | - // create more than one entity with same label & entity type. |
|
| 220 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 221 | - $entity['main_type'] : null; |
|
| 218 | + // Only if the current entity is created from scratch let's avoid to |
|
| 219 | + // create more than one entity with same label & entity type. |
|
| 220 | + $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 221 | + $entity['main_type'] : null; |
|
| 222 | 222 | |
| 223 | - // Look if current entity uri matches an internal existing entity, meaning: |
|
| 224 | - // 1. when $entity_uri is an internal uri |
|
| 225 | - // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 226 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 223 | + // Look if current entity uri matches an internal existing entity, meaning: |
|
| 224 | + // 1. when $entity_uri is an internal uri |
|
| 225 | + // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 226 | + $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 227 | 227 | |
| 228 | - // Detect the uri depending if is an existing or a new entity |
|
| 229 | - $uri = ( null === $ie ) ? |
|
| 230 | - Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 231 | - $entity['label'], |
|
| 232 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 233 | - $entity_type |
|
| 234 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 228 | + // Detect the uri depending if is an existing or a new entity |
|
| 229 | + $uri = ( null === $ie ) ? |
|
| 230 | + Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 231 | + $entity['label'], |
|
| 232 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 233 | + $entity_type |
|
| 234 | + ) : wl_get_entity_uri( $ie->ID ); |
|
| 235 | 235 | |
| 236 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 237 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 236 | + wl_write_log( "Map $entity_uri on $uri" ); |
|
| 237 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 238 | 238 | |
| 239 | - // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 240 | - // These uris need to be rewritten here and replaced in the content |
|
| 241 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 242 | - // Override the entity obj |
|
| 243 | - $entity['uri'] = $uri; |
|
| 244 | - } |
|
| 239 | + // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 240 | + // These uris need to be rewritten here and replaced in the content |
|
| 241 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 242 | + // Override the entity obj |
|
| 243 | + $entity['uri'] = $uri; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - // Update entity data with related post |
|
| 247 | - $entity['related_post_id'] = $post_id; |
|
| 246 | + // Update entity data with related post |
|
| 247 | + $entity['related_post_id'] = $post_id; |
|
| 248 | 248 | |
| 249 | - // Save the entity if is a new entity |
|
| 250 | - if ( null === $ie ) { |
|
| 251 | - wl_save_entity( $entity ); |
|
| 252 | - } |
|
| 249 | + // Save the entity if is a new entity |
|
| 250 | + if ( null === $ie ) { |
|
| 251 | + wl_save_entity( $entity ); |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - } |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - } |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - // Replace tmp uris in content post if needed |
|
| 259 | - $updated_post_content = $post->post_content; |
|
| 258 | + // Replace tmp uris in content post if needed |
|
| 259 | + $updated_post_content = $post->post_content; |
|
| 260 | 260 | |
| 261 | - // Update the post content if we found mappings of new entities. |
|
| 262 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 263 | - // Save each entity and store the post id. |
|
| 264 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 265 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 266 | - } |
|
| 261 | + // Update the post content if we found mappings of new entities. |
|
| 262 | + if ( ! empty( $entities_uri_mapping ) ) { |
|
| 263 | + // Save each entity and store the post id. |
|
| 264 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 265 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - // Update the post content. |
|
| 269 | - wp_update_post( array( |
|
| 270 | - 'ID' => $post->ID, |
|
| 271 | - 'post_content' => $updated_post_content, |
|
| 272 | - ) ); |
|
| 273 | - } |
|
| 268 | + // Update the post content. |
|
| 269 | + wp_update_post( array( |
|
| 270 | + 'ID' => $post->ID, |
|
| 271 | + 'post_content' => $updated_post_content, |
|
| 272 | + ) ); |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - // Extract related/referenced entities from text. |
|
| 276 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 275 | + // Extract related/referenced entities from text. |
|
| 276 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 277 | 277 | |
| 278 | - // Reset previously saved instances. |
|
| 279 | - wl_core_delete_relation_instances( $post_id ); |
|
| 278 | + // Reset previously saved instances. |
|
| 279 | + wl_core_delete_relation_instances( $post_id ); |
|
| 280 | 280 | |
| 281 | - // Save relation instances |
|
| 282 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 281 | + // Save relation instances |
|
| 282 | + foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 283 | 283 | |
| 284 | - wl_core_add_relation_instance( |
|
| 285 | - $post_id, |
|
| 286 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 287 | - $referenced_entity_id |
|
| 288 | - ); |
|
| 284 | + wl_core_add_relation_instance( |
|
| 285 | + $post_id, |
|
| 286 | + $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 287 | + $referenced_entity_id |
|
| 288 | + ); |
|
| 289 | 289 | |
| 290 | - } |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 293 | - // Save post metadata if available |
|
| 294 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 295 | - $_POST['wl_metadata'] : array(); |
|
| 296 | - |
|
| 297 | - $fields = array( |
|
| 298 | - Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 299 | - Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 300 | - ); |
|
| 301 | - |
|
| 302 | - // Unlink topic taxonomy terms |
|
| 303 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 304 | - |
|
| 305 | - foreach ( $fields as $field ) { |
|
| 306 | - |
|
| 307 | - // Delete current values |
|
| 308 | - delete_post_meta( $post->ID, $field ); |
|
| 309 | - // Retrieve the entity uri |
|
| 310 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 311 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 312 | - |
|
| 313 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 314 | - |
|
| 315 | - if ( $entity ) { |
|
| 316 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 317 | - // Set also the topic taxonomy |
|
| 318 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 319 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - // Push the post to Redlink. |
|
| 326 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 327 | - |
|
| 328 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 292 | + if ( isset( $_POST['wl_entities'] ) ) { |
|
| 293 | + // Save post metadata if available |
|
| 294 | + $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 295 | + $_POST['wl_metadata'] : array(); |
|
| 296 | + |
|
| 297 | + $fields = array( |
|
| 298 | + Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 299 | + Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 300 | + ); |
|
| 301 | + |
|
| 302 | + // Unlink topic taxonomy terms |
|
| 303 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 304 | + |
|
| 305 | + foreach ( $fields as $field ) { |
|
| 306 | + |
|
| 307 | + // Delete current values |
|
| 308 | + delete_post_meta( $post->ID, $field ); |
|
| 309 | + // Retrieve the entity uri |
|
| 310 | + $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 311 | + stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 312 | + |
|
| 313 | + $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 314 | + |
|
| 315 | + if ( $entity ) { |
|
| 316 | + add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 317 | + // Set also the topic taxonomy |
|
| 318 | + if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 319 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + // Push the post to Redlink. |
|
| 326 | + Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 327 | + |
|
| 328 | + add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 329 | 329 | } |
| 330 | 330 | |
| 331 | 331 | add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
@@ -348,208 +348,208 @@ discard block |
||
| 348 | 348 | */ |
| 349 | 349 | function wl_save_entity( $entity_data ) { |
| 350 | 350 | |
| 351 | - // Required for REST API calls |
|
| 352 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 353 | - require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 354 | - } |
|
| 351 | + // Required for REST API calls |
|
| 352 | + if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 353 | + require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 356 | + $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 357 | 357 | |
| 358 | - $uri = $entity_data['uri']; |
|
| 359 | - /* |
|
| 358 | + $uri = $entity_data['uri']; |
|
| 359 | + /* |
|
| 360 | 360 | * Data is coming from a $_POST, sanitize it. |
| 361 | 361 | * |
| 362 | 362 | * @since 3.19.4 |
| 363 | 363 | * |
| 364 | 364 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
| 365 | 365 | */ |
| 366 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 367 | - $type_uri = $entity_data['main_type']; |
|
| 368 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 369 | - $description = $entity_data['description']; |
|
| 370 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 371 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 372 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 373 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 374 | - |
|
| 375 | - // Get the synonyms. |
|
| 376 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 377 | - |
|
| 378 | - // Check whether an entity already exists with the provided URI. |
|
| 379 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 380 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 381 | - |
|
| 382 | - return $post; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - // Prepare properties of the new entity. |
|
| 386 | - $params = array( |
|
| 387 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 388 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 389 | - 'post_title' => $label, |
|
| 390 | - 'post_content' => $description, |
|
| 391 | - 'post_excerpt' => '', |
|
| 392 | - // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 393 | - // entity with a post_name already set, since this call is made only for |
|
| 394 | - // new entities. |
|
| 395 | - // |
|
| 396 | - // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 397 | - 'post_name' => sanitize_title( $label ), |
|
| 398 | - ); |
|
| 399 | - |
|
| 400 | - // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 401 | - // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 402 | - // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 403 | - // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 404 | - // is created when saving a post. |
|
| 405 | - global $wpseo_metabox, $seo_ultimate; |
|
| 406 | - if ( isset( $wpseo_metabox ) ) { |
|
| 407 | - remove_action( 'wp_insert_post', array( |
|
| 408 | - $wpseo_metabox, |
|
| 409 | - 'save_postdata', |
|
| 410 | - ) ); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - if ( isset( $seo_ultimate ) ) { |
|
| 414 | - remove_action( 'save_post', array( |
|
| 415 | - $seo_ultimate, |
|
| 416 | - 'save_postmeta_box', |
|
| 417 | - ) ); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 421 | - // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 422 | - // to the save_post and restore them after we saved the entity. |
|
| 423 | - // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 424 | - // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 425 | - // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 426 | - global $wp_filter; |
|
| 427 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 428 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 429 | - |
|
| 430 | - |
|
| 431 | - $log->trace( 'Going to insert new post...' ); |
|
| 432 | - |
|
| 433 | - // create or update the post. |
|
| 434 | - $post_id = wp_insert_post( $params, true ); |
|
| 435 | - |
|
| 436 | - // Setting the alternative labels for this entity. |
|
| 437 | - Wordlift_Entity_Service::get_instance() |
|
| 438 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 439 | - |
|
| 440 | - // Restore all the existing filters. |
|
| 441 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 442 | - |
|
| 443 | - // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 444 | - if ( isset( $wpseo_metabox ) ) { |
|
| 445 | - add_action( 'wp_insert_post', array( |
|
| 446 | - $wpseo_metabox, |
|
| 447 | - 'save_postdata', |
|
| 448 | - ) ); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 452 | - if ( isset( $seo_ultimate ) ) { |
|
| 453 | - add_action( 'save_post', array( |
|
| 454 | - $seo_ultimate, |
|
| 455 | - 'save_postmeta_box', |
|
| 456 | - ), 10, 2 ); |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - // TODO: handle errors. |
|
| 460 | - if ( is_wp_error( $post_id ) ) { |
|
| 461 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 462 | - |
|
| 463 | - // inform an error occurred. |
|
| 464 | - return null; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 468 | - |
|
| 469 | - // Save the entity types. |
|
| 470 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 471 | - |
|
| 472 | - // Get a dataset URI for the entity. |
|
| 473 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 474 | - |
|
| 475 | - // Save the entity URI. |
|
| 476 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 477 | - |
|
| 478 | - // Add the uri to the sameAs data if it's not a local URI. |
|
| 479 | - if ( $wl_uri !== $uri ) { |
|
| 480 | - array_push( $same_as, $uri ); |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 484 | - |
|
| 485 | - // Save the sameAs data for the entity. |
|
| 486 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 487 | - |
|
| 488 | - // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 489 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 490 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - // Call hooks. |
|
| 494 | - do_action( 'wl_save_entity', $post_id ); |
|
| 495 | - |
|
| 496 | - foreach ( $images as $image_remote_url ) { |
|
| 497 | - |
|
| 498 | - // Check if image is already present in local DB |
|
| 499 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 500 | - // Do nothing. |
|
| 501 | - continue; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - // Check if there is an existing attachment for this post ID and source URL. |
|
| 505 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 506 | - |
|
| 507 | - // Skip if an existing image is found. |
|
| 508 | - if ( null !== $existing_image ) { |
|
| 509 | - continue; |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - // Save the image and get the local path. |
|
| 513 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 514 | - |
|
| 515 | - if ( false === $image || is_wp_error( $image ) ) { |
|
| 516 | - continue; |
|
| 517 | - } |
|
| 366 | + $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 367 | + $type_uri = $entity_data['main_type']; |
|
| 368 | + $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 369 | + $description = $entity_data['description']; |
|
| 370 | + $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 371 | + $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 372 | + $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 373 | + $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 374 | + |
|
| 375 | + // Get the synonyms. |
|
| 376 | + $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 377 | + |
|
| 378 | + // Check whether an entity already exists with the provided URI. |
|
| 379 | + if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 380 | + $log->debug( "Post already exists for URI $uri." ); |
|
| 381 | + |
|
| 382 | + return $post; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + // Prepare properties of the new entity. |
|
| 386 | + $params = array( |
|
| 387 | + 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 388 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 389 | + 'post_title' => $label, |
|
| 390 | + 'post_content' => $description, |
|
| 391 | + 'post_excerpt' => '', |
|
| 392 | + // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 393 | + // entity with a post_name already set, since this call is made only for |
|
| 394 | + // new entities. |
|
| 395 | + // |
|
| 396 | + // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 397 | + 'post_name' => sanitize_title( $label ), |
|
| 398 | + ); |
|
| 399 | + |
|
| 400 | + // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 401 | + // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 402 | + // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 403 | + // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 404 | + // is created when saving a post. |
|
| 405 | + global $wpseo_metabox, $seo_ultimate; |
|
| 406 | + if ( isset( $wpseo_metabox ) ) { |
|
| 407 | + remove_action( 'wp_insert_post', array( |
|
| 408 | + $wpseo_metabox, |
|
| 409 | + 'save_postdata', |
|
| 410 | + ) ); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + if ( isset( $seo_ultimate ) ) { |
|
| 414 | + remove_action( 'save_post', array( |
|
| 415 | + $seo_ultimate, |
|
| 416 | + 'save_postmeta_box', |
|
| 417 | + ) ); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 421 | + // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 422 | + // to the save_post and restore them after we saved the entity. |
|
| 423 | + // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 424 | + // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 425 | + // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 426 | + global $wp_filter; |
|
| 427 | + $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 428 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 429 | + |
|
| 430 | + |
|
| 431 | + $log->trace( 'Going to insert new post...' ); |
|
| 432 | + |
|
| 433 | + // create or update the post. |
|
| 434 | + $post_id = wp_insert_post( $params, true ); |
|
| 435 | + |
|
| 436 | + // Setting the alternative labels for this entity. |
|
| 437 | + Wordlift_Entity_Service::get_instance() |
|
| 438 | + ->set_alternative_labels( $post_id, $synonyms ); |
|
| 439 | + |
|
| 440 | + // Restore all the existing filters. |
|
| 441 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 442 | + |
|
| 443 | + // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 444 | + if ( isset( $wpseo_metabox ) ) { |
|
| 445 | + add_action( 'wp_insert_post', array( |
|
| 446 | + $wpseo_metabox, |
|
| 447 | + 'save_postdata', |
|
| 448 | + ) ); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 452 | + if ( isset( $seo_ultimate ) ) { |
|
| 453 | + add_action( 'save_post', array( |
|
| 454 | + $seo_ultimate, |
|
| 455 | + 'save_postmeta_box', |
|
| 456 | + ), 10, 2 ); |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + // TODO: handle errors. |
|
| 460 | + if ( is_wp_error( $post_id ) ) { |
|
| 461 | + $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 462 | + |
|
| 463 | + // inform an error occurred. |
|
| 464 | + return null; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 468 | + |
|
| 469 | + // Save the entity types. |
|
| 470 | + wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 471 | + |
|
| 472 | + // Get a dataset URI for the entity. |
|
| 473 | + $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 474 | + |
|
| 475 | + // Save the entity URI. |
|
| 476 | + wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 477 | + |
|
| 478 | + // Add the uri to the sameAs data if it's not a local URI. |
|
| 479 | + if ( $wl_uri !== $uri ) { |
|
| 480 | + array_push( $same_as, $uri ); |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + $new_uri = wl_get_entity_uri( $post_id ); |
|
| 484 | + |
|
| 485 | + // Save the sameAs data for the entity. |
|
| 486 | + wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 487 | + |
|
| 488 | + // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 489 | + foreach ( $other_properties as $property_name => $property_value ) { |
|
| 490 | + wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + // Call hooks. |
|
| 494 | + do_action( 'wl_save_entity', $post_id ); |
|
| 495 | + |
|
| 496 | + foreach ( $images as $image_remote_url ) { |
|
| 497 | + |
|
| 498 | + // Check if image is already present in local DB |
|
| 499 | + if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 500 | + // Do nothing. |
|
| 501 | + continue; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + // Check if there is an existing attachment for this post ID and source URL. |
|
| 505 | + $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 506 | + |
|
| 507 | + // Skip if an existing image is found. |
|
| 508 | + if ( null !== $existing_image ) { |
|
| 509 | + continue; |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + // Save the image and get the local path. |
|
| 513 | + $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 514 | + |
|
| 515 | + if ( false === $image || is_wp_error( $image ) ) { |
|
| 516 | + continue; |
|
| 517 | + } |
|
| 518 | 518 | |
| 519 | - // Get the local URL. |
|
| 520 | - $filename = $image['path']; |
|
| 521 | - $url = $image['url']; |
|
| 522 | - $content_type = $image['content_type']; |
|
| 519 | + // Get the local URL. |
|
| 520 | + $filename = $image['path']; |
|
| 521 | + $url = $image['url']; |
|
| 522 | + $content_type = $image['content_type']; |
|
| 523 | 523 | |
| 524 | - $attachment = array( |
|
| 525 | - 'guid' => $url, |
|
| 526 | - // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 527 | - 'post_title' => $label, |
|
| 528 | - // Set the title to the post title. |
|
| 529 | - 'post_content' => '', |
|
| 530 | - 'post_status' => 'inherit', |
|
| 531 | - 'post_mime_type' => $content_type, |
|
| 532 | - ); |
|
| 533 | - |
|
| 534 | - // Create the attachment in WordPress and generate the related metadata. |
|
| 535 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 536 | - |
|
| 537 | - // Set the source URL for the image. |
|
| 538 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 524 | + $attachment = array( |
|
| 525 | + 'guid' => $url, |
|
| 526 | + // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 527 | + 'post_title' => $label, |
|
| 528 | + // Set the title to the post title. |
|
| 529 | + 'post_content' => '', |
|
| 530 | + 'post_status' => 'inherit', |
|
| 531 | + 'post_mime_type' => $content_type, |
|
| 532 | + ); |
|
| 533 | + |
|
| 534 | + // Create the attachment in WordPress and generate the related metadata. |
|
| 535 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 536 | + |
|
| 537 | + // Set the source URL for the image. |
|
| 538 | + wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 539 | 539 | |
| 540 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 541 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 540 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 541 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 542 | 542 | |
| 543 | - // Set it as the featured image. |
|
| 544 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 545 | - } |
|
| 543 | + // Set it as the featured image. |
|
| 544 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 545 | + } |
|
| 546 | 546 | |
| 547 | - // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 548 | - // save the entity in the triple store. |
|
| 549 | - Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 547 | + // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 548 | + // save the entity in the triple store. |
|
| 549 | + Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 550 | 550 | |
| 551 | - // finally return the entity post. |
|
| 552 | - return get_post( $post_id ); |
|
| 551 | + // finally return the entity post. |
|
| 552 | + return get_post( $post_id ); |
|
| 553 | 553 | } |
| 554 | 554 | |
| 555 | 555 | /** |
@@ -563,40 +563,40 @@ discard block |
||
| 563 | 563 | */ |
| 564 | 564 | function wl_linked_data_content_get_embedded_entities( $content ) { |
| 565 | 565 | |
| 566 | - // Remove quote escapes. |
|
| 567 | - $content = str_replace( '\\"', '"', $content ); |
|
| 566 | + // Remove quote escapes. |
|
| 567 | + $content = str_replace( '\\"', '"', $content ); |
|
| 568 | 568 | |
| 569 | - // Match all itemid attributes. |
|
| 570 | - $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 569 | + // Match all itemid attributes. |
|
| 570 | + $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 571 | 571 | |
| 572 | - // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 572 | + // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 573 | 573 | |
| 574 | - // Remove the pattern while it is found (match nested annotations). |
|
| 575 | - $matches = array(); |
|
| 574 | + // Remove the pattern while it is found (match nested annotations). |
|
| 575 | + $matches = array(); |
|
| 576 | 576 | |
| 577 | - // In case of errors, return an empty array. |
|
| 578 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 579 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 577 | + // In case of errors, return an empty array. |
|
| 578 | + if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 579 | + wl_write_log( "Found no entities embedded in content" ); |
|
| 580 | 580 | |
| 581 | - return array(); |
|
| 582 | - } |
|
| 581 | + return array(); |
|
| 582 | + } |
|
| 583 | 583 | |
| 584 | 584 | // wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]"); |
| 585 | 585 | |
| 586 | - // Collect the entities. |
|
| 587 | - $entities = array(); |
|
| 588 | - foreach ( $matches[1] as $uri ) { |
|
| 589 | - $uri_d = html_entity_decode( $uri ); |
|
| 586 | + // Collect the entities. |
|
| 587 | + $entities = array(); |
|
| 588 | + foreach ( $matches[1] as $uri ) { |
|
| 589 | + $uri_d = html_entity_decode( $uri ); |
|
| 590 | 590 | |
| 591 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 591 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 592 | 592 | |
| 593 | - if ( null !== $entity ) { |
|
| 594 | - array_push( $entities, $entity->ID ); |
|
| 595 | - } |
|
| 596 | - } |
|
| 593 | + if ( null !== $entity ) { |
|
| 594 | + array_push( $entities, $entity->ID ); |
|
| 595 | + } |
|
| 596 | + } |
|
| 597 | 597 | |
| 598 | - // $count = sizeof( $entities ); |
|
| 599 | - // wl_write_log( "Found $count entities embedded in content" ); |
|
| 598 | + // $count = sizeof( $entities ); |
|
| 599 | + // wl_write_log( "Found $count entities embedded in content" ); |
|
| 600 | 600 | |
| 601 | - return $entities; |
|
| 601 | + return $entities; |
|
| 602 | 602 | } |
@@ -14,15 +14,15 @@ discard block |
||
| 14 | 14 | * |
| 15 | 15 | * @param int $post_id The post id. |
| 16 | 16 | */ |
| 17 | -function wl_linked_data_save_post( $post_id ) { |
|
| 17 | +function wl_linked_data_save_post($post_id) { |
|
| 18 | 18 | |
| 19 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 19 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post'); |
|
| 20 | 20 | |
| 21 | - $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 21 | + $log->trace("Saving post $post_id to Linked Data..."); |
|
| 22 | 22 | |
| 23 | 23 | // If it's not numeric exit from here. |
| 24 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 25 | - $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 24 | + if ( ! is_numeric($post_id) || is_numeric(wp_is_post_revision($post_id))) { |
|
| 25 | + $log->debug("Skipping $post_id, because id is not numeric or is a post revision."); |
|
| 26 | 26 | |
| 27 | 27 | return; |
| 28 | 28 | } |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | // Get the post type and check whether it supports the editor. |
| 31 | 31 | // |
| 32 | 32 | // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
| 33 | - $post_type = get_post_type( $post_id ); |
|
| 33 | + $post_type = get_post_type($post_id); |
|
| 34 | 34 | /** |
| 35 | 35 | * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
| 36 | 36 | * |
@@ -38,26 +38,26 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
| 40 | 40 | */ |
| 41 | - $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 41 | + $is_editor_supported = wl_post_type_supports_editor($post_type); |
|
| 42 | 42 | |
| 43 | 43 | // Bail out if it's not an entity. |
| 44 | - if ( ! $is_editor_supported ) { |
|
| 45 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 44 | + if ( ! $is_editor_supported) { |
|
| 45 | + $log->debug("Skipping $post_id, because $post_type doesn't support the editor (content)."); |
|
| 46 | 46 | |
| 47 | 47 | return; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | // Unhook this function so it doesn't loop infinitely. |
| 51 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 51 | + remove_action('save_post', 'wl_linked_data_save_post'); |
|
| 52 | 52 | |
| 53 | 53 | // raise the *wl_linked_data_save_post* event. |
| 54 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 54 | + do_action('wl_linked_data_save_post', $post_id); |
|
| 55 | 55 | |
| 56 | 56 | // Re-hook this function. |
| 57 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 57 | + add_action('save_post', 'wl_linked_data_save_post'); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | -add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 60 | +add_action('save_post', 'wl_linked_data_save_post'); |
|
| 61 | 61 | |
| 62 | 62 | //add_action( 'rest_insert_post', 'wl_linked_data_rest_insert_post', 10, 3 ); |
| 63 | 63 | // |
@@ -174,23 +174,23 @@ discard block |
||
| 174 | 174 | * |
| 175 | 175 | * @param int $post_id The post id being saved. |
| 176 | 176 | */ |
| 177 | -function wl_linked_data_save_post_and_related_entities( $post_id ) { |
|
| 177 | +function wl_linked_data_save_post_and_related_entities($post_id) { |
|
| 178 | 178 | |
| 179 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 179 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities'); |
|
| 180 | 180 | |
| 181 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 181 | + $log->trace("Saving $post_id to Linked Data along with related entities..."); |
|
| 182 | 182 | |
| 183 | 183 | // Ignore auto-saves |
| 184 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 185 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 184 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 185 | + $log->trace('Doing autosave, skipping...'); |
|
| 186 | 186 | |
| 187 | 187 | return; |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | // get the current post. |
| 191 | - $post = get_post( $post_id ); |
|
| 191 | + $post = get_post($post_id); |
|
| 192 | 192 | |
| 193 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 193 | + remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 194 | 194 | |
| 195 | 195 | // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
| 196 | 196 | |
@@ -201,44 +201,44 @@ discard block |
||
| 201 | 201 | $entities_uri_mapping = array(); |
| 202 | 202 | |
| 203 | 203 | // Save the entities coming with POST data. |
| 204 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 204 | + if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) { |
|
| 205 | 205 | |
| 206 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 207 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 208 | - wl_write_log( "]" ); |
|
| 209 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 210 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 211 | - wl_write_log( "]" ); |
|
| 206 | + wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: "); |
|
| 207 | + wl_write_log(json_encode($_POST['wl_entities'])); |
|
| 208 | + wl_write_log("]"); |
|
| 209 | + wl_write_log("[ post id :: $post_id ][ POST(wl_boxes) :: "); |
|
| 210 | + wl_write_log(json_encode($_POST['wl_boxes'], true)); |
|
| 211 | + wl_write_log("]"); |
|
| 212 | 212 | |
| 213 | 213 | $entities_via_post = $_POST['wl_entities']; |
| 214 | 214 | $boxes_via_post = $_POST['wl_boxes']; |
| 215 | 215 | |
| 216 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 216 | + foreach ($entities_via_post as $entity_uri => $entity) { |
|
| 217 | 217 | |
| 218 | 218 | // Only if the current entity is created from scratch let's avoid to |
| 219 | 219 | // create more than one entity with same label & entity type. |
| 220 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 220 | + $entity_type = (preg_match('/^local-entity-.+/', $entity_uri) > 0) ? |
|
| 221 | 221 | $entity['main_type'] : null; |
| 222 | 222 | |
| 223 | 223 | // Look if current entity uri matches an internal existing entity, meaning: |
| 224 | 224 | // 1. when $entity_uri is an internal uri |
| 225 | 225 | // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
| 226 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 226 | + $ie = $entity_service->get_entity_post_by_uri($entity_uri); |
|
| 227 | 227 | |
| 228 | 228 | // Detect the uri depending if is an existing or a new entity |
| 229 | - $uri = ( null === $ie ) ? |
|
| 229 | + $uri = (null === $ie) ? |
|
| 230 | 230 | Wordlift_Uri_Service::get_instance()->build_uri( |
| 231 | 231 | $entity['label'], |
| 232 | 232 | Wordlift_Entity_Service::TYPE_NAME, |
| 233 | 233 | $entity_type |
| 234 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 234 | + ) : wl_get_entity_uri($ie->ID); |
|
| 235 | 235 | |
| 236 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 237 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 236 | + wl_write_log("Map $entity_uri on $uri"); |
|
| 237 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
| 238 | 238 | |
| 239 | 239 | // Local entities have a tmp uri with 'local-entity-' prefix |
| 240 | 240 | // These uris need to be rewritten here and replaced in the content |
| 241 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 241 | + if (preg_match('/^local-entity-.+/', $entity_uri) > 0) { |
|
| 242 | 242 | // Override the entity obj |
| 243 | 243 | $entity['uri'] = $uri; |
| 244 | 244 | } |
@@ -247,8 +247,8 @@ discard block |
||
| 247 | 247 | $entity['related_post_id'] = $post_id; |
| 248 | 248 | |
| 249 | 249 | // Save the entity if is a new entity |
| 250 | - if ( null === $ie ) { |
|
| 251 | - wl_save_entity( $entity ); |
|
| 250 | + if (null === $ie) { |
|
| 251 | + wl_save_entity($entity); |
|
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | } |
@@ -259,39 +259,39 @@ discard block |
||
| 259 | 259 | $updated_post_content = $post->post_content; |
| 260 | 260 | |
| 261 | 261 | // Update the post content if we found mappings of new entities. |
| 262 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 262 | + if ( ! empty($entities_uri_mapping)) { |
|
| 263 | 263 | // Save each entity and store the post id. |
| 264 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 265 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 264 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
| 265 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | // Update the post content. |
| 269 | - wp_update_post( array( |
|
| 269 | + wp_update_post(array( |
|
| 270 | 270 | 'ID' => $post->ID, |
| 271 | 271 | 'post_content' => $updated_post_content, |
| 272 | - ) ); |
|
| 272 | + )); |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | // Extract related/referenced entities from text. |
| 276 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 276 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content); |
|
| 277 | 277 | |
| 278 | 278 | // Reset previously saved instances. |
| 279 | - wl_core_delete_relation_instances( $post_id ); |
|
| 279 | + wl_core_delete_relation_instances($post_id); |
|
| 280 | 280 | |
| 281 | 281 | // Save relation instances |
| 282 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 282 | + foreach (array_unique($disambiguated_entities) as $referenced_entity_id) { |
|
| 283 | 283 | |
| 284 | 284 | wl_core_add_relation_instance( |
| 285 | 285 | $post_id, |
| 286 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 286 | + $entity_service->get_classification_scope_for($referenced_entity_id), |
|
| 287 | 287 | $referenced_entity_id |
| 288 | 288 | ); |
| 289 | 289 | |
| 290 | 290 | } |
| 291 | 291 | |
| 292 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 292 | + if (isset($_POST['wl_entities'])) { |
|
| 293 | 293 | // Save post metadata if available |
| 294 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 294 | + $metadata_via_post = (isset($_POST['wl_metadata'])) ? |
|
| 295 | 295 | $_POST['wl_metadata'] : array(); |
| 296 | 296 | |
| 297 | 297 | $fields = array( |
@@ -300,35 +300,35 @@ discard block |
||
| 300 | 300 | ); |
| 301 | 301 | |
| 302 | 302 | // Unlink topic taxonomy terms |
| 303 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 303 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID); |
|
| 304 | 304 | |
| 305 | - foreach ( $fields as $field ) { |
|
| 305 | + foreach ($fields as $field) { |
|
| 306 | 306 | |
| 307 | 307 | // Delete current values |
| 308 | - delete_post_meta( $post->ID, $field ); |
|
| 308 | + delete_post_meta($post->ID, $field); |
|
| 309 | 309 | // Retrieve the entity uri |
| 310 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 311 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 310 | + $uri = (isset($metadata_via_post[$field])) ? |
|
| 311 | + stripslashes($metadata_via_post[$field]) : ''; |
|
| 312 | 312 | |
| 313 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 313 | + $entity = $entity_service->get_entity_post_by_uri($uri); |
|
| 314 | 314 | |
| 315 | - if ( $entity ) { |
|
| 316 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 315 | + if ($entity) { |
|
| 316 | + add_post_meta($post->ID, $field, $entity->ID, true); |
|
| 317 | 317 | // Set also the topic taxonomy |
| 318 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 319 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 318 | + if ($field === Wordlift_Schema_Service::FIELD_TOPIC) { |
|
| 319 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity); |
|
| 320 | 320 | } |
| 321 | 321 | } |
| 322 | 322 | } |
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | // Push the post to Redlink. |
| 326 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 326 | + Wordlift_Linked_Data_Service::get_instance()->push($post->ID); |
|
| 327 | 327 | |
| 328 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 328 | + add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 329 | 329 | } |
| 330 | 330 | |
| 331 | -add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 331 | +add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 332 | 332 | |
| 333 | 333 | /** |
| 334 | 334 | * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is |
@@ -346,14 +346,14 @@ discard block |
||
| 346 | 346 | * |
| 347 | 347 | * @return null|WP_Post A post instance or null in case of failure. |
| 348 | 348 | */ |
| 349 | -function wl_save_entity( $entity_data ) { |
|
| 349 | +function wl_save_entity($entity_data) { |
|
| 350 | 350 | |
| 351 | 351 | // Required for REST API calls |
| 352 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 353 | - require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 352 | + if ( ! function_exists('wp_crop_image')) { |
|
| 353 | + require_once(ABSPATH.'wp-admin/includes/image.php'); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 356 | + $log = Wordlift_Log_Service::get_logger('wl_save_entity'); |
|
| 357 | 357 | |
| 358 | 358 | $uri = $entity_data['uri']; |
| 359 | 359 | /* |
@@ -363,28 +363,28 @@ discard block |
||
| 363 | 363 | * |
| 364 | 364 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
| 365 | 365 | */ |
| 366 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 366 | + $label = preg_replace('/\xEF\xBB\xBF/', '', sanitize_text_field($entity_data['label'])); |
|
| 367 | 367 | $type_uri = $entity_data['main_type']; |
| 368 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 368 | + $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array(); |
|
| 369 | 369 | $description = $entity_data['description']; |
| 370 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 371 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 372 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 373 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 370 | + $images = isset($entity_data['image']) ? (array) $entity_data['image'] : array(); |
|
| 371 | + $same_as = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array(); |
|
| 372 | + $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null; |
|
| 373 | + $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array(); |
|
| 374 | 374 | |
| 375 | 375 | // Get the synonyms. |
| 376 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 376 | + $synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array(); |
|
| 377 | 377 | |
| 378 | 378 | // Check whether an entity already exists with the provided URI. |
| 379 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 380 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 379 | + if (null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri)) { |
|
| 380 | + $log->debug("Post already exists for URI $uri."); |
|
| 381 | 381 | |
| 382 | 382 | return $post; |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | // Prepare properties of the new entity. |
| 386 | 386 | $params = array( |
| 387 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 387 | + 'post_status' => (is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft'), |
|
| 388 | 388 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 389 | 389 | 'post_title' => $label, |
| 390 | 390 | 'post_content' => $description, |
@@ -394,7 +394,7 @@ discard block |
||
| 394 | 394 | // new entities. |
| 395 | 395 | // |
| 396 | 396 | // See https://github.com/insideout10/wordlift-plugin/issues/282 |
| 397 | - 'post_name' => sanitize_title( $label ), |
|
| 397 | + 'post_name' => sanitize_title($label), |
|
| 398 | 398 | ); |
| 399 | 399 | |
| 400 | 400 | // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
@@ -403,18 +403,18 @@ discard block |
||
| 403 | 403 | // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
| 404 | 404 | // is created when saving a post. |
| 405 | 405 | global $wpseo_metabox, $seo_ultimate; |
| 406 | - if ( isset( $wpseo_metabox ) ) { |
|
| 407 | - remove_action( 'wp_insert_post', array( |
|
| 406 | + if (isset($wpseo_metabox)) { |
|
| 407 | + remove_action('wp_insert_post', array( |
|
| 408 | 408 | $wpseo_metabox, |
| 409 | 409 | 'save_postdata', |
| 410 | - ) ); |
|
| 410 | + )); |
|
| 411 | 411 | } |
| 412 | 412 | |
| 413 | - if ( isset( $seo_ultimate ) ) { |
|
| 414 | - remove_action( 'save_post', array( |
|
| 413 | + if (isset($seo_ultimate)) { |
|
| 414 | + remove_action('save_post', array( |
|
| 415 | 415 | $seo_ultimate, |
| 416 | 416 | 'save_postmeta_box', |
| 417 | - ) ); |
|
| 417 | + )); |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
@@ -424,95 +424,95 @@ discard block |
||
| 424 | 424 | // see https://github.com/insideout10/wordlift-plugin/issues/156 |
| 425 | 425 | // see https://github.com/insideout10/wordlift-plugin/issues/148 |
| 426 | 426 | global $wp_filter; |
| 427 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 428 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 427 | + $save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 428 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 429 | 429 | |
| 430 | 430 | |
| 431 | - $log->trace( 'Going to insert new post...' ); |
|
| 431 | + $log->trace('Going to insert new post...'); |
|
| 432 | 432 | |
| 433 | 433 | // create or update the post. |
| 434 | - $post_id = wp_insert_post( $params, true ); |
|
| 434 | + $post_id = wp_insert_post($params, true); |
|
| 435 | 435 | |
| 436 | 436 | // Setting the alternative labels for this entity. |
| 437 | 437 | Wordlift_Entity_Service::get_instance() |
| 438 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 438 | + ->set_alternative_labels($post_id, $synonyms); |
|
| 439 | 439 | |
| 440 | 440 | // Restore all the existing filters. |
| 441 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 441 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 442 | 442 | |
| 443 | 443 | // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
| 444 | - if ( isset( $wpseo_metabox ) ) { |
|
| 445 | - add_action( 'wp_insert_post', array( |
|
| 444 | + if (isset($wpseo_metabox)) { |
|
| 445 | + add_action('wp_insert_post', array( |
|
| 446 | 446 | $wpseo_metabox, |
| 447 | 447 | 'save_postdata', |
| 448 | - ) ); |
|
| 448 | + )); |
|
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
| 452 | - if ( isset( $seo_ultimate ) ) { |
|
| 453 | - add_action( 'save_post', array( |
|
| 452 | + if (isset($seo_ultimate)) { |
|
| 453 | + add_action('save_post', array( |
|
| 454 | 454 | $seo_ultimate, |
| 455 | 455 | 'save_postmeta_box', |
| 456 | - ), 10, 2 ); |
|
| 456 | + ), 10, 2); |
|
| 457 | 457 | } |
| 458 | 458 | |
| 459 | 459 | // TODO: handle errors. |
| 460 | - if ( is_wp_error( $post_id ) ) { |
|
| 461 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 460 | + if (is_wp_error($post_id)) { |
|
| 461 | + $log->error('An error occurred: '.$post_id->get_error_message()); |
|
| 462 | 462 | |
| 463 | 463 | // inform an error occurred. |
| 464 | 464 | return null; |
| 465 | 465 | } |
| 466 | 466 | |
| 467 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 467 | + wl_set_entity_main_type($post_id, $type_uri); |
|
| 468 | 468 | |
| 469 | 469 | // Save the entity types. |
| 470 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 470 | + wl_set_entity_rdf_types($post_id, $entity_types); |
|
| 471 | 471 | |
| 472 | 472 | // Get a dataset URI for the entity. |
| 473 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 473 | + $wl_uri = wl_build_entity_uri($post_id); |
|
| 474 | 474 | |
| 475 | 475 | // Save the entity URI. |
| 476 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 476 | + wl_set_entity_uri($post_id, $wl_uri); |
|
| 477 | 477 | |
| 478 | 478 | // Add the uri to the sameAs data if it's not a local URI. |
| 479 | - if ( $wl_uri !== $uri ) { |
|
| 480 | - array_push( $same_as, $uri ); |
|
| 479 | + if ($wl_uri !== $uri) { |
|
| 480 | + array_push($same_as, $uri); |
|
| 481 | 481 | } |
| 482 | 482 | |
| 483 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 483 | + $new_uri = wl_get_entity_uri($post_id); |
|
| 484 | 484 | |
| 485 | 485 | // Save the sameAs data for the entity. |
| 486 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 486 | + wl_schema_set_value($post_id, 'sameAs', $same_as); |
|
| 487 | 487 | |
| 488 | 488 | // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
| 489 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 490 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 489 | + foreach ($other_properties as $property_name => $property_value) { |
|
| 490 | + wl_schema_set_value($post_id, $property_name, $property_value); |
|
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | // Call hooks. |
| 494 | - do_action( 'wl_save_entity', $post_id ); |
|
| 494 | + do_action('wl_save_entity', $post_id); |
|
| 495 | 495 | |
| 496 | - foreach ( $images as $image_remote_url ) { |
|
| 496 | + foreach ($images as $image_remote_url) { |
|
| 497 | 497 | |
| 498 | 498 | // Check if image is already present in local DB |
| 499 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 499 | + if (strpos($image_remote_url, site_url()) !== false) { |
|
| 500 | 500 | // Do nothing. |
| 501 | 501 | continue; |
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | // Check if there is an existing attachment for this post ID and source URL. |
| 505 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 505 | + $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url); |
|
| 506 | 506 | |
| 507 | 507 | // Skip if an existing image is found. |
| 508 | - if ( null !== $existing_image ) { |
|
| 508 | + if (null !== $existing_image) { |
|
| 509 | 509 | continue; |
| 510 | 510 | } |
| 511 | 511 | |
| 512 | 512 | // Save the image and get the local path. |
| 513 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 513 | + $image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url); |
|
| 514 | 514 | |
| 515 | - if ( false === $image || is_wp_error( $image ) ) { |
|
| 515 | + if (false === $image || is_wp_error($image)) { |
|
| 516 | 516 | continue; |
| 517 | 517 | } |
| 518 | 518 | |
@@ -532,24 +532,24 @@ discard block |
||
| 532 | 532 | ); |
| 533 | 533 | |
| 534 | 534 | // Create the attachment in WordPress and generate the related metadata. |
| 535 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 535 | + $attachment_id = wp_insert_attachment($attachment, $filename, $post_id); |
|
| 536 | 536 | |
| 537 | 537 | // Set the source URL for the image. |
| 538 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 538 | + wl_set_source_url($attachment_id, $image_remote_url); |
|
| 539 | 539 | |
| 540 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 541 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 540 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 541 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 542 | 542 | |
| 543 | 543 | // Set it as the featured image. |
| 544 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 544 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 545 | 545 | } |
| 546 | 546 | |
| 547 | 547 | // The entity is pushed to Redlink on save by the function hooked to save_post. |
| 548 | 548 | // save the entity in the triple store. |
| 549 | - Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 549 | + Wordlift_Linked_Data_Service::get_instance()->push($post_id); |
|
| 550 | 550 | |
| 551 | 551 | // finally return the entity post. |
| 552 | - return get_post( $post_id ); |
|
| 552 | + return get_post($post_id); |
|
| 553 | 553 | } |
| 554 | 554 | |
| 555 | 555 | /** |
@@ -561,10 +561,10 @@ discard block |
||
| 561 | 561 | * |
| 562 | 562 | * @return array An array of entity posts. |
| 563 | 563 | */ |
| 564 | -function wl_linked_data_content_get_embedded_entities( $content ) { |
|
| 564 | +function wl_linked_data_content_get_embedded_entities($content) { |
|
| 565 | 565 | |
| 566 | 566 | // Remove quote escapes. |
| 567 | - $content = str_replace( '\\"', '"', $content ); |
|
| 567 | + $content = str_replace('\\"', '"', $content); |
|
| 568 | 568 | |
| 569 | 569 | // Match all itemid attributes. |
| 570 | 570 | $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
@@ -575,8 +575,8 @@ discard block |
||
| 575 | 575 | $matches = array(); |
| 576 | 576 | |
| 577 | 577 | // In case of errors, return an empty array. |
| 578 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 579 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 578 | + if (false === preg_match_all($pattern, $content, $matches)) { |
|
| 579 | + wl_write_log("Found no entities embedded in content"); |
|
| 580 | 580 | |
| 581 | 581 | return array(); |
| 582 | 582 | } |
@@ -585,13 +585,13 @@ discard block |
||
| 585 | 585 | |
| 586 | 586 | // Collect the entities. |
| 587 | 587 | $entities = array(); |
| 588 | - foreach ( $matches[1] as $uri ) { |
|
| 589 | - $uri_d = html_entity_decode( $uri ); |
|
| 588 | + foreach ($matches[1] as $uri) { |
|
| 589 | + $uri_d = html_entity_decode($uri); |
|
| 590 | 590 | |
| 591 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 591 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri_d); |
|
| 592 | 592 | |
| 593 | - if ( null !== $entity ) { |
|
| 594 | - array_push( $entities, $entity->ID ); |
|
| 593 | + if (null !== $entity) { |
|
| 594 | + array_push($entities, $entity->ID); |
|
| 595 | 595 | } |
| 596 | 596 | } |
| 597 | 597 | |