@@ -18,48 +18,48 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Admin_Settings_Page_Action_Link { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The {@link Wordlift_Admin_Settings_Page} instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.11.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Admin_Settings_Page $settings_page The {@link Wordlift_Admin_Settings_Page} instance. |
|
| 27 | - */ |
|
| 28 | - private $settings_page; |
|
| 21 | + /** |
|
| 22 | + * The {@link Wordlift_Admin_Settings_Page} instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.11.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Admin_Settings_Page $settings_page The {@link Wordlift_Admin_Settings_Page} instance. |
|
| 27 | + */ |
|
| 28 | + private $settings_page; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Create a {@link Wordlift_Admin_Settings_Page_Action_Link} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.11.0 |
|
| 34 | - * |
|
| 35 | - * @param \Wordlift_Admin_Settings_Page $settings_page The {@link Wordlift_Admin_Settings_Page} instance. |
|
| 36 | - */ |
|
| 37 | - function __construct( $settings_page ) { |
|
| 30 | + /** |
|
| 31 | + * Create a {@link Wordlift_Admin_Settings_Page_Action_Link} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.11.0 |
|
| 34 | + * |
|
| 35 | + * @param \Wordlift_Admin_Settings_Page $settings_page The {@link Wordlift_Admin_Settings_Page} instance. |
|
| 36 | + */ |
|
| 37 | + function __construct( $settings_page ) { |
|
| 38 | 38 | |
| 39 | - $this->settings_page = $settings_page; |
|
| 39 | + $this->settings_page = $settings_page; |
|
| 40 | 40 | |
| 41 | - } |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Create a link to the WordLift settings page. |
|
| 45 | - * |
|
| 46 | - * @since 3.11.0 |
|
| 47 | - * |
|
| 48 | - * @param array $links An array of links. |
|
| 49 | - * |
|
| 50 | - * @return array An array of links including those added by the plugin. |
|
| 51 | - */ |
|
| 52 | - function action_links( $links ) { |
|
| 43 | + /** |
|
| 44 | + * Create a link to the WordLift settings page. |
|
| 45 | + * |
|
| 46 | + * @since 3.11.0 |
|
| 47 | + * |
|
| 48 | + * @param array $links An array of links. |
|
| 49 | + * |
|
| 50 | + * @return array An array of links including those added by the plugin. |
|
| 51 | + */ |
|
| 52 | + function action_links( $links ) { |
|
| 53 | 53 | |
| 54 | - // Get the menu slug from the page, then prepare the path, hence the url. |
|
| 55 | - $menu_slug = $this->settings_page->get_menu_slug(); |
|
| 56 | - $path = "admin.php?page=$menu_slug"; |
|
| 57 | - $url = get_admin_url( null, $path ); |
|
| 54 | + // Get the menu slug from the page, then prepare the path, hence the url. |
|
| 55 | + $menu_slug = $this->settings_page->get_menu_slug(); |
|
| 56 | + $path = "admin.php?page=$menu_slug"; |
|
| 57 | + $url = get_admin_url( null, $path ); |
|
| 58 | 58 | |
| 59 | - // Add our own link to the list of links. |
|
| 60 | - return array_merge( $links, array( |
|
| 61 | - sprintf( "<a href='$url'>%s</a>", __( 'Settings', 'wordlift' ) ), |
|
| 62 | - ) ); |
|
| 63 | - } |
|
| 59 | + // Add our own link to the list of links. |
|
| 60 | + return array_merge( $links, array( |
|
| 61 | + sprintf( "<a href='$url'>%s</a>", __( 'Settings', 'wordlift' ) ), |
|
| 62 | + ) ); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | 65 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @param \Wordlift_Admin_Settings_Page $settings_page The {@link Wordlift_Admin_Settings_Page} instance. |
| 36 | 36 | */ |
| 37 | - function __construct( $settings_page ) { |
|
| 37 | + function __construct($settings_page) { |
|
| 38 | 38 | |
| 39 | 39 | $this->settings_page = $settings_page; |
| 40 | 40 | |
@@ -49,17 +49,17 @@ discard block |
||
| 49 | 49 | * |
| 50 | 50 | * @return array An array of links including those added by the plugin. |
| 51 | 51 | */ |
| 52 | - function action_links( $links ) { |
|
| 52 | + function action_links($links) { |
|
| 53 | 53 | |
| 54 | 54 | // Get the menu slug from the page, then prepare the path, hence the url. |
| 55 | 55 | $menu_slug = $this->settings_page->get_menu_slug(); |
| 56 | 56 | $path = "admin.php?page=$menu_slug"; |
| 57 | - $url = get_admin_url( null, $path ); |
|
| 57 | + $url = get_admin_url(null, $path); |
|
| 58 | 58 | |
| 59 | 59 | // Add our own link to the list of links. |
| 60 | - return array_merge( $links, array( |
|
| 61 | - sprintf( "<a href='$url'>%s</a>", __( 'Settings', 'wordlift' ) ), |
|
| 62 | - ) ); |
|
| 60 | + return array_merge($links, array( |
|
| 61 | + sprintf("<a href='$url'>%s</a>", __('Settings', 'wordlift')), |
|
| 62 | + )); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | } |
@@ -90,7 +90,7 @@ |
||
| 90 | 90 | * |
| 91 | 91 | * @since 3.18.0 |
| 92 | 92 | * |
| 93 | - * @return string|bool The file extension on success and |
|
| 93 | + * @return string|false The file extension on success and |
|
| 94 | 94 | * false on fail or if the content type is not supported. |
| 95 | 95 | */ |
| 96 | 96 | private static function get_extension_from_content_type( $content_type ) { |
@@ -8,134 +8,134 @@ |
||
| 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 | - * @since 3.18.0 |
|
| 17 | - * |
|
| 18 | - * @return array|false An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
| 19 | - * url, *content_type*: the image content type) or false on error. |
|
| 20 | - */ |
|
| 21 | - public static function save_from_url( $url ) { |
|
| 22 | - |
|
| 23 | - // Load `WP_Filesystem`. |
|
| 24 | - WP_Filesystem(); |
|
| 25 | - global $wp_filesystem; |
|
| 26 | - |
|
| 27 | - // Parse the url. |
|
| 28 | - $parts = wp_parse_url( $url ); |
|
| 29 | - |
|
| 30 | - // Get the bare filename (filename w/o the extension). |
|
| 31 | - $basename = pathinfo( $parts['path'], PATHINFO_FILENAME ); |
|
| 32 | - |
|
| 33 | - // Get the base dir. |
|
| 34 | - $wp_upload_dir = wp_upload_dir(); |
|
| 35 | - |
|
| 36 | - // Set the upload directory and URL. |
|
| 37 | - $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 38 | - $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 39 | - |
|
| 40 | - // Get the full path to the local filename. |
|
| 41 | - $image_full_path = $upload_dir . '/' . $basename; |
|
| 42 | - $image_full_url = $upload_url . '/' . $basename; |
|
| 43 | - |
|
| 44 | - // Create custom directory and bail on failure. |
|
| 45 | - if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 46 | - wl_write_log( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 47 | - |
|
| 48 | - return false; |
|
| 49 | - }; |
|
| 50 | - |
|
| 51 | - $response = self::get_response( $url ); |
|
| 52 | - |
|
| 53 | - // Bail if the response is not set. |
|
| 54 | - if ( false === $response ) { |
|
| 55 | - wl_write_log( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 56 | - |
|
| 57 | - return false; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - // Get the content type of response. |
|
| 61 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 62 | - |
|
| 63 | - // Get the file extension. |
|
| 64 | - $extension = self::get_extension_from_content_type( $content_type ); |
|
| 65 | - |
|
| 66 | - // Bail if the content type is not supported. |
|
| 67 | - if ( empty( $extension ) ) { |
|
| 68 | - return false; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // Complete the local filename. |
|
| 72 | - $image_full_path .= $extension; |
|
| 73 | - $image_full_url .= $extension; |
|
| 74 | - |
|
| 75 | - // Store the data locally. |
|
| 76 | - $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 77 | - |
|
| 78 | - // Return the path. |
|
| 79 | - return array( |
|
| 80 | - 'path' => $image_full_path, |
|
| 81 | - 'url' => $image_full_url, |
|
| 82 | - 'content_type' => $content_type, |
|
| 83 | - ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Returns the file extension using the content type. |
|
| 88 | - * |
|
| 89 | - * @param string $content_type File content type. |
|
| 90 | - * |
|
| 91 | - * @since 3.18.0 |
|
| 92 | - * |
|
| 93 | - * @return string|bool The file extension on success and |
|
| 94 | - * false on fail or if the content type is not supported. |
|
| 95 | - */ |
|
| 96 | - private static function get_extension_from_content_type( $content_type ) { |
|
| 97 | - |
|
| 98 | - // Return the extension if match. |
|
| 99 | - switch ( $content_type ) { |
|
| 100 | - case 'image/jpeg': |
|
| 101 | - case 'image/jpg': |
|
| 102 | - return '.jpg'; |
|
| 103 | - case 'image/gif': |
|
| 104 | - return '.gif'; |
|
| 105 | - case 'image/png': |
|
| 106 | - return '.png'; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // Otherwise return false. |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Retrieve the response from url and sets the response. |
|
| 115 | - * |
|
| 116 | - * @param string $url The url to retrieve. |
|
| 117 | - * |
|
| 118 | - * @since 3.18.0 |
|
| 119 | - * |
|
| 120 | - * @return false|array True on success and false on failure. |
|
| 121 | - */ |
|
| 122 | - private static function get_response( $url ) { |
|
| 123 | - // Request the remote file. |
|
| 124 | - $response = wp_remote_get( $url ); |
|
| 125 | - |
|
| 126 | - // Bail out if the response is not ok. |
|
| 127 | - if ( |
|
| 128 | - is_wp_error( $response ) |
|
| 129 | - || 200 !== (int) $response['response']['code'] |
|
| 130 | - || ! isset( $response['body'] ) |
|
| 131 | - ) { |
|
| 132 | - wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 133 | - |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - // Set the response. |
|
| 138 | - return $response; |
|
| 139 | - } |
|
| 11 | + /** |
|
| 12 | + * Save the image with the specified URL locally. |
|
| 13 | + * |
|
| 14 | + * @param string $url The image remote URL. |
|
| 15 | + * |
|
| 16 | + * @since 3.18.0 |
|
| 17 | + * |
|
| 18 | + * @return array|false An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
| 19 | + * url, *content_type*: the image content type) or false on error. |
|
| 20 | + */ |
|
| 21 | + public static function save_from_url( $url ) { |
|
| 22 | + |
|
| 23 | + // Load `WP_Filesystem`. |
|
| 24 | + WP_Filesystem(); |
|
| 25 | + global $wp_filesystem; |
|
| 26 | + |
|
| 27 | + // Parse the url. |
|
| 28 | + $parts = wp_parse_url( $url ); |
|
| 29 | + |
|
| 30 | + // Get the bare filename (filename w/o the extension). |
|
| 31 | + $basename = pathinfo( $parts['path'], PATHINFO_FILENAME ); |
|
| 32 | + |
|
| 33 | + // Get the base dir. |
|
| 34 | + $wp_upload_dir = wp_upload_dir(); |
|
| 35 | + |
|
| 36 | + // Set the upload directory and URL. |
|
| 37 | + $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 38 | + $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 39 | + |
|
| 40 | + // Get the full path to the local filename. |
|
| 41 | + $image_full_path = $upload_dir . '/' . $basename; |
|
| 42 | + $image_full_url = $upload_url . '/' . $basename; |
|
| 43 | + |
|
| 44 | + // Create custom directory and bail on failure. |
|
| 45 | + if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 46 | + wl_write_log( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 47 | + |
|
| 48 | + return false; |
|
| 49 | + }; |
|
| 50 | + |
|
| 51 | + $response = self::get_response( $url ); |
|
| 52 | + |
|
| 53 | + // Bail if the response is not set. |
|
| 54 | + if ( false === $response ) { |
|
| 55 | + wl_write_log( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 56 | + |
|
| 57 | + return false; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + // Get the content type of response. |
|
| 61 | + $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 62 | + |
|
| 63 | + // Get the file extension. |
|
| 64 | + $extension = self::get_extension_from_content_type( $content_type ); |
|
| 65 | + |
|
| 66 | + // Bail if the content type is not supported. |
|
| 67 | + if ( empty( $extension ) ) { |
|
| 68 | + return false; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // Complete the local filename. |
|
| 72 | + $image_full_path .= $extension; |
|
| 73 | + $image_full_url .= $extension; |
|
| 74 | + |
|
| 75 | + // Store the data locally. |
|
| 76 | + $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 77 | + |
|
| 78 | + // Return the path. |
|
| 79 | + return array( |
|
| 80 | + 'path' => $image_full_path, |
|
| 81 | + 'url' => $image_full_url, |
|
| 82 | + 'content_type' => $content_type, |
|
| 83 | + ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Returns the file extension using the content type. |
|
| 88 | + * |
|
| 89 | + * @param string $content_type File content type. |
|
| 90 | + * |
|
| 91 | + * @since 3.18.0 |
|
| 92 | + * |
|
| 93 | + * @return string|bool The file extension on success and |
|
| 94 | + * false on fail or if the content type is not supported. |
|
| 95 | + */ |
|
| 96 | + private static function get_extension_from_content_type( $content_type ) { |
|
| 97 | + |
|
| 98 | + // Return the extension if match. |
|
| 99 | + switch ( $content_type ) { |
|
| 100 | + case 'image/jpeg': |
|
| 101 | + case 'image/jpg': |
|
| 102 | + return '.jpg'; |
|
| 103 | + case 'image/gif': |
|
| 104 | + return '.gif'; |
|
| 105 | + case 'image/png': |
|
| 106 | + return '.png'; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // Otherwise return false. |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Retrieve the response from url and sets the response. |
|
| 115 | + * |
|
| 116 | + * @param string $url The url to retrieve. |
|
| 117 | + * |
|
| 118 | + * @since 3.18.0 |
|
| 119 | + * |
|
| 120 | + * @return false|array True on success and false on failure. |
|
| 121 | + */ |
|
| 122 | + private static function get_response( $url ) { |
|
| 123 | + // Request the remote file. |
|
| 124 | + $response = wp_remote_get( $url ); |
|
| 125 | + |
|
| 126 | + // Bail out if the response is not ok. |
|
| 127 | + if ( |
|
| 128 | + is_wp_error( $response ) |
|
| 129 | + || 200 !== (int) $response['response']['code'] |
|
| 130 | + || ! isset( $response['body'] ) |
|
| 131 | + ) { |
|
| 132 | + wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 133 | + |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + // Set the response. |
|
| 138 | + return $response; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | 141 | } |
@@ -18,53 +18,53 @@ discard block |
||
| 18 | 18 | * @return array|false An array with information about the saved image (*path*: the local path to the image, *url*: the local |
| 19 | 19 | * url, *content_type*: the image content type) or false on error. |
| 20 | 20 | */ |
| 21 | - public static function save_from_url( $url ) { |
|
| 21 | + public static function save_from_url($url) { |
|
| 22 | 22 | |
| 23 | 23 | // Load `WP_Filesystem`. |
| 24 | 24 | WP_Filesystem(); |
| 25 | 25 | global $wp_filesystem; |
| 26 | 26 | |
| 27 | 27 | // Parse the url. |
| 28 | - $parts = wp_parse_url( $url ); |
|
| 28 | + $parts = wp_parse_url($url); |
|
| 29 | 29 | |
| 30 | 30 | // Get the bare filename (filename w/o the extension). |
| 31 | - $basename = pathinfo( $parts['path'], PATHINFO_FILENAME ); |
|
| 31 | + $basename = pathinfo($parts['path'], PATHINFO_FILENAME); |
|
| 32 | 32 | |
| 33 | 33 | // Get the base dir. |
| 34 | 34 | $wp_upload_dir = wp_upload_dir(); |
| 35 | 35 | |
| 36 | 36 | // Set the upload directory and URL. |
| 37 | - $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 38 | - $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 37 | + $upload_dir = $wp_upload_dir['basedir'].'/wl'.$wp_upload_dir['subdir']; |
|
| 38 | + $upload_url = $wp_upload_dir['baseurl'].'/wl'.$wp_upload_dir['subdir']; |
|
| 39 | 39 | |
| 40 | 40 | // Get the full path to the local filename. |
| 41 | - $image_full_path = $upload_dir . '/' . $basename; |
|
| 42 | - $image_full_url = $upload_url . '/' . $basename; |
|
| 41 | + $image_full_path = $upload_dir.'/'.$basename; |
|
| 42 | + $image_full_url = $upload_url.'/'.$basename; |
|
| 43 | 43 | |
| 44 | 44 | // Create custom directory and bail on failure. |
| 45 | - if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 46 | - wl_write_log( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 45 | + if ( ! wp_mkdir_p($upload_dir)) { |
|
| 46 | + wl_write_log("save_image_from_url : failed creating upload dir $upload_dir \n"); |
|
| 47 | 47 | |
| 48 | 48 | return false; |
| 49 | 49 | }; |
| 50 | 50 | |
| 51 | - $response = self::get_response( $url ); |
|
| 51 | + $response = self::get_response($url); |
|
| 52 | 52 | |
| 53 | 53 | // Bail if the response is not set. |
| 54 | - if ( false === $response ) { |
|
| 55 | - wl_write_log( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 54 | + if (false === $response) { |
|
| 55 | + wl_write_log("save_image_from_url : failed to fetch the response from: $url \n"); |
|
| 56 | 56 | |
| 57 | 57 | return false; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | // Get the content type of response. |
| 61 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 61 | + $content_type = wp_remote_retrieve_header($response, 'content-type'); |
|
| 62 | 62 | |
| 63 | 63 | // Get the file extension. |
| 64 | - $extension = self::get_extension_from_content_type( $content_type ); |
|
| 64 | + $extension = self::get_extension_from_content_type($content_type); |
|
| 65 | 65 | |
| 66 | 66 | // Bail if the content type is not supported. |
| 67 | - if ( empty( $extension ) ) { |
|
| 67 | + if (empty($extension)) { |
|
| 68 | 68 | return false; |
| 69 | 69 | } |
| 70 | 70 | |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | $image_full_url .= $extension; |
| 74 | 74 | |
| 75 | 75 | // Store the data locally. |
| 76 | - $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 76 | + $wp_filesystem->put_contents($image_full_path, wp_remote_retrieve_body($response)); |
|
| 77 | 77 | |
| 78 | 78 | // Return the path. |
| 79 | 79 | return array( |
@@ -93,10 +93,10 @@ discard block |
||
| 93 | 93 | * @return string|bool The file extension on success and |
| 94 | 94 | * false on fail or if the content type is not supported. |
| 95 | 95 | */ |
| 96 | - private static function get_extension_from_content_type( $content_type ) { |
|
| 96 | + private static function get_extension_from_content_type($content_type) { |
|
| 97 | 97 | |
| 98 | 98 | // Return the extension if match. |
| 99 | - switch ( $content_type ) { |
|
| 99 | + switch ($content_type) { |
|
| 100 | 100 | case 'image/jpeg': |
| 101 | 101 | case 'image/jpg': |
| 102 | 102 | return '.jpg'; |
@@ -119,17 +119,17 @@ discard block |
||
| 119 | 119 | * |
| 120 | 120 | * @return false|array True on success and false on failure. |
| 121 | 121 | */ |
| 122 | - private static function get_response( $url ) { |
|
| 122 | + private static function get_response($url) { |
|
| 123 | 123 | // Request the remote file. |
| 124 | - $response = wp_remote_get( $url ); |
|
| 124 | + $response = wp_remote_get($url); |
|
| 125 | 125 | |
| 126 | 126 | // Bail out if the response is not ok. |
| 127 | 127 | if ( |
| 128 | - is_wp_error( $response ) |
|
| 128 | + is_wp_error($response) |
|
| 129 | 129 | || 200 !== (int) $response['response']['code'] |
| 130 | - || ! isset( $response['body'] ) |
|
| 130 | + || ! isset($response['body']) |
|
| 131 | 131 | ) { |
| 132 | - wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 132 | + wl_write_log("save_image_from_url : error fetching image $url \n"); |
|
| 133 | 133 | |
| 134 | 134 | return false; |
| 135 | 135 | } |
@@ -16,38 +16,38 @@ 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 | - $is_editor_supported = post_type_supports( $post_type, 'editor' ); |
|
| 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 | + $is_editor_supported = post_type_supports( $post_type, 'editor' ); |
|
| 35 | 35 | |
| 36 | - // Bail out if it's not an entity. |
|
| 37 | - if ( ! $is_editor_supported ) { |
|
| 38 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 36 | + // Bail out if it's not an entity. |
|
| 37 | + if ( ! $is_editor_supported ) { |
|
| 38 | + $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 39 | 39 | |
| 40 | - return; |
|
| 41 | - } |
|
| 40 | + return; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - // Unhook this function so it doesn't loop infinitely. |
|
| 44 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 43 | + // Unhook this function so it doesn't loop infinitely. |
|
| 44 | + remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 45 | 45 | |
| 46 | - // raise the *wl_linked_data_save_post* event. |
|
| 47 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 46 | + // raise the *wl_linked_data_save_post* event. |
|
| 47 | + do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 48 | 48 | |
| 49 | - // Re-hook this function. |
|
| 50 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 49 | + // Re-hook this function. |
|
| 50 | + add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | add_action( 'save_post', 'wl_linked_data_save_post' ); |
@@ -61,156 +61,156 @@ discard block |
||
| 61 | 61 | */ |
| 62 | 62 | function wl_linked_data_save_post_and_related_entities( $post_id ) { |
| 63 | 63 | |
| 64 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 64 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 65 | 65 | |
| 66 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 66 | + $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 67 | 67 | |
| 68 | - // Ignore auto-saves |
|
| 69 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 70 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 68 | + // Ignore auto-saves |
|
| 69 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 70 | + $log->trace( 'Doing autosave, skipping...' ); |
|
| 71 | 71 | |
| 72 | - return; |
|
| 73 | - } |
|
| 72 | + return; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - // get the current post. |
|
| 76 | - $post = get_post( $post_id ); |
|
| 75 | + // get the current post. |
|
| 76 | + $post = get_post( $post_id ); |
|
| 77 | 77 | |
| 78 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 78 | + remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 79 | 79 | |
| 80 | - // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 80 | + // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 81 | 81 | |
| 82 | - // Get the entity service instance. |
|
| 83 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 82 | + // Get the entity service instance. |
|
| 83 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 84 | 84 | |
| 85 | - // Store mapping between tmp new entities uris and real new entities uri |
|
| 86 | - $entities_uri_mapping = array(); |
|
| 85 | + // Store mapping between tmp new entities uris and real new entities uri |
|
| 86 | + $entities_uri_mapping = array(); |
|
| 87 | 87 | |
| 88 | - // Save the entities coming with POST data. |
|
| 89 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 88 | + // Save the entities coming with POST data. |
|
| 89 | + if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 90 | 90 | |
| 91 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 92 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 93 | - wl_write_log( "]" ); |
|
| 94 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 95 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 96 | - wl_write_log( "]" ); |
|
| 91 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 92 | + wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 93 | + wl_write_log( "]" ); |
|
| 94 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 95 | + wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 96 | + wl_write_log( "]" ); |
|
| 97 | 97 | |
| 98 | - $entities_via_post = $_POST['wl_entities']; |
|
| 99 | - $boxes_via_post = $_POST['wl_boxes']; |
|
| 98 | + $entities_via_post = $_POST['wl_entities']; |
|
| 99 | + $boxes_via_post = $_POST['wl_boxes']; |
|
| 100 | 100 | |
| 101 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 101 | + foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 102 | 102 | |
| 103 | - // Only if the current entity is created from scratch let's avoid to |
|
| 104 | - // create more than one entity with same label & entity type. |
|
| 105 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 106 | - $entity['main_type'] : null; |
|
| 103 | + // Only if the current entity is created from scratch let's avoid to |
|
| 104 | + // create more than one entity with same label & entity type. |
|
| 105 | + $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 106 | + $entity['main_type'] : null; |
|
| 107 | 107 | |
| 108 | - // Look if current entity uri matches an internal existing entity, meaning: |
|
| 109 | - // 1. when $entity_uri is an internal uri |
|
| 110 | - // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 111 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 108 | + // Look if current entity uri matches an internal existing entity, meaning: |
|
| 109 | + // 1. when $entity_uri is an internal uri |
|
| 110 | + // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 111 | + $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 112 | 112 | |
| 113 | - // Detect the uri depending if is an existing or a new entity |
|
| 114 | - $uri = ( null === $ie ) ? |
|
| 115 | - Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 116 | - $entity['label'], |
|
| 117 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 118 | - $entity_type |
|
| 119 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 113 | + // Detect the uri depending if is an existing or a new entity |
|
| 114 | + $uri = ( null === $ie ) ? |
|
| 115 | + Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 116 | + $entity['label'], |
|
| 117 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 118 | + $entity_type |
|
| 119 | + ) : wl_get_entity_uri( $ie->ID ); |
|
| 120 | 120 | |
| 121 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 122 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 121 | + wl_write_log( "Map $entity_uri on $uri" ); |
|
| 122 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 123 | 123 | |
| 124 | - // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 125 | - // These uris need to be rewritten here and replaced in the content |
|
| 126 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 127 | - // Override the entity obj |
|
| 128 | - $entity['uri'] = $uri; |
|
| 129 | - } |
|
| 124 | + // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 125 | + // These uris need to be rewritten here and replaced in the content |
|
| 126 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 127 | + // Override the entity obj |
|
| 128 | + $entity['uri'] = $uri; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - // Update entity data with related post |
|
| 132 | - $entity['related_post_id'] = $post_id; |
|
| 131 | + // Update entity data with related post |
|
| 132 | + $entity['related_post_id'] = $post_id; |
|
| 133 | 133 | |
| 134 | - // Save the entity if is a new entity |
|
| 135 | - if ( null === $ie ) { |
|
| 136 | - wl_save_entity( $entity ); |
|
| 137 | - } |
|
| 134 | + // Save the entity if is a new entity |
|
| 135 | + if ( null === $ie ) { |
|
| 136 | + wl_save_entity( $entity ); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - } |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - } |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - // Replace tmp uris in content post if needed |
|
| 144 | - $updated_post_content = $post->post_content; |
|
| 143 | + // Replace tmp uris in content post if needed |
|
| 144 | + $updated_post_content = $post->post_content; |
|
| 145 | 145 | |
| 146 | - // Update the post content if we found mappings of new entities. |
|
| 147 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 148 | - // Save each entity and store the post id. |
|
| 149 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 150 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 151 | - } |
|
| 146 | + // Update the post content if we found mappings of new entities. |
|
| 147 | + if ( ! empty( $entities_uri_mapping ) ) { |
|
| 148 | + // Save each entity and store the post id. |
|
| 149 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 150 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - // Update the post content. |
|
| 154 | - wp_update_post( array( |
|
| 155 | - 'ID' => $post->ID, |
|
| 156 | - 'post_content' => $updated_post_content, |
|
| 157 | - ) ); |
|
| 158 | - } |
|
| 153 | + // Update the post content. |
|
| 154 | + wp_update_post( array( |
|
| 155 | + 'ID' => $post->ID, |
|
| 156 | + 'post_content' => $updated_post_content, |
|
| 157 | + ) ); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - // Extract related/referenced entities from text. |
|
| 161 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 160 | + // Extract related/referenced entities from text. |
|
| 161 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 162 | 162 | |
| 163 | - // Reset previously saved instances. |
|
| 164 | - wl_core_delete_relation_instances( $post_id ); |
|
| 163 | + // Reset previously saved instances. |
|
| 164 | + wl_core_delete_relation_instances( $post_id ); |
|
| 165 | 165 | |
| 166 | - // Save relation instances |
|
| 167 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 166 | + // Save relation instances |
|
| 167 | + foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 168 | 168 | |
| 169 | - wl_core_add_relation_instance( |
|
| 170 | - $post_id, |
|
| 171 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 172 | - $referenced_entity_id |
|
| 173 | - ); |
|
| 169 | + wl_core_add_relation_instance( |
|
| 170 | + $post_id, |
|
| 171 | + $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 172 | + $referenced_entity_id |
|
| 173 | + ); |
|
| 174 | 174 | |
| 175 | - } |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 178 | - // Save post metadata if available |
|
| 179 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 180 | - $_POST['wl_metadata'] : array(); |
|
| 181 | - |
|
| 182 | - $fields = array( |
|
| 183 | - Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 184 | - Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 185 | - ); |
|
| 186 | - |
|
| 187 | - // Unlink topic taxonomy terms |
|
| 188 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 189 | - |
|
| 190 | - foreach ( $fields as $field ) { |
|
| 191 | - |
|
| 192 | - // Delete current values |
|
| 193 | - delete_post_meta( $post->ID, $field ); |
|
| 194 | - // Retrieve the entity uri |
|
| 195 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 196 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 197 | - |
|
| 198 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 199 | - |
|
| 200 | - if ( $entity ) { |
|
| 201 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 202 | - // Set also the topic taxonomy |
|
| 203 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 204 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - // Push the post to Redlink. |
|
| 211 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 212 | - |
|
| 213 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 177 | + if ( isset( $_POST['wl_entities'] ) ) { |
|
| 178 | + // Save post metadata if available |
|
| 179 | + $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 180 | + $_POST['wl_metadata'] : array(); |
|
| 181 | + |
|
| 182 | + $fields = array( |
|
| 183 | + Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 184 | + Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 185 | + ); |
|
| 186 | + |
|
| 187 | + // Unlink topic taxonomy terms |
|
| 188 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 189 | + |
|
| 190 | + foreach ( $fields as $field ) { |
|
| 191 | + |
|
| 192 | + // Delete current values |
|
| 193 | + delete_post_meta( $post->ID, $field ); |
|
| 194 | + // Retrieve the entity uri |
|
| 195 | + $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 196 | + stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 197 | + |
|
| 198 | + $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 199 | + |
|
| 200 | + if ( $entity ) { |
|
| 201 | + add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 202 | + // Set also the topic taxonomy |
|
| 203 | + if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 204 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + // Push the post to Redlink. |
|
| 211 | + Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 212 | + |
|
| 213 | + add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
@@ -233,196 +233,196 @@ discard block |
||
| 233 | 233 | */ |
| 234 | 234 | function wl_save_entity( $entity_data ) { |
| 235 | 235 | |
| 236 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 237 | - |
|
| 238 | - $uri = $entity_data['uri']; |
|
| 239 | - $label = $entity_data['label']; |
|
| 240 | - $type_uri = $entity_data['main_type']; |
|
| 241 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 242 | - $description = $entity_data['description']; |
|
| 243 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 244 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 245 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 246 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 247 | - |
|
| 248 | - // Get the synonyms. |
|
| 249 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 250 | - |
|
| 251 | - // Check whether an entity already exists with the provided URI. |
|
| 252 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 253 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 254 | - |
|
| 255 | - return $post; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - // Prepare properties of the new entity. |
|
| 259 | - $params = array( |
|
| 260 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 261 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 262 | - 'post_title' => $label, |
|
| 263 | - 'post_content' => $description, |
|
| 264 | - 'post_excerpt' => '', |
|
| 265 | - // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 266 | - // entity with a post_name already set, since this call is made only for |
|
| 267 | - // new entities. |
|
| 268 | - // |
|
| 269 | - // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 270 | - 'post_name' => sanitize_title( $label ), |
|
| 271 | - ); |
|
| 272 | - |
|
| 273 | - // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 274 | - // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 275 | - // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 276 | - // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 277 | - // is created when saving a post. |
|
| 278 | - global $wpseo_metabox, $seo_ultimate; |
|
| 279 | - if ( isset( $wpseo_metabox ) ) { |
|
| 280 | - remove_action( 'wp_insert_post', array( |
|
| 281 | - $wpseo_metabox, |
|
| 282 | - 'save_postdata', |
|
| 283 | - ) ); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - if ( isset( $seo_ultimate ) ) { |
|
| 287 | - remove_action( 'save_post', array( |
|
| 288 | - $seo_ultimate, |
|
| 289 | - 'save_postmeta_box', |
|
| 290 | - ) ); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 294 | - // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 295 | - // to the save_post and restore them after we saved the entity. |
|
| 296 | - // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 297 | - // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 298 | - // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 299 | - global $wp_filter; |
|
| 300 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 301 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 302 | - |
|
| 303 | - |
|
| 304 | - $log->trace( 'Going to insert new post...' ); |
|
| 305 | - |
|
| 306 | - // create or update the post. |
|
| 307 | - $post_id = wp_insert_post( $params, true ); |
|
| 308 | - |
|
| 309 | - // Setting the alternative labels for this entity. |
|
| 310 | - Wordlift_Entity_Service::get_instance() |
|
| 311 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 312 | - |
|
| 313 | - // Restore all the existing filters. |
|
| 314 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 315 | - |
|
| 316 | - // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 317 | - if ( isset( $wpseo_metabox ) ) { |
|
| 318 | - add_action( 'wp_insert_post', array( |
|
| 319 | - $wpseo_metabox, |
|
| 320 | - 'save_postdata', |
|
| 321 | - ) ); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 325 | - if ( isset( $seo_ultimate ) ) { |
|
| 326 | - add_action( 'save_post', array( |
|
| 327 | - $seo_ultimate, |
|
| 328 | - 'save_postmeta_box', |
|
| 329 | - ), 10, 2 ); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - // TODO: handle errors. |
|
| 333 | - if ( is_wp_error( $post_id ) ) { |
|
| 334 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 335 | - |
|
| 336 | - // inform an error occurred. |
|
| 337 | - return null; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 341 | - |
|
| 342 | - // Save the entity types. |
|
| 343 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 344 | - |
|
| 345 | - // Get a dataset URI for the entity. |
|
| 346 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 347 | - |
|
| 348 | - // Save the entity URI. |
|
| 349 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 350 | - |
|
| 351 | - // Add the uri to the sameAs data if it's not a local URI. |
|
| 352 | - if ( $wl_uri !== $uri ) { |
|
| 353 | - array_push( $same_as, $uri ); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 357 | - |
|
| 358 | - // Save the sameAs data for the entity. |
|
| 359 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 360 | - |
|
| 361 | - // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 362 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 363 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - // Call hooks. |
|
| 367 | - do_action( 'wl_save_entity', $post_id ); |
|
| 368 | - |
|
| 369 | - foreach ( $images as $image_remote_url ) { |
|
| 370 | - |
|
| 371 | - // Check if image is already present in local DB |
|
| 372 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 373 | - // Do nothing. |
|
| 374 | - continue; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - // Check if there is an existing attachment for this post ID and source URL. |
|
| 378 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 379 | - |
|
| 380 | - // Skip if an existing image is found. |
|
| 381 | - if ( null !== $existing_image ) { |
|
| 382 | - continue; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - // Save the image and get the local path. |
|
| 386 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 387 | - |
|
| 388 | - if ( false === $image ) { |
|
| 389 | - continue; |
|
| 390 | - } |
|
| 236 | + $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 237 | + |
|
| 238 | + $uri = $entity_data['uri']; |
|
| 239 | + $label = $entity_data['label']; |
|
| 240 | + $type_uri = $entity_data['main_type']; |
|
| 241 | + $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 242 | + $description = $entity_data['description']; |
|
| 243 | + $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 244 | + $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 245 | + $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 246 | + $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 247 | + |
|
| 248 | + // Get the synonyms. |
|
| 249 | + $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 250 | + |
|
| 251 | + // Check whether an entity already exists with the provided URI. |
|
| 252 | + if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 253 | + $log->debug( "Post already exists for URI $uri." ); |
|
| 254 | + |
|
| 255 | + return $post; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + // Prepare properties of the new entity. |
|
| 259 | + $params = array( |
|
| 260 | + 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 261 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 262 | + 'post_title' => $label, |
|
| 263 | + 'post_content' => $description, |
|
| 264 | + 'post_excerpt' => '', |
|
| 265 | + // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 266 | + // entity with a post_name already set, since this call is made only for |
|
| 267 | + // new entities. |
|
| 268 | + // |
|
| 269 | + // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 270 | + 'post_name' => sanitize_title( $label ), |
|
| 271 | + ); |
|
| 272 | + |
|
| 273 | + // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 274 | + // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 275 | + // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 276 | + // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 277 | + // is created when saving a post. |
|
| 278 | + global $wpseo_metabox, $seo_ultimate; |
|
| 279 | + if ( isset( $wpseo_metabox ) ) { |
|
| 280 | + remove_action( 'wp_insert_post', array( |
|
| 281 | + $wpseo_metabox, |
|
| 282 | + 'save_postdata', |
|
| 283 | + ) ); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + if ( isset( $seo_ultimate ) ) { |
|
| 287 | + remove_action( 'save_post', array( |
|
| 288 | + $seo_ultimate, |
|
| 289 | + 'save_postmeta_box', |
|
| 290 | + ) ); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 294 | + // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 295 | + // to the save_post and restore them after we saved the entity. |
|
| 296 | + // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 297 | + // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 298 | + // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 299 | + global $wp_filter; |
|
| 300 | + $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 301 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 302 | + |
|
| 303 | + |
|
| 304 | + $log->trace( 'Going to insert new post...' ); |
|
| 305 | + |
|
| 306 | + // create or update the post. |
|
| 307 | + $post_id = wp_insert_post( $params, true ); |
|
| 308 | + |
|
| 309 | + // Setting the alternative labels for this entity. |
|
| 310 | + Wordlift_Entity_Service::get_instance() |
|
| 311 | + ->set_alternative_labels( $post_id, $synonyms ); |
|
| 312 | + |
|
| 313 | + // Restore all the existing filters. |
|
| 314 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 315 | + |
|
| 316 | + // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 317 | + if ( isset( $wpseo_metabox ) ) { |
|
| 318 | + add_action( 'wp_insert_post', array( |
|
| 319 | + $wpseo_metabox, |
|
| 320 | + 'save_postdata', |
|
| 321 | + ) ); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 325 | + if ( isset( $seo_ultimate ) ) { |
|
| 326 | + add_action( 'save_post', array( |
|
| 327 | + $seo_ultimate, |
|
| 328 | + 'save_postmeta_box', |
|
| 329 | + ), 10, 2 ); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + // TODO: handle errors. |
|
| 333 | + if ( is_wp_error( $post_id ) ) { |
|
| 334 | + $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 335 | + |
|
| 336 | + // inform an error occurred. |
|
| 337 | + return null; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 341 | + |
|
| 342 | + // Save the entity types. |
|
| 343 | + wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 344 | + |
|
| 345 | + // Get a dataset URI for the entity. |
|
| 346 | + $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 347 | + |
|
| 348 | + // Save the entity URI. |
|
| 349 | + wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 350 | + |
|
| 351 | + // Add the uri to the sameAs data if it's not a local URI. |
|
| 352 | + if ( $wl_uri !== $uri ) { |
|
| 353 | + array_push( $same_as, $uri ); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + $new_uri = wl_get_entity_uri( $post_id ); |
|
| 357 | + |
|
| 358 | + // Save the sameAs data for the entity. |
|
| 359 | + wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 360 | + |
|
| 361 | + // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 362 | + foreach ( $other_properties as $property_name => $property_value ) { |
|
| 363 | + wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + // Call hooks. |
|
| 367 | + do_action( 'wl_save_entity', $post_id ); |
|
| 368 | + |
|
| 369 | + foreach ( $images as $image_remote_url ) { |
|
| 370 | + |
|
| 371 | + // Check if image is already present in local DB |
|
| 372 | + if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 373 | + // Do nothing. |
|
| 374 | + continue; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + // Check if there is an existing attachment for this post ID and source URL. |
|
| 378 | + $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 379 | + |
|
| 380 | + // Skip if an existing image is found. |
|
| 381 | + if ( null !== $existing_image ) { |
|
| 382 | + continue; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + // Save the image and get the local path. |
|
| 386 | + $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 387 | + |
|
| 388 | + if ( false === $image ) { |
|
| 389 | + continue; |
|
| 390 | + } |
|
| 391 | 391 | |
| 392 | - // Get the local URL. |
|
| 393 | - $filename = $image['path']; |
|
| 394 | - $url = $image['url']; |
|
| 395 | - $content_type = $image['content_type']; |
|
| 392 | + // Get the local URL. |
|
| 393 | + $filename = $image['path']; |
|
| 394 | + $url = $image['url']; |
|
| 395 | + $content_type = $image['content_type']; |
|
| 396 | 396 | |
| 397 | - $attachment = array( |
|
| 398 | - 'guid' => $url, |
|
| 399 | - // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 400 | - 'post_title' => $label, |
|
| 401 | - // Set the title to the post title. |
|
| 402 | - 'post_content' => '', |
|
| 403 | - 'post_status' => 'inherit', |
|
| 404 | - 'post_mime_type' => $content_type, |
|
| 405 | - ); |
|
| 406 | - |
|
| 407 | - // Create the attachment in WordPress and generate the related metadata. |
|
| 408 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 409 | - |
|
| 410 | - // Set the source URL for the image. |
|
| 411 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 397 | + $attachment = array( |
|
| 398 | + 'guid' => $url, |
|
| 399 | + // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 400 | + 'post_title' => $label, |
|
| 401 | + // Set the title to the post title. |
|
| 402 | + 'post_content' => '', |
|
| 403 | + 'post_status' => 'inherit', |
|
| 404 | + 'post_mime_type' => $content_type, |
|
| 405 | + ); |
|
| 406 | + |
|
| 407 | + // Create the attachment in WordPress and generate the related metadata. |
|
| 408 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 409 | + |
|
| 410 | + // Set the source URL for the image. |
|
| 411 | + wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 412 | 412 | |
| 413 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 414 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 413 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 414 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 415 | 415 | |
| 416 | - // Set it as the featured image. |
|
| 417 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 418 | - } |
|
| 416 | + // Set it as the featured image. |
|
| 417 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 418 | + } |
|
| 419 | 419 | |
| 420 | - // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 421 | - // save the entity in the triple store. |
|
| 422 | - Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 420 | + // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 421 | + // save the entity in the triple store. |
|
| 422 | + Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 423 | 423 | |
| 424 | - // finally return the entity post. |
|
| 425 | - return get_post( $post_id ); |
|
| 424 | + // finally return the entity post. |
|
| 425 | + return get_post( $post_id ); |
|
| 426 | 426 | } |
| 427 | 427 | |
| 428 | 428 | /** |
@@ -436,40 +436,40 @@ discard block |
||
| 436 | 436 | */ |
| 437 | 437 | function wl_linked_data_content_get_embedded_entities( $content ) { |
| 438 | 438 | |
| 439 | - // Remove quote escapes. |
|
| 440 | - $content = str_replace( '\\"', '"', $content ); |
|
| 439 | + // Remove quote escapes. |
|
| 440 | + $content = str_replace( '\\"', '"', $content ); |
|
| 441 | 441 | |
| 442 | - // Match all itemid attributes. |
|
| 443 | - $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 442 | + // Match all itemid attributes. |
|
| 443 | + $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 444 | 444 | |
| 445 | - // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 445 | + // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 446 | 446 | |
| 447 | - // Remove the pattern while it is found (match nested annotations). |
|
| 448 | - $matches = array(); |
|
| 447 | + // Remove the pattern while it is found (match nested annotations). |
|
| 448 | + $matches = array(); |
|
| 449 | 449 | |
| 450 | - // In case of errors, return an empty array. |
|
| 451 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 452 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 450 | + // In case of errors, return an empty array. |
|
| 451 | + if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 452 | + wl_write_log( "Found no entities embedded in content" ); |
|
| 453 | 453 | |
| 454 | - return array(); |
|
| 455 | - } |
|
| 454 | + return array(); |
|
| 455 | + } |
|
| 456 | 456 | |
| 457 | 457 | // wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]"); |
| 458 | 458 | |
| 459 | - // Collect the entities. |
|
| 460 | - $entities = array(); |
|
| 461 | - foreach ( $matches[1] as $uri ) { |
|
| 462 | - $uri_d = html_entity_decode( $uri ); |
|
| 459 | + // Collect the entities. |
|
| 460 | + $entities = array(); |
|
| 461 | + foreach ( $matches[1] as $uri ) { |
|
| 462 | + $uri_d = html_entity_decode( $uri ); |
|
| 463 | 463 | |
| 464 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 464 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 465 | 465 | |
| 466 | - if ( null !== $entity ) { |
|
| 467 | - array_push( $entities, $entity->ID ); |
|
| 468 | - } |
|
| 469 | - } |
|
| 466 | + if ( null !== $entity ) { |
|
| 467 | + array_push( $entities, $entity->ID ); |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | 470 | |
| 471 | - // $count = sizeof( $entities ); |
|
| 472 | - // wl_write_log( "Found $count entities embedded in content" ); |
|
| 471 | + // $count = sizeof( $entities ); |
|
| 472 | + // wl_write_log( "Found $count entities embedded in content" ); |
|
| 473 | 473 | |
| 474 | - return $entities; |
|
| 474 | + return $entities; |
|
| 475 | 475 | } |
@@ -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,27 +30,27 @@ 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 ); |
|
| 34 | - $is_editor_supported = post_type_supports( $post_type, 'editor' ); |
|
| 33 | + $post_type = get_post_type($post_id); |
|
| 34 | + $is_editor_supported = post_type_supports($post_type, 'editor'); |
|
| 35 | 35 | |
| 36 | 36 | // Bail out if it's not an entity. |
| 37 | - if ( ! $is_editor_supported ) { |
|
| 38 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 37 | + if ( ! $is_editor_supported) { |
|
| 38 | + $log->debug("Skipping $post_id, because $post_type doesn't support the editor (content)."); |
|
| 39 | 39 | |
| 40 | 40 | return; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | // Unhook this function so it doesn't loop infinitely. |
| 44 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 44 | + remove_action('save_post', 'wl_linked_data_save_post'); |
|
| 45 | 45 | |
| 46 | 46 | // raise the *wl_linked_data_save_post* event. |
| 47 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 47 | + do_action('wl_linked_data_save_post', $post_id); |
|
| 48 | 48 | |
| 49 | 49 | // Re-hook this function. |
| 50 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 50 | + add_action('save_post', 'wl_linked_data_save_post'); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | -add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 53 | +add_action('save_post', 'wl_linked_data_save_post'); |
|
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * Save the post to the triple store. Also saves the entities locally and on the triple store. |
@@ -59,23 +59,23 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @param int $post_id The post id being saved. |
| 61 | 61 | */ |
| 62 | -function wl_linked_data_save_post_and_related_entities( $post_id ) { |
|
| 62 | +function wl_linked_data_save_post_and_related_entities($post_id) { |
|
| 63 | 63 | |
| 64 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 64 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities'); |
|
| 65 | 65 | |
| 66 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 66 | + $log->trace("Saving $post_id to Linked Data along with related entities..."); |
|
| 67 | 67 | |
| 68 | 68 | // Ignore auto-saves |
| 69 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 70 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 69 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 70 | + $log->trace('Doing autosave, skipping...'); |
|
| 71 | 71 | |
| 72 | 72 | return; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // get the current post. |
| 76 | - $post = get_post( $post_id ); |
|
| 76 | + $post = get_post($post_id); |
|
| 77 | 77 | |
| 78 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 78 | + remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 79 | 79 | |
| 80 | 80 | // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
| 81 | 81 | |
@@ -86,44 +86,44 @@ discard block |
||
| 86 | 86 | $entities_uri_mapping = array(); |
| 87 | 87 | |
| 88 | 88 | // Save the entities coming with POST data. |
| 89 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 89 | + if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) { |
|
| 90 | 90 | |
| 91 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 92 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 93 | - wl_write_log( "]" ); |
|
| 94 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 95 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 96 | - wl_write_log( "]" ); |
|
| 91 | + wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: "); |
|
| 92 | + wl_write_log(json_encode($_POST['wl_entities'])); |
|
| 93 | + wl_write_log("]"); |
|
| 94 | + wl_write_log("[ post id :: $post_id ][ POST(wl_boxes) :: "); |
|
| 95 | + wl_write_log(json_encode($_POST['wl_boxes'], true)); |
|
| 96 | + wl_write_log("]"); |
|
| 97 | 97 | |
| 98 | 98 | $entities_via_post = $_POST['wl_entities']; |
| 99 | 99 | $boxes_via_post = $_POST['wl_boxes']; |
| 100 | 100 | |
| 101 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 101 | + foreach ($entities_via_post as $entity_uri => $entity) { |
|
| 102 | 102 | |
| 103 | 103 | // Only if the current entity is created from scratch let's avoid to |
| 104 | 104 | // create more than one entity with same label & entity type. |
| 105 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 105 | + $entity_type = (preg_match('/^local-entity-.+/', $entity_uri) > 0) ? |
|
| 106 | 106 | $entity['main_type'] : null; |
| 107 | 107 | |
| 108 | 108 | // Look if current entity uri matches an internal existing entity, meaning: |
| 109 | 109 | // 1. when $entity_uri is an internal uri |
| 110 | 110 | // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
| 111 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 111 | + $ie = $entity_service->get_entity_post_by_uri($entity_uri); |
|
| 112 | 112 | |
| 113 | 113 | // Detect the uri depending if is an existing or a new entity |
| 114 | - $uri = ( null === $ie ) ? |
|
| 114 | + $uri = (null === $ie) ? |
|
| 115 | 115 | Wordlift_Uri_Service::get_instance()->build_uri( |
| 116 | 116 | $entity['label'], |
| 117 | 117 | Wordlift_Entity_Service::TYPE_NAME, |
| 118 | 118 | $entity_type |
| 119 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 119 | + ) : wl_get_entity_uri($ie->ID); |
|
| 120 | 120 | |
| 121 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 122 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 121 | + wl_write_log("Map $entity_uri on $uri"); |
|
| 122 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
| 123 | 123 | |
| 124 | 124 | // Local entities have a tmp uri with 'local-entity-' prefix |
| 125 | 125 | // These uris need to be rewritten here and replaced in the content |
| 126 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 126 | + if (preg_match('/^local-entity-.+/', $entity_uri) > 0) { |
|
| 127 | 127 | // Override the entity obj |
| 128 | 128 | $entity['uri'] = $uri; |
| 129 | 129 | } |
@@ -132,8 +132,8 @@ discard block |
||
| 132 | 132 | $entity['related_post_id'] = $post_id; |
| 133 | 133 | |
| 134 | 134 | // Save the entity if is a new entity |
| 135 | - if ( null === $ie ) { |
|
| 136 | - wl_save_entity( $entity ); |
|
| 135 | + if (null === $ie) { |
|
| 136 | + wl_save_entity($entity); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | } |
@@ -144,39 +144,39 @@ discard block |
||
| 144 | 144 | $updated_post_content = $post->post_content; |
| 145 | 145 | |
| 146 | 146 | // Update the post content if we found mappings of new entities. |
| 147 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 147 | + if ( ! empty($entities_uri_mapping)) { |
|
| 148 | 148 | // Save each entity and store the post id. |
| 149 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 150 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 149 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
| 150 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | // Update the post content. |
| 154 | - wp_update_post( array( |
|
| 154 | + wp_update_post(array( |
|
| 155 | 155 | 'ID' => $post->ID, |
| 156 | 156 | 'post_content' => $updated_post_content, |
| 157 | - ) ); |
|
| 157 | + )); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Extract related/referenced entities from text. |
| 161 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 161 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content); |
|
| 162 | 162 | |
| 163 | 163 | // Reset previously saved instances. |
| 164 | - wl_core_delete_relation_instances( $post_id ); |
|
| 164 | + wl_core_delete_relation_instances($post_id); |
|
| 165 | 165 | |
| 166 | 166 | // Save relation instances |
| 167 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 167 | + foreach (array_unique($disambiguated_entities) as $referenced_entity_id) { |
|
| 168 | 168 | |
| 169 | 169 | wl_core_add_relation_instance( |
| 170 | 170 | $post_id, |
| 171 | - $entity_service->get_classification_scope_for( $referenced_entity_id ), |
|
| 171 | + $entity_service->get_classification_scope_for($referenced_entity_id), |
|
| 172 | 172 | $referenced_entity_id |
| 173 | 173 | ); |
| 174 | 174 | |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 177 | + if (isset($_POST['wl_entities'])) { |
|
| 178 | 178 | // Save post metadata if available |
| 179 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 179 | + $metadata_via_post = (isset($_POST['wl_metadata'])) ? |
|
| 180 | 180 | $_POST['wl_metadata'] : array(); |
| 181 | 181 | |
| 182 | 182 | $fields = array( |
@@ -185,35 +185,35 @@ discard block |
||
| 185 | 185 | ); |
| 186 | 186 | |
| 187 | 187 | // Unlink topic taxonomy terms |
| 188 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 188 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID); |
|
| 189 | 189 | |
| 190 | - foreach ( $fields as $field ) { |
|
| 190 | + foreach ($fields as $field) { |
|
| 191 | 191 | |
| 192 | 192 | // Delete current values |
| 193 | - delete_post_meta( $post->ID, $field ); |
|
| 193 | + delete_post_meta($post->ID, $field); |
|
| 194 | 194 | // Retrieve the entity uri |
| 195 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 196 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 195 | + $uri = (isset($metadata_via_post[$field])) ? |
|
| 196 | + stripslashes($metadata_via_post[$field]) : ''; |
|
| 197 | 197 | |
| 198 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 198 | + $entity = $entity_service->get_entity_post_by_uri($uri); |
|
| 199 | 199 | |
| 200 | - if ( $entity ) { |
|
| 201 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 200 | + if ($entity) { |
|
| 201 | + add_post_meta($post->ID, $field, $entity->ID, true); |
|
| 202 | 202 | // Set also the topic taxonomy |
| 203 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 204 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 203 | + if ($field === Wordlift_Schema_Service::FIELD_TOPIC) { |
|
| 204 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity); |
|
| 205 | 205 | } |
| 206 | 206 | } |
| 207 | 207 | } |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | // Push the post to Redlink. |
| 211 | - Wordlift_Linked_Data_Service::get_instance()->push( $post->ID ); |
|
| 211 | + Wordlift_Linked_Data_Service::get_instance()->push($post->ID); |
|
| 212 | 212 | |
| 213 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 213 | + add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | -add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 216 | +add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 217 | 217 | |
| 218 | 218 | /** |
| 219 | 219 | * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is |
@@ -231,33 +231,33 @@ discard block |
||
| 231 | 231 | * |
| 232 | 232 | * @return null|WP_Post A post instance or null in case of failure. |
| 233 | 233 | */ |
| 234 | -function wl_save_entity( $entity_data ) { |
|
| 234 | +function wl_save_entity($entity_data) { |
|
| 235 | 235 | |
| 236 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 236 | + $log = Wordlift_Log_Service::get_logger('wl_save_entity'); |
|
| 237 | 237 | |
| 238 | 238 | $uri = $entity_data['uri']; |
| 239 | 239 | $label = $entity_data['label']; |
| 240 | 240 | $type_uri = $entity_data['main_type']; |
| 241 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 241 | + $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array(); |
|
| 242 | 242 | $description = $entity_data['description']; |
| 243 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 244 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 245 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 246 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 243 | + $images = isset($entity_data['image']) ? (array) $entity_data['image'] : array(); |
|
| 244 | + $same_as = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array(); |
|
| 245 | + $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null; |
|
| 246 | + $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array(); |
|
| 247 | 247 | |
| 248 | 248 | // Get the synonyms. |
| 249 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 249 | + $synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array(); |
|
| 250 | 250 | |
| 251 | 251 | // Check whether an entity already exists with the provided URI. |
| 252 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 253 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 252 | + if (null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri)) { |
|
| 253 | + $log->debug("Post already exists for URI $uri."); |
|
| 254 | 254 | |
| 255 | 255 | return $post; |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | // Prepare properties of the new entity. |
| 259 | 259 | $params = array( |
| 260 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 260 | + 'post_status' => (is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft'), |
|
| 261 | 261 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 262 | 262 | 'post_title' => $label, |
| 263 | 263 | 'post_content' => $description, |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | // new entities. |
| 268 | 268 | // |
| 269 | 269 | // See https://github.com/insideout10/wordlift-plugin/issues/282 |
| 270 | - 'post_name' => sanitize_title( $label ), |
|
| 270 | + 'post_name' => sanitize_title($label), |
|
| 271 | 271 | ); |
| 272 | 272 | |
| 273 | 273 | // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
@@ -276,18 +276,18 @@ discard block |
||
| 276 | 276 | // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
| 277 | 277 | // is created when saving a post. |
| 278 | 278 | global $wpseo_metabox, $seo_ultimate; |
| 279 | - if ( isset( $wpseo_metabox ) ) { |
|
| 280 | - remove_action( 'wp_insert_post', array( |
|
| 279 | + if (isset($wpseo_metabox)) { |
|
| 280 | + remove_action('wp_insert_post', array( |
|
| 281 | 281 | $wpseo_metabox, |
| 282 | 282 | 'save_postdata', |
| 283 | - ) ); |
|
| 283 | + )); |
|
| 284 | 284 | } |
| 285 | 285 | |
| 286 | - if ( isset( $seo_ultimate ) ) { |
|
| 287 | - remove_action( 'save_post', array( |
|
| 286 | + if (isset($seo_ultimate)) { |
|
| 287 | + remove_action('save_post', array( |
|
| 288 | 288 | $seo_ultimate, |
| 289 | 289 | 'save_postmeta_box', |
| 290 | - ) ); |
|
| 290 | + )); |
|
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
@@ -297,95 +297,95 @@ discard block |
||
| 297 | 297 | // see https://github.com/insideout10/wordlift-plugin/issues/156 |
| 298 | 298 | // see https://github.com/insideout10/wordlift-plugin/issues/148 |
| 299 | 299 | global $wp_filter; |
| 300 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 301 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 300 | + $save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 301 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 302 | 302 | |
| 303 | 303 | |
| 304 | - $log->trace( 'Going to insert new post...' ); |
|
| 304 | + $log->trace('Going to insert new post...'); |
|
| 305 | 305 | |
| 306 | 306 | // create or update the post. |
| 307 | - $post_id = wp_insert_post( $params, true ); |
|
| 307 | + $post_id = wp_insert_post($params, true); |
|
| 308 | 308 | |
| 309 | 309 | // Setting the alternative labels for this entity. |
| 310 | 310 | Wordlift_Entity_Service::get_instance() |
| 311 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 311 | + ->set_alternative_labels($post_id, $synonyms); |
|
| 312 | 312 | |
| 313 | 313 | // Restore all the existing filters. |
| 314 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 314 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 315 | 315 | |
| 316 | 316 | // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
| 317 | - if ( isset( $wpseo_metabox ) ) { |
|
| 318 | - add_action( 'wp_insert_post', array( |
|
| 317 | + if (isset($wpseo_metabox)) { |
|
| 318 | + add_action('wp_insert_post', array( |
|
| 319 | 319 | $wpseo_metabox, |
| 320 | 320 | 'save_postdata', |
| 321 | - ) ); |
|
| 321 | + )); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
| 325 | - if ( isset( $seo_ultimate ) ) { |
|
| 326 | - add_action( 'save_post', array( |
|
| 325 | + if (isset($seo_ultimate)) { |
|
| 326 | + add_action('save_post', array( |
|
| 327 | 327 | $seo_ultimate, |
| 328 | 328 | 'save_postmeta_box', |
| 329 | - ), 10, 2 ); |
|
| 329 | + ), 10, 2); |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | // TODO: handle errors. |
| 333 | - if ( is_wp_error( $post_id ) ) { |
|
| 334 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 333 | + if (is_wp_error($post_id)) { |
|
| 334 | + $log->error('An error occurred: '.$post_id->get_error_message()); |
|
| 335 | 335 | |
| 336 | 336 | // inform an error occurred. |
| 337 | 337 | return null; |
| 338 | 338 | } |
| 339 | 339 | |
| 340 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 340 | + wl_set_entity_main_type($post_id, $type_uri); |
|
| 341 | 341 | |
| 342 | 342 | // Save the entity types. |
| 343 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 343 | + wl_set_entity_rdf_types($post_id, $entity_types); |
|
| 344 | 344 | |
| 345 | 345 | // Get a dataset URI for the entity. |
| 346 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 346 | + $wl_uri = wl_build_entity_uri($post_id); |
|
| 347 | 347 | |
| 348 | 348 | // Save the entity URI. |
| 349 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 349 | + wl_set_entity_uri($post_id, $wl_uri); |
|
| 350 | 350 | |
| 351 | 351 | // Add the uri to the sameAs data if it's not a local URI. |
| 352 | - if ( $wl_uri !== $uri ) { |
|
| 353 | - array_push( $same_as, $uri ); |
|
| 352 | + if ($wl_uri !== $uri) { |
|
| 353 | + array_push($same_as, $uri); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 356 | + $new_uri = wl_get_entity_uri($post_id); |
|
| 357 | 357 | |
| 358 | 358 | // Save the sameAs data for the entity. |
| 359 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 359 | + wl_schema_set_value($post_id, 'sameAs', $same_as); |
|
| 360 | 360 | |
| 361 | 361 | // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
| 362 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 363 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 362 | + foreach ($other_properties as $property_name => $property_value) { |
|
| 363 | + wl_schema_set_value($post_id, $property_name, $property_value); |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | // Call hooks. |
| 367 | - do_action( 'wl_save_entity', $post_id ); |
|
| 367 | + do_action('wl_save_entity', $post_id); |
|
| 368 | 368 | |
| 369 | - foreach ( $images as $image_remote_url ) { |
|
| 369 | + foreach ($images as $image_remote_url) { |
|
| 370 | 370 | |
| 371 | 371 | // Check if image is already present in local DB |
| 372 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 372 | + if (strpos($image_remote_url, site_url()) !== false) { |
|
| 373 | 373 | // Do nothing. |
| 374 | 374 | continue; |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | // Check if there is an existing attachment for this post ID and source URL. |
| 378 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 378 | + $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url); |
|
| 379 | 379 | |
| 380 | 380 | // Skip if an existing image is found. |
| 381 | - if ( null !== $existing_image ) { |
|
| 381 | + if (null !== $existing_image) { |
|
| 382 | 382 | continue; |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | // Save the image and get the local path. |
| 386 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 386 | + $image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url); |
|
| 387 | 387 | |
| 388 | - if ( false === $image ) { |
|
| 388 | + if (false === $image) { |
|
| 389 | 389 | continue; |
| 390 | 390 | } |
| 391 | 391 | |
@@ -405,24 +405,24 @@ discard block |
||
| 405 | 405 | ); |
| 406 | 406 | |
| 407 | 407 | // Create the attachment in WordPress and generate the related metadata. |
| 408 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 408 | + $attachment_id = wp_insert_attachment($attachment, $filename, $post_id); |
|
| 409 | 409 | |
| 410 | 410 | // Set the source URL for the image. |
| 411 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 411 | + wl_set_source_url($attachment_id, $image_remote_url); |
|
| 412 | 412 | |
| 413 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 414 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 413 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 414 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 415 | 415 | |
| 416 | 416 | // Set it as the featured image. |
| 417 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 417 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | // The entity is pushed to Redlink on save by the function hooked to save_post. |
| 421 | 421 | // save the entity in the triple store. |
| 422 | - Wordlift_Linked_Data_Service::get_instance()->push( $post_id ); |
|
| 422 | + Wordlift_Linked_Data_Service::get_instance()->push($post_id); |
|
| 423 | 423 | |
| 424 | 424 | // finally return the entity post. |
| 425 | - return get_post( $post_id ); |
|
| 425 | + return get_post($post_id); |
|
| 426 | 426 | } |
| 427 | 427 | |
| 428 | 428 | /** |
@@ -434,10 +434,10 @@ discard block |
||
| 434 | 434 | * |
| 435 | 435 | * @return array An array of entity posts. |
| 436 | 436 | */ |
| 437 | -function wl_linked_data_content_get_embedded_entities( $content ) { |
|
| 437 | +function wl_linked_data_content_get_embedded_entities($content) { |
|
| 438 | 438 | |
| 439 | 439 | // Remove quote escapes. |
| 440 | - $content = str_replace( '\\"', '"', $content ); |
|
| 440 | + $content = str_replace('\\"', '"', $content); |
|
| 441 | 441 | |
| 442 | 442 | // Match all itemid attributes. |
| 443 | 443 | $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
@@ -448,8 +448,8 @@ discard block |
||
| 448 | 448 | $matches = array(); |
| 449 | 449 | |
| 450 | 450 | // In case of errors, return an empty array. |
| 451 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 452 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 451 | + if (false === preg_match_all($pattern, $content, $matches)) { |
|
| 452 | + wl_write_log("Found no entities embedded in content"); |
|
| 453 | 453 | |
| 454 | 454 | return array(); |
| 455 | 455 | } |
@@ -458,13 +458,13 @@ discard block |
||
| 458 | 458 | |
| 459 | 459 | // Collect the entities. |
| 460 | 460 | $entities = array(); |
| 461 | - foreach ( $matches[1] as $uri ) { |
|
| 462 | - $uri_d = html_entity_decode( $uri ); |
|
| 461 | + foreach ($matches[1] as $uri) { |
|
| 462 | + $uri_d = html_entity_decode($uri); |
|
| 463 | 463 | |
| 464 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 464 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri_d); |
|
| 465 | 465 | |
| 466 | - if ( null !== $entity ) { |
|
| 467 | - array_push( $entities, $entity->ID ); |
|
| 466 | + if (null !== $entity) { |
|
| 467 | + array_push($entities, $entity->ID); |
|
| 468 | 468 | } |
| 469 | 469 | } |
| 470 | 470 | |
@@ -26,23 +26,23 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | function wl_transition_post_status( $new_status, $old_status, $post ) { |
| 28 | 28 | |
| 29 | - // wl_write_log( "wl_transition_post_status [ new status :: $new_status ][ old status :: $old_status ][ post ID :: $post->ID ]" ); |
|
| 30 | - |
|
| 31 | - // transition from *published* to any other status: delete the post. |
|
| 32 | - if ( 'publish' === $old_status && 'publish' !== $new_status ) { |
|
| 33 | - // Delete the post from the triple store. |
|
| 34 | - rl_delete_post( $post ); |
|
| 35 | - |
|
| 36 | - // Remove all relation instances for the current post from `wl_relation_instances`. |
|
| 37 | - wl_core_delete_relation_instances( $post->ID ); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - // when a post is published, then all the referenced entities must be published. |
|
| 41 | - if ( 'publish' !== $old_status && 'publish' === $new_status ) { |
|
| 42 | - foreach ( wl_core_get_related_entity_ids( $post->ID ) as $entity_id ) { |
|
| 43 | - wl_update_post_status( $entity_id, 'publish' ); |
|
| 44 | - } |
|
| 45 | - } |
|
| 29 | + // wl_write_log( "wl_transition_post_status [ new status :: $new_status ][ old status :: $old_status ][ post ID :: $post->ID ]" ); |
|
| 30 | + |
|
| 31 | + // transition from *published* to any other status: delete the post. |
|
| 32 | + if ( 'publish' === $old_status && 'publish' !== $new_status ) { |
|
| 33 | + // Delete the post from the triple store. |
|
| 34 | + rl_delete_post( $post ); |
|
| 35 | + |
|
| 36 | + // Remove all relation instances for the current post from `wl_relation_instances`. |
|
| 37 | + wl_core_delete_relation_instances( $post->ID ); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + // when a post is published, then all the referenced entities must be published. |
|
| 41 | + if ( 'publish' !== $old_status && 'publish' === $new_status ) { |
|
| 42 | + foreach ( wl_core_get_related_entity_ids( $post->ID ) as $entity_id ) { |
|
| 43 | + wl_update_post_status( $entity_id, 'publish' ); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | // hook save events. |
@@ -56,25 +56,25 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | function rl_delete_post( $post ) { |
| 58 | 58 | |
| 59 | - $post_id = ( is_numeric( $post ) ? $post : $post->ID ); |
|
| 59 | + $post_id = ( is_numeric( $post ) ? $post : $post->ID ); |
|
| 60 | 60 | |
| 61 | - // hide all entities that are not referenced by any published post. |
|
| 62 | - foreach ( wl_core_get_related_entity_ids( $post_id ) as $entity_id ) { |
|
| 61 | + // hide all entities that are not referenced by any published post. |
|
| 62 | + foreach ( wl_core_get_related_entity_ids( $post_id ) as $entity_id ) { |
|
| 63 | 63 | |
| 64 | - // check if there is at least one referencing post published. |
|
| 65 | - $is_published = array_reduce( wl_core_get_related_post_ids( $entity_id ), function ( $carry, $item ) { |
|
| 66 | - $post = get_post( $item ); |
|
| 64 | + // check if there is at least one referencing post published. |
|
| 65 | + $is_published = array_reduce( wl_core_get_related_post_ids( $entity_id ), function ( $carry, $item ) { |
|
| 66 | + $post = get_post( $item ); |
|
| 67 | 67 | |
| 68 | - return ( $carry || ( 'publish' === $post->post_status ) ); |
|
| 69 | - } ); |
|
| 70 | - // set the entity to draft if no referencing posts are published. |
|
| 71 | - if ( ! $is_published ) { |
|
| 72 | - wl_update_post_status( $entity_id, 'draft' ); |
|
| 73 | - } |
|
| 74 | - } |
|
| 68 | + return ( $carry || ( 'publish' === $post->post_status ) ); |
|
| 69 | + } ); |
|
| 70 | + // set the entity to draft if no referencing posts are published. |
|
| 71 | + if ( ! $is_published ) { |
|
| 72 | + wl_update_post_status( $entity_id, 'draft' ); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - // Remove the post. |
|
| 77 | - Wordlift_Linked_Data_Service::get_instance()->remove( $post_id ); |
|
| 76 | + // Remove the post. |
|
| 77 | + Wordlift_Linked_Data_Service::get_instance()->remove( $post_id ); |
|
| 78 | 78 | |
| 79 | 79 | } |
| 80 | 80 | |
@@ -86,33 +86,33 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | function wl_update_post_status( $post_id, $status ) { |
| 88 | 88 | |
| 89 | - wl_write_log( "wl_update_post_status [ post ID :: $post_id ][ status :: $status ]" ); |
|
| 89 | + wl_write_log( "wl_update_post_status [ post ID :: $post_id ][ status :: $status ]" ); |
|
| 90 | 90 | |
| 91 | - global $wpdb; |
|
| 91 | + global $wpdb; |
|
| 92 | 92 | |
| 93 | - if ( ! $post = get_post( $post_id ) ) { |
|
| 94 | - return; |
|
| 95 | - } |
|
| 93 | + if ( ! $post = get_post( $post_id ) ) { |
|
| 94 | + return; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - if ( $status === $post->post_status ) { |
|
| 98 | - return; |
|
| 99 | - } |
|
| 97 | + if ( $status === $post->post_status ) { |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - $wpdb->update( $wpdb->posts, array( 'post_status' => $status ), array( 'ID' => $post->ID ) ); |
|
| 101 | + $wpdb->update( $wpdb->posts, array( 'post_status' => $status ), array( 'ID' => $post->ID ) ); |
|
| 102 | 102 | |
| 103 | - clean_post_cache( $post->ID ); |
|
| 103 | + clean_post_cache( $post->ID ); |
|
| 104 | 104 | |
| 105 | - $old_status = $post->post_status; |
|
| 106 | - $post->post_status = $status; |
|
| 105 | + $old_status = $post->post_status; |
|
| 106 | + $post->post_status = $status; |
|
| 107 | 107 | |
| 108 | - wp_transition_post_status( $status, $old_status, $post ); |
|
| 108 | + wp_transition_post_status( $status, $old_status, $post ); |
|
| 109 | 109 | |
| 110 | - /** This action is documented in wp-includes/post.php */ |
|
| 111 | - do_action( 'edit_post', $post->ID, $post ); |
|
| 112 | - /** This action is documented in wp-includes/post.php */ |
|
| 113 | - do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); |
|
| 114 | - /** This action is documented in wp-includes/post.php */ |
|
| 115 | - do_action( 'wl_linked_data_save_post', $post->ID ); |
|
| 116 | - /** This action is documented in wp-includes/post.php */ |
|
| 117 | - do_action( 'wp_insert_post', $post->ID, $post, true ); |
|
| 110 | + /** This action is documented in wp-includes/post.php */ |
|
| 111 | + do_action( 'edit_post', $post->ID, $post ); |
|
| 112 | + /** This action is documented in wp-includes/post.php */ |
|
| 113 | + do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); |
|
| 114 | + /** This action is documented in wp-includes/post.php */ |
|
| 115 | + do_action( 'wl_linked_data_save_post', $post->ID ); |
|
| 116 | + /** This action is documented in wp-includes/post.php */ |
|
| 117 | + do_action( 'wp_insert_post', $post->ID, $post, true ); |
|
| 118 | 118 | } |
@@ -24,29 +24,29 @@ discard block |
||
| 24 | 24 | * @param string $old_status The old post status |
| 25 | 25 | * @param array $post An array with the post data |
| 26 | 26 | */ |
| 27 | -function wl_transition_post_status( $new_status, $old_status, $post ) { |
|
| 27 | +function wl_transition_post_status($new_status, $old_status, $post) { |
|
| 28 | 28 | |
| 29 | 29 | // wl_write_log( "wl_transition_post_status [ new status :: $new_status ][ old status :: $old_status ][ post ID :: $post->ID ]" ); |
| 30 | 30 | |
| 31 | 31 | // transition from *published* to any other status: delete the post. |
| 32 | - if ( 'publish' === $old_status && 'publish' !== $new_status ) { |
|
| 32 | + if ('publish' === $old_status && 'publish' !== $new_status) { |
|
| 33 | 33 | // Delete the post from the triple store. |
| 34 | - rl_delete_post( $post ); |
|
| 34 | + rl_delete_post($post); |
|
| 35 | 35 | |
| 36 | 36 | // Remove all relation instances for the current post from `wl_relation_instances`. |
| 37 | - wl_core_delete_relation_instances( $post->ID ); |
|
| 37 | + wl_core_delete_relation_instances($post->ID); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | // when a post is published, then all the referenced entities must be published. |
| 41 | - if ( 'publish' !== $old_status && 'publish' === $new_status ) { |
|
| 42 | - foreach ( wl_core_get_related_entity_ids( $post->ID ) as $entity_id ) { |
|
| 43 | - wl_update_post_status( $entity_id, 'publish' ); |
|
| 41 | + if ('publish' !== $old_status && 'publish' === $new_status) { |
|
| 42 | + foreach (wl_core_get_related_entity_ids($post->ID) as $entity_id) { |
|
| 43 | + wl_update_post_status($entity_id, 'publish'); |
|
| 44 | 44 | } |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | // hook save events. |
| 49 | -add_action( 'transition_post_status', 'wl_transition_post_status', 10, 3 ); |
|
| 49 | +add_action('transition_post_status', 'wl_transition_post_status', 10, 3); |
|
| 50 | 50 | |
| 51 | 51 | |
| 52 | 52 | /** |
@@ -54,27 +54,27 @@ discard block |
||
| 54 | 54 | * |
| 55 | 55 | * @param array|int $post An array of post data |
| 56 | 56 | */ |
| 57 | -function rl_delete_post( $post ) { |
|
| 57 | +function rl_delete_post($post) { |
|
| 58 | 58 | |
| 59 | - $post_id = ( is_numeric( $post ) ? $post : $post->ID ); |
|
| 59 | + $post_id = (is_numeric($post) ? $post : $post->ID); |
|
| 60 | 60 | |
| 61 | 61 | // hide all entities that are not referenced by any published post. |
| 62 | - foreach ( wl_core_get_related_entity_ids( $post_id ) as $entity_id ) { |
|
| 62 | + foreach (wl_core_get_related_entity_ids($post_id) as $entity_id) { |
|
| 63 | 63 | |
| 64 | 64 | // check if there is at least one referencing post published. |
| 65 | - $is_published = array_reduce( wl_core_get_related_post_ids( $entity_id ), function ( $carry, $item ) { |
|
| 66 | - $post = get_post( $item ); |
|
| 65 | + $is_published = array_reduce(wl_core_get_related_post_ids($entity_id), function($carry, $item) { |
|
| 66 | + $post = get_post($item); |
|
| 67 | 67 | |
| 68 | - return ( $carry || ( 'publish' === $post->post_status ) ); |
|
| 68 | + return ($carry || ('publish' === $post->post_status)); |
|
| 69 | 69 | } ); |
| 70 | 70 | // set the entity to draft if no referencing posts are published. |
| 71 | - if ( ! $is_published ) { |
|
| 72 | - wl_update_post_status( $entity_id, 'draft' ); |
|
| 71 | + if ( ! $is_published) { |
|
| 72 | + wl_update_post_status($entity_id, 'draft'); |
|
| 73 | 73 | } |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // Remove the post. |
| 77 | - Wordlift_Linked_Data_Service::get_instance()->remove( $post_id ); |
|
| 77 | + Wordlift_Linked_Data_Service::get_instance()->remove($post_id); |
|
| 78 | 78 | |
| 79 | 79 | } |
| 80 | 80 | |
@@ -84,35 +84,35 @@ discard block |
||
| 84 | 84 | * @param int $post_id The post ID |
| 85 | 85 | * @param string $status The new status |
| 86 | 86 | */ |
| 87 | -function wl_update_post_status( $post_id, $status ) { |
|
| 87 | +function wl_update_post_status($post_id, $status) { |
|
| 88 | 88 | |
| 89 | - wl_write_log( "wl_update_post_status [ post ID :: $post_id ][ status :: $status ]" ); |
|
| 89 | + wl_write_log("wl_update_post_status [ post ID :: $post_id ][ status :: $status ]"); |
|
| 90 | 90 | |
| 91 | 91 | global $wpdb; |
| 92 | 92 | |
| 93 | - if ( ! $post = get_post( $post_id ) ) { |
|
| 93 | + if ( ! $post = get_post($post_id)) { |
|
| 94 | 94 | return; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - if ( $status === $post->post_status ) { |
|
| 97 | + if ($status === $post->post_status) { |
|
| 98 | 98 | return; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - $wpdb->update( $wpdb->posts, array( 'post_status' => $status ), array( 'ID' => $post->ID ) ); |
|
| 101 | + $wpdb->update($wpdb->posts, array('post_status' => $status), array('ID' => $post->ID)); |
|
| 102 | 102 | |
| 103 | - clean_post_cache( $post->ID ); |
|
| 103 | + clean_post_cache($post->ID); |
|
| 104 | 104 | |
| 105 | 105 | $old_status = $post->post_status; |
| 106 | 106 | $post->post_status = $status; |
| 107 | 107 | |
| 108 | - wp_transition_post_status( $status, $old_status, $post ); |
|
| 108 | + wp_transition_post_status($status, $old_status, $post); |
|
| 109 | 109 | |
| 110 | 110 | /** This action is documented in wp-includes/post.php */ |
| 111 | - do_action( 'edit_post', $post->ID, $post ); |
|
| 111 | + do_action('edit_post', $post->ID, $post); |
|
| 112 | 112 | /** This action is documented in wp-includes/post.php */ |
| 113 | - do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); |
|
| 113 | + do_action("save_post_{$post->post_type}", $post->ID, $post, true); |
|
| 114 | 114 | /** This action is documented in wp-includes/post.php */ |
| 115 | - do_action( 'wl_linked_data_save_post', $post->ID ); |
|
| 115 | + do_action('wl_linked_data_save_post', $post->ID); |
|
| 116 | 116 | /** This action is documented in wp-includes/post.php */ |
| 117 | - do_action( 'wp_insert_post', $post->ID, $post, true ); |
|
| 117 | + do_action('wp_insert_post', $post->ID, $post, true); |
|
| 118 | 118 | } |
@@ -6,59 +6,59 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class Wordlift_Entity_Type_Taxonomy_Service { |
| 8 | 8 | |
| 9 | - // @@todo: rename using singular. |
|
| 9 | + // @@todo: rename using singular. |
|
| 10 | 10 | |
| 11 | - const TAXONOMY_NAME = 'wl_entity_type'; |
|
| 11 | + const TAXONOMY_NAME = 'wl_entity_type'; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * Register the taxonomies. |
|
| 15 | - * |
|
| 16 | - * @since 3.18.0 |
|
| 17 | - */ |
|
| 18 | - public function init() { |
|
| 13 | + /** |
|
| 14 | + * Register the taxonomies. |
|
| 15 | + * |
|
| 16 | + * @since 3.18.0 |
|
| 17 | + */ |
|
| 18 | + public function init() { |
|
| 19 | 19 | |
| 20 | - $labels = array( |
|
| 21 | - 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
| 22 | - 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
| 23 | - 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
| 24 | - 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
| 25 | - 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
| 26 | - 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
| 27 | - 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
| 28 | - 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
| 29 | - 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
| 30 | - 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
| 31 | - 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
| 32 | - ); |
|
| 20 | + $labels = array( |
|
| 21 | + 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
| 22 | + 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
| 23 | + 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
| 24 | + 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
| 25 | + 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
| 26 | + 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
| 27 | + 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
| 28 | + 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
| 29 | + 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
| 30 | + 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
| 31 | + 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
| 32 | + ); |
|
| 33 | 33 | |
| 34 | - // Take away GUI for taxonomy editing. |
|
| 35 | - // TODO: read capabilities when editing of the WL <-> schema.org mapping is possible. |
|
| 36 | - $capabilities = array( |
|
| 37 | - // We enable editors to change the title/description of terms: |
|
| 38 | - // |
|
| 39 | - // See https://github.com/insideout10/wordlift-plugin/issues/398 |
|
| 40 | - 'manage_terms' => 'manage_options', |
|
| 41 | - 'edit_terms' => 'wl_entity_type_edit_term', |
|
| 42 | - 'delete_terms' => 'wl_entity_type_delete_term', |
|
| 43 | - 'assign_terms' => 'edit_posts', |
|
| 44 | - ); |
|
| 34 | + // Take away GUI for taxonomy editing. |
|
| 35 | + // TODO: read capabilities when editing of the WL <-> schema.org mapping is possible. |
|
| 36 | + $capabilities = array( |
|
| 37 | + // We enable editors to change the title/description of terms: |
|
| 38 | + // |
|
| 39 | + // See https://github.com/insideout10/wordlift-plugin/issues/398 |
|
| 40 | + 'manage_terms' => 'manage_options', |
|
| 41 | + 'edit_terms' => 'wl_entity_type_edit_term', |
|
| 42 | + 'delete_terms' => 'wl_entity_type_delete_term', |
|
| 43 | + 'assign_terms' => 'edit_posts', |
|
| 44 | + ); |
|
| 45 | 45 | |
| 46 | - $args = array( |
|
| 47 | - 'labels' => $labels, |
|
| 48 | - 'capabilities' => $capabilities, |
|
| 49 | - 'hierarchical' => true, |
|
| 50 | - 'show_admin_column' => true, |
|
| 51 | - 'show_in_quick_edit' => false, |
|
| 52 | - ); |
|
| 46 | + $args = array( |
|
| 47 | + 'labels' => $labels, |
|
| 48 | + 'capabilities' => $capabilities, |
|
| 49 | + 'hierarchical' => true, |
|
| 50 | + 'show_admin_column' => true, |
|
| 51 | + 'show_in_quick_edit' => false, |
|
| 52 | + ); |
|
| 53 | 53 | |
| 54 | - register_taxonomy( |
|
| 55 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, // Taxonomy name. |
|
| 56 | - Wordlift_Entity_Service::valid_entity_post_types(), // Taxonomy post types. |
|
| 57 | - $args // Taxonomy args. |
|
| 58 | - ); |
|
| 54 | + register_taxonomy( |
|
| 55 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, // Taxonomy name. |
|
| 56 | + Wordlift_Entity_Service::valid_entity_post_types(), // Taxonomy post types. |
|
| 57 | + $args // Taxonomy args. |
|
| 58 | + ); |
|
| 59 | 59 | |
| 60 | - // Add filter to change the metabox CSS class |
|
| 61 | - add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
| 62 | - } |
|
| 60 | + // Add filter to change the metabox CSS class |
|
| 61 | + add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | 64 | } |
@@ -18,17 +18,17 @@ discard block |
||
| 18 | 18 | public function init() { |
| 19 | 19 | |
| 20 | 20 | $labels = array( |
| 21 | - 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
| 22 | - 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
| 23 | - 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
| 24 | - 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
| 25 | - 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
| 26 | - 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
| 27 | - 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
| 28 | - 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
| 29 | - 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
| 30 | - 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
| 31 | - 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
| 21 | + 'name' => _x('Entity Types', 'taxonomy general name', 'wordlift'), |
|
| 22 | + 'singular_name' => _x('Entity Type', 'taxonomy singular name', 'wordlift'), |
|
| 23 | + 'search_items' => __('Search Entity Types', 'wordlift'), |
|
| 24 | + 'all_items' => __('All Entity Types', 'wordlift'), |
|
| 25 | + 'parent_item' => __('Parent Entity Type', 'wordlift'), |
|
| 26 | + 'parent_item_colon' => __('Parent Entity Type:', 'wordlift'), |
|
| 27 | + 'edit_item' => __('Edit Entity Type', 'wordlift'), |
|
| 28 | + 'update_item' => __('Update Entity Type', 'wordlift'), |
|
| 29 | + 'add_new_item' => __('Add New Entity Type', 'wordlift'), |
|
| 30 | + 'new_item_name' => __('New Entity Type', 'wordlift'), |
|
| 31 | + 'menu_name' => __('Entity Types', 'wordlift'), |
|
| 32 | 32 | ); |
| 33 | 33 | |
| 34 | 34 | // Take away GUI for taxonomy editing. |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | ); |
| 59 | 59 | |
| 60 | 60 | // Add filter to change the metabox CSS class |
| 61 | - add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
| 61 | + add_filter('postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class'); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | } |
@@ -16,130 +16,130 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Timeline_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The Log service. |
|
| 21 | - * |
|
| 22 | - * @since 3.1.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The Entity service. |
|
| 30 | - * |
|
| 31 | - * @since 3.1.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The number of words to use for the excerpt, set in the `to_json` function |
|
| 39 | - * and used by a filter. |
|
| 40 | - * |
|
| 41 | - * @since 3.7.0 |
|
| 42 | - * @access private |
|
| 43 | - * @var int $excerpt_length The number of words to use for the excerpt. |
|
| 44 | - */ |
|
| 45 | - private $excerpt_length; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 49 | - * |
|
| 50 | - * @since 3.15.0 |
|
| 51 | - * @access private |
|
| 52 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 53 | - */ |
|
| 54 | - private $entity_type_service; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * A singleton instance of the Timeline service (useful for unit tests). |
|
| 58 | - * |
|
| 59 | - * @since 3.1.0 |
|
| 60 | - * @access private |
|
| 61 | - * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
| 62 | - */ |
|
| 63 | - private static $instance; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Create a Wordlift_Timeline_Service instance. |
|
| 67 | - * |
|
| 68 | - * @since 3.1.0 |
|
| 69 | - * |
|
| 70 | - * @param \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 71 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 72 | - */ |
|
| 73 | - public function __construct( $entity_service, $entity_type_service ) { |
|
| 74 | - |
|
| 75 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 76 | - |
|
| 77 | - $this->entity_service = $entity_service; |
|
| 78 | - $this->entity_type_service = $entity_type_service; |
|
| 79 | - |
|
| 80 | - self::$instance = $this; |
|
| 81 | - |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Get the singleton instance of the Wordlift_Timeline_Service |
|
| 86 | - * |
|
| 87 | - * @since 3.1.0 |
|
| 88 | - * |
|
| 89 | - * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
| 90 | - */ |
|
| 91 | - public static function get_instance() { |
|
| 92 | - |
|
| 93 | - return self::$instance; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Retrieve timeline events and output them in JSON. |
|
| 98 | - * |
|
| 99 | - * @since 3.1.0 |
|
| 100 | - */ |
|
| 101 | - public function ajax_timeline() { |
|
| 102 | - |
|
| 103 | - // Get the ID of the post who requested the timeline. |
|
| 104 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null ); |
|
| 105 | - |
|
| 106 | - // Get the events and transform them for the JSON response, then send them to the client. |
|
| 107 | - wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 108 | - |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Retrieve timeline events. |
|
| 113 | - * |
|
| 114 | - * @since 3.1.0 |
|
| 115 | - * |
|
| 116 | - * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
| 117 | - * |
|
| 118 | - * @param int $post_id The post ID. |
|
| 119 | - * |
|
| 120 | - * @return array An array of event posts. |
|
| 121 | - */ |
|
| 122 | - public function get_events( $post_id = null ) { |
|
| 123 | - |
|
| 124 | - // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
| 125 | - // posts if no post has been specified. |
|
| 126 | - $ids = ( is_numeric( $post_id ) |
|
| 127 | - ? wl_core_get_related_entity_ids( $post_id ) |
|
| 128 | - : $this->get_all_related_to_last_50_published_posts() ); |
|
| 129 | - |
|
| 130 | - // Add the post itself if it's an entity. |
|
| 131 | - if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) { |
|
| 132 | - $ids[] = $post_id; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // If there's no entities, return an empty array right away. |
|
| 136 | - if ( 0 === sizeof( $ids ) ) { |
|
| 137 | - $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 138 | - |
|
| 139 | - return array(); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 19 | + /** |
|
| 20 | + * The Log service. |
|
| 21 | + * |
|
| 22 | + * @since 3.1.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The Entity service. |
|
| 30 | + * |
|
| 31 | + * @since 3.1.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The number of words to use for the excerpt, set in the `to_json` function |
|
| 39 | + * and used by a filter. |
|
| 40 | + * |
|
| 41 | + * @since 3.7.0 |
|
| 42 | + * @access private |
|
| 43 | + * @var int $excerpt_length The number of words to use for the excerpt. |
|
| 44 | + */ |
|
| 45 | + private $excerpt_length; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 49 | + * |
|
| 50 | + * @since 3.15.0 |
|
| 51 | + * @access private |
|
| 52 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 53 | + */ |
|
| 54 | + private $entity_type_service; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * A singleton instance of the Timeline service (useful for unit tests). |
|
| 58 | + * |
|
| 59 | + * @since 3.1.0 |
|
| 60 | + * @access private |
|
| 61 | + * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
| 62 | + */ |
|
| 63 | + private static $instance; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Create a Wordlift_Timeline_Service instance. |
|
| 67 | + * |
|
| 68 | + * @since 3.1.0 |
|
| 69 | + * |
|
| 70 | + * @param \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 71 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 72 | + */ |
|
| 73 | + public function __construct( $entity_service, $entity_type_service ) { |
|
| 74 | + |
|
| 75 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 76 | + |
|
| 77 | + $this->entity_service = $entity_service; |
|
| 78 | + $this->entity_type_service = $entity_type_service; |
|
| 79 | + |
|
| 80 | + self::$instance = $this; |
|
| 81 | + |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Get the singleton instance of the Wordlift_Timeline_Service |
|
| 86 | + * |
|
| 87 | + * @since 3.1.0 |
|
| 88 | + * |
|
| 89 | + * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
| 90 | + */ |
|
| 91 | + public static function get_instance() { |
|
| 92 | + |
|
| 93 | + return self::$instance; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Retrieve timeline events and output them in JSON. |
|
| 98 | + * |
|
| 99 | + * @since 3.1.0 |
|
| 100 | + */ |
|
| 101 | + public function ajax_timeline() { |
|
| 102 | + |
|
| 103 | + // Get the ID of the post who requested the timeline. |
|
| 104 | + $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null ); |
|
| 105 | + |
|
| 106 | + // Get the events and transform them for the JSON response, then send them to the client. |
|
| 107 | + wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 108 | + |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Retrieve timeline events. |
|
| 113 | + * |
|
| 114 | + * @since 3.1.0 |
|
| 115 | + * |
|
| 116 | + * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
| 117 | + * |
|
| 118 | + * @param int $post_id The post ID. |
|
| 119 | + * |
|
| 120 | + * @return array An array of event posts. |
|
| 121 | + */ |
|
| 122 | + public function get_events( $post_id = null ) { |
|
| 123 | + |
|
| 124 | + // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
| 125 | + // posts if no post has been specified. |
|
| 126 | + $ids = ( is_numeric( $post_id ) |
|
| 127 | + ? wl_core_get_related_entity_ids( $post_id ) |
|
| 128 | + : $this->get_all_related_to_last_50_published_posts() ); |
|
| 129 | + |
|
| 130 | + // Add the post itself if it's an entity. |
|
| 131 | + if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) { |
|
| 132 | + $ids[] = $post_id; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // If there's no entities, return an empty array right away. |
|
| 136 | + if ( 0 === sizeof( $ids ) ) { |
|
| 137 | + $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 138 | + |
|
| 139 | + return array(); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 143 | 143 | |
| 144 | 144 | // $entity_type_service = $this->entity_type_service; |
| 145 | 145 | |
@@ -151,237 +151,237 @@ discard block |
||
| 151 | 151 | // return $entity_type_service->has_entity_type( $item, 'http://schema.org/Event' ); |
| 152 | 152 | // } ) ); |
| 153 | 153 | |
| 154 | - $args = array( |
|
| 155 | - 'post__in' => $ids, |
|
| 156 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 157 | - 'post_status' => 'publish', |
|
| 158 | - 'posts_per_page' => - 1, |
|
| 159 | - 'meta_query' => array( |
|
| 160 | - 'relation' => 'AND', |
|
| 161 | - array( |
|
| 162 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 163 | - 'value' => null, |
|
| 164 | - 'compare' => '!=', |
|
| 165 | - ), |
|
| 166 | - array( |
|
| 167 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
| 168 | - 'value' => null, |
|
| 169 | - 'compare' => '!=', |
|
| 170 | - ), |
|
| 171 | - ), |
|
| 172 | - 'tax_query' => array( |
|
| 173 | - array( |
|
| 174 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 175 | - 'field' => 'slug', |
|
| 176 | - 'terms' => 'event', |
|
| 177 | - ), |
|
| 178 | - ), |
|
| 179 | - ); |
|
| 180 | - |
|
| 181 | - return get_posts( $args ); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Convert timeline events to JSON. This function sets the global post in order |
|
| 186 | - * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
| 187 | - * we're not taking care of restoring any previous post: there isn't any. |
|
| 188 | - * |
|
| 189 | - * @since 3.1.0 |
|
| 190 | - * |
|
| 191 | - * @param array $posts An array of posts. |
|
| 192 | - * |
|
| 193 | - * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
| 194 | - */ |
|
| 195 | - public function to_json( $posts ) { |
|
| 196 | - |
|
| 197 | - // If there are no events, return empty JSON |
|
| 198 | - if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 199 | - return ''; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
| 203 | - $display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media'; |
|
| 204 | - |
|
| 205 | - // The number of words for the excerpt (by default 55, as WordPress). |
|
| 206 | - $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55; |
|
| 207 | - add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 208 | - |
|
| 209 | - // Add a filter to remove the [...] after excerpts, since we're adding |
|
| 210 | - // a link to the post itself. |
|
| 211 | - add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 212 | - |
|
| 213 | - // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
| 214 | - $start_at_slide = 0; |
|
| 215 | - $event_index = - 1; |
|
| 216 | - $now = time(); |
|
| 217 | - |
|
| 218 | - // Prepare the timeline variable. |
|
| 219 | - $timeline = array(); |
|
| 220 | - |
|
| 221 | - // Populate the arrays. |
|
| 222 | - $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 223 | - |
|
| 224 | - // Get the start and end dates. |
|
| 225 | - $start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ); |
|
| 226 | - $end_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ); |
|
| 227 | - |
|
| 228 | - // Set the starting slide. |
|
| 229 | - $event_index ++; |
|
| 230 | - if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 231 | - $start_at_slide = $event_index; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // Load thumbnail |
|
| 235 | - if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $item->ID ) ) |
|
| 236 | - && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 237 | - ) { |
|
| 238 | - |
|
| 239 | - // Set the thumbnail URL. |
|
| 240 | - if ( 'background' === $display_images_as ) { |
|
| 241 | - $date['background'] = array( 'url' => $attachment[0] ); |
|
| 242 | - $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 243 | - } else { |
|
| 244 | - $date['media'] = array( |
|
| 245 | - 'url' => $attachment[0], |
|
| 246 | - 'thumbnail' => $attachment[0], |
|
| 247 | - ); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - // Set the start/end dates by converting them to TimelineJS required format. |
|
| 252 | - $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 253 | - $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 254 | - |
|
| 255 | - setup_postdata( $GLOBALS['post'] = $item ); |
|
| 256 | - |
|
| 257 | - $more_link_text = sprintf( |
|
| 258 | - '<span aria-label="%1$s">%2$s</span>', |
|
| 259 | - sprintf( |
|
| 260 | - /* translators: %s: Name of current post */ |
|
| 261 | - __( 'Continue reading %s' ), |
|
| 262 | - the_title_attribute( array( 'echo' => false ) ) |
|
| 263 | - ), |
|
| 264 | - __( '(more…)' ) |
|
| 265 | - ); |
|
| 266 | - |
|
| 267 | - // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
| 268 | - $date['text'] = array( |
|
| 269 | - 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 270 | - ); |
|
| 271 | - |
|
| 272 | - // If we have an excerpt, set it. |
|
| 273 | - if ( 0 < $excerpt_length ) { |
|
| 274 | - $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - return $date; |
|
| 278 | - |
|
| 279 | - }, $posts ); |
|
| 280 | - |
|
| 281 | - // Finally remove the excerpt filter. |
|
| 282 | - remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 283 | - |
|
| 284 | - // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
| 285 | - return array( |
|
| 286 | - 'timeline' => $timeline, |
|
| 287 | - 'start_at_slide' => $start_at_slide, |
|
| 288 | - ); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * This function filters {@link excerpt_more} by removing it, since we're |
|
| 293 | - * adding the 'read more' link. This filter is set by {@see to_json}. |
|
| 294 | - * |
|
| 295 | - * @since 3.7.0 |
|
| 296 | - * |
|
| 297 | - * @param string $excerpt_more The excerpt more preset. |
|
| 298 | - * |
|
| 299 | - * @return string An empty string. |
|
| 300 | - */ |
|
| 301 | - public function excerpt_more( $excerpt_more ) { |
|
| 302 | - |
|
| 303 | - return ''; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
| 308 | - * how many words to return according to the client setting. |
|
| 309 | - * |
|
| 310 | - * @since 3.7.0 |
|
| 311 | - * |
|
| 312 | - * @param int $length The preset number of words. |
|
| 313 | - * |
|
| 314 | - * @return int The number of words for the preset. |
|
| 315 | - */ |
|
| 316 | - public function excerpt_length( $length ) { |
|
| 317 | - |
|
| 318 | - return $this->excerpt_length; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Convert the date to a date array. |
|
| 324 | - * |
|
| 325 | - * @since 3.7.0 |
|
| 326 | - * |
|
| 327 | - * @param $value int A date value. |
|
| 328 | - * |
|
| 329 | - * @return array An array containing year, month and day values. |
|
| 330 | - */ |
|
| 331 | - public static function date( $value ) { |
|
| 332 | - |
|
| 333 | - return array( |
|
| 334 | - 'year' => (int) date( 'Y', $value ), |
|
| 335 | - 'month' => (int) date( 'm', $value ), |
|
| 336 | - 'day' => (int) date( 'd', $value ), |
|
| 337 | - |
|
| 338 | - ); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 343 | - * its specific function). |
|
| 344 | - * |
|
| 345 | - * @since 3.1.0 |
|
| 346 | - * |
|
| 347 | - * @return array An array of post IDs. |
|
| 348 | - */ |
|
| 349 | - public function get_all_related_to_last_50_published_posts() { |
|
| 350 | - |
|
| 351 | - // Global timeline. Get entities from the latest posts. |
|
| 352 | - $latest_posts_ids = get_posts( array( |
|
| 353 | - 'numberposts' => 50, |
|
| 354 | - 'fields' => 'ids', //only get post IDs |
|
| 355 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 356 | - 'tax_query' => array( |
|
| 357 | - 'relation' => 'OR', |
|
| 358 | - array( |
|
| 359 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 360 | - 'operator' => 'NOT EXISTS', |
|
| 361 | - ), |
|
| 362 | - array( |
|
| 363 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 364 | - 'field' => 'slug', |
|
| 365 | - 'terms' => 'article', |
|
| 366 | - ), |
|
| 367 | - ), |
|
| 368 | - 'post_status' => 'publish', |
|
| 369 | - ) ); |
|
| 370 | - |
|
| 371 | - if ( empty( $latest_posts_ids ) ) { |
|
| 372 | - // There are no posts. |
|
| 373 | - return array(); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - // Collect entities related to latest posts |
|
| 377 | - $entity_ids = array(); |
|
| 378 | - foreach ( $latest_posts_ids as $id ) { |
|
| 379 | - $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 380 | - 'status' => 'publish', |
|
| 381 | - ) ) ); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - return $entity_ids; |
|
| 385 | - } |
|
| 154 | + $args = array( |
|
| 155 | + 'post__in' => $ids, |
|
| 156 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 157 | + 'post_status' => 'publish', |
|
| 158 | + 'posts_per_page' => - 1, |
|
| 159 | + 'meta_query' => array( |
|
| 160 | + 'relation' => 'AND', |
|
| 161 | + array( |
|
| 162 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 163 | + 'value' => null, |
|
| 164 | + 'compare' => '!=', |
|
| 165 | + ), |
|
| 166 | + array( |
|
| 167 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
| 168 | + 'value' => null, |
|
| 169 | + 'compare' => '!=', |
|
| 170 | + ), |
|
| 171 | + ), |
|
| 172 | + 'tax_query' => array( |
|
| 173 | + array( |
|
| 174 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 175 | + 'field' => 'slug', |
|
| 176 | + 'terms' => 'event', |
|
| 177 | + ), |
|
| 178 | + ), |
|
| 179 | + ); |
|
| 180 | + |
|
| 181 | + return get_posts( $args ); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Convert timeline events to JSON. This function sets the global post in order |
|
| 186 | + * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
| 187 | + * we're not taking care of restoring any previous post: there isn't any. |
|
| 188 | + * |
|
| 189 | + * @since 3.1.0 |
|
| 190 | + * |
|
| 191 | + * @param array $posts An array of posts. |
|
| 192 | + * |
|
| 193 | + * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
| 194 | + */ |
|
| 195 | + public function to_json( $posts ) { |
|
| 196 | + |
|
| 197 | + // If there are no events, return empty JSON |
|
| 198 | + if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 199 | + return ''; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
| 203 | + $display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media'; |
|
| 204 | + |
|
| 205 | + // The number of words for the excerpt (by default 55, as WordPress). |
|
| 206 | + $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55; |
|
| 207 | + add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 208 | + |
|
| 209 | + // Add a filter to remove the [...] after excerpts, since we're adding |
|
| 210 | + // a link to the post itself. |
|
| 211 | + add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 212 | + |
|
| 213 | + // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
| 214 | + $start_at_slide = 0; |
|
| 215 | + $event_index = - 1; |
|
| 216 | + $now = time(); |
|
| 217 | + |
|
| 218 | + // Prepare the timeline variable. |
|
| 219 | + $timeline = array(); |
|
| 220 | + |
|
| 221 | + // Populate the arrays. |
|
| 222 | + $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 223 | + |
|
| 224 | + // Get the start and end dates. |
|
| 225 | + $start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ); |
|
| 226 | + $end_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ); |
|
| 227 | + |
|
| 228 | + // Set the starting slide. |
|
| 229 | + $event_index ++; |
|
| 230 | + if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 231 | + $start_at_slide = $event_index; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // Load thumbnail |
|
| 235 | + if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $item->ID ) ) |
|
| 236 | + && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 237 | + ) { |
|
| 238 | + |
|
| 239 | + // Set the thumbnail URL. |
|
| 240 | + if ( 'background' === $display_images_as ) { |
|
| 241 | + $date['background'] = array( 'url' => $attachment[0] ); |
|
| 242 | + $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 243 | + } else { |
|
| 244 | + $date['media'] = array( |
|
| 245 | + 'url' => $attachment[0], |
|
| 246 | + 'thumbnail' => $attachment[0], |
|
| 247 | + ); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + // Set the start/end dates by converting them to TimelineJS required format. |
|
| 252 | + $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 253 | + $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 254 | + |
|
| 255 | + setup_postdata( $GLOBALS['post'] = $item ); |
|
| 256 | + |
|
| 257 | + $more_link_text = sprintf( |
|
| 258 | + '<span aria-label="%1$s">%2$s</span>', |
|
| 259 | + sprintf( |
|
| 260 | + /* translators: %s: Name of current post */ |
|
| 261 | + __( 'Continue reading %s' ), |
|
| 262 | + the_title_attribute( array( 'echo' => false ) ) |
|
| 263 | + ), |
|
| 264 | + __( '(more…)' ) |
|
| 265 | + ); |
|
| 266 | + |
|
| 267 | + // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
| 268 | + $date['text'] = array( |
|
| 269 | + 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 270 | + ); |
|
| 271 | + |
|
| 272 | + // If we have an excerpt, set it. |
|
| 273 | + if ( 0 < $excerpt_length ) { |
|
| 274 | + $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + return $date; |
|
| 278 | + |
|
| 279 | + }, $posts ); |
|
| 280 | + |
|
| 281 | + // Finally remove the excerpt filter. |
|
| 282 | + remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 283 | + |
|
| 284 | + // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
| 285 | + return array( |
|
| 286 | + 'timeline' => $timeline, |
|
| 287 | + 'start_at_slide' => $start_at_slide, |
|
| 288 | + ); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * This function filters {@link excerpt_more} by removing it, since we're |
|
| 293 | + * adding the 'read more' link. This filter is set by {@see to_json}. |
|
| 294 | + * |
|
| 295 | + * @since 3.7.0 |
|
| 296 | + * |
|
| 297 | + * @param string $excerpt_more The excerpt more preset. |
|
| 298 | + * |
|
| 299 | + * @return string An empty string. |
|
| 300 | + */ |
|
| 301 | + public function excerpt_more( $excerpt_more ) { |
|
| 302 | + |
|
| 303 | + return ''; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
| 308 | + * how many words to return according to the client setting. |
|
| 309 | + * |
|
| 310 | + * @since 3.7.0 |
|
| 311 | + * |
|
| 312 | + * @param int $length The preset number of words. |
|
| 313 | + * |
|
| 314 | + * @return int The number of words for the preset. |
|
| 315 | + */ |
|
| 316 | + public function excerpt_length( $length ) { |
|
| 317 | + |
|
| 318 | + return $this->excerpt_length; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Convert the date to a date array. |
|
| 324 | + * |
|
| 325 | + * @since 3.7.0 |
|
| 326 | + * |
|
| 327 | + * @param $value int A date value. |
|
| 328 | + * |
|
| 329 | + * @return array An array containing year, month and day values. |
|
| 330 | + */ |
|
| 331 | + public static function date( $value ) { |
|
| 332 | + |
|
| 333 | + return array( |
|
| 334 | + 'year' => (int) date( 'Y', $value ), |
|
| 335 | + 'month' => (int) date( 'm', $value ), |
|
| 336 | + 'day' => (int) date( 'd', $value ), |
|
| 337 | + |
|
| 338 | + ); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 343 | + * its specific function). |
|
| 344 | + * |
|
| 345 | + * @since 3.1.0 |
|
| 346 | + * |
|
| 347 | + * @return array An array of post IDs. |
|
| 348 | + */ |
|
| 349 | + public function get_all_related_to_last_50_published_posts() { |
|
| 350 | + |
|
| 351 | + // Global timeline. Get entities from the latest posts. |
|
| 352 | + $latest_posts_ids = get_posts( array( |
|
| 353 | + 'numberposts' => 50, |
|
| 354 | + 'fields' => 'ids', //only get post IDs |
|
| 355 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 356 | + 'tax_query' => array( |
|
| 357 | + 'relation' => 'OR', |
|
| 358 | + array( |
|
| 359 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 360 | + 'operator' => 'NOT EXISTS', |
|
| 361 | + ), |
|
| 362 | + array( |
|
| 363 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 364 | + 'field' => 'slug', |
|
| 365 | + 'terms' => 'article', |
|
| 366 | + ), |
|
| 367 | + ), |
|
| 368 | + 'post_status' => 'publish', |
|
| 369 | + ) ); |
|
| 370 | + |
|
| 371 | + if ( empty( $latest_posts_ids ) ) { |
|
| 372 | + // There are no posts. |
|
| 373 | + return array(); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + // Collect entities related to latest posts |
|
| 377 | + $entity_ids = array(); |
|
| 378 | + foreach ( $latest_posts_ids as $id ) { |
|
| 379 | + $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 380 | + 'status' => 'publish', |
|
| 381 | + ) ) ); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + return $entity_ids; |
|
| 385 | + } |
|
| 386 | 386 | |
| 387 | 387 | } |
@@ -17,182 +17,182 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Uri_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * The title regex to sanitize titles in paths. |
|
| 22 | - * |
|
| 23 | - * According to RFC2396 (http://www.ietf.org/rfc/rfc2396.txt) these characters are reserved: |
|
| 24 | - * ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | |
|
| 25 | - * "$" | "," |
|
| 26 | - * |
|
| 27 | - * We also remove the space and the UTF-8 BOM sequence. |
|
| 28 | - * |
|
| 29 | - * @since 3.7.1 |
|
| 30 | - */ |
|
| 31 | - const INVALID_CHARACTERS = "/[ ;\\/?:@&=\\+\\\$,]|(?:\\xEF\\xBB\\xBF)/"; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A {@link Wordlift_Log_Service} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.6.0 |
|
| 37 | - * @access private |
|
| 38 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 39 | - */ |
|
| 40 | - private $log; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The global WordPress database connection. |
|
| 44 | - * |
|
| 45 | - * @since 3.6.0 |
|
| 46 | - * @access private |
|
| 47 | - * @var \wpdb $wpdb The global WordPress database connection. |
|
| 48 | - */ |
|
| 49 | - private $wpdb; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The {@link Wordlift_Uri_Service} singleton instance. |
|
| 53 | - * |
|
| 54 | - * @since 3.7.2 |
|
| 55 | - * @access private |
|
| 56 | - * @var \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance. |
|
| 57 | - */ |
|
| 58 | - private static $instance; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Create an instance of Wordlift_Uri_Service. |
|
| 62 | - * |
|
| 63 | - * @since 3.6.0 |
|
| 64 | - * |
|
| 65 | - * @param \wpdb $wpdb The global WordPress database connection. |
|
| 66 | - */ |
|
| 67 | - public function __construct( $wpdb ) { |
|
| 68 | - |
|
| 69 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Uri_Service' ); |
|
| 70 | - |
|
| 71 | - $this->wpdb = $wpdb; |
|
| 72 | - |
|
| 73 | - self::$instance = $this; |
|
| 74 | - |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get the {@link Wordlift_Uri_Service} singleton instance. |
|
| 79 | - * |
|
| 80 | - * @since 3.7.2 |
|
| 81 | - * @return \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance. |
|
| 82 | - */ |
|
| 83 | - public static function get_instance() { |
|
| 84 | - |
|
| 85 | - return self::$instance; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Delete all generated URIs from the database. |
|
| 90 | - * |
|
| 91 | - * @since 3.6.0 |
|
| 92 | - */ |
|
| 93 | - public function delete_all() { |
|
| 94 | - |
|
| 95 | - $this->log->trace( 'Going to delete all the `entity_url` post metas...' ); |
|
| 96 | - |
|
| 97 | - // Delete URIs associated with posts/entities. |
|
| 98 | - $this->wpdb->delete( $this->wpdb->postmeta, array( 'meta_key' => 'entity_url' ) ); |
|
| 99 | - |
|
| 100 | - $this->log->trace( 'Going to delete all the `_wl_uri` user metas...' ); |
|
| 101 | - |
|
| 102 | - // Delete URIs associated with authors. |
|
| 103 | - $this->wpdb->delete( $this->wpdb->usermeta, array( 'meta_key' => '_wl_uri' ) ); |
|
| 104 | - |
|
| 105 | - $this->log->debug( '`entity_url` post metas and `_wl_uri` user metas deleted.' ); |
|
| 106 | - |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Sanitizes an URI path by replacing the non allowed characters with an underscore. |
|
| 111 | - * |
|
| 112 | - * @since 3.7.2 |
|
| 113 | - * @uses sanitize_title() to manage not ASCII chars |
|
| 114 | - * |
|
| 115 | - * @see https://codex.wordpress.org/Function_Reference/sanitize_title |
|
| 116 | - * |
|
| 117 | - * @param string $path The path to sanitize. |
|
| 118 | - * @param string $char The replacement character (by default an underscore). |
|
| 119 | - * |
|
| 120 | - * @return string The sanitized path. |
|
| 121 | - */ |
|
| 122 | - public function sanitize_path( $path, $char = '_' ) { |
|
| 123 | - |
|
| 124 | - // Ensure the path is ASCII. |
|
| 125 | - // see https://github.com/insideout10/wordlift-plugin/issues/386 |
|
| 20 | + /** |
|
| 21 | + * The title regex to sanitize titles in paths. |
|
| 22 | + * |
|
| 23 | + * According to RFC2396 (http://www.ietf.org/rfc/rfc2396.txt) these characters are reserved: |
|
| 24 | + * ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | |
|
| 25 | + * "$" | "," |
|
| 26 | + * |
|
| 27 | + * We also remove the space and the UTF-8 BOM sequence. |
|
| 28 | + * |
|
| 29 | + * @since 3.7.1 |
|
| 30 | + */ |
|
| 31 | + const INVALID_CHARACTERS = "/[ ;\\/?:@&=\\+\\\$,]|(?:\\xEF\\xBB\\xBF)/"; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A {@link Wordlift_Log_Service} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.6.0 |
|
| 37 | + * @access private |
|
| 38 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 39 | + */ |
|
| 40 | + private $log; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The global WordPress database connection. |
|
| 44 | + * |
|
| 45 | + * @since 3.6.0 |
|
| 46 | + * @access private |
|
| 47 | + * @var \wpdb $wpdb The global WordPress database connection. |
|
| 48 | + */ |
|
| 49 | + private $wpdb; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The {@link Wordlift_Uri_Service} singleton instance. |
|
| 53 | + * |
|
| 54 | + * @since 3.7.2 |
|
| 55 | + * @access private |
|
| 56 | + * @var \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance. |
|
| 57 | + */ |
|
| 58 | + private static $instance; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Create an instance of Wordlift_Uri_Service. |
|
| 62 | + * |
|
| 63 | + * @since 3.6.0 |
|
| 64 | + * |
|
| 65 | + * @param \wpdb $wpdb The global WordPress database connection. |
|
| 66 | + */ |
|
| 67 | + public function __construct( $wpdb ) { |
|
| 68 | + |
|
| 69 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Uri_Service' ); |
|
| 70 | + |
|
| 71 | + $this->wpdb = $wpdb; |
|
| 72 | + |
|
| 73 | + self::$instance = $this; |
|
| 74 | + |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get the {@link Wordlift_Uri_Service} singleton instance. |
|
| 79 | + * |
|
| 80 | + * @since 3.7.2 |
|
| 81 | + * @return \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance. |
|
| 82 | + */ |
|
| 83 | + public static function get_instance() { |
|
| 84 | + |
|
| 85 | + return self::$instance; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Delete all generated URIs from the database. |
|
| 90 | + * |
|
| 91 | + * @since 3.6.0 |
|
| 92 | + */ |
|
| 93 | + public function delete_all() { |
|
| 94 | + |
|
| 95 | + $this->log->trace( 'Going to delete all the `entity_url` post metas...' ); |
|
| 96 | + |
|
| 97 | + // Delete URIs associated with posts/entities. |
|
| 98 | + $this->wpdb->delete( $this->wpdb->postmeta, array( 'meta_key' => 'entity_url' ) ); |
|
| 99 | + |
|
| 100 | + $this->log->trace( 'Going to delete all the `_wl_uri` user metas...' ); |
|
| 101 | + |
|
| 102 | + // Delete URIs associated with authors. |
|
| 103 | + $this->wpdb->delete( $this->wpdb->usermeta, array( 'meta_key' => '_wl_uri' ) ); |
|
| 104 | + |
|
| 105 | + $this->log->debug( '`entity_url` post metas and `_wl_uri` user metas deleted.' ); |
|
| 106 | + |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Sanitizes an URI path by replacing the non allowed characters with an underscore. |
|
| 111 | + * |
|
| 112 | + * @since 3.7.2 |
|
| 113 | + * @uses sanitize_title() to manage not ASCII chars |
|
| 114 | + * |
|
| 115 | + * @see https://codex.wordpress.org/Function_Reference/sanitize_title |
|
| 116 | + * |
|
| 117 | + * @param string $path The path to sanitize. |
|
| 118 | + * @param string $char The replacement character (by default an underscore). |
|
| 119 | + * |
|
| 120 | + * @return string The sanitized path. |
|
| 121 | + */ |
|
| 122 | + public function sanitize_path( $path, $char = '_' ) { |
|
| 123 | + |
|
| 124 | + // Ensure the path is ASCII. |
|
| 125 | + // see https://github.com/insideout10/wordlift-plugin/issues/386 |
|
| 126 | 126 | // $path_ascii = mb_convert_encoding( $path, 'ASCII' ); |
| 127 | 127 | |
| 128 | - return sanitize_title( preg_replace( self::INVALID_CHARACTERS, $char, stripslashes( $path ) ) ); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Build an entity uri for a given title. The uri is composed using a given |
|
| 133 | - * post_type and a title. If already exists an entity e2 with a given uri a |
|
| 134 | - * numeric suffix is added. If a schema type is given entities with same label |
|
| 135 | - * and same type are overridden. |
|
| 136 | - * |
|
| 137 | - * @since 3.5.0 |
|
| 138 | - * |
|
| 139 | - * @param string $title A post title. |
|
| 140 | - * @param string $post_type A post type. Default value is 'entity' |
|
| 141 | - * @param string $schema_type A schema org type. |
|
| 142 | - * @param integer $increment_digit A digit used to call recursively the same function. |
|
| 143 | - * |
|
| 144 | - * @return string Returns an uri. |
|
| 145 | - */ |
|
| 146 | - public function build_uri( $title, $post_type, $schema_type = null, $increment_digit = 0 ) { |
|
| 147 | - |
|
| 148 | - // Get the entity slug suffix digit |
|
| 149 | - $suffix_digit = $increment_digit + 1; |
|
| 150 | - |
|
| 151 | - // Get a sanitized uri for a given title |
|
| 152 | - $entity_slug = ( 0 == $increment_digit ) ? |
|
| 153 | - wl_sanitize_uri_path( $title ) : |
|
| 154 | - wl_sanitize_uri_path( $title . '_' . $suffix_digit ); |
|
| 155 | - |
|
| 156 | - // Compose a candidate uri. |
|
| 157 | - $new_entity_uri = sprintf( '%s/%s/%s', |
|
| 158 | - wl_configuration_get_redlink_dataset_uri(), |
|
| 159 | - $post_type, |
|
| 160 | - $entity_slug |
|
| 161 | - ); |
|
| 162 | - |
|
| 163 | - $this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" ); |
|
| 164 | - |
|
| 165 | - global $wpdb; |
|
| 166 | - |
|
| 167 | - // Check if the candidated uri already is used |
|
| 168 | - $stmt = $wpdb->prepare( |
|
| 169 | - "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s LIMIT 1", |
|
| 170 | - WL_ENTITY_URL_META_NAME, |
|
| 171 | - $new_entity_uri |
|
| 172 | - ); |
|
| 173 | - |
|
| 174 | - // Perform the query |
|
| 175 | - $post_id = $wpdb->get_var( $stmt ); |
|
| 176 | - |
|
| 177 | - // If the post does not exist, then the new uri is returned |
|
| 178 | - if ( ! is_numeric( $post_id ) ) { |
|
| 179 | - $this->log->trace( "Going to return uri [ new_entity_uri :: $new_entity_uri ]" ); |
|
| 180 | - |
|
| 181 | - return $new_entity_uri; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // If schema_type is equal to schema org type of post x, then the new uri is returned |
|
| 185 | - $schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 186 | - |
|
| 187 | - // @todo: we shouldn't rely on css classes to take such decisions. |
|
| 188 | - if ( $schema_type === $schema_post_type['css_class'] ) { |
|
| 189 | - $this->log->trace( "An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]" ); |
|
| 190 | - |
|
| 191 | - return $new_entity_uri; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - // Otherwise the same function is called recursively |
|
| 195 | - return $this->build_uri( $title, $post_type, $schema_type, ++ $increment_digit ); |
|
| 196 | - } |
|
| 128 | + return sanitize_title( preg_replace( self::INVALID_CHARACTERS, $char, stripslashes( $path ) ) ); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Build an entity uri for a given title. The uri is composed using a given |
|
| 133 | + * post_type and a title. If already exists an entity e2 with a given uri a |
|
| 134 | + * numeric suffix is added. If a schema type is given entities with same label |
|
| 135 | + * and same type are overridden. |
|
| 136 | + * |
|
| 137 | + * @since 3.5.0 |
|
| 138 | + * |
|
| 139 | + * @param string $title A post title. |
|
| 140 | + * @param string $post_type A post type. Default value is 'entity' |
|
| 141 | + * @param string $schema_type A schema org type. |
|
| 142 | + * @param integer $increment_digit A digit used to call recursively the same function. |
|
| 143 | + * |
|
| 144 | + * @return string Returns an uri. |
|
| 145 | + */ |
|
| 146 | + public function build_uri( $title, $post_type, $schema_type = null, $increment_digit = 0 ) { |
|
| 147 | + |
|
| 148 | + // Get the entity slug suffix digit |
|
| 149 | + $suffix_digit = $increment_digit + 1; |
|
| 150 | + |
|
| 151 | + // Get a sanitized uri for a given title |
|
| 152 | + $entity_slug = ( 0 == $increment_digit ) ? |
|
| 153 | + wl_sanitize_uri_path( $title ) : |
|
| 154 | + wl_sanitize_uri_path( $title . '_' . $suffix_digit ); |
|
| 155 | + |
|
| 156 | + // Compose a candidate uri. |
|
| 157 | + $new_entity_uri = sprintf( '%s/%s/%s', |
|
| 158 | + wl_configuration_get_redlink_dataset_uri(), |
|
| 159 | + $post_type, |
|
| 160 | + $entity_slug |
|
| 161 | + ); |
|
| 162 | + |
|
| 163 | + $this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" ); |
|
| 164 | + |
|
| 165 | + global $wpdb; |
|
| 166 | + |
|
| 167 | + // Check if the candidated uri already is used |
|
| 168 | + $stmt = $wpdb->prepare( |
|
| 169 | + "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s LIMIT 1", |
|
| 170 | + WL_ENTITY_URL_META_NAME, |
|
| 171 | + $new_entity_uri |
|
| 172 | + ); |
|
| 173 | + |
|
| 174 | + // Perform the query |
|
| 175 | + $post_id = $wpdb->get_var( $stmt ); |
|
| 176 | + |
|
| 177 | + // If the post does not exist, then the new uri is returned |
|
| 178 | + if ( ! is_numeric( $post_id ) ) { |
|
| 179 | + $this->log->trace( "Going to return uri [ new_entity_uri :: $new_entity_uri ]" ); |
|
| 180 | + |
|
| 181 | + return $new_entity_uri; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // If schema_type is equal to schema org type of post x, then the new uri is returned |
|
| 185 | + $schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 186 | + |
|
| 187 | + // @todo: we shouldn't rely on css classes to take such decisions. |
|
| 188 | + if ( $schema_type === $schema_post_type['css_class'] ) { |
|
| 189 | + $this->log->trace( "An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]" ); |
|
| 190 | + |
|
| 191 | + return $new_entity_uri; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + // Otherwise the same function is called recursively |
|
| 195 | + return $this->build_uri( $title, $post_type, $schema_type, ++ $increment_digit ); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | 198 | } |
@@ -64,9 +64,9 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @param \wpdb $wpdb The global WordPress database connection. |
| 66 | 66 | */ |
| 67 | - public function __construct( $wpdb ) { |
|
| 67 | + public function __construct($wpdb) { |
|
| 68 | 68 | |
| 69 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Uri_Service' ); |
|
| 69 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Uri_Service'); |
|
| 70 | 70 | |
| 71 | 71 | $this->wpdb = $wpdb; |
| 72 | 72 | |
@@ -92,17 +92,17 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | public function delete_all() { |
| 94 | 94 | |
| 95 | - $this->log->trace( 'Going to delete all the `entity_url` post metas...' ); |
|
| 95 | + $this->log->trace('Going to delete all the `entity_url` post metas...'); |
|
| 96 | 96 | |
| 97 | 97 | // Delete URIs associated with posts/entities. |
| 98 | - $this->wpdb->delete( $this->wpdb->postmeta, array( 'meta_key' => 'entity_url' ) ); |
|
| 98 | + $this->wpdb->delete($this->wpdb->postmeta, array('meta_key' => 'entity_url')); |
|
| 99 | 99 | |
| 100 | - $this->log->trace( 'Going to delete all the `_wl_uri` user metas...' ); |
|
| 100 | + $this->log->trace('Going to delete all the `_wl_uri` user metas...'); |
|
| 101 | 101 | |
| 102 | 102 | // Delete URIs associated with authors. |
| 103 | - $this->wpdb->delete( $this->wpdb->usermeta, array( 'meta_key' => '_wl_uri' ) ); |
|
| 103 | + $this->wpdb->delete($this->wpdb->usermeta, array('meta_key' => '_wl_uri')); |
|
| 104 | 104 | |
| 105 | - $this->log->debug( '`entity_url` post metas and `_wl_uri` user metas deleted.' ); |
|
| 105 | + $this->log->debug('`entity_url` post metas and `_wl_uri` user metas deleted.'); |
|
| 106 | 106 | |
| 107 | 107 | } |
| 108 | 108 | |
@@ -119,13 +119,13 @@ discard block |
||
| 119 | 119 | * |
| 120 | 120 | * @return string The sanitized path. |
| 121 | 121 | */ |
| 122 | - public function sanitize_path( $path, $char = '_' ) { |
|
| 122 | + public function sanitize_path($path, $char = '_') { |
|
| 123 | 123 | |
| 124 | 124 | // Ensure the path is ASCII. |
| 125 | 125 | // see https://github.com/insideout10/wordlift-plugin/issues/386 |
| 126 | 126 | // $path_ascii = mb_convert_encoding( $path, 'ASCII' ); |
| 127 | 127 | |
| 128 | - return sanitize_title( preg_replace( self::INVALID_CHARACTERS, $char, stripslashes( $path ) ) ); |
|
| 128 | + return sanitize_title(preg_replace(self::INVALID_CHARACTERS, $char, stripslashes($path))); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
@@ -143,24 +143,23 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @return string Returns an uri. |
| 145 | 145 | */ |
| 146 | - public function build_uri( $title, $post_type, $schema_type = null, $increment_digit = 0 ) { |
|
| 146 | + public function build_uri($title, $post_type, $schema_type = null, $increment_digit = 0) { |
|
| 147 | 147 | |
| 148 | 148 | // Get the entity slug suffix digit |
| 149 | 149 | $suffix_digit = $increment_digit + 1; |
| 150 | 150 | |
| 151 | 151 | // Get a sanitized uri for a given title |
| 152 | - $entity_slug = ( 0 == $increment_digit ) ? |
|
| 153 | - wl_sanitize_uri_path( $title ) : |
|
| 154 | - wl_sanitize_uri_path( $title . '_' . $suffix_digit ); |
|
| 152 | + $entity_slug = (0 == $increment_digit) ? |
|
| 153 | + wl_sanitize_uri_path($title) : wl_sanitize_uri_path($title.'_'.$suffix_digit); |
|
| 155 | 154 | |
| 156 | 155 | // Compose a candidate uri. |
| 157 | - $new_entity_uri = sprintf( '%s/%s/%s', |
|
| 156 | + $new_entity_uri = sprintf('%s/%s/%s', |
|
| 158 | 157 | wl_configuration_get_redlink_dataset_uri(), |
| 159 | 158 | $post_type, |
| 160 | 159 | $entity_slug |
| 161 | 160 | ); |
| 162 | 161 | |
| 163 | - $this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" ); |
|
| 162 | + $this->log->trace("Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]"); |
|
| 164 | 163 | |
| 165 | 164 | global $wpdb; |
| 166 | 165 | |
@@ -172,27 +171,27 @@ discard block |
||
| 172 | 171 | ); |
| 173 | 172 | |
| 174 | 173 | // Perform the query |
| 175 | - $post_id = $wpdb->get_var( $stmt ); |
|
| 174 | + $post_id = $wpdb->get_var($stmt); |
|
| 176 | 175 | |
| 177 | 176 | // If the post does not exist, then the new uri is returned |
| 178 | - if ( ! is_numeric( $post_id ) ) { |
|
| 179 | - $this->log->trace( "Going to return uri [ new_entity_uri :: $new_entity_uri ]" ); |
|
| 177 | + if ( ! is_numeric($post_id)) { |
|
| 178 | + $this->log->trace("Going to return uri [ new_entity_uri :: $new_entity_uri ]"); |
|
| 180 | 179 | |
| 181 | 180 | return $new_entity_uri; |
| 182 | 181 | } |
| 183 | 182 | |
| 184 | 183 | // If schema_type is equal to schema org type of post x, then the new uri is returned |
| 185 | - $schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 184 | + $schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 186 | 185 | |
| 187 | 186 | // @todo: we shouldn't rely on css classes to take such decisions. |
| 188 | - if ( $schema_type === $schema_post_type['css_class'] ) { |
|
| 189 | - $this->log->trace( "An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]" ); |
|
| 187 | + if ($schema_type === $schema_post_type['css_class']) { |
|
| 188 | + $this->log->trace("An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]"); |
|
| 190 | 189 | |
| 191 | 190 | return $new_entity_uri; |
| 192 | 191 | } |
| 193 | 192 | |
| 194 | 193 | // Otherwise the same function is called recursively |
| 195 | - return $this->build_uri( $title, $post_type, $schema_type, ++ $increment_digit ); |
|
| 194 | + return $this->build_uri($title, $post_type, $schema_type, ++ $increment_digit); |
|
| 196 | 195 | } |
| 197 | 196 | |
| 198 | 197 | } |
@@ -18,151 +18,151 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Publisher_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Counts the number of potential publishers. |
|
| 23 | - * |
|
| 24 | - * @since 3.11.0 |
|
| 25 | - * |
|
| 26 | - * @return int The number of potential publishers. |
|
| 27 | - */ |
|
| 28 | - public function count() { |
|
| 29 | - |
|
| 30 | - // Search for entities which are either a Person |
|
| 31 | - // or Organization. |
|
| 32 | - |
|
| 33 | - // Get only the ids as all we need is the count. |
|
| 34 | - $entities = get_posts( array( |
|
| 35 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 36 | - 'post_status' => 'publish', |
|
| 37 | - 'posts_per_page' => - 1, |
|
| 38 | - 'tax_query' => array( |
|
| 39 | - array( |
|
| 40 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 41 | - 'field' => 'slug', |
|
| 42 | - 'terms' => array( 'organization', 'person' ), |
|
| 43 | - ), |
|
| 44 | - ), |
|
| 45 | - 'fields' => 'ids', |
|
| 46 | - ) ); |
|
| 47 | - |
|
| 48 | - // Finally return the count. |
|
| 49 | - return count( $entities ); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Search SQL filter for matching against post title only. |
|
| 54 | - * |
|
| 55 | - * @link http://wordpress.stackexchange.com/a/11826/1685 |
|
| 56 | - * |
|
| 57 | - * @since 3.15.0 |
|
| 58 | - * |
|
| 59 | - * @param string $search The search string. |
|
| 60 | - * @param WP_Query $wp_query The {@link WP_Query} instance. |
|
| 61 | - * |
|
| 62 | - * @return array|string An array of results. |
|
| 63 | - */ |
|
| 64 | - public function limit_search_to_title( $search, $wp_query ) { |
|
| 65 | - |
|
| 66 | - // Bail out if the search or the `search_terms` haven't been set. |
|
| 67 | - if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) { |
|
| 68 | - return $search; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - global $wpdb; |
|
| 72 | - |
|
| 73 | - $query_vars = $wp_query->query_vars; |
|
| 74 | - $percent = ! empty( $query_vars['exact'] ) ? '' : '%'; |
|
| 75 | - $search = array(); |
|
| 76 | - |
|
| 77 | - foreach ( (array) $query_vars['search_terms'] as $term ) { |
|
| 78 | - $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent ); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - if ( ! is_user_logged_in() ) { |
|
| 82 | - $search[] = "$wpdb->posts.post_password = ''"; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $search = ' AND ' . implode( ' AND ', $search ); |
|
| 86 | - |
|
| 87 | - return $search; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Query WP for potential publishers, i.e. {@link WP_Post}s which are associated` |
|
| 92 | - * with `wl_entity_type` (taxonomy) terms of `Organization` or `Person`. |
|
| 93 | - * |
|
| 94 | - * @since 3.11.0 |
|
| 95 | - * |
|
| 96 | - * @param string $filter The title filter. |
|
| 97 | - * |
|
| 98 | - * @return array An array of results in a select2 friendly format. |
|
| 99 | - */ |
|
| 100 | - public function query( $filter = '' ) { |
|
| 101 | - |
|
| 102 | - // Search for the filter in the titles only. |
|
| 103 | - add_filter( 'posts_search', array( |
|
| 104 | - $this, |
|
| 105 | - 'limit_search_to_title', |
|
| 106 | - ), 10, 2 ); |
|
| 107 | - |
|
| 108 | - /* |
|
| 21 | + /** |
|
| 22 | + * Counts the number of potential publishers. |
|
| 23 | + * |
|
| 24 | + * @since 3.11.0 |
|
| 25 | + * |
|
| 26 | + * @return int The number of potential publishers. |
|
| 27 | + */ |
|
| 28 | + public function count() { |
|
| 29 | + |
|
| 30 | + // Search for entities which are either a Person |
|
| 31 | + // or Organization. |
|
| 32 | + |
|
| 33 | + // Get only the ids as all we need is the count. |
|
| 34 | + $entities = get_posts( array( |
|
| 35 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 36 | + 'post_status' => 'publish', |
|
| 37 | + 'posts_per_page' => - 1, |
|
| 38 | + 'tax_query' => array( |
|
| 39 | + array( |
|
| 40 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 41 | + 'field' => 'slug', |
|
| 42 | + 'terms' => array( 'organization', 'person' ), |
|
| 43 | + ), |
|
| 44 | + ), |
|
| 45 | + 'fields' => 'ids', |
|
| 46 | + ) ); |
|
| 47 | + |
|
| 48 | + // Finally return the count. |
|
| 49 | + return count( $entities ); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Search SQL filter for matching against post title only. |
|
| 54 | + * |
|
| 55 | + * @link http://wordpress.stackexchange.com/a/11826/1685 |
|
| 56 | + * |
|
| 57 | + * @since 3.15.0 |
|
| 58 | + * |
|
| 59 | + * @param string $search The search string. |
|
| 60 | + * @param WP_Query $wp_query The {@link WP_Query} instance. |
|
| 61 | + * |
|
| 62 | + * @return array|string An array of results. |
|
| 63 | + */ |
|
| 64 | + public function limit_search_to_title( $search, $wp_query ) { |
|
| 65 | + |
|
| 66 | + // Bail out if the search or the `search_terms` haven't been set. |
|
| 67 | + if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) { |
|
| 68 | + return $search; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + global $wpdb; |
|
| 72 | + |
|
| 73 | + $query_vars = $wp_query->query_vars; |
|
| 74 | + $percent = ! empty( $query_vars['exact'] ) ? '' : '%'; |
|
| 75 | + $search = array(); |
|
| 76 | + |
|
| 77 | + foreach ( (array) $query_vars['search_terms'] as $term ) { |
|
| 78 | + $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent ); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + if ( ! is_user_logged_in() ) { |
|
| 82 | + $search[] = "$wpdb->posts.post_password = ''"; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $search = ' AND ' . implode( ' AND ', $search ); |
|
| 86 | + |
|
| 87 | + return $search; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Query WP for potential publishers, i.e. {@link WP_Post}s which are associated` |
|
| 92 | + * with `wl_entity_type` (taxonomy) terms of `Organization` or `Person`. |
|
| 93 | + * |
|
| 94 | + * @since 3.11.0 |
|
| 95 | + * |
|
| 96 | + * @param string $filter The title filter. |
|
| 97 | + * |
|
| 98 | + * @return array An array of results in a select2 friendly format. |
|
| 99 | + */ |
|
| 100 | + public function query( $filter = '' ) { |
|
| 101 | + |
|
| 102 | + // Search for the filter in the titles only. |
|
| 103 | + add_filter( 'posts_search', array( |
|
| 104 | + $this, |
|
| 105 | + 'limit_search_to_title', |
|
| 106 | + ), 10, 2 ); |
|
| 107 | + |
|
| 108 | + /* |
|
| 109 | 109 | * Search for entities which are either a Person |
| 110 | 110 | * or Organization. Sort the results by title in ascending order. |
| 111 | 111 | */ |
| 112 | - $entities = get_posts( array( |
|
| 113 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 114 | - 'post_status' => 'publish', |
|
| 115 | - 'posts_per_page' => - 1, |
|
| 116 | - 'tax_query' => array( |
|
| 117 | - array( |
|
| 118 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 119 | - 'field' => 'slug', |
|
| 120 | - 'terms' => array( 'organization', 'person' ), |
|
| 121 | - ), |
|
| 122 | - ), |
|
| 123 | - 's' => $filter, |
|
| 124 | - 'orderby' => 'title', |
|
| 125 | - 'order' => 'ASC', |
|
| 126 | - ) ); |
|
| 127 | - |
|
| 128 | - // Remove the search filter added before the query. |
|
| 129 | - remove_filter( 'posts_search', array( |
|
| 130 | - $this, |
|
| 131 | - 'limit_search_to_title', |
|
| 132 | - ), 10, 2 ); |
|
| 133 | - |
|
| 134 | - // Set a reference to ourselves to pass to the closure. |
|
| 135 | - $publisher_service = $this; |
|
| 136 | - |
|
| 137 | - // Map the results in a `Select2` compatible array. |
|
| 138 | - return array_map( function ( $entity ) use ( $publisher_service ) { |
|
| 139 | - $type = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 140 | - $thumb_id = get_post_thumbnail_id( $entity->ID ); |
|
| 141 | - |
|
| 142 | - return array( |
|
| 143 | - 'id' => $entity->ID, |
|
| 144 | - 'text' => $entity->post_title, |
|
| 145 | - 'type' => $type[0]->name, |
|
| 146 | - 'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ), |
|
| 147 | - ); |
|
| 148 | - }, $entities ); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Get the thumbnail's URL. |
|
| 153 | - * |
|
| 154 | - * @since 3.11.0 |
|
| 155 | - * |
|
| 156 | - * @param int $attachment_id The attachment id. |
|
| 157 | - * @param string $size The attachment size (default = 'thumbnail'). |
|
| 158 | - * |
|
| 159 | - * @return string|bool The image URL or false if not found. |
|
| 160 | - */ |
|
| 161 | - public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) { |
|
| 162 | - |
|
| 163 | - $image = wp_get_attachment_image_src( $attachment_id, $size ); |
|
| 164 | - |
|
| 165 | - return isset( $image['0'] ) ? $image['0'] : false; |
|
| 166 | - } |
|
| 112 | + $entities = get_posts( array( |
|
| 113 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 114 | + 'post_status' => 'publish', |
|
| 115 | + 'posts_per_page' => - 1, |
|
| 116 | + 'tax_query' => array( |
|
| 117 | + array( |
|
| 118 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 119 | + 'field' => 'slug', |
|
| 120 | + 'terms' => array( 'organization', 'person' ), |
|
| 121 | + ), |
|
| 122 | + ), |
|
| 123 | + 's' => $filter, |
|
| 124 | + 'orderby' => 'title', |
|
| 125 | + 'order' => 'ASC', |
|
| 126 | + ) ); |
|
| 127 | + |
|
| 128 | + // Remove the search filter added before the query. |
|
| 129 | + remove_filter( 'posts_search', array( |
|
| 130 | + $this, |
|
| 131 | + 'limit_search_to_title', |
|
| 132 | + ), 10, 2 ); |
|
| 133 | + |
|
| 134 | + // Set a reference to ourselves to pass to the closure. |
|
| 135 | + $publisher_service = $this; |
|
| 136 | + |
|
| 137 | + // Map the results in a `Select2` compatible array. |
|
| 138 | + return array_map( function ( $entity ) use ( $publisher_service ) { |
|
| 139 | + $type = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 140 | + $thumb_id = get_post_thumbnail_id( $entity->ID ); |
|
| 141 | + |
|
| 142 | + return array( |
|
| 143 | + 'id' => $entity->ID, |
|
| 144 | + 'text' => $entity->post_title, |
|
| 145 | + 'type' => $type[0]->name, |
|
| 146 | + 'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ), |
|
| 147 | + ); |
|
| 148 | + }, $entities ); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Get the thumbnail's URL. |
|
| 153 | + * |
|
| 154 | + * @since 3.11.0 |
|
| 155 | + * |
|
| 156 | + * @param int $attachment_id The attachment id. |
|
| 157 | + * @param string $size The attachment size (default = 'thumbnail'). |
|
| 158 | + * |
|
| 159 | + * @return string|bool The image URL or false if not found. |
|
| 160 | + */ |
|
| 161 | + public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) { |
|
| 162 | + |
|
| 163 | + $image = wp_get_attachment_image_src( $attachment_id, $size ); |
|
| 164 | + |
|
| 165 | + return isset( $image['0'] ) ? $image['0'] : false; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | 168 | } |
@@ -31,22 +31,22 @@ discard block |
||
| 31 | 31 | // or Organization. |
| 32 | 32 | |
| 33 | 33 | // Get only the ids as all we need is the count. |
| 34 | - $entities = get_posts( array( |
|
| 34 | + $entities = get_posts(array( |
|
| 35 | 35 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
| 36 | 36 | 'post_status' => 'publish', |
| 37 | - 'posts_per_page' => - 1, |
|
| 37 | + 'posts_per_page' => -1, |
|
| 38 | 38 | 'tax_query' => array( |
| 39 | 39 | array( |
| 40 | 40 | 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 41 | 41 | 'field' => 'slug', |
| 42 | - 'terms' => array( 'organization', 'person' ), |
|
| 42 | + 'terms' => array('organization', 'person'), |
|
| 43 | 43 | ), |
| 44 | 44 | ), |
| 45 | 45 | 'fields' => 'ids', |
| 46 | - ) ); |
|
| 46 | + )); |
|
| 47 | 47 | |
| 48 | 48 | // Finally return the count. |
| 49 | - return count( $entities ); |
|
| 49 | + return count($entities); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
@@ -61,28 +61,28 @@ discard block |
||
| 61 | 61 | * |
| 62 | 62 | * @return array|string An array of results. |
| 63 | 63 | */ |
| 64 | - public function limit_search_to_title( $search, $wp_query ) { |
|
| 64 | + public function limit_search_to_title($search, $wp_query) { |
|
| 65 | 65 | |
| 66 | 66 | // Bail out if the search or the `search_terms` haven't been set. |
| 67 | - if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) { |
|
| 67 | + if (empty($search) || empty($wp_query->query_vars['search_terms'])) { |
|
| 68 | 68 | return $search; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | global $wpdb; |
| 72 | 72 | |
| 73 | 73 | $query_vars = $wp_query->query_vars; |
| 74 | - $percent = ! empty( $query_vars['exact'] ) ? '' : '%'; |
|
| 74 | + $percent = ! empty($query_vars['exact']) ? '' : '%'; |
|
| 75 | 75 | $search = array(); |
| 76 | 76 | |
| 77 | - foreach ( (array) $query_vars['search_terms'] as $term ) { |
|
| 78 | - $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent ); |
|
| 77 | + foreach ((array) $query_vars['search_terms'] as $term) { |
|
| 78 | + $search[] = $wpdb->prepare("$wpdb->posts.post_title LIKE %s", $percent.$wpdb->esc_like($term).$percent); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - if ( ! is_user_logged_in() ) { |
|
| 81 | + if ( ! is_user_logged_in()) { |
|
| 82 | 82 | $search[] = "$wpdb->posts.post_password = ''"; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - $search = ' AND ' . implode( ' AND ', $search ); |
|
| 85 | + $search = ' AND '.implode(' AND ', $search); |
|
| 86 | 86 | |
| 87 | 87 | return $search; |
| 88 | 88 | } |
@@ -97,55 +97,55 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * @return array An array of results in a select2 friendly format. |
| 99 | 99 | */ |
| 100 | - public function query( $filter = '' ) { |
|
| 100 | + public function query($filter = '') { |
|
| 101 | 101 | |
| 102 | 102 | // Search for the filter in the titles only. |
| 103 | - add_filter( 'posts_search', array( |
|
| 103 | + add_filter('posts_search', array( |
|
| 104 | 104 | $this, |
| 105 | 105 | 'limit_search_to_title', |
| 106 | - ), 10, 2 ); |
|
| 106 | + ), 10, 2); |
|
| 107 | 107 | |
| 108 | 108 | /* |
| 109 | 109 | * Search for entities which are either a Person |
| 110 | 110 | * or Organization. Sort the results by title in ascending order. |
| 111 | 111 | */ |
| 112 | - $entities = get_posts( array( |
|
| 112 | + $entities = get_posts(array( |
|
| 113 | 113 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
| 114 | 114 | 'post_status' => 'publish', |
| 115 | - 'posts_per_page' => - 1, |
|
| 115 | + 'posts_per_page' => -1, |
|
| 116 | 116 | 'tax_query' => array( |
| 117 | 117 | array( |
| 118 | 118 | 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 119 | 119 | 'field' => 'slug', |
| 120 | - 'terms' => array( 'organization', 'person' ), |
|
| 120 | + 'terms' => array('organization', 'person'), |
|
| 121 | 121 | ), |
| 122 | 122 | ), |
| 123 | 123 | 's' => $filter, |
| 124 | 124 | 'orderby' => 'title', |
| 125 | 125 | 'order' => 'ASC', |
| 126 | - ) ); |
|
| 126 | + )); |
|
| 127 | 127 | |
| 128 | 128 | // Remove the search filter added before the query. |
| 129 | - remove_filter( 'posts_search', array( |
|
| 129 | + remove_filter('posts_search', array( |
|
| 130 | 130 | $this, |
| 131 | 131 | 'limit_search_to_title', |
| 132 | - ), 10, 2 ); |
|
| 132 | + ), 10, 2); |
|
| 133 | 133 | |
| 134 | 134 | // Set a reference to ourselves to pass to the closure. |
| 135 | 135 | $publisher_service = $this; |
| 136 | 136 | |
| 137 | 137 | // Map the results in a `Select2` compatible array. |
| 138 | - return array_map( function ( $entity ) use ( $publisher_service ) { |
|
| 139 | - $type = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 140 | - $thumb_id = get_post_thumbnail_id( $entity->ID ); |
|
| 138 | + return array_map(function($entity) use ($publisher_service) { |
|
| 139 | + $type = wp_get_post_terms($entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 140 | + $thumb_id = get_post_thumbnail_id($entity->ID); |
|
| 141 | 141 | |
| 142 | 142 | return array( |
| 143 | 143 | 'id' => $entity->ID, |
| 144 | 144 | 'text' => $entity->post_title, |
| 145 | 145 | 'type' => $type[0]->name, |
| 146 | - 'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ), |
|
| 146 | + 'thumbnail_url' => $publisher_service->get_attachment_image_url($thumb_id), |
|
| 147 | 147 | ); |
| 148 | - }, $entities ); |
|
| 148 | + }, $entities); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | /** |
@@ -158,11 +158,11 @@ discard block |
||
| 158 | 158 | * |
| 159 | 159 | * @return string|bool The image URL or false if not found. |
| 160 | 160 | */ |
| 161 | - public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) { |
|
| 161 | + public function get_attachment_image_url($attachment_id, $size = 'thumbnail') { |
|
| 162 | 162 | |
| 163 | - $image = wp_get_attachment_image_src( $attachment_id, $size ); |
|
| 163 | + $image = wp_get_attachment_image_src($attachment_id, $size); |
|
| 164 | 164 | |
| 165 | - return isset( $image['0'] ) ? $image['0'] : false; |
|
| 165 | + return isset($image['0']) ? $image['0'] : false; |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | } |
@@ -17,52 +17,52 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Entity_Page_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Set the entity post types as one to be included in archive pages. |
|
| 22 | - * |
|
| 23 | - * In order to have entities show up in standard WP categories (Posts categories) |
|
| 24 | - * we configure the `entity` post type, but we also need to alter the main |
|
| 25 | - * WP query (which by default queries posts only) to include the `entities`. |
|
| 26 | - * |
|
| 27 | - * @since 3.12.0 |
|
| 28 | - * |
|
| 29 | - * @param WP_Query $query WP's {@link WP_Query} instance. |
|
| 30 | - */ |
|
| 31 | - public function pre_get_posts( $query ) { |
|
| 32 | - |
|
| 33 | - // Only for the main query, avoid problems with widgets and what not. |
|
| 34 | - if ( ! $query->is_main_query() ) { |
|
| 35 | - return; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - // We don't want to alter the query if we're in the admin UI, if this is |
|
| 39 | - // not a entity type achieve query, or if the `suppress_filters` is set. |
|
| 40 | - // |
|
| 41 | - // Note that it is unlikely for `suppress_filter` to be set on the front |
|
| 42 | - // end, but let's be safe if it is set the calling code assumes no |
|
| 43 | - // modifications of queries. |
|
| 44 | - |
|
| 45 | - // Ignore admin side request, requests for which filters should be |
|
| 46 | - // suppressed, and when we are not on a entity type archive page. |
|
| 47 | - if ( is_admin() || |
|
| 48 | - ! is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) || |
|
| 49 | - ! empty( $query->query_vars['suppress_filters'] ) |
|
| 50 | - ) { |
|
| 51 | - return; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - // Events should be sorted by start date in descending order. |
|
| 55 | - if ( is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ) { |
|
| 56 | - |
|
| 57 | - // Update the query to use the start time meta and desc order. |
|
| 58 | - $meta_query[] = array( |
|
| 59 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 60 | - ); |
|
| 61 | - $query->set( 'meta_query', $meta_query ); |
|
| 62 | - $query->set( 'orderby', 'meta_value' ); |
|
| 63 | - $query->set( 'order', 'DESC' ); |
|
| 64 | - } else { |
|
| 65 | - /* |
|
| 20 | + /** |
|
| 21 | + * Set the entity post types as one to be included in archive pages. |
|
| 22 | + * |
|
| 23 | + * In order to have entities show up in standard WP categories (Posts categories) |
|
| 24 | + * we configure the `entity` post type, but we also need to alter the main |
|
| 25 | + * WP query (which by default queries posts only) to include the `entities`. |
|
| 26 | + * |
|
| 27 | + * @since 3.12.0 |
|
| 28 | + * |
|
| 29 | + * @param WP_Query $query WP's {@link WP_Query} instance. |
|
| 30 | + */ |
|
| 31 | + public function pre_get_posts( $query ) { |
|
| 32 | + |
|
| 33 | + // Only for the main query, avoid problems with widgets and what not. |
|
| 34 | + if ( ! $query->is_main_query() ) { |
|
| 35 | + return; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + // We don't want to alter the query if we're in the admin UI, if this is |
|
| 39 | + // not a entity type achieve query, or if the `suppress_filters` is set. |
|
| 40 | + // |
|
| 41 | + // Note that it is unlikely for `suppress_filter` to be set on the front |
|
| 42 | + // end, but let's be safe if it is set the calling code assumes no |
|
| 43 | + // modifications of queries. |
|
| 44 | + |
|
| 45 | + // Ignore admin side request, requests for which filters should be |
|
| 46 | + // suppressed, and when we are not on a entity type archive page. |
|
| 47 | + if ( is_admin() || |
|
| 48 | + ! is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) || |
|
| 49 | + ! empty( $query->query_vars['suppress_filters'] ) |
|
| 50 | + ) { |
|
| 51 | + return; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + // Events should be sorted by start date in descending order. |
|
| 55 | + if ( is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ) { |
|
| 56 | + |
|
| 57 | + // Update the query to use the start time meta and desc order. |
|
| 58 | + $meta_query[] = array( |
|
| 59 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 60 | + ); |
|
| 61 | + $query->set( 'meta_query', $meta_query ); |
|
| 62 | + $query->set( 'orderby', 'meta_value' ); |
|
| 63 | + $query->set( 'order', 'DESC' ); |
|
| 64 | + } else { |
|
| 65 | + /* |
|
| 66 | 66 | * All other entity types should be sorted by their connectivity. |
| 67 | 67 | * For this we need to query the relationship table which has |
| 68 | 68 | * to be done by manipulating the SQL generated for the query. |
@@ -70,73 +70,73 @@ discard block |
||
| 70 | 70 | * additional filters to handle it. |
| 71 | 71 | */ |
| 72 | 72 | |
| 73 | - add_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 74 | - add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 75 | - add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Filter handler that sets the join part of a query to include the |
|
| 81 | - * relationship table to be able to use it in the sorting. |
|
| 82 | - * |
|
| 83 | - * @since 3.15.0 |
|
| 84 | - * |
|
| 85 | - * @param string $join_statement The join part of the SQL statement which is used for the query. |
|
| 86 | - * |
|
| 87 | - * @return string An join SQL which add the relationships table to the join. |
|
| 88 | - */ |
|
| 89 | - public function posts_join( $join_statement ) { |
|
| 90 | - |
|
| 91 | - global $wpdb; |
|
| 92 | - |
|
| 93 | - $join_statement .= " LEFT JOIN {$wpdb->prefix}wl_relation_instances ri " |
|
| 94 | - . " ON (ri.object_id = {$wpdb->posts}.ID)"; |
|
| 95 | - |
|
| 96 | - // Remove to make sure it will not run agan in other context. |
|
| 97 | - remove_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 98 | - |
|
| 99 | - return $join_statement; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Filter handler that sets the groupby part of a query to include the |
|
| 104 | - * relationship table to be able to use it in the sorting. |
|
| 105 | - * |
|
| 106 | - * @since 3.15.0 |
|
| 107 | - * |
|
| 108 | - * @param string $groupby_statement The groupby part of the SQL statement which is used for the query. |
|
| 109 | - * |
|
| 110 | - * @return string A groupby SQL which add the relationships table to the join. |
|
| 111 | - */ |
|
| 112 | - public function posts_groupby( $groupby_statement ) { |
|
| 113 | - |
|
| 114 | - $groupby_statement = 'ri.object_id, ' . $groupby_statement; |
|
| 115 | - |
|
| 116 | - // Remove to make sure it will not run agan in other context. |
|
| 117 | - remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 118 | - |
|
| 119 | - return $groupby_statement; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Filter handler that sets the orderby part of a query to sort by number of |
|
| 124 | - * relationships. |
|
| 125 | - * |
|
| 126 | - * @since 3.15.0 |
|
| 127 | - * |
|
| 128 | - * @param string $orderby_statement The orderby part of the SQL statement which is used for the query. |
|
| 129 | - * |
|
| 130 | - * @return string An orderby SQL which sorts by the number of relationships |
|
| 131 | - */ |
|
| 132 | - public function posts_orderby( $orderby_statement ) { |
|
| 133 | - |
|
| 134 | - $orderby_statement = 'COUNT( ri.object_id ) DESC, ' . $orderby_statement; |
|
| 135 | - |
|
| 136 | - // Remove to make sure it will not run agan in other context. |
|
| 137 | - remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 138 | - |
|
| 139 | - return $orderby_statement; |
|
| 140 | - } |
|
| 73 | + add_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 74 | + add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 75 | + add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Filter handler that sets the join part of a query to include the |
|
| 81 | + * relationship table to be able to use it in the sorting. |
|
| 82 | + * |
|
| 83 | + * @since 3.15.0 |
|
| 84 | + * |
|
| 85 | + * @param string $join_statement The join part of the SQL statement which is used for the query. |
|
| 86 | + * |
|
| 87 | + * @return string An join SQL which add the relationships table to the join. |
|
| 88 | + */ |
|
| 89 | + public function posts_join( $join_statement ) { |
|
| 90 | + |
|
| 91 | + global $wpdb; |
|
| 92 | + |
|
| 93 | + $join_statement .= " LEFT JOIN {$wpdb->prefix}wl_relation_instances ri " |
|
| 94 | + . " ON (ri.object_id = {$wpdb->posts}.ID)"; |
|
| 95 | + |
|
| 96 | + // Remove to make sure it will not run agan in other context. |
|
| 97 | + remove_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 98 | + |
|
| 99 | + return $join_statement; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Filter handler that sets the groupby part of a query to include the |
|
| 104 | + * relationship table to be able to use it in the sorting. |
|
| 105 | + * |
|
| 106 | + * @since 3.15.0 |
|
| 107 | + * |
|
| 108 | + * @param string $groupby_statement The groupby part of the SQL statement which is used for the query. |
|
| 109 | + * |
|
| 110 | + * @return string A groupby SQL which add the relationships table to the join. |
|
| 111 | + */ |
|
| 112 | + public function posts_groupby( $groupby_statement ) { |
|
| 113 | + |
|
| 114 | + $groupby_statement = 'ri.object_id, ' . $groupby_statement; |
|
| 115 | + |
|
| 116 | + // Remove to make sure it will not run agan in other context. |
|
| 117 | + remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 118 | + |
|
| 119 | + return $groupby_statement; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Filter handler that sets the orderby part of a query to sort by number of |
|
| 124 | + * relationships. |
|
| 125 | + * |
|
| 126 | + * @since 3.15.0 |
|
| 127 | + * |
|
| 128 | + * @param string $orderby_statement The orderby part of the SQL statement which is used for the query. |
|
| 129 | + * |
|
| 130 | + * @return string An orderby SQL which sorts by the number of relationships |
|
| 131 | + */ |
|
| 132 | + public function posts_orderby( $orderby_statement ) { |
|
| 133 | + |
|
| 134 | + $orderby_statement = 'COUNT( ri.object_id ) DESC, ' . $orderby_statement; |
|
| 135 | + |
|
| 136 | + // Remove to make sure it will not run agan in other context. |
|
| 137 | + remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 138 | + |
|
| 139 | + return $orderby_statement; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | 142 | } |
@@ -28,10 +28,10 @@ discard block |
||
| 28 | 28 | * |
| 29 | 29 | * @param WP_Query $query WP's {@link WP_Query} instance. |
| 30 | 30 | */ |
| 31 | - public function pre_get_posts( $query ) { |
|
| 31 | + public function pre_get_posts($query) { |
|
| 32 | 32 | |
| 33 | 33 | // Only for the main query, avoid problems with widgets and what not. |
| 34 | - if ( ! $query->is_main_query() ) { |
|
| 34 | + if ( ! $query->is_main_query()) { |
|
| 35 | 35 | return; |
| 36 | 36 | } |
| 37 | 37 | |
@@ -44,23 +44,23 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | // Ignore admin side request, requests for which filters should be |
| 46 | 46 | // suppressed, and when we are not on a entity type archive page. |
| 47 | - if ( is_admin() || |
|
| 48 | - ! is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) || |
|
| 49 | - ! empty( $query->query_vars['suppress_filters'] ) |
|
| 47 | + if (is_admin() || |
|
| 48 | + ! is_tax(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME) || |
|
| 49 | + ! empty($query->query_vars['suppress_filters']) |
|
| 50 | 50 | ) { |
| 51 | 51 | return; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | // Events should be sorted by start date in descending order. |
| 55 | - if ( is_tax( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ) { |
|
| 55 | + if (is_tax(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, 'event')) { |
|
| 56 | 56 | |
| 57 | 57 | // Update the query to use the start time meta and desc order. |
| 58 | 58 | $meta_query[] = array( |
| 59 | 59 | 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
| 60 | 60 | ); |
| 61 | - $query->set( 'meta_query', $meta_query ); |
|
| 62 | - $query->set( 'orderby', 'meta_value' ); |
|
| 63 | - $query->set( 'order', 'DESC' ); |
|
| 61 | + $query->set('meta_query', $meta_query); |
|
| 62 | + $query->set('orderby', 'meta_value'); |
|
| 63 | + $query->set('order', 'DESC'); |
|
| 64 | 64 | } else { |
| 65 | 65 | /* |
| 66 | 66 | * All other entity types should be sorted by their connectivity. |
@@ -70,9 +70,9 @@ discard block |
||
| 70 | 70 | * additional filters to handle it. |
| 71 | 71 | */ |
| 72 | 72 | |
| 73 | - add_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 74 | - add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 75 | - add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 73 | + add_filter('posts_join', array($this, 'posts_join')); |
|
| 74 | + add_filter('posts_groupby', array($this, 'posts_groupby')); |
|
| 75 | + add_filter('posts_orderby', array($this, 'posts_orderby')); |
|
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | 78 | |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | * |
| 87 | 87 | * @return string An join SQL which add the relationships table to the join. |
| 88 | 88 | */ |
| 89 | - public function posts_join( $join_statement ) { |
|
| 89 | + public function posts_join($join_statement) { |
|
| 90 | 90 | |
| 91 | 91 | global $wpdb; |
| 92 | 92 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | . " ON (ri.object_id = {$wpdb->posts}.ID)"; |
| 95 | 95 | |
| 96 | 96 | // Remove to make sure it will not run agan in other context. |
| 97 | - remove_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 97 | + remove_filter('posts_join', array($this, 'posts_join')); |
|
| 98 | 98 | |
| 99 | 99 | return $join_statement; |
| 100 | 100 | } |
@@ -109,12 +109,12 @@ discard block |
||
| 109 | 109 | * |
| 110 | 110 | * @return string A groupby SQL which add the relationships table to the join. |
| 111 | 111 | */ |
| 112 | - public function posts_groupby( $groupby_statement ) { |
|
| 112 | + public function posts_groupby($groupby_statement) { |
|
| 113 | 113 | |
| 114 | - $groupby_statement = 'ri.object_id, ' . $groupby_statement; |
|
| 114 | + $groupby_statement = 'ri.object_id, '.$groupby_statement; |
|
| 115 | 115 | |
| 116 | 116 | // Remove to make sure it will not run agan in other context. |
| 117 | - remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 117 | + remove_filter('posts_groupby', array($this, 'posts_groupby')); |
|
| 118 | 118 | |
| 119 | 119 | return $groupby_statement; |
| 120 | 120 | } |
@@ -129,12 +129,12 @@ discard block |
||
| 129 | 129 | * |
| 130 | 130 | * @return string An orderby SQL which sorts by the number of relationships |
| 131 | 131 | */ |
| 132 | - public function posts_orderby( $orderby_statement ) { |
|
| 132 | + public function posts_orderby($orderby_statement) { |
|
| 133 | 133 | |
| 134 | - $orderby_statement = 'COUNT( ri.object_id ) DESC, ' . $orderby_statement; |
|
| 134 | + $orderby_statement = 'COUNT( ri.object_id ) DESC, '.$orderby_statement; |
|
| 135 | 135 | |
| 136 | 136 | // Remove to make sure it will not run agan in other context. |
| 137 | - remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 137 | + remove_filter('posts_orderby', array($this, 'posts_orderby')); |
|
| 138 | 138 | |
| 139 | 139 | return $orderby_statement; |
| 140 | 140 | } |