@@ -94,7 +94,7 @@ |
||
| 94 | 94 | * |
| 95 | 95 | * @param string $content_type File content type. |
| 96 | 96 | * |
| 97 | - * @return string|bool The file extension on success and |
|
| 97 | + * @return string|false The file extension on success and |
|
| 98 | 98 | * false on fail or if the content type is not supported. |
| 99 | 99 | * @since 3.18.0 |
| 100 | 100 | * |
@@ -8,140 +8,140 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Wordlift_Remote_Image_Service { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Save the image with the specified URL locally. |
|
| 13 | - * |
|
| 14 | - * @param string $url The image remote URL. |
|
| 15 | - * |
|
| 16 | - * @return array|false An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
| 17 | - * url, *content_type*: the image content type) or false on error. |
|
| 18 | - * @since 3.18.0 |
|
| 19 | - * |
|
| 20 | - */ |
|
| 21 | - public static function save_from_url( $url ) { |
|
| 22 | - |
|
| 23 | - // Required for REST API calls |
|
| 24 | - if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 26 | - } |
|
| 27 | - // Load `WP_Filesystem`. |
|
| 28 | - WP_Filesystem(); |
|
| 29 | - global $wp_filesystem; |
|
| 30 | - |
|
| 31 | - // Parse the url. |
|
| 32 | - $parts = wp_parse_url( $url ); |
|
| 33 | - |
|
| 34 | - // Get the bare filename (filename w/o the extension). |
|
| 35 | - $basename = str_replace( DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 36 | - pathinfo( $parts['path'], PATHINFO_FILENAME ) |
|
| 37 | - ) ); |
|
| 38 | - |
|
| 39 | - // Get the base dir. |
|
| 40 | - $wp_upload_dir = wp_upload_dir(); |
|
| 41 | - |
|
| 42 | - // Set the upload directory and URL. |
|
| 43 | - $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 44 | - $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 45 | - |
|
| 46 | - // Get the full path to the local filename. |
|
| 47 | - $image_full_path = $upload_dir . '/' . $basename; |
|
| 48 | - $image_full_url = $upload_url . '/' . $basename; |
|
| 49 | - |
|
| 50 | - // Create custom directory and bail on failure. |
|
| 51 | - if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 52 | - wl_write_log( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 53 | - |
|
| 54 | - return false; |
|
| 55 | - }; |
|
| 56 | - |
|
| 57 | - $response = self::get_response( $url ); |
|
| 58 | - |
|
| 59 | - // Bail if the response is not set. |
|
| 60 | - if ( false === $response ) { |
|
| 61 | - wl_write_log( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 62 | - |
|
| 63 | - return false; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // Get the content type of response. |
|
| 67 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 68 | - |
|
| 69 | - // Get the file extension. |
|
| 70 | - $extension = self::get_extension_from_content_type( $content_type ); |
|
| 71 | - |
|
| 72 | - // Bail if the content type is not supported. |
|
| 73 | - if ( empty( $extension ) ) { |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - // Complete the local filename. |
|
| 78 | - $image_full_path .= $extension; |
|
| 79 | - $image_full_url .= $extension; |
|
| 80 | - |
|
| 81 | - // Store the data locally. |
|
| 82 | - $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 83 | - |
|
| 84 | - // Return the path. |
|
| 85 | - return array( |
|
| 86 | - 'path' => $image_full_path, |
|
| 87 | - 'url' => $image_full_url, |
|
| 88 | - 'content_type' => $content_type, |
|
| 89 | - ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Returns the file extension using the content type. |
|
| 94 | - * |
|
| 95 | - * @param string $content_type File content type. |
|
| 96 | - * |
|
| 97 | - * @return string|bool The file extension on success and |
|
| 98 | - * false on fail or if the content type is not supported. |
|
| 99 | - * @since 3.18.0 |
|
| 100 | - * |
|
| 101 | - */ |
|
| 102 | - private static function get_extension_from_content_type( $content_type ) { |
|
| 103 | - |
|
| 104 | - // Return the extension if match. |
|
| 105 | - switch ( $content_type ) { |
|
| 106 | - case 'image/jpeg': |
|
| 107 | - case 'image/jpg': |
|
| 108 | - return '.jpg'; |
|
| 109 | - case 'image/gif': |
|
| 110 | - return '.gif'; |
|
| 111 | - case 'image/png': |
|
| 112 | - return '.png'; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - // Otherwise return false. |
|
| 116 | - return false; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Retrieve the response from url and sets the response. |
|
| 121 | - * |
|
| 122 | - * @param string $url The url to retrieve. |
|
| 123 | - * |
|
| 124 | - * @return false|array True on success and false on failure. |
|
| 125 | - * @since 3.18.0 |
|
| 126 | - * |
|
| 127 | - */ |
|
| 128 | - private static function get_response( $url ) { |
|
| 129 | - // Request the remote file. |
|
| 130 | - $response = wp_remote_get( $url ); |
|
| 131 | - |
|
| 132 | - // Bail out if the response is not ok. |
|
| 133 | - if ( |
|
| 134 | - is_wp_error( $response ) |
|
| 135 | - || 200 !== (int) $response['response']['code'] |
|
| 136 | - || ! isset( $response['body'] ) |
|
| 137 | - ) { |
|
| 138 | - wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 139 | - |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Set the response. |
|
| 144 | - return $response; |
|
| 145 | - } |
|
| 11 | + /** |
|
| 12 | + * Save the image with the specified URL locally. |
|
| 13 | + * |
|
| 14 | + * @param string $url The image remote URL. |
|
| 15 | + * |
|
| 16 | + * @return array|false An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
| 17 | + * url, *content_type*: the image content type) or false on error. |
|
| 18 | + * @since 3.18.0 |
|
| 19 | + * |
|
| 20 | + */ |
|
| 21 | + public static function save_from_url( $url ) { |
|
| 22 | + |
|
| 23 | + // Required for REST API calls |
|
| 24 | + if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | + require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 26 | + } |
|
| 27 | + // Load `WP_Filesystem`. |
|
| 28 | + WP_Filesystem(); |
|
| 29 | + global $wp_filesystem; |
|
| 30 | + |
|
| 31 | + // Parse the url. |
|
| 32 | + $parts = wp_parse_url( $url ); |
|
| 33 | + |
|
| 34 | + // Get the bare filename (filename w/o the extension). |
|
| 35 | + $basename = str_replace( DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 36 | + pathinfo( $parts['path'], PATHINFO_FILENAME ) |
|
| 37 | + ) ); |
|
| 38 | + |
|
| 39 | + // Get the base dir. |
|
| 40 | + $wp_upload_dir = wp_upload_dir(); |
|
| 41 | + |
|
| 42 | + // Set the upload directory and URL. |
|
| 43 | + $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 44 | + $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 45 | + |
|
| 46 | + // Get the full path to the local filename. |
|
| 47 | + $image_full_path = $upload_dir . '/' . $basename; |
|
| 48 | + $image_full_url = $upload_url . '/' . $basename; |
|
| 49 | + |
|
| 50 | + // Create custom directory and bail on failure. |
|
| 51 | + if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 52 | + wl_write_log( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 53 | + |
|
| 54 | + return false; |
|
| 55 | + }; |
|
| 56 | + |
|
| 57 | + $response = self::get_response( $url ); |
|
| 58 | + |
|
| 59 | + // Bail if the response is not set. |
|
| 60 | + if ( false === $response ) { |
|
| 61 | + wl_write_log( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 62 | + |
|
| 63 | + return false; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // Get the content type of response. |
|
| 67 | + $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 68 | + |
|
| 69 | + // Get the file extension. |
|
| 70 | + $extension = self::get_extension_from_content_type( $content_type ); |
|
| 71 | + |
|
| 72 | + // Bail if the content type is not supported. |
|
| 73 | + if ( empty( $extension ) ) { |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + // Complete the local filename. |
|
| 78 | + $image_full_path .= $extension; |
|
| 79 | + $image_full_url .= $extension; |
|
| 80 | + |
|
| 81 | + // Store the data locally. |
|
| 82 | + $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 83 | + |
|
| 84 | + // Return the path. |
|
| 85 | + return array( |
|
| 86 | + 'path' => $image_full_path, |
|
| 87 | + 'url' => $image_full_url, |
|
| 88 | + 'content_type' => $content_type, |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Returns the file extension using the content type. |
|
| 94 | + * |
|
| 95 | + * @param string $content_type File content type. |
|
| 96 | + * |
|
| 97 | + * @return string|bool The file extension on success and |
|
| 98 | + * false on fail or if the content type is not supported. |
|
| 99 | + * @since 3.18.0 |
|
| 100 | + * |
|
| 101 | + */ |
|
| 102 | + private static function get_extension_from_content_type( $content_type ) { |
|
| 103 | + |
|
| 104 | + // Return the extension if match. |
|
| 105 | + switch ( $content_type ) { |
|
| 106 | + case 'image/jpeg': |
|
| 107 | + case 'image/jpg': |
|
| 108 | + return '.jpg'; |
|
| 109 | + case 'image/gif': |
|
| 110 | + return '.gif'; |
|
| 111 | + case 'image/png': |
|
| 112 | + return '.png'; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + // Otherwise return false. |
|
| 116 | + return false; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Retrieve the response from url and sets the response. |
|
| 121 | + * |
|
| 122 | + * @param string $url The url to retrieve. |
|
| 123 | + * |
|
| 124 | + * @return false|array True on success and false on failure. |
|
| 125 | + * @since 3.18.0 |
|
| 126 | + * |
|
| 127 | + */ |
|
| 128 | + private static function get_response( $url ) { |
|
| 129 | + // Request the remote file. |
|
| 130 | + $response = wp_remote_get( $url ); |
|
| 131 | + |
|
| 132 | + // Bail out if the response is not ok. |
|
| 133 | + if ( |
|
| 134 | + is_wp_error( $response ) |
|
| 135 | + || 200 !== (int) $response['response']['code'] |
|
| 136 | + || ! isset( $response['body'] ) |
|
| 137 | + ) { |
|
| 138 | + wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 139 | + |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Set the response. |
|
| 144 | + return $response; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | 147 | } |
@@ -18,59 +18,59 @@ discard block |
||
| 18 | 18 | * @since 3.18.0 |
| 19 | 19 | * |
| 20 | 20 | */ |
| 21 | - public static function save_from_url( $url ) { |
|
| 21 | + public static function save_from_url($url) { |
|
| 22 | 22 | |
| 23 | 23 | // Required for REST API calls |
| 24 | - if ( ! function_exists( 'WP_Filesystem' ) ) { |
|
| 25 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
|
| 24 | + if ( ! function_exists('WP_Filesystem')) { |
|
| 25 | + require_once(ABSPATH.'wp-admin/includes/file.php'); |
|
| 26 | 26 | } |
| 27 | 27 | // Load `WP_Filesystem`. |
| 28 | 28 | WP_Filesystem(); |
| 29 | 29 | global $wp_filesystem; |
| 30 | 30 | |
| 31 | 31 | // Parse the url. |
| 32 | - $parts = wp_parse_url( $url ); |
|
| 32 | + $parts = wp_parse_url($url); |
|
| 33 | 33 | |
| 34 | 34 | // Get the bare filename (filename w/o the extension). |
| 35 | - $basename = str_replace( DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 36 | - pathinfo( $parts['path'], PATHINFO_FILENAME ) |
|
| 37 | - ) ); |
|
| 35 | + $basename = str_replace(DIRECTORY_SEPARATOR, '_', rawurldecode( |
|
| 36 | + pathinfo($parts['path'], PATHINFO_FILENAME) |
|
| 37 | + )); |
|
| 38 | 38 | |
| 39 | 39 | // Get the base dir. |
| 40 | 40 | $wp_upload_dir = wp_upload_dir(); |
| 41 | 41 | |
| 42 | 42 | // Set the upload directory and URL. |
| 43 | - $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 44 | - $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
| 43 | + $upload_dir = $wp_upload_dir['basedir'].'/wl'.$wp_upload_dir['subdir']; |
|
| 44 | + $upload_url = $wp_upload_dir['baseurl'].'/wl'.$wp_upload_dir['subdir']; |
|
| 45 | 45 | |
| 46 | 46 | // Get the full path to the local filename. |
| 47 | - $image_full_path = $upload_dir . '/' . $basename; |
|
| 48 | - $image_full_url = $upload_url . '/' . $basename; |
|
| 47 | + $image_full_path = $upload_dir.'/'.$basename; |
|
| 48 | + $image_full_url = $upload_url.'/'.$basename; |
|
| 49 | 49 | |
| 50 | 50 | // Create custom directory and bail on failure. |
| 51 | - if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
| 52 | - wl_write_log( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
| 51 | + if ( ! wp_mkdir_p($upload_dir)) { |
|
| 52 | + wl_write_log("save_image_from_url : failed creating upload dir $upload_dir \n"); |
|
| 53 | 53 | |
| 54 | 54 | return false; |
| 55 | 55 | }; |
| 56 | 56 | |
| 57 | - $response = self::get_response( $url ); |
|
| 57 | + $response = self::get_response($url); |
|
| 58 | 58 | |
| 59 | 59 | // Bail if the response is not set. |
| 60 | - if ( false === $response ) { |
|
| 61 | - wl_write_log( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
| 60 | + if (false === $response) { |
|
| 61 | + wl_write_log("save_image_from_url : failed to fetch the response from: $url \n"); |
|
| 62 | 62 | |
| 63 | 63 | return false; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Get the content type of response. |
| 67 | - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
| 67 | + $content_type = wp_remote_retrieve_header($response, 'content-type'); |
|
| 68 | 68 | |
| 69 | 69 | // Get the file extension. |
| 70 | - $extension = self::get_extension_from_content_type( $content_type ); |
|
| 70 | + $extension = self::get_extension_from_content_type($content_type); |
|
| 71 | 71 | |
| 72 | 72 | // Bail if the content type is not supported. |
| 73 | - if ( empty( $extension ) ) { |
|
| 73 | + if (empty($extension)) { |
|
| 74 | 74 | return false; |
| 75 | 75 | } |
| 76 | 76 | |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | $image_full_url .= $extension; |
| 80 | 80 | |
| 81 | 81 | // Store the data locally. |
| 82 | - $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
| 82 | + $wp_filesystem->put_contents($image_full_path, wp_remote_retrieve_body($response)); |
|
| 83 | 83 | |
| 84 | 84 | // Return the path. |
| 85 | 85 | return array( |
@@ -99,10 +99,10 @@ discard block |
||
| 99 | 99 | * @since 3.18.0 |
| 100 | 100 | * |
| 101 | 101 | */ |
| 102 | - private static function get_extension_from_content_type( $content_type ) { |
|
| 102 | + private static function get_extension_from_content_type($content_type) { |
|
| 103 | 103 | |
| 104 | 104 | // Return the extension if match. |
| 105 | - switch ( $content_type ) { |
|
| 105 | + switch ($content_type) { |
|
| 106 | 106 | case 'image/jpeg': |
| 107 | 107 | case 'image/jpg': |
| 108 | 108 | return '.jpg'; |
@@ -125,17 +125,17 @@ discard block |
||
| 125 | 125 | * @since 3.18.0 |
| 126 | 126 | * |
| 127 | 127 | */ |
| 128 | - private static function get_response( $url ) { |
|
| 128 | + private static function get_response($url) { |
|
| 129 | 129 | // Request the remote file. |
| 130 | - $response = wp_remote_get( $url ); |
|
| 130 | + $response = wp_remote_get($url); |
|
| 131 | 131 | |
| 132 | 132 | // Bail out if the response is not ok. |
| 133 | 133 | if ( |
| 134 | - is_wp_error( $response ) |
|
| 134 | + is_wp_error($response) |
|
| 135 | 135 | || 200 !== (int) $response['response']['code'] |
| 136 | - || ! isset( $response['body'] ) |
|
| 136 | + || ! isset($response['body']) |
|
| 137 | 137 | ) { |
| 138 | - wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
| 138 | + wl_write_log("save_image_from_url : error fetching image $url \n"); |
|
| 139 | 139 | |
| 140 | 140 | return false; |
| 141 | 141 | } |
@@ -18,157 +18,157 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Vocabulary_Shortcode extends Wordlift_Shortcode { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The shortcode. |
|
| 23 | - * |
|
| 24 | - * @since 3.17.0 |
|
| 25 | - */ |
|
| 26 | - const SHORTCODE = 'wl_vocabulary'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.11.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $configuration_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * A {@link Wordlift_Log_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.17.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $log; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The vocabulary id |
|
| 48 | - * |
|
| 49 | - * @since 3.18.3 |
|
| 50 | - * @access private |
|
| 51 | - * @var int $vocabulary_id The vocabulary unique id. |
|
| 52 | - */ |
|
| 53 | - private static $vocabulary_id = 0; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Create a {@link Wordlift_Glossary_Shortcode} instance. |
|
| 57 | - * |
|
| 58 | - * @since 3.16.0 |
|
| 59 | - * |
|
| 60 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 61 | - */ |
|
| 62 | - public function __construct( $configuration_service ) { |
|
| 63 | - parent::__construct(); |
|
| 64 | - |
|
| 65 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 66 | - |
|
| 67 | - $this->configuration_service = $configuration_service; |
|
| 68 | - |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Check whether the requirements for this shortcode to work are available. |
|
| 73 | - * |
|
| 74 | - * @since 3.17.0 |
|
| 75 | - * @return bool True if the requirements are satisfied otherwise false. |
|
| 76 | - */ |
|
| 77 | - private static function are_requirements_satisfied() { |
|
| 78 | - |
|
| 79 | - return function_exists( 'mb_strlen' ) && |
|
| 80 | - function_exists( 'mb_substr' ) && |
|
| 81 | - function_exists( 'mb_strtolower' ) && |
|
| 82 | - function_exists( 'mb_strtoupper' ) && |
|
| 83 | - function_exists( 'mb_convert_case' ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Render the shortcode. |
|
| 88 | - * |
|
| 89 | - * @since 3.16.0 |
|
| 90 | - * |
|
| 91 | - * @param array $atts An array of shortcode attributes as set by the editor. |
|
| 92 | - * |
|
| 93 | - * @return string The output html code. |
|
| 94 | - */ |
|
| 95 | - public function render( $atts ) { |
|
| 96 | - |
|
| 97 | - // Bail out if the requirements aren't satisfied: we need mbstring for |
|
| 98 | - // the vocabulary widget to work. |
|
| 99 | - if ( ! self::are_requirements_satisfied() ) { |
|
| 100 | - $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
| 101 | - |
|
| 102 | - return ''; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css' ); |
|
| 106 | - |
|
| 107 | - // Extract attributes and set default values. |
|
| 108 | - $atts = shortcode_atts( |
|
| 109 | - array( |
|
| 110 | - // The entity type, such as `person`, `organization`, ... |
|
| 111 | - 'type' => 'all', |
|
| 112 | - // Limit the number of posts to 100 by default. Use -1 to remove the limit. |
|
| 113 | - 'limit' => 100, |
|
| 114 | - // Sort by title. |
|
| 115 | - 'orderby' => 'post_date', |
|
| 116 | - // Sort DESC. |
|
| 117 | - 'order' => 'DESC', |
|
| 118 | - // Allow to specify the category ID. |
|
| 119 | - 'cat' => '', |
|
| 120 | - ), $atts |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - // Get the posts. Note that if a `type` is specified before, then the |
|
| 124 | - // `tax_query` from the `add_criterias` call isn't added. |
|
| 125 | - $posts = $this->get_posts( $atts ); |
|
| 126 | - |
|
| 127 | - // Get the alphabet. |
|
| 128 | - $language_code = $this->configuration_service->get_language_code(); |
|
| 129 | - $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
| 130 | - |
|
| 131 | - // Add posts to the alphabet. |
|
| 132 | - foreach ( $posts as $post ) { |
|
| 133 | - $this->add_to_alphabet( $alphabet, $post->ID ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $header = ''; |
|
| 137 | - $sections = ''; |
|
| 138 | - |
|
| 139 | - // Get unique id for each vocabulary shortcode. |
|
| 140 | - $vocabulary_id = self::get_and_increment_vocabulary_id(); |
|
| 141 | - |
|
| 142 | - // Generate the header. |
|
| 143 | - foreach ( $alphabet as $item => $translations ) { |
|
| 144 | - $template = ( empty( $translations ) |
|
| 145 | - ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
|
| 146 | - : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>' ); |
|
| 147 | - |
|
| 148 | - $header .= sprintf( $template, esc_html( $item ), esc_attr( $item ), $vocabulary_id ); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // Generate the sections. |
|
| 152 | - foreach ( $alphabet as $item => $translations ) { |
|
| 153 | - // @since 3.19.3 we use `mb_strtolower` and `mb_strtoupper` with a custom function to handle sorting, |
|
| 154 | - // since we had `AB` being placed before `Aa` with `asort`. |
|
| 155 | - // |
|
| 156 | - // Order the translations alphabetically. |
|
| 157 | - // asort( $translations ); |
|
| 158 | - uasort( $translations, function ( $a, $b ) { |
|
| 159 | - if ( mb_strtolower( $a ) === mb_strtolower( $b ) |
|
| 160 | - || mb_strtoupper( $a ) === mb_strtoupper( $b ) ) { |
|
| 161 | - return 0; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return ( mb_strtolower( $a ) < mb_strtolower( $b ) ) ? - 1 : 1; |
|
| 165 | - } ); |
|
| 166 | - $sections .= $this->get_section( $item, $translations, $vocabulary_id ); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - // Return HTML template. |
|
| 170 | - ob_start(); |
|
| 171 | - ?> |
|
| 21 | + /** |
|
| 22 | + * The shortcode. |
|
| 23 | + * |
|
| 24 | + * @since 3.17.0 |
|
| 25 | + */ |
|
| 26 | + const SHORTCODE = 'wl_vocabulary'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.11.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $configuration_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * A {@link Wordlift_Log_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.17.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $log; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The vocabulary id |
|
| 48 | + * |
|
| 49 | + * @since 3.18.3 |
|
| 50 | + * @access private |
|
| 51 | + * @var int $vocabulary_id The vocabulary unique id. |
|
| 52 | + */ |
|
| 53 | + private static $vocabulary_id = 0; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Create a {@link Wordlift_Glossary_Shortcode} instance. |
|
| 57 | + * |
|
| 58 | + * @since 3.16.0 |
|
| 59 | + * |
|
| 60 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 61 | + */ |
|
| 62 | + public function __construct( $configuration_service ) { |
|
| 63 | + parent::__construct(); |
|
| 64 | + |
|
| 65 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 66 | + |
|
| 67 | + $this->configuration_service = $configuration_service; |
|
| 68 | + |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Check whether the requirements for this shortcode to work are available. |
|
| 73 | + * |
|
| 74 | + * @since 3.17.0 |
|
| 75 | + * @return bool True if the requirements are satisfied otherwise false. |
|
| 76 | + */ |
|
| 77 | + private static function are_requirements_satisfied() { |
|
| 78 | + |
|
| 79 | + return function_exists( 'mb_strlen' ) && |
|
| 80 | + function_exists( 'mb_substr' ) && |
|
| 81 | + function_exists( 'mb_strtolower' ) && |
|
| 82 | + function_exists( 'mb_strtoupper' ) && |
|
| 83 | + function_exists( 'mb_convert_case' ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Render the shortcode. |
|
| 88 | + * |
|
| 89 | + * @since 3.16.0 |
|
| 90 | + * |
|
| 91 | + * @param array $atts An array of shortcode attributes as set by the editor. |
|
| 92 | + * |
|
| 93 | + * @return string The output html code. |
|
| 94 | + */ |
|
| 95 | + public function render( $atts ) { |
|
| 96 | + |
|
| 97 | + // Bail out if the requirements aren't satisfied: we need mbstring for |
|
| 98 | + // the vocabulary widget to work. |
|
| 99 | + if ( ! self::are_requirements_satisfied() ) { |
|
| 100 | + $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
| 101 | + |
|
| 102 | + return ''; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css' ); |
|
| 106 | + |
|
| 107 | + // Extract attributes and set default values. |
|
| 108 | + $atts = shortcode_atts( |
|
| 109 | + array( |
|
| 110 | + // The entity type, such as `person`, `organization`, ... |
|
| 111 | + 'type' => 'all', |
|
| 112 | + // Limit the number of posts to 100 by default. Use -1 to remove the limit. |
|
| 113 | + 'limit' => 100, |
|
| 114 | + // Sort by title. |
|
| 115 | + 'orderby' => 'post_date', |
|
| 116 | + // Sort DESC. |
|
| 117 | + 'order' => 'DESC', |
|
| 118 | + // Allow to specify the category ID. |
|
| 119 | + 'cat' => '', |
|
| 120 | + ), $atts |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + // Get the posts. Note that if a `type` is specified before, then the |
|
| 124 | + // `tax_query` from the `add_criterias` call isn't added. |
|
| 125 | + $posts = $this->get_posts( $atts ); |
|
| 126 | + |
|
| 127 | + // Get the alphabet. |
|
| 128 | + $language_code = $this->configuration_service->get_language_code(); |
|
| 129 | + $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
| 130 | + |
|
| 131 | + // Add posts to the alphabet. |
|
| 132 | + foreach ( $posts as $post ) { |
|
| 133 | + $this->add_to_alphabet( $alphabet, $post->ID ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $header = ''; |
|
| 137 | + $sections = ''; |
|
| 138 | + |
|
| 139 | + // Get unique id for each vocabulary shortcode. |
|
| 140 | + $vocabulary_id = self::get_and_increment_vocabulary_id(); |
|
| 141 | + |
|
| 142 | + // Generate the header. |
|
| 143 | + foreach ( $alphabet as $item => $translations ) { |
|
| 144 | + $template = ( empty( $translations ) |
|
| 145 | + ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
|
| 146 | + : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>' ); |
|
| 147 | + |
|
| 148 | + $header .= sprintf( $template, esc_html( $item ), esc_attr( $item ), $vocabulary_id ); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // Generate the sections. |
|
| 152 | + foreach ( $alphabet as $item => $translations ) { |
|
| 153 | + // @since 3.19.3 we use `mb_strtolower` and `mb_strtoupper` with a custom function to handle sorting, |
|
| 154 | + // since we had `AB` being placed before `Aa` with `asort`. |
|
| 155 | + // |
|
| 156 | + // Order the translations alphabetically. |
|
| 157 | + // asort( $translations ); |
|
| 158 | + uasort( $translations, function ( $a, $b ) { |
|
| 159 | + if ( mb_strtolower( $a ) === mb_strtolower( $b ) |
|
| 160 | + || mb_strtoupper( $a ) === mb_strtoupper( $b ) ) { |
|
| 161 | + return 0; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return ( mb_strtolower( $a ) < mb_strtolower( $b ) ) ? - 1 : 1; |
|
| 165 | + } ); |
|
| 166 | + $sections .= $this->get_section( $item, $translations, $vocabulary_id ); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + // Return HTML template. |
|
| 170 | + ob_start(); |
|
| 171 | + ?> |
|
| 172 | 172 | <div class='wl-vocabulary'> |
| 173 | 173 | <nav class='wl-vocabulary-alphabet-nav'> |
| 174 | 174 | <?php echo $header; ?> |
@@ -178,34 +178,34 @@ discard block |
||
| 178 | 178 | </div> |
| 179 | 179 | </div> |
| 180 | 180 | <?php |
| 181 | - $html = ob_get_clean(); |
|
| 182 | - |
|
| 183 | - return $html; |
|
| 184 | - |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Generate the html code for the section. |
|
| 189 | - * |
|
| 190 | - * @since 3.17.0 |
|
| 191 | - * |
|
| 192 | - * @param string $letter The section's letter. |
|
| 193 | - * @param array $posts An array of `$post_id => $post_title` associated with |
|
| 194 | - * the section. |
|
| 195 | - * @param int $vocabulary_id Unique vocabulary id. |
|
| 196 | - * |
|
| 197 | - * @return string The section html code (or an empty string if the section has |
|
| 198 | - * no posts). |
|
| 199 | - */ |
|
| 200 | - private function get_section( $letter, $posts, $vocabulary_id ) { |
|
| 201 | - |
|
| 202 | - // Return an empty string if there are no posts. |
|
| 203 | - if ( 0 === count( $posts ) ) { |
|
| 204 | - return ''; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return sprintf( |
|
| 208 | - ' |
|
| 181 | + $html = ob_get_clean(); |
|
| 182 | + |
|
| 183 | + return $html; |
|
| 184 | + |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Generate the html code for the section. |
|
| 189 | + * |
|
| 190 | + * @since 3.17.0 |
|
| 191 | + * |
|
| 192 | + * @param string $letter The section's letter. |
|
| 193 | + * @param array $posts An array of `$post_id => $post_title` associated with |
|
| 194 | + * the section. |
|
| 195 | + * @param int $vocabulary_id Unique vocabulary id. |
|
| 196 | + * |
|
| 197 | + * @return string The section html code (or an empty string if the section has |
|
| 198 | + * no posts). |
|
| 199 | + */ |
|
| 200 | + private function get_section( $letter, $posts, $vocabulary_id ) { |
|
| 201 | + |
|
| 202 | + // Return an empty string if there are no posts. |
|
| 203 | + if ( 0 === count( $posts ) ) { |
|
| 204 | + return ''; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return sprintf( |
|
| 208 | + ' |
|
| 209 | 209 | <div class="wl-vocabulary-letter-block" id="wl-vocabulary-%d-%s"> |
| 210 | 210 | <aside class="wl-vocabulary-left-column">%s</aside> |
| 211 | 211 | <div class="wl-vocabulary-right-column"> |
@@ -215,127 +215,127 @@ discard block |
||
| 215 | 215 | </div> |
| 216 | 216 | </div> |
| 217 | 217 | ', $vocabulary_id, esc_attr( $letter ), esc_html( $letter ), $this->format_posts_as_list( $posts ) |
| 218 | - ); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Format an array post `$post_id => $post_title` as a list. |
|
| 223 | - * |
|
| 224 | - * @since 3.17.0 |
|
| 225 | - * |
|
| 226 | - * @param array $posts An array of `$post_id => $post_title` key, value pairs. |
|
| 227 | - * |
|
| 228 | - * @return string A list. |
|
| 229 | - */ |
|
| 230 | - private function format_posts_as_list( $posts ) { |
|
| 231 | - |
|
| 232 | - return array_reduce( |
|
| 233 | - array_keys( $posts ), function ( $carry, $item ) use ( $posts ) { |
|
| 234 | - return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
| 235 | - }, '' |
|
| 236 | - ); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Get the posts from WordPress using the provided attributes. |
|
| 241 | - * |
|
| 242 | - * @since 3.17.0 |
|
| 243 | - * |
|
| 244 | - * @param array $atts The shortcode attributes. |
|
| 245 | - * |
|
| 246 | - * @return array An array of {@link WP_Post}s. |
|
| 247 | - */ |
|
| 248 | - private function get_posts( $atts ) { |
|
| 249 | - // The default arguments for the query. |
|
| 250 | - $args = array( |
|
| 251 | - 'posts_per_page' => intval( $atts['limit'] ), |
|
| 252 | - 'update_post_meta_cache' => false, |
|
| 253 | - 'update_post_term_cache' => false, |
|
| 254 | - 'orderby' => $atts['orderby'], |
|
| 255 | - 'order' => $atts['order'], |
|
| 256 | - // Exclude the publisher. |
|
| 257 | - 'post__not_in' => array( $this->configuration_service->get_publisher_id() ), |
|
| 258 | - ); |
|
| 259 | - |
|
| 260 | - // Limit the based entity type if needed. |
|
| 261 | - if ( 'all' !== $atts['type'] ) { |
|
| 262 | - $args['tax_query'] = array( |
|
| 263 | - array( |
|
| 264 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 265 | - 'field' => 'slug', |
|
| 266 | - 'terms' => $atts['type'], |
|
| 267 | - ), |
|
| 268 | - ); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - if ( ! empty( $atts['cat'] ) ) { |
|
| 272 | - $args['cat'] = $atts['cat']; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - // Get the posts. Note that if a `type` is specified before, then the |
|
| 276 | - // `tax_query` from the `add_criterias` call isn't added. |
|
| 277 | - return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
| 278 | - |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Populate the alphabet with posts. |
|
| 283 | - * |
|
| 284 | - * @since 3.17.0 |
|
| 285 | - * |
|
| 286 | - * @param array $alphabet An array of letters. |
|
| 287 | - * @param int $post_id The {@link WP_Post} id. |
|
| 288 | - */ |
|
| 289 | - private function add_to_alphabet( &$alphabet, $post_id ) { |
|
| 290 | - |
|
| 291 | - // Get the title without accents. |
|
| 292 | - $title = remove_accents( get_the_title( $post_id ) ); |
|
| 293 | - |
|
| 294 | - // Get the initial letter. |
|
| 295 | - $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
| 296 | - |
|
| 297 | - // Add the post. |
|
| 298 | - $alphabet[ $letter ][ $post_id ] = $title; |
|
| 299 | - |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Find the first letter in the alphabet. |
|
| 304 | - * |
|
| 305 | - * In some alphabets a letter is a compound of letters, therefore this function |
|
| 306 | - * will look for groups of 2 or 3 letters in the alphabet before looking for a |
|
| 307 | - * single letter. In case the letter is not found a # (hash) key is returned. |
|
| 308 | - * |
|
| 309 | - * @since 3.17.0 |
|
| 310 | - * |
|
| 311 | - * @param array $alphabet An array of alphabet letters. |
|
| 312 | - * @param string $title The title to match. |
|
| 313 | - * |
|
| 314 | - * @return string The initial letter or a `#` key. |
|
| 315 | - */ |
|
| 316 | - private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
| 317 | - |
|
| 318 | - // Need to handle letters which consist of 3 and 2 characters. |
|
| 319 | - for ( $i = 3; $i > 0; $i -- ) { |
|
| 320 | - $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
| 321 | - if ( isset( $alphabet[ $letter ] ) ) { |
|
| 322 | - return $letter; |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - return '#'; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Get and increment the `$vocabulary_id`. |
|
| 331 | - * |
|
| 332 | - * @since 3.18.3 |
|
| 333 | - * |
|
| 334 | - * @return int The incremented vocabulary id. |
|
| 335 | - */ |
|
| 336 | - private static function get_and_increment_vocabulary_id() { |
|
| 337 | - return self::$vocabulary_id ++; |
|
| 338 | - } |
|
| 218 | + ); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Format an array post `$post_id => $post_title` as a list. |
|
| 223 | + * |
|
| 224 | + * @since 3.17.0 |
|
| 225 | + * |
|
| 226 | + * @param array $posts An array of `$post_id => $post_title` key, value pairs. |
|
| 227 | + * |
|
| 228 | + * @return string A list. |
|
| 229 | + */ |
|
| 230 | + private function format_posts_as_list( $posts ) { |
|
| 231 | + |
|
| 232 | + return array_reduce( |
|
| 233 | + array_keys( $posts ), function ( $carry, $item ) use ( $posts ) { |
|
| 234 | + return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
| 235 | + }, '' |
|
| 236 | + ); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Get the posts from WordPress using the provided attributes. |
|
| 241 | + * |
|
| 242 | + * @since 3.17.0 |
|
| 243 | + * |
|
| 244 | + * @param array $atts The shortcode attributes. |
|
| 245 | + * |
|
| 246 | + * @return array An array of {@link WP_Post}s. |
|
| 247 | + */ |
|
| 248 | + private function get_posts( $atts ) { |
|
| 249 | + // The default arguments for the query. |
|
| 250 | + $args = array( |
|
| 251 | + 'posts_per_page' => intval( $atts['limit'] ), |
|
| 252 | + 'update_post_meta_cache' => false, |
|
| 253 | + 'update_post_term_cache' => false, |
|
| 254 | + 'orderby' => $atts['orderby'], |
|
| 255 | + 'order' => $atts['order'], |
|
| 256 | + // Exclude the publisher. |
|
| 257 | + 'post__not_in' => array( $this->configuration_service->get_publisher_id() ), |
|
| 258 | + ); |
|
| 259 | + |
|
| 260 | + // Limit the based entity type if needed. |
|
| 261 | + if ( 'all' !== $atts['type'] ) { |
|
| 262 | + $args['tax_query'] = array( |
|
| 263 | + array( |
|
| 264 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 265 | + 'field' => 'slug', |
|
| 266 | + 'terms' => $atts['type'], |
|
| 267 | + ), |
|
| 268 | + ); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + if ( ! empty( $atts['cat'] ) ) { |
|
| 272 | + $args['cat'] = $atts['cat']; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + // Get the posts. Note that if a `type` is specified before, then the |
|
| 276 | + // `tax_query` from the `add_criterias` call isn't added. |
|
| 277 | + return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
| 278 | + |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Populate the alphabet with posts. |
|
| 283 | + * |
|
| 284 | + * @since 3.17.0 |
|
| 285 | + * |
|
| 286 | + * @param array $alphabet An array of letters. |
|
| 287 | + * @param int $post_id The {@link WP_Post} id. |
|
| 288 | + */ |
|
| 289 | + private function add_to_alphabet( &$alphabet, $post_id ) { |
|
| 290 | + |
|
| 291 | + // Get the title without accents. |
|
| 292 | + $title = remove_accents( get_the_title( $post_id ) ); |
|
| 293 | + |
|
| 294 | + // Get the initial letter. |
|
| 295 | + $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
| 296 | + |
|
| 297 | + // Add the post. |
|
| 298 | + $alphabet[ $letter ][ $post_id ] = $title; |
|
| 299 | + |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Find the first letter in the alphabet. |
|
| 304 | + * |
|
| 305 | + * In some alphabets a letter is a compound of letters, therefore this function |
|
| 306 | + * will look for groups of 2 or 3 letters in the alphabet before looking for a |
|
| 307 | + * single letter. In case the letter is not found a # (hash) key is returned. |
|
| 308 | + * |
|
| 309 | + * @since 3.17.0 |
|
| 310 | + * |
|
| 311 | + * @param array $alphabet An array of alphabet letters. |
|
| 312 | + * @param string $title The title to match. |
|
| 313 | + * |
|
| 314 | + * @return string The initial letter or a `#` key. |
|
| 315 | + */ |
|
| 316 | + private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
| 317 | + |
|
| 318 | + // Need to handle letters which consist of 3 and 2 characters. |
|
| 319 | + for ( $i = 3; $i > 0; $i -- ) { |
|
| 320 | + $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
| 321 | + if ( isset( $alphabet[ $letter ] ) ) { |
|
| 322 | + return $letter; |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + return '#'; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Get and increment the `$vocabulary_id`. |
|
| 331 | + * |
|
| 332 | + * @since 3.18.3 |
|
| 333 | + * |
|
| 334 | + * @return int The incremented vocabulary id. |
|
| 335 | + */ |
|
| 336 | + private static function get_and_increment_vocabulary_id() { |
|
| 337 | + return self::$vocabulary_id ++; |
|
| 338 | + } |
|
| 339 | 339 | |
| 340 | 340 | } |
| 341 | 341 | |
@@ -343,41 +343,41 @@ discard block |
||
| 343 | 343 | * register_block_type for Gutenberg blocks |
| 344 | 344 | */ |
| 345 | 345 | add_action( 'init', function() { |
| 346 | - // Bail out if the `register_block_type` function isn't available. |
|
| 347 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 348 | - return; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - register_block_type('wordlift/vocabulary', array( |
|
| 352 | - 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 353 | - 'render_callback' => function($attributes){ |
|
| 354 | - $attr_code = ''; |
|
| 355 | - foreach ($attributes as $key => $value) { |
|
| 356 | - $attr_code .= $key.'="'.$value.'" '; |
|
| 357 | - } |
|
| 358 | - return '[wl_vocabulary '.$attr_code.']'; |
|
| 359 | - }, |
|
| 360 | - 'attributes' => array( |
|
| 361 | - 'type' => array( |
|
| 362 | - 'type' => 'string', |
|
| 363 | - 'default' => 'all' |
|
| 346 | + // Bail out if the `register_block_type` function isn't available. |
|
| 347 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 348 | + return; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + register_block_type('wordlift/vocabulary', array( |
|
| 352 | + 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 353 | + 'render_callback' => function($attributes){ |
|
| 354 | + $attr_code = ''; |
|
| 355 | + foreach ($attributes as $key => $value) { |
|
| 356 | + $attr_code .= $key.'="'.$value.'" '; |
|
| 357 | + } |
|
| 358 | + return '[wl_vocabulary '.$attr_code.']'; |
|
| 359 | + }, |
|
| 360 | + 'attributes' => array( |
|
| 361 | + 'type' => array( |
|
| 362 | + 'type' => 'string', |
|
| 363 | + 'default' => 'all' |
|
| 364 | 364 | ), |
| 365 | - 'limit' => array( |
|
| 366 | - 'type' => 'number', |
|
| 367 | - 'default' => 100 |
|
| 365 | + 'limit' => array( |
|
| 366 | + 'type' => 'number', |
|
| 367 | + 'default' => 100 |
|
| 368 | 368 | ), |
| 369 | - 'orderby' => array( |
|
| 370 | - 'type' => 'string', |
|
| 371 | - 'default' => 'post_date' |
|
| 369 | + 'orderby' => array( |
|
| 370 | + 'type' => 'string', |
|
| 371 | + 'default' => 'post_date' |
|
| 372 | 372 | ), |
| 373 | - 'order' => array( |
|
| 374 | - 'type' => 'string', |
|
| 375 | - 'default' => 'DESC' |
|
| 373 | + 'order' => array( |
|
| 374 | + 'type' => 'string', |
|
| 375 | + 'default' => 'DESC' |
|
| 376 | 376 | ), |
| 377 | - 'cat' => array( |
|
| 378 | - 'type' => 'string', |
|
| 379 | - 'default' => '' |
|
| 377 | + 'cat' => array( |
|
| 378 | + 'type' => 'string', |
|
| 379 | + 'default' => '' |
|
| 380 | 380 | ), |
| 381 | 381 | ) |
| 382 | - )); |
|
| 382 | + )); |
|
| 383 | 383 | } ); |
@@ -59,10 +59,10 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 61 | 61 | */ |
| 62 | - public function __construct( $configuration_service ) { |
|
| 62 | + public function __construct($configuration_service) { |
|
| 63 | 63 | parent::__construct(); |
| 64 | 64 | |
| 65 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 65 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 66 | 66 | |
| 67 | 67 | $this->configuration_service = $configuration_service; |
| 68 | 68 | |
@@ -76,11 +76,11 @@ discard block |
||
| 76 | 76 | */ |
| 77 | 77 | private static function are_requirements_satisfied() { |
| 78 | 78 | |
| 79 | - return function_exists( 'mb_strlen' ) && |
|
| 80 | - function_exists( 'mb_substr' ) && |
|
| 81 | - function_exists( 'mb_strtolower' ) && |
|
| 82 | - function_exists( 'mb_strtoupper' ) && |
|
| 83 | - function_exists( 'mb_convert_case' ); |
|
| 79 | + return function_exists('mb_strlen') && |
|
| 80 | + function_exists('mb_substr') && |
|
| 81 | + function_exists('mb_strtolower') && |
|
| 82 | + function_exists('mb_strtoupper') && |
|
| 83 | + function_exists('mb_convert_case'); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
@@ -92,17 +92,17 @@ discard block |
||
| 92 | 92 | * |
| 93 | 93 | * @return string The output html code. |
| 94 | 94 | */ |
| 95 | - public function render( $atts ) { |
|
| 95 | + public function render($atts) { |
|
| 96 | 96 | |
| 97 | 97 | // Bail out if the requirements aren't satisfied: we need mbstring for |
| 98 | 98 | // the vocabulary widget to work. |
| 99 | - if ( ! self::are_requirements_satisfied() ) { |
|
| 100 | - $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
| 99 | + if ( ! self::are_requirements_satisfied()) { |
|
| 100 | + $this->log->warn("The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements."); |
|
| 101 | 101 | |
| 102 | 102 | return ''; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css' ); |
|
| 105 | + wp_enqueue_style('wl-vocabulary-shortcode', dirname(plugin_dir_url(__FILE__)).'/public/css/wordlift-vocabulary-shortcode.css'); |
|
| 106 | 106 | |
| 107 | 107 | // Extract attributes and set default values. |
| 108 | 108 | $atts = shortcode_atts( |
@@ -122,15 +122,15 @@ discard block |
||
| 122 | 122 | |
| 123 | 123 | // Get the posts. Note that if a `type` is specified before, then the |
| 124 | 124 | // `tax_query` from the `add_criterias` call isn't added. |
| 125 | - $posts = $this->get_posts( $atts ); |
|
| 125 | + $posts = $this->get_posts($atts); |
|
| 126 | 126 | |
| 127 | 127 | // Get the alphabet. |
| 128 | 128 | $language_code = $this->configuration_service->get_language_code(); |
| 129 | - $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
| 129 | + $alphabet = Wordlift_Alphabet_Service::get($language_code); |
|
| 130 | 130 | |
| 131 | 131 | // Add posts to the alphabet. |
| 132 | - foreach ( $posts as $post ) { |
|
| 133 | - $this->add_to_alphabet( $alphabet, $post->ID ); |
|
| 132 | + foreach ($posts as $post) { |
|
| 133 | + $this->add_to_alphabet($alphabet, $post->ID); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | $header = ''; |
@@ -140,30 +140,30 @@ discard block |
||
| 140 | 140 | $vocabulary_id = self::get_and_increment_vocabulary_id(); |
| 141 | 141 | |
| 142 | 142 | // Generate the header. |
| 143 | - foreach ( $alphabet as $item => $translations ) { |
|
| 144 | - $template = ( empty( $translations ) |
|
| 143 | + foreach ($alphabet as $item => $translations) { |
|
| 144 | + $template = (empty($translations) |
|
| 145 | 145 | ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
| 146 | - : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>' ); |
|
| 146 | + : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>'); |
|
| 147 | 147 | |
| 148 | - $header .= sprintf( $template, esc_html( $item ), esc_attr( $item ), $vocabulary_id ); |
|
| 148 | + $header .= sprintf($template, esc_html($item), esc_attr($item), $vocabulary_id); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | // Generate the sections. |
| 152 | - foreach ( $alphabet as $item => $translations ) { |
|
| 152 | + foreach ($alphabet as $item => $translations) { |
|
| 153 | 153 | // @since 3.19.3 we use `mb_strtolower` and `mb_strtoupper` with a custom function to handle sorting, |
| 154 | 154 | // since we had `AB` being placed before `Aa` with `asort`. |
| 155 | 155 | // |
| 156 | 156 | // Order the translations alphabetically. |
| 157 | 157 | // asort( $translations ); |
| 158 | - uasort( $translations, function ( $a, $b ) { |
|
| 159 | - if ( mb_strtolower( $a ) === mb_strtolower( $b ) |
|
| 160 | - || mb_strtoupper( $a ) === mb_strtoupper( $b ) ) { |
|
| 158 | + uasort($translations, function($a, $b) { |
|
| 159 | + if (mb_strtolower($a) === mb_strtolower($b) |
|
| 160 | + || mb_strtoupper($a) === mb_strtoupper($b)) { |
|
| 161 | 161 | return 0; |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | - return ( mb_strtolower( $a ) < mb_strtolower( $b ) ) ? - 1 : 1; |
|
| 164 | + return (mb_strtolower($a) < mb_strtolower($b)) ? -1 : 1; |
|
| 165 | 165 | } ); |
| 166 | - $sections .= $this->get_section( $item, $translations, $vocabulary_id ); |
|
| 166 | + $sections .= $this->get_section($item, $translations, $vocabulary_id); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | // Return HTML template. |
@@ -197,10 +197,10 @@ discard block |
||
| 197 | 197 | * @return string The section html code (or an empty string if the section has |
| 198 | 198 | * no posts). |
| 199 | 199 | */ |
| 200 | - private function get_section( $letter, $posts, $vocabulary_id ) { |
|
| 200 | + private function get_section($letter, $posts, $vocabulary_id) { |
|
| 201 | 201 | |
| 202 | 202 | // Return an empty string if there are no posts. |
| 203 | - if ( 0 === count( $posts ) ) { |
|
| 203 | + if (0 === count($posts)) { |
|
| 204 | 204 | return ''; |
| 205 | 205 | } |
| 206 | 206 | |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | </ul> |
| 215 | 215 | </div> |
| 216 | 216 | </div> |
| 217 | - ', $vocabulary_id, esc_attr( $letter ), esc_html( $letter ), $this->format_posts_as_list( $posts ) |
|
| 217 | + ', $vocabulary_id, esc_attr($letter), esc_html($letter), $this->format_posts_as_list($posts) |
|
| 218 | 218 | ); |
| 219 | 219 | } |
| 220 | 220 | |
@@ -227,11 +227,11 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | * @return string A list. |
| 229 | 229 | */ |
| 230 | - private function format_posts_as_list( $posts ) { |
|
| 230 | + private function format_posts_as_list($posts) { |
|
| 231 | 231 | |
| 232 | 232 | return array_reduce( |
| 233 | - array_keys( $posts ), function ( $carry, $item ) use ( $posts ) { |
|
| 234 | - return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
| 233 | + array_keys($posts), function($carry, $item) use ($posts) { |
|
| 234 | + return $carry.sprintf('<li><a href="%s">%s</a></li>', esc_attr(get_permalink($item)), esc_html($posts[$item])); |
|
| 235 | 235 | }, '' |
| 236 | 236 | ); |
| 237 | 237 | } |
@@ -245,20 +245,20 @@ discard block |
||
| 245 | 245 | * |
| 246 | 246 | * @return array An array of {@link WP_Post}s. |
| 247 | 247 | */ |
| 248 | - private function get_posts( $atts ) { |
|
| 248 | + private function get_posts($atts) { |
|
| 249 | 249 | // The default arguments for the query. |
| 250 | 250 | $args = array( |
| 251 | - 'posts_per_page' => intval( $atts['limit'] ), |
|
| 251 | + 'posts_per_page' => intval($atts['limit']), |
|
| 252 | 252 | 'update_post_meta_cache' => false, |
| 253 | 253 | 'update_post_term_cache' => false, |
| 254 | 254 | 'orderby' => $atts['orderby'], |
| 255 | 255 | 'order' => $atts['order'], |
| 256 | 256 | // Exclude the publisher. |
| 257 | - 'post__not_in' => array( $this->configuration_service->get_publisher_id() ), |
|
| 257 | + 'post__not_in' => array($this->configuration_service->get_publisher_id()), |
|
| 258 | 258 | ); |
| 259 | 259 | |
| 260 | 260 | // Limit the based entity type if needed. |
| 261 | - if ( 'all' !== $atts['type'] ) { |
|
| 261 | + if ('all' !== $atts['type']) { |
|
| 262 | 262 | $args['tax_query'] = array( |
| 263 | 263 | array( |
| 264 | 264 | 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
@@ -268,13 +268,13 @@ discard block |
||
| 268 | 268 | ); |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - if ( ! empty( $atts['cat'] ) ) { |
|
| 271 | + if ( ! empty($atts['cat'])) { |
|
| 272 | 272 | $args['cat'] = $atts['cat']; |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | // Get the posts. Note that if a `type` is specified before, then the |
| 276 | 276 | // `tax_query` from the `add_criterias` call isn't added. |
| 277 | - return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
| 277 | + return get_posts(Wordlift_Entity_Service::add_criterias($args)); |
|
| 278 | 278 | |
| 279 | 279 | } |
| 280 | 280 | |
@@ -286,16 +286,16 @@ discard block |
||
| 286 | 286 | * @param array $alphabet An array of letters. |
| 287 | 287 | * @param int $post_id The {@link WP_Post} id. |
| 288 | 288 | */ |
| 289 | - private function add_to_alphabet( &$alphabet, $post_id ) { |
|
| 289 | + private function add_to_alphabet(&$alphabet, $post_id) { |
|
| 290 | 290 | |
| 291 | 291 | // Get the title without accents. |
| 292 | - $title = remove_accents( get_the_title( $post_id ) ); |
|
| 292 | + $title = remove_accents(get_the_title($post_id)); |
|
| 293 | 293 | |
| 294 | 294 | // Get the initial letter. |
| 295 | - $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
| 295 | + $letter = $this->get_first_letter_in_alphabet_or_hash($alphabet, $title); |
|
| 296 | 296 | |
| 297 | 297 | // Add the post. |
| 298 | - $alphabet[ $letter ][ $post_id ] = $title; |
|
| 298 | + $alphabet[$letter][$post_id] = $title; |
|
| 299 | 299 | |
| 300 | 300 | } |
| 301 | 301 | |
@@ -313,12 +313,12 @@ discard block |
||
| 313 | 313 | * |
| 314 | 314 | * @return string The initial letter or a `#` key. |
| 315 | 315 | */ |
| 316 | - private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
| 316 | + private function get_first_letter_in_alphabet_or_hash($alphabet, $title) { |
|
| 317 | 317 | |
| 318 | 318 | // Need to handle letters which consist of 3 and 2 characters. |
| 319 | - for ( $i = 3; $i > 0; $i -- ) { |
|
| 320 | - $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
| 321 | - if ( isset( $alphabet[ $letter ] ) ) { |
|
| 319 | + for ($i = 3; $i > 0; $i--) { |
|
| 320 | + $letter = mb_convert_case(mb_substr($title, 0, $i), MB_CASE_UPPER); |
|
| 321 | + if (isset($alphabet[$letter])) { |
|
| 322 | 322 | return $letter; |
| 323 | 323 | } |
| 324 | 324 | } |
@@ -334,7 +334,7 @@ discard block |
||
| 334 | 334 | * @return int The incremented vocabulary id. |
| 335 | 335 | */ |
| 336 | 336 | private static function get_and_increment_vocabulary_id() { |
| 337 | - return self::$vocabulary_id ++; |
|
| 337 | + return self::$vocabulary_id++; |
|
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | } |
@@ -342,15 +342,15 @@ discard block |
||
| 342 | 342 | /** |
| 343 | 343 | * register_block_type for Gutenberg blocks |
| 344 | 344 | */ |
| 345 | -add_action( 'init', function() { |
|
| 345 | +add_action('init', function() { |
|
| 346 | 346 | // Bail out if the `register_block_type` function isn't available. |
| 347 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 347 | + if ( ! function_exists('register_block_type')) { |
|
| 348 | 348 | return; |
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | register_block_type('wordlift/vocabulary', array( |
| 352 | 352 | 'editor_script' => 'wordlift-admin-edit-gutenberg', |
| 353 | - 'render_callback' => function($attributes){ |
|
| 353 | + 'render_callback' => function($attributes) { |
|
| 354 | 354 | $attr_code = ''; |
| 355 | 355 | foreach ($attributes as $key => $value) { |
| 356 | 356 | $attr_code .= $key.'="'.$value.'" '; |
@@ -16,126 +16,126 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Related_Entities_Cloud_Shortcode extends Wordlift_Shortcode { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * {@inheritdoc} |
|
| 21 | - */ |
|
| 22 | - const SHORTCODE = 'wl_cloud'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.15.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $relation_service; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Create a {@link Wordlift_Related_Entities_Cloud_Shortcode} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.15.0 |
|
| 37 | - * |
|
| 38 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 39 | - */ |
|
| 40 | - public function __construct( $relation_service ) { |
|
| 41 | - parent::__construct(); |
|
| 42 | - |
|
| 43 | - $this->relation_service = $relation_service; |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * {@inheritdoc} |
|
| 49 | - */ |
|
| 50 | - public function render( $atts ) { |
|
| 51 | - |
|
| 52 | - $tags = $this->get_related_entities_tags(); |
|
| 53 | - |
|
| 54 | - // Bail out if there are no associated entities. |
|
| 55 | - if ( empty( $tags ) ) { |
|
| 56 | - return ''; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /* |
|
| 19 | + /** |
|
| 20 | + * {@inheritdoc} |
|
| 21 | + */ |
|
| 22 | + const SHORTCODE = 'wl_cloud'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.15.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $relation_service; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Create a {@link Wordlift_Related_Entities_Cloud_Shortcode} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.15.0 |
|
| 37 | + * |
|
| 38 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 39 | + */ |
|
| 40 | + public function __construct( $relation_service ) { |
|
| 41 | + parent::__construct(); |
|
| 42 | + |
|
| 43 | + $this->relation_service = $relation_service; |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * {@inheritdoc} |
|
| 49 | + */ |
|
| 50 | + public function render( $atts ) { |
|
| 51 | + |
|
| 52 | + $tags = $this->get_related_entities_tags(); |
|
| 53 | + |
|
| 54 | + // Bail out if there are no associated entities. |
|
| 55 | + if ( empty( $tags ) ) { |
|
| 56 | + return ''; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /* |
|
| 60 | 60 | * Since the output is use in the widget as well, we need to have the |
| 61 | 61 | * same class as the core tagcloud widget, to easily inherit its styling. |
| 62 | 62 | */ |
| 63 | 63 | |
| 64 | - return '<div class="tagcloud wl-related-entities-cloud">' . |
|
| 65 | - wp_generate_tag_cloud( $tags ) . |
|
| 66 | - '</div>'; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Find the related entities to the currently displayed post and |
|
| 71 | - * calculate the "tags" for them as wp_generate_tag_cloud expects to get. |
|
| 72 | - * |
|
| 73 | - * @since 3.11.0 |
|
| 74 | - * |
|
| 75 | - * @return array Array of tags. Empty array in case we re not in a context |
|
| 76 | - * of a post, or it has no related entities. |
|
| 77 | - */ |
|
| 78 | - public function get_related_entities_tags() { |
|
| 79 | - |
|
| 80 | - // Define the supported types list. |
|
| 81 | - $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 82 | - |
|
| 83 | - // Show nothing if not on a post or entity page. |
|
| 84 | - if ( ! is_singular( $supported_types ) ) { |
|
| 85 | - return array(); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - // Get the IDs of entities related to current post. |
|
| 89 | - $related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) ); |
|
| 90 | - |
|
| 91 | - // Bail out if there are no associated entities. |
|
| 92 | - if ( empty( $related_entities ) ) { |
|
| 93 | - return array(); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /* |
|
| 64 | + return '<div class="tagcloud wl-related-entities-cloud">' . |
|
| 65 | + wp_generate_tag_cloud( $tags ) . |
|
| 66 | + '</div>'; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Find the related entities to the currently displayed post and |
|
| 71 | + * calculate the "tags" for them as wp_generate_tag_cloud expects to get. |
|
| 72 | + * |
|
| 73 | + * @since 3.11.0 |
|
| 74 | + * |
|
| 75 | + * @return array Array of tags. Empty array in case we re not in a context |
|
| 76 | + * of a post, or it has no related entities. |
|
| 77 | + */ |
|
| 78 | + public function get_related_entities_tags() { |
|
| 79 | + |
|
| 80 | + // Define the supported types list. |
|
| 81 | + $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 82 | + |
|
| 83 | + // Show nothing if not on a post or entity page. |
|
| 84 | + if ( ! is_singular( $supported_types ) ) { |
|
| 85 | + return array(); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + // Get the IDs of entities related to current post. |
|
| 89 | + $related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) ); |
|
| 90 | + |
|
| 91 | + // Bail out if there are no associated entities. |
|
| 92 | + if ( empty( $related_entities ) ) { |
|
| 93 | + return array(); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /* |
|
| 97 | 97 | * Create an array of "tags" to feed to wp_generate_tag_cloud. |
| 98 | 98 | * Use the number of posts and entities connected to the entity as a weight. |
| 99 | 99 | */ |
| 100 | - $tags = array(); |
|
| 100 | + $tags = array(); |
|
| 101 | 101 | |
| 102 | - foreach ( array_unique( $related_entities ) as $entity_id ) { |
|
| 102 | + foreach ( array_unique( $related_entities ) as $entity_id ) { |
|
| 103 | 103 | |
| 104 | - $connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) ); |
|
| 105 | - $connected_posts = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) ); |
|
| 104 | + $connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) ); |
|
| 105 | + $connected_posts = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) ); |
|
| 106 | 106 | |
| 107 | - $tags[] = (object) array( |
|
| 108 | - 'id' => $entity_id, |
|
| 109 | - // Used to give a unique class on the tag. |
|
| 110 | - 'name' => get_the_title( $entity_id ), |
|
| 111 | - // The text of the tag. |
|
| 112 | - 'slug' => get_the_title( $entity_id ), |
|
| 113 | - // Required but not seem to be relevant |
|
| 114 | - 'link' => get_permalink( $entity_id ), |
|
| 115 | - // the url the tag links to. |
|
| 116 | - 'count' => $connected_entities + $connected_posts, |
|
| 117 | - // The weight. |
|
| 118 | - ); |
|
| 107 | + $tags[] = (object) array( |
|
| 108 | + 'id' => $entity_id, |
|
| 109 | + // Used to give a unique class on the tag. |
|
| 110 | + 'name' => get_the_title( $entity_id ), |
|
| 111 | + // The text of the tag. |
|
| 112 | + 'slug' => get_the_title( $entity_id ), |
|
| 113 | + // Required but not seem to be relevant |
|
| 114 | + 'link' => get_permalink( $entity_id ), |
|
| 115 | + // the url the tag links to. |
|
| 116 | + 'count' => $connected_entities + $connected_posts, |
|
| 117 | + // The weight. |
|
| 118 | + ); |
|
| 119 | 119 | |
| 120 | - } |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - return $tags; |
|
| 123 | - } |
|
| 122 | + return $tags; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | 125 | } |
| 126 | 126 | /** |
| 127 | 127 | * register_block_type for Gutenberg blocks |
| 128 | 128 | */ |
| 129 | 129 | add_action( 'init', function() { |
| 130 | - // Bail out if the `register_block_type` function isn't available. |
|
| 131 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 132 | - return; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - register_block_type('wordlift/cloud', array( |
|
| 136 | - 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 137 | - 'render_callback' => function($attributes){ |
|
| 138 | - return '[wl_cloud]'; |
|
| 139 | - } |
|
| 140 | - )); |
|
| 130 | + // Bail out if the `register_block_type` function isn't available. |
|
| 131 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 132 | + return; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + register_block_type('wordlift/cloud', array( |
|
| 136 | + 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 137 | + 'render_callback' => function($attributes){ |
|
| 138 | + return '[wl_cloud]'; |
|
| 139 | + } |
|
| 140 | + )); |
|
| 141 | 141 | } ); |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * |
| 38 | 38 | * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
| 39 | 39 | */ |
| 40 | - public function __construct( $relation_service ) { |
|
| 40 | + public function __construct($relation_service) { |
|
| 41 | 41 | parent::__construct(); |
| 42 | 42 | |
| 43 | 43 | $this->relation_service = $relation_service; |
@@ -47,12 +47,12 @@ discard block |
||
| 47 | 47 | /** |
| 48 | 48 | * {@inheritdoc} |
| 49 | 49 | */ |
| 50 | - public function render( $atts ) { |
|
| 50 | + public function render($atts) { |
|
| 51 | 51 | |
| 52 | 52 | $tags = $this->get_related_entities_tags(); |
| 53 | 53 | |
| 54 | 54 | // Bail out if there are no associated entities. |
| 55 | - if ( empty( $tags ) ) { |
|
| 55 | + if (empty($tags)) { |
|
| 56 | 56 | return ''; |
| 57 | 57 | } |
| 58 | 58 | |
@@ -61,8 +61,8 @@ discard block |
||
| 61 | 61 | * same class as the core tagcloud widget, to easily inherit its styling. |
| 62 | 62 | */ |
| 63 | 63 | |
| 64 | - return '<div class="tagcloud wl-related-entities-cloud">' . |
|
| 65 | - wp_generate_tag_cloud( $tags ) . |
|
| 64 | + return '<div class="tagcloud wl-related-entities-cloud">'. |
|
| 65 | + wp_generate_tag_cloud($tags). |
|
| 66 | 66 | '</div>'; |
| 67 | 67 | } |
| 68 | 68 | |
@@ -81,15 +81,15 @@ discard block |
||
| 81 | 81 | $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
| 82 | 82 | |
| 83 | 83 | // Show nothing if not on a post or entity page. |
| 84 | - if ( ! is_singular( $supported_types ) ) { |
|
| 84 | + if ( ! is_singular($supported_types)) { |
|
| 85 | 85 | return array(); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | // Get the IDs of entities related to current post. |
| 89 | - $related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) ); |
|
| 89 | + $related_entities = wl_core_get_related_entity_ids(get_the_ID(), array('status' => 'publish')); |
|
| 90 | 90 | |
| 91 | 91 | // Bail out if there are no associated entities. |
| 92 | - if ( empty( $related_entities ) ) { |
|
| 92 | + if (empty($related_entities)) { |
|
| 93 | 93 | return array(); |
| 94 | 94 | } |
| 95 | 95 | |
@@ -99,19 +99,19 @@ discard block |
||
| 99 | 99 | */ |
| 100 | 100 | $tags = array(); |
| 101 | 101 | |
| 102 | - foreach ( array_unique( $related_entities ) as $entity_id ) { |
|
| 102 | + foreach (array_unique($related_entities) as $entity_id) { |
|
| 103 | 103 | |
| 104 | - $connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) ); |
|
| 105 | - $connected_posts = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) ); |
|
| 104 | + $connected_entities = count(wl_core_get_related_entity_ids($entity_id, array('status' => 'publish'))); |
|
| 105 | + $connected_posts = count($this->relation_service->get_article_subjects($entity_id, '*', null, 'publish')); |
|
| 106 | 106 | |
| 107 | 107 | $tags[] = (object) array( |
| 108 | 108 | 'id' => $entity_id, |
| 109 | 109 | // Used to give a unique class on the tag. |
| 110 | - 'name' => get_the_title( $entity_id ), |
|
| 110 | + 'name' => get_the_title($entity_id), |
|
| 111 | 111 | // The text of the tag. |
| 112 | - 'slug' => get_the_title( $entity_id ), |
|
| 112 | + 'slug' => get_the_title($entity_id), |
|
| 113 | 113 | // Required but not seem to be relevant |
| 114 | - 'link' => get_permalink( $entity_id ), |
|
| 114 | + 'link' => get_permalink($entity_id), |
|
| 115 | 115 | // the url the tag links to. |
| 116 | 116 | 'count' => $connected_entities + $connected_posts, |
| 117 | 117 | // The weight. |
@@ -126,15 +126,15 @@ discard block |
||
| 126 | 126 | /** |
| 127 | 127 | * register_block_type for Gutenberg blocks |
| 128 | 128 | */ |
| 129 | -add_action( 'init', function() { |
|
| 129 | +add_action('init', function() { |
|
| 130 | 130 | // Bail out if the `register_block_type` function isn't available. |
| 131 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 131 | + if ( ! function_exists('register_block_type')) { |
|
| 132 | 132 | return; |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | register_block_type('wordlift/cloud', array( |
| 136 | 136 | 'editor_script' => 'wordlift-admin-edit-gutenberg', |
| 137 | - 'render_callback' => function($attributes){ |
|
| 137 | + 'render_callback' => function($attributes) { |
|
| 138 | 138 | return '[wl_cloud]'; |
| 139 | 139 | } |
| 140 | 140 | )); |
@@ -17,116 +17,116 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_ShareThis_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * The ShareThis function which prints the buttons. |
|
| 22 | - * |
|
| 23 | - * @since 3.2.0 |
|
| 24 | - */ |
|
| 25 | - const ADD_WIDGET_FUNCTION_NAME = 'st_add_widget'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The Log service. |
|
| 29 | - * |
|
| 30 | - * @since 3.2.0 |
|
| 31 | - * @access private |
|
| 32 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 33 | - */ |
|
| 34 | - private $log_service; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Create an instance of the ShareThis service. |
|
| 38 | - * |
|
| 39 | - * @since 3.2.0 |
|
| 40 | - */ |
|
| 41 | - public function __construct() { |
|
| 42 | - |
|
| 43 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_ShareThis_Service' ); |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Receive <em>the_content</em> filter calls from WordPress. |
|
| 49 | - * |
|
| 50 | - * @since 3.2.0 |
|
| 51 | - * |
|
| 52 | - * @param string $content The post content. |
|
| 53 | - * |
|
| 54 | - * @return string The updated post content. |
|
| 55 | - */ |
|
| 56 | - public function the_content( $content ) { |
|
| 57 | - |
|
| 58 | - return $this->call_sharethis( 'the_content', $content ); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Receive <em>the_excerpt</em> filter calls from WordPress. |
|
| 63 | - * |
|
| 64 | - * @since 3.2.0 |
|
| 65 | - * |
|
| 66 | - * @param string $content The post excerpt. |
|
| 67 | - * |
|
| 68 | - * @return string The updated post excerpt. |
|
| 69 | - */ |
|
| 70 | - public function the_excerpt( $content ) { |
|
| 71 | - |
|
| 72 | - return $this->call_sharethis( 'the_excerpt', $content ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Call the ShareThis function. |
|
| 77 | - * |
|
| 78 | - * @since 3.2.0 |
|
| 79 | - * |
|
| 80 | - * @param string $tag The filter tag. |
|
| 81 | - * @param string $content The post content. |
|
| 82 | - * |
|
| 83 | - * @return string The updated post content. |
|
| 84 | - */ |
|
| 85 | - private function call_sharethis( $tag, $content ) { |
|
| 86 | - |
|
| 87 | - // Get the current post. |
|
| 88 | - global $post; |
|
| 89 | - |
|
| 90 | - // Bail out if the global $post instance isn't set. |
|
| 91 | - if ( !isset( $post ) ) { |
|
| 92 | - return $content; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - // Bail out if the current entity is a post/page since this is already handled by ShareThis. |
|
| 96 | - // |
|
| 97 | - // See https://github.com/insideout10/wordlift-plugin/issues/819 |
|
| 98 | - if ( 'post' === $post->post_type || 'page' === $post->post_type ) { |
|
| 99 | - return $content; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // If it's not the entity type, return. |
|
| 103 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 104 | - if ( null === $post || ! $entity_service->is_entity( get_the_ID() ) ) { |
|
| 105 | - return $content; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // If the ShareThis function doesn't exist, return. |
|
| 109 | - if ( ! function_exists( self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 110 | - return $content; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - // If ShareThis hasn't been added as a filter, return. |
|
| 114 | - if ( ! has_filter( $tag, self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 115 | - return $content; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - // Temporary pop the post type and replace it with post. |
|
| 119 | - $post_type = $post->post_type; |
|
| 120 | - $post->post_type = 'post'; |
|
| 121 | - |
|
| 122 | - // Call ShareThis (disguised as a post). |
|
| 123 | - $content = call_user_func_array( self::ADD_WIDGET_FUNCTION_NAME, array( $content ) ); |
|
| 124 | - |
|
| 125 | - // Restore our post type. |
|
| 126 | - $post->post_type = $post_type; |
|
| 127 | - |
|
| 128 | - // Finally return the content. |
|
| 129 | - return $content; |
|
| 130 | - } |
|
| 20 | + /** |
|
| 21 | + * The ShareThis function which prints the buttons. |
|
| 22 | + * |
|
| 23 | + * @since 3.2.0 |
|
| 24 | + */ |
|
| 25 | + const ADD_WIDGET_FUNCTION_NAME = 'st_add_widget'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The Log service. |
|
| 29 | + * |
|
| 30 | + * @since 3.2.0 |
|
| 31 | + * @access private |
|
| 32 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 33 | + */ |
|
| 34 | + private $log_service; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Create an instance of the ShareThis service. |
|
| 38 | + * |
|
| 39 | + * @since 3.2.0 |
|
| 40 | + */ |
|
| 41 | + public function __construct() { |
|
| 42 | + |
|
| 43 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_ShareThis_Service' ); |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Receive <em>the_content</em> filter calls from WordPress. |
|
| 49 | + * |
|
| 50 | + * @since 3.2.0 |
|
| 51 | + * |
|
| 52 | + * @param string $content The post content. |
|
| 53 | + * |
|
| 54 | + * @return string The updated post content. |
|
| 55 | + */ |
|
| 56 | + public function the_content( $content ) { |
|
| 57 | + |
|
| 58 | + return $this->call_sharethis( 'the_content', $content ); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Receive <em>the_excerpt</em> filter calls from WordPress. |
|
| 63 | + * |
|
| 64 | + * @since 3.2.0 |
|
| 65 | + * |
|
| 66 | + * @param string $content The post excerpt. |
|
| 67 | + * |
|
| 68 | + * @return string The updated post excerpt. |
|
| 69 | + */ |
|
| 70 | + public function the_excerpt( $content ) { |
|
| 71 | + |
|
| 72 | + return $this->call_sharethis( 'the_excerpt', $content ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Call the ShareThis function. |
|
| 77 | + * |
|
| 78 | + * @since 3.2.0 |
|
| 79 | + * |
|
| 80 | + * @param string $tag The filter tag. |
|
| 81 | + * @param string $content The post content. |
|
| 82 | + * |
|
| 83 | + * @return string The updated post content. |
|
| 84 | + */ |
|
| 85 | + private function call_sharethis( $tag, $content ) { |
|
| 86 | + |
|
| 87 | + // Get the current post. |
|
| 88 | + global $post; |
|
| 89 | + |
|
| 90 | + // Bail out if the global $post instance isn't set. |
|
| 91 | + if ( !isset( $post ) ) { |
|
| 92 | + return $content; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + // Bail out if the current entity is a post/page since this is already handled by ShareThis. |
|
| 96 | + // |
|
| 97 | + // See https://github.com/insideout10/wordlift-plugin/issues/819 |
|
| 98 | + if ( 'post' === $post->post_type || 'page' === $post->post_type ) { |
|
| 99 | + return $content; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // If it's not the entity type, return. |
|
| 103 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 104 | + if ( null === $post || ! $entity_service->is_entity( get_the_ID() ) ) { |
|
| 105 | + return $content; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // If the ShareThis function doesn't exist, return. |
|
| 109 | + if ( ! function_exists( self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 110 | + return $content; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + // If ShareThis hasn't been added as a filter, return. |
|
| 114 | + if ( ! has_filter( $tag, self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 115 | + return $content; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + // Temporary pop the post type and replace it with post. |
|
| 119 | + $post_type = $post->post_type; |
|
| 120 | + $post->post_type = 'post'; |
|
| 121 | + |
|
| 122 | + // Call ShareThis (disguised as a post). |
|
| 123 | + $content = call_user_func_array( self::ADD_WIDGET_FUNCTION_NAME, array( $content ) ); |
|
| 124 | + |
|
| 125 | + // Restore our post type. |
|
| 126 | + $post->post_type = $post_type; |
|
| 127 | + |
|
| 128 | + // Finally return the content. |
|
| 129 | + return $content; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | 132 | } |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public function __construct() { |
| 42 | 42 | |
| 43 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_ShareThis_Service' ); |
|
| 43 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_ShareThis_Service'); |
|
| 44 | 44 | |
| 45 | 45 | } |
| 46 | 46 | |
@@ -53,9 +53,9 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @return string The updated post content. |
| 55 | 55 | */ |
| 56 | - public function the_content( $content ) { |
|
| 56 | + public function the_content($content) { |
|
| 57 | 57 | |
| 58 | - return $this->call_sharethis( 'the_content', $content ); |
|
| 58 | + return $this->call_sharethis('the_content', $content); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
@@ -67,9 +67,9 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return string The updated post excerpt. |
| 69 | 69 | */ |
| 70 | - public function the_excerpt( $content ) { |
|
| 70 | + public function the_excerpt($content) { |
|
| 71 | 71 | |
| 72 | - return $this->call_sharethis( 'the_excerpt', $content ); |
|
| 72 | + return $this->call_sharethis('the_excerpt', $content); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -82,36 +82,36 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @return string The updated post content. |
| 84 | 84 | */ |
| 85 | - private function call_sharethis( $tag, $content ) { |
|
| 85 | + private function call_sharethis($tag, $content) { |
|
| 86 | 86 | |
| 87 | 87 | // Get the current post. |
| 88 | 88 | global $post; |
| 89 | 89 | |
| 90 | 90 | // Bail out if the global $post instance isn't set. |
| 91 | - if ( !isset( $post ) ) { |
|
| 91 | + if ( ! isset($post)) { |
|
| 92 | 92 | return $content; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | // Bail out if the current entity is a post/page since this is already handled by ShareThis. |
| 96 | 96 | // |
| 97 | 97 | // See https://github.com/insideout10/wordlift-plugin/issues/819 |
| 98 | - if ( 'post' === $post->post_type || 'page' === $post->post_type ) { |
|
| 98 | + if ('post' === $post->post_type || 'page' === $post->post_type) { |
|
| 99 | 99 | return $content; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | // If it's not the entity type, return. |
| 103 | 103 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 104 | - if ( null === $post || ! $entity_service->is_entity( get_the_ID() ) ) { |
|
| 104 | + if (null === $post || ! $entity_service->is_entity(get_the_ID())) { |
|
| 105 | 105 | return $content; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // If the ShareThis function doesn't exist, return. |
| 109 | - if ( ! function_exists( self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 109 | + if ( ! function_exists(self::ADD_WIDGET_FUNCTION_NAME)) { |
|
| 110 | 110 | return $content; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | // If ShareThis hasn't been added as a filter, return. |
| 114 | - if ( ! has_filter( $tag, self::ADD_WIDGET_FUNCTION_NAME ) ) { |
|
| 114 | + if ( ! has_filter($tag, self::ADD_WIDGET_FUNCTION_NAME)) { |
|
| 115 | 115 | return $content; |
| 116 | 116 | } |
| 117 | 117 | |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | $post->post_type = 'post'; |
| 121 | 121 | |
| 122 | 122 | // Call ShareThis (disguised as a post). |
| 123 | - $content = call_user_func_array( self::ADD_WIDGET_FUNCTION_NAME, array( $content ) ); |
|
| 123 | + $content = call_user_func_array(self::ADD_WIDGET_FUNCTION_NAME, array($content)); |
|
| 124 | 124 | |
| 125 | 125 | // Restore our post type. |
| 126 | 126 | $post->post_type = $post_type; |
@@ -7,203 +7,203 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Timeline_Shortcode extends Wordlift_Shortcode { |
| 9 | 9 | |
| 10 | - const SHORTCODE = 'wl_timeline'; |
|
| 11 | - |
|
| 12 | - /** |
|
| 13 | - * The list of locales supported by TimelineJS (correspond to the list of |
|
| 14 | - * files in the locale subfolder). |
|
| 15 | - * |
|
| 16 | - * @since 3.7.0 |
|
| 17 | - * @var array An array of two-letters language codes. |
|
| 18 | - */ |
|
| 19 | - private static $supported_locales = array( |
|
| 20 | - 'ur', |
|
| 21 | - 'uk', |
|
| 22 | - 'tr', |
|
| 23 | - 'tl', |
|
| 24 | - 'th', |
|
| 25 | - 'te', |
|
| 26 | - 'ta', |
|
| 27 | - 'sv', |
|
| 28 | - 'sr', |
|
| 29 | - 'sl', |
|
| 30 | - 'sk', |
|
| 31 | - 'si', |
|
| 32 | - 'ru', |
|
| 33 | - 'ro', |
|
| 34 | - 'rm', |
|
| 35 | - 'pt', |
|
| 36 | - 'pl', |
|
| 37 | - 'no', |
|
| 38 | - 'nl', |
|
| 39 | - 'ne', |
|
| 40 | - 'ms', |
|
| 41 | - 'lv', |
|
| 42 | - 'lt', |
|
| 43 | - 'lb', |
|
| 44 | - 'ko', |
|
| 45 | - 'ka', |
|
| 46 | - 'ja', |
|
| 47 | - 'iw', |
|
| 48 | - 'it', |
|
| 49 | - 'is', |
|
| 50 | - 'id', |
|
| 51 | - 'hy', |
|
| 52 | - 'hu', |
|
| 53 | - 'hr', |
|
| 54 | - 'hi', |
|
| 55 | - 'he', |
|
| 56 | - 'gl', |
|
| 57 | - 'ga', |
|
| 58 | - 'fy', |
|
| 59 | - 'fr', |
|
| 60 | - 'fo', |
|
| 61 | - 'fi', |
|
| 62 | - 'fa', |
|
| 63 | - 'eu', |
|
| 64 | - 'et', |
|
| 65 | - 'es', |
|
| 66 | - 'eo', |
|
| 67 | - 'en', |
|
| 68 | - 'el', |
|
| 69 | - 'de', |
|
| 70 | - 'da', |
|
| 71 | - 'cz', |
|
| 72 | - 'ca', |
|
| 73 | - 'bg', |
|
| 74 | - 'be', |
|
| 75 | - 'ar', |
|
| 76 | - 'af' |
|
| 77 | - ); |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * The Log service. |
|
| 81 | - * |
|
| 82 | - * @since 3.1.0 |
|
| 83 | - * @access private |
|
| 84 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 85 | - */ |
|
| 86 | - private $log_service; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Create a Wordlift_Timeline_Shortcode instance. |
|
| 90 | - * |
|
| 91 | - * @since 3.1.0 |
|
| 92 | - */ |
|
| 93 | - public function __construct() { |
|
| 94 | - parent::__construct(); |
|
| 95 | - |
|
| 96 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Shortcode' ); |
|
| 97 | - |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - public function get_timelinejs_default_options() { |
|
| 101 | - return array( |
|
| 102 | - 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, |
|
| 103 | - 'height' => NULL, |
|
| 104 | - 'width' => NULL, |
|
| 105 | - 'is_embed' => FALSE, |
|
| 106 | - 'hash_bookmark' => FALSE, |
|
| 107 | - 'default_bg_color' => 'white', |
|
| 108 | - 'scale_factor' => 2, |
|
| 109 | - 'initial_zoom' => NULL, |
|
| 110 | - 'zoom_sequence' => '[0.5, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]', |
|
| 111 | - 'timenav_position' => 'bottom', |
|
| 112 | - 'optimal_tick_width' => 100, |
|
| 113 | - 'base_class' => 'tl-timeline', |
|
| 114 | - 'timenav_height' => 150, |
|
| 115 | - 'timenav_height_percentage' => NULL, |
|
| 116 | - 'timenav_mobile_height_percentage' => 40, |
|
| 117 | - 'timenav_height_min' => 150, |
|
| 118 | - 'marker_height_min' => 30, |
|
| 119 | - 'marker_width_min' => 100, |
|
| 120 | - 'marker_padding' => 5, |
|
| 121 | - 'start_at_slide' => 0, |
|
| 122 | - 'start_at_end' => FALSE, |
|
| 123 | - 'menubar_height' => 0, |
|
| 124 | - 'use_bc' => FALSE, |
|
| 125 | - 'duration' => 1000, |
|
| 126 | - 'ease' => 'TL.Ease.easeInOutQuint', |
|
| 127 | - 'slide_default_fade' => '0%', |
|
| 128 | - 'language' => $this->get_locale(), |
|
| 129 | - 'ga_property_id' => NULL, |
|
| 130 | - 'track_events' => "['back_to_start','nav_next','nav_previous','zoom_in','zoom_out']" |
|
| 131 | - ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Renders the Timeline. |
|
| 136 | - * |
|
| 137 | - * @since 3.1.0 |
|
| 138 | - * |
|
| 139 | - * @param array $atts An array of shortcode attributes. |
|
| 140 | - * |
|
| 141 | - * @return string The rendered HTML. |
|
| 142 | - */ |
|
| 143 | - public function render( $atts ) { |
|
| 144 | - |
|
| 145 | - //extract attributes and set default values |
|
| 146 | - $settings = shortcode_atts( array_merge($this->get_timelinejs_default_options(), array( |
|
| 147 | - 'global' => FALSE, |
|
| 148 | - 'display_images_as' => 'media', |
|
| 149 | - 'excerpt_length' => 55 |
|
| 150 | - )), $atts ); |
|
| 151 | - |
|
| 152 | - // Load the TimelineJS stylesheets and scripts. |
|
| 153 | - wp_enqueue_style( 'timelinejs', dirname( plugin_dir_url( __FILE__ ) ) . '/timelinejs/css/timeline.css' ); |
|
| 154 | - wp_enqueue_script( 'timelinejs', dirname( plugin_dir_url( __FILE__ ) ) . '/timelinejs/js/timeline' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '-min' : '' ) . '.js' ); |
|
| 155 | - |
|
| 156 | - // Enqueue the scripts for the timeline. |
|
| 157 | - $this->enqueue_scripts(); |
|
| 158 | - |
|
| 159 | - // Provide the script with options. |
|
| 160 | - wp_localize_script( 'timelinejs', 'wl_timeline_params', array( |
|
| 161 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 162 | - // TODO: this parameter is already provided by WP |
|
| 163 | - 'action' => 'wl_timeline', |
|
| 164 | - // These settings apply to our wl_timeline AJAX endpoint. |
|
| 165 | - 'display_images_as' => $settings['display_images_as'], |
|
| 166 | - 'excerpt_length' => $settings['excerpt_length'], |
|
| 167 | - // These settings apply to the timeline javascript client. |
|
| 168 | - 'settings' => array_filter( $settings, function ( $value ) { |
|
| 169 | - // Do not set NULL values. |
|
| 170 | - return ( NULL !== $value ); |
|
| 171 | - } ) |
|
| 172 | - ) ); |
|
| 173 | - |
|
| 174 | - // Get the current post id or set null if global is set to true. |
|
| 175 | - $post_id = ( $settings['global'] ? NULL : get_the_ID() ); |
|
| 176 | - |
|
| 177 | - // Escaping atts. |
|
| 178 | - $style = sprintf( 'style="%s%s"', isset( $settings['width'] ) ? "width:{$settings['width']};" : '', isset( $settings['height'] ) ? "height:{$settings['height']};" : '' ); |
|
| 179 | - $data_post_id = ( isset( $post_id ) ? "data-post-id='$post_id'" : '' ); |
|
| 180 | - |
|
| 181 | - // Generate a unique ID for this timeline. |
|
| 182 | - $element_id = uniqid( 'wl-timeline-' ); |
|
| 183 | - |
|
| 184 | - if ( WP_DEBUG ) { |
|
| 185 | - $this->log_service->trace( "Creating a timeline widget [ element id :: $element_id ][ post id :: $post_id ]" ); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - // Building template. |
|
| 189 | - return sprintf( '<div class="wl-timeline-container" %s><div class="wl-timeline" id="%s" %s></div></div>', $style, $element_id, $data_post_id ); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Return the locale for the TimelineJS according to WP's configured locale and |
|
| 194 | - * support TimelineJS locales. If WP's locale is not supported, english is used. |
|
| 195 | - * |
|
| 196 | - * @since 3.7.0 |
|
| 197 | - * @return string The locale (2 letters code). |
|
| 198 | - */ |
|
| 199 | - private function get_locale() { |
|
| 200 | - |
|
| 201 | - // Get the first 2 letters. |
|
| 202 | - $locale = substr( get_locale(), 0, 2 ); |
|
| 203 | - |
|
| 204 | - // Check that the specified locale is supported otherwise use English. |
|
| 205 | - return in_array( $locale, self::$supported_locales ) ? $locale : 'en'; |
|
| 206 | - } |
|
| 10 | + const SHORTCODE = 'wl_timeline'; |
|
| 11 | + |
|
| 12 | + /** |
|
| 13 | + * The list of locales supported by TimelineJS (correspond to the list of |
|
| 14 | + * files in the locale subfolder). |
|
| 15 | + * |
|
| 16 | + * @since 3.7.0 |
|
| 17 | + * @var array An array of two-letters language codes. |
|
| 18 | + */ |
|
| 19 | + private static $supported_locales = array( |
|
| 20 | + 'ur', |
|
| 21 | + 'uk', |
|
| 22 | + 'tr', |
|
| 23 | + 'tl', |
|
| 24 | + 'th', |
|
| 25 | + 'te', |
|
| 26 | + 'ta', |
|
| 27 | + 'sv', |
|
| 28 | + 'sr', |
|
| 29 | + 'sl', |
|
| 30 | + 'sk', |
|
| 31 | + 'si', |
|
| 32 | + 'ru', |
|
| 33 | + 'ro', |
|
| 34 | + 'rm', |
|
| 35 | + 'pt', |
|
| 36 | + 'pl', |
|
| 37 | + 'no', |
|
| 38 | + 'nl', |
|
| 39 | + 'ne', |
|
| 40 | + 'ms', |
|
| 41 | + 'lv', |
|
| 42 | + 'lt', |
|
| 43 | + 'lb', |
|
| 44 | + 'ko', |
|
| 45 | + 'ka', |
|
| 46 | + 'ja', |
|
| 47 | + 'iw', |
|
| 48 | + 'it', |
|
| 49 | + 'is', |
|
| 50 | + 'id', |
|
| 51 | + 'hy', |
|
| 52 | + 'hu', |
|
| 53 | + 'hr', |
|
| 54 | + 'hi', |
|
| 55 | + 'he', |
|
| 56 | + 'gl', |
|
| 57 | + 'ga', |
|
| 58 | + 'fy', |
|
| 59 | + 'fr', |
|
| 60 | + 'fo', |
|
| 61 | + 'fi', |
|
| 62 | + 'fa', |
|
| 63 | + 'eu', |
|
| 64 | + 'et', |
|
| 65 | + 'es', |
|
| 66 | + 'eo', |
|
| 67 | + 'en', |
|
| 68 | + 'el', |
|
| 69 | + 'de', |
|
| 70 | + 'da', |
|
| 71 | + 'cz', |
|
| 72 | + 'ca', |
|
| 73 | + 'bg', |
|
| 74 | + 'be', |
|
| 75 | + 'ar', |
|
| 76 | + 'af' |
|
| 77 | + ); |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * The Log service. |
|
| 81 | + * |
|
| 82 | + * @since 3.1.0 |
|
| 83 | + * @access private |
|
| 84 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 85 | + */ |
|
| 86 | + private $log_service; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Create a Wordlift_Timeline_Shortcode instance. |
|
| 90 | + * |
|
| 91 | + * @since 3.1.0 |
|
| 92 | + */ |
|
| 93 | + public function __construct() { |
|
| 94 | + parent::__construct(); |
|
| 95 | + |
|
| 96 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Shortcode' ); |
|
| 97 | + |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + public function get_timelinejs_default_options() { |
|
| 101 | + return array( |
|
| 102 | + 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, |
|
| 103 | + 'height' => NULL, |
|
| 104 | + 'width' => NULL, |
|
| 105 | + 'is_embed' => FALSE, |
|
| 106 | + 'hash_bookmark' => FALSE, |
|
| 107 | + 'default_bg_color' => 'white', |
|
| 108 | + 'scale_factor' => 2, |
|
| 109 | + 'initial_zoom' => NULL, |
|
| 110 | + 'zoom_sequence' => '[0.5, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]', |
|
| 111 | + 'timenav_position' => 'bottom', |
|
| 112 | + 'optimal_tick_width' => 100, |
|
| 113 | + 'base_class' => 'tl-timeline', |
|
| 114 | + 'timenav_height' => 150, |
|
| 115 | + 'timenav_height_percentage' => NULL, |
|
| 116 | + 'timenav_mobile_height_percentage' => 40, |
|
| 117 | + 'timenav_height_min' => 150, |
|
| 118 | + 'marker_height_min' => 30, |
|
| 119 | + 'marker_width_min' => 100, |
|
| 120 | + 'marker_padding' => 5, |
|
| 121 | + 'start_at_slide' => 0, |
|
| 122 | + 'start_at_end' => FALSE, |
|
| 123 | + 'menubar_height' => 0, |
|
| 124 | + 'use_bc' => FALSE, |
|
| 125 | + 'duration' => 1000, |
|
| 126 | + 'ease' => 'TL.Ease.easeInOutQuint', |
|
| 127 | + 'slide_default_fade' => '0%', |
|
| 128 | + 'language' => $this->get_locale(), |
|
| 129 | + 'ga_property_id' => NULL, |
|
| 130 | + 'track_events' => "['back_to_start','nav_next','nav_previous','zoom_in','zoom_out']" |
|
| 131 | + ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Renders the Timeline. |
|
| 136 | + * |
|
| 137 | + * @since 3.1.0 |
|
| 138 | + * |
|
| 139 | + * @param array $atts An array of shortcode attributes. |
|
| 140 | + * |
|
| 141 | + * @return string The rendered HTML. |
|
| 142 | + */ |
|
| 143 | + public function render( $atts ) { |
|
| 144 | + |
|
| 145 | + //extract attributes and set default values |
|
| 146 | + $settings = shortcode_atts( array_merge($this->get_timelinejs_default_options(), array( |
|
| 147 | + 'global' => FALSE, |
|
| 148 | + 'display_images_as' => 'media', |
|
| 149 | + 'excerpt_length' => 55 |
|
| 150 | + )), $atts ); |
|
| 151 | + |
|
| 152 | + // Load the TimelineJS stylesheets and scripts. |
|
| 153 | + wp_enqueue_style( 'timelinejs', dirname( plugin_dir_url( __FILE__ ) ) . '/timelinejs/css/timeline.css' ); |
|
| 154 | + wp_enqueue_script( 'timelinejs', dirname( plugin_dir_url( __FILE__ ) ) . '/timelinejs/js/timeline' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '-min' : '' ) . '.js' ); |
|
| 155 | + |
|
| 156 | + // Enqueue the scripts for the timeline. |
|
| 157 | + $this->enqueue_scripts(); |
|
| 158 | + |
|
| 159 | + // Provide the script with options. |
|
| 160 | + wp_localize_script( 'timelinejs', 'wl_timeline_params', array( |
|
| 161 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 162 | + // TODO: this parameter is already provided by WP |
|
| 163 | + 'action' => 'wl_timeline', |
|
| 164 | + // These settings apply to our wl_timeline AJAX endpoint. |
|
| 165 | + 'display_images_as' => $settings['display_images_as'], |
|
| 166 | + 'excerpt_length' => $settings['excerpt_length'], |
|
| 167 | + // These settings apply to the timeline javascript client. |
|
| 168 | + 'settings' => array_filter( $settings, function ( $value ) { |
|
| 169 | + // Do not set NULL values. |
|
| 170 | + return ( NULL !== $value ); |
|
| 171 | + } ) |
|
| 172 | + ) ); |
|
| 173 | + |
|
| 174 | + // Get the current post id or set null if global is set to true. |
|
| 175 | + $post_id = ( $settings['global'] ? NULL : get_the_ID() ); |
|
| 176 | + |
|
| 177 | + // Escaping atts. |
|
| 178 | + $style = sprintf( 'style="%s%s"', isset( $settings['width'] ) ? "width:{$settings['width']};" : '', isset( $settings['height'] ) ? "height:{$settings['height']};" : '' ); |
|
| 179 | + $data_post_id = ( isset( $post_id ) ? "data-post-id='$post_id'" : '' ); |
|
| 180 | + |
|
| 181 | + // Generate a unique ID for this timeline. |
|
| 182 | + $element_id = uniqid( 'wl-timeline-' ); |
|
| 183 | + |
|
| 184 | + if ( WP_DEBUG ) { |
|
| 185 | + $this->log_service->trace( "Creating a timeline widget [ element id :: $element_id ][ post id :: $post_id ]" ); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + // Building template. |
|
| 189 | + return sprintf( '<div class="wl-timeline-container" %s><div class="wl-timeline" id="%s" %s></div></div>', $style, $element_id, $data_post_id ); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Return the locale for the TimelineJS according to WP's configured locale and |
|
| 194 | + * support TimelineJS locales. If WP's locale is not supported, english is used. |
|
| 195 | + * |
|
| 196 | + * @since 3.7.0 |
|
| 197 | + * @return string The locale (2 letters code). |
|
| 198 | + */ |
|
| 199 | + private function get_locale() { |
|
| 200 | + |
|
| 201 | + // Get the first 2 letters. |
|
| 202 | + $locale = substr( get_locale(), 0, 2 ); |
|
| 203 | + |
|
| 204 | + // Check that the specified locale is supported otherwise use English. |
|
| 205 | + return in_array( $locale, self::$supported_locales ) ? $locale : 'en'; |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | 208 | } |
| 209 | 209 | |
@@ -211,43 +211,43 @@ discard block |
||
| 211 | 211 | * register_block_type for Gutenberg blocks |
| 212 | 212 | */ |
| 213 | 213 | add_action( 'init', function() { |
| 214 | - // Bail out if the `register_block_type` function isn't available. |
|
| 215 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 216 | - return; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode(); |
|
| 220 | - register_block_type('wordlift/timeline', array( |
|
| 221 | - 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 222 | - 'render_callback' => function($attributes){ |
|
| 223 | - $attr_code = ''; |
|
| 224 | - $timelinejs_options = json_decode($attributes['timelinejs_options'], true); |
|
| 225 | - unset($attributes['timelinejs_options']); |
|
| 226 | - $attributes_all = array_merge($attributes, $timelinejs_options); |
|
| 227 | - foreach ($attributes_all as $key => $value) { |
|
| 228 | - if($value && strpos($value, '[') === false && strpos($value, ']') === false) { |
|
| 229 | - $attr_code .= $key.'="'.$value.'" '; |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - return '[wl_timeline '.$attr_code.']'; |
|
| 233 | - }, |
|
| 234 | - 'attributes' => array( |
|
| 235 | - 'display_images_as' => array( |
|
| 236 | - 'type' => 'string', |
|
| 237 | - 'default' => 'media' |
|
| 238 | - ), |
|
| 239 | - 'excerpt_length' => array( |
|
| 240 | - 'type' => 'number', |
|
| 241 | - 'default' => 55 |
|
| 242 | - ), |
|
| 243 | - 'global' => array( |
|
| 244 | - 'type' => 'bool', |
|
| 245 | - 'default' => false |
|
| 246 | - ), |
|
| 247 | - 'timelinejs_options' => array( |
|
| 248 | - 'type' => 'string', // https://timeline.knightlab.com/docs/options.html |
|
| 249 | - 'default' => json_encode($wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT) |
|
| 250 | - ) |
|
| 251 | - ) |
|
| 252 | - )); |
|
| 214 | + // Bail out if the `register_block_type` function isn't available. |
|
| 215 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 216 | + return; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode(); |
|
| 220 | + register_block_type('wordlift/timeline', array( |
|
| 221 | + 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 222 | + 'render_callback' => function($attributes){ |
|
| 223 | + $attr_code = ''; |
|
| 224 | + $timelinejs_options = json_decode($attributes['timelinejs_options'], true); |
|
| 225 | + unset($attributes['timelinejs_options']); |
|
| 226 | + $attributes_all = array_merge($attributes, $timelinejs_options); |
|
| 227 | + foreach ($attributes_all as $key => $value) { |
|
| 228 | + if($value && strpos($value, '[') === false && strpos($value, ']') === false) { |
|
| 229 | + $attr_code .= $key.'="'.$value.'" '; |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + return '[wl_timeline '.$attr_code.']'; |
|
| 233 | + }, |
|
| 234 | + 'attributes' => array( |
|
| 235 | + 'display_images_as' => array( |
|
| 236 | + 'type' => 'string', |
|
| 237 | + 'default' => 'media' |
|
| 238 | + ), |
|
| 239 | + 'excerpt_length' => array( |
|
| 240 | + 'type' => 'number', |
|
| 241 | + 'default' => 55 |
|
| 242 | + ), |
|
| 243 | + 'global' => array( |
|
| 244 | + 'type' => 'bool', |
|
| 245 | + 'default' => false |
|
| 246 | + ), |
|
| 247 | + 'timelinejs_options' => array( |
|
| 248 | + 'type' => 'string', // https://timeline.knightlab.com/docs/options.html |
|
| 249 | + 'default' => json_encode($wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT) |
|
| 250 | + ) |
|
| 251 | + ) |
|
| 252 | + )); |
|
| 253 | 253 | } ); |
@@ -93,13 +93,13 @@ discard block |
||
| 93 | 93 | public function __construct() { |
| 94 | 94 | parent::__construct(); |
| 95 | 95 | |
| 96 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Shortcode' ); |
|
| 96 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Timeline_Shortcode'); |
|
| 97 | 97 | |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | public function get_timelinejs_default_options() { |
| 101 | 101 | return array( |
| 102 | - 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, |
|
| 102 | + 'debug' => defined('WP_DEBUG') && WP_DEBUG, |
|
| 103 | 103 | 'height' => NULL, |
| 104 | 104 | 'width' => NULL, |
| 105 | 105 | 'is_embed' => FALSE, |
@@ -140,53 +140,53 @@ discard block |
||
| 140 | 140 | * |
| 141 | 141 | * @return string The rendered HTML. |
| 142 | 142 | */ |
| 143 | - public function render( $atts ) { |
|
| 143 | + public function render($atts) { |
|
| 144 | 144 | |
| 145 | 145 | //extract attributes and set default values |
| 146 | - $settings = shortcode_atts( array_merge($this->get_timelinejs_default_options(), array( |
|
| 146 | + $settings = shortcode_atts(array_merge($this->get_timelinejs_default_options(), array( |
|
| 147 | 147 | 'global' => FALSE, |
| 148 | 148 | 'display_images_as' => 'media', |
| 149 | 149 | 'excerpt_length' => 55 |
| 150 | - )), $atts ); |
|
| 150 | + )), $atts); |
|
| 151 | 151 | |
| 152 | 152 | // Load the TimelineJS stylesheets and scripts. |
| 153 | - wp_enqueue_style( 'timelinejs', dirname( plugin_dir_url( __FILE__ ) ) . '/timelinejs/css/timeline.css' ); |
|
| 154 | - wp_enqueue_script( 'timelinejs', dirname( plugin_dir_url( __FILE__ ) ) . '/timelinejs/js/timeline' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '-min' : '' ) . '.js' ); |
|
| 153 | + wp_enqueue_style('timelinejs', dirname(plugin_dir_url(__FILE__)).'/timelinejs/css/timeline.css'); |
|
| 154 | + wp_enqueue_script('timelinejs', dirname(plugin_dir_url(__FILE__)).'/timelinejs/js/timeline'.( ! defined('SCRIPT_DEBUG') || ! SCRIPT_DEBUG ? '-min' : '').'.js'); |
|
| 155 | 155 | |
| 156 | 156 | // Enqueue the scripts for the timeline. |
| 157 | 157 | $this->enqueue_scripts(); |
| 158 | 158 | |
| 159 | 159 | // Provide the script with options. |
| 160 | - wp_localize_script( 'timelinejs', 'wl_timeline_params', array( |
|
| 161 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 160 | + wp_localize_script('timelinejs', 'wl_timeline_params', array( |
|
| 161 | + 'ajax_url' => admin_url('admin-ajax.php'), |
|
| 162 | 162 | // TODO: this parameter is already provided by WP |
| 163 | 163 | 'action' => 'wl_timeline', |
| 164 | 164 | // These settings apply to our wl_timeline AJAX endpoint. |
| 165 | 165 | 'display_images_as' => $settings['display_images_as'], |
| 166 | 166 | 'excerpt_length' => $settings['excerpt_length'], |
| 167 | 167 | // These settings apply to the timeline javascript client. |
| 168 | - 'settings' => array_filter( $settings, function ( $value ) { |
|
| 168 | + 'settings' => array_filter($settings, function($value) { |
|
| 169 | 169 | // Do not set NULL values. |
| 170 | - return ( NULL !== $value ); |
|
| 170 | + return (NULL !== $value); |
|
| 171 | 171 | } ) |
| 172 | - ) ); |
|
| 172 | + )); |
|
| 173 | 173 | |
| 174 | 174 | // Get the current post id or set null if global is set to true. |
| 175 | - $post_id = ( $settings['global'] ? NULL : get_the_ID() ); |
|
| 175 | + $post_id = ($settings['global'] ? NULL : get_the_ID()); |
|
| 176 | 176 | |
| 177 | 177 | // Escaping atts. |
| 178 | - $style = sprintf( 'style="%s%s"', isset( $settings['width'] ) ? "width:{$settings['width']};" : '', isset( $settings['height'] ) ? "height:{$settings['height']};" : '' ); |
|
| 179 | - $data_post_id = ( isset( $post_id ) ? "data-post-id='$post_id'" : '' ); |
|
| 178 | + $style = sprintf('style="%s%s"', isset($settings['width']) ? "width:{$settings['width']};" : '', isset($settings['height']) ? "height:{$settings['height']};" : ''); |
|
| 179 | + $data_post_id = (isset($post_id) ? "data-post-id='$post_id'" : ''); |
|
| 180 | 180 | |
| 181 | 181 | // Generate a unique ID for this timeline. |
| 182 | - $element_id = uniqid( 'wl-timeline-' ); |
|
| 182 | + $element_id = uniqid('wl-timeline-'); |
|
| 183 | 183 | |
| 184 | - if ( WP_DEBUG ) { |
|
| 185 | - $this->log_service->trace( "Creating a timeline widget [ element id :: $element_id ][ post id :: $post_id ]" ); |
|
| 184 | + if (WP_DEBUG) { |
|
| 185 | + $this->log_service->trace("Creating a timeline widget [ element id :: $element_id ][ post id :: $post_id ]"); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | // Building template. |
| 189 | - return sprintf( '<div class="wl-timeline-container" %s><div class="wl-timeline" id="%s" %s></div></div>', $style, $element_id, $data_post_id ); |
|
| 189 | + return sprintf('<div class="wl-timeline-container" %s><div class="wl-timeline" id="%s" %s></div></div>', $style, $element_id, $data_post_id); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | /** |
@@ -199,10 +199,10 @@ discard block |
||
| 199 | 199 | private function get_locale() { |
| 200 | 200 | |
| 201 | 201 | // Get the first 2 letters. |
| 202 | - $locale = substr( get_locale(), 0, 2 ); |
|
| 202 | + $locale = substr(get_locale(), 0, 2); |
|
| 203 | 203 | |
| 204 | 204 | // Check that the specified locale is supported otherwise use English. |
| 205 | - return in_array( $locale, self::$supported_locales ) ? $locale : 'en'; |
|
| 205 | + return in_array($locale, self::$supported_locales) ? $locale : 'en'; |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | } |
@@ -210,22 +210,22 @@ discard block |
||
| 210 | 210 | /** |
| 211 | 211 | * register_block_type for Gutenberg blocks |
| 212 | 212 | */ |
| 213 | -add_action( 'init', function() { |
|
| 213 | +add_action('init', function() { |
|
| 214 | 214 | // Bail out if the `register_block_type` function isn't available. |
| 215 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 215 | + if ( ! function_exists('register_block_type')) { |
|
| 216 | 216 | return; |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | $wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode(); |
| 220 | 220 | register_block_type('wordlift/timeline', array( |
| 221 | 221 | 'editor_script' => 'wordlift-admin-edit-gutenberg', |
| 222 | - 'render_callback' => function($attributes){ |
|
| 222 | + 'render_callback' => function($attributes) { |
|
| 223 | 223 | $attr_code = ''; |
| 224 | 224 | $timelinejs_options = json_decode($attributes['timelinejs_options'], true); |
| 225 | 225 | unset($attributes['timelinejs_options']); |
| 226 | 226 | $attributes_all = array_merge($attributes, $timelinejs_options); |
| 227 | 227 | foreach ($attributes_all as $key => $value) { |
| 228 | - if($value && strpos($value, '[') === false && strpos($value, ']') === false) { |
|
| 228 | + if ($value && strpos($value, '[') === false && strpos($value, ']') === false) { |
|
| 229 | 229 | $attr_code .= $key.'="'.$value.'" '; |
| 230 | 230 | } |
| 231 | 231 | } |
@@ -14,100 +14,100 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | function wl_shortcode_navigator_data() { |
| 16 | 16 | |
| 17 | - // Post ID must be defined |
|
| 18 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 19 | - wp_die( 'No post_id given' ); |
|
| 17 | + // Post ID must be defined |
|
| 18 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
| 19 | + wp_die( 'No post_id given' ); |
|
| 20 | 20 | |
| 21 | - return; |
|
| 22 | - } |
|
| 21 | + return; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - $current_post_id = $_GET['post_id']; |
|
| 25 | - $current_post = get_post( $current_post_id ); |
|
| 24 | + $current_post_id = $_GET['post_id']; |
|
| 25 | + $current_post = get_post( $current_post_id ); |
|
| 26 | 26 | |
| 27 | - // Post ID has to match an existing item |
|
| 28 | - if ( null === $current_post ) { |
|
| 29 | - wp_die( 'No valid post_id given' ); |
|
| 27 | + // Post ID has to match an existing item |
|
| 28 | + if ( null === $current_post ) { |
|
| 29 | + wp_die( 'No valid post_id given' ); |
|
| 30 | 30 | |
| 31 | - return; |
|
| 32 | - } |
|
| 31 | + return; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - // prepare structures to memorize other related posts |
|
| 35 | - $results = array(); |
|
| 36 | - $blacklist_ids = array( $current_post_id ); |
|
| 37 | - $related_entities = array(); |
|
| 34 | + // prepare structures to memorize other related posts |
|
| 35 | + $results = array(); |
|
| 36 | + $blacklist_ids = array( $current_post_id ); |
|
| 37 | + $related_entities = array(); |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | - $relation_service = Wordlift_Relation_Service::get_instance(); |
|
| 40 | + $relation_service = Wordlift_Relation_Service::get_instance(); |
|
| 41 | 41 | |
| 42 | - // Get the related entities, ordering them by WHO, WHAT, WHERE, WHEN |
|
| 43 | - // TODO Replace with a single query if it is possible |
|
| 44 | - // We select in inverse order to give priority to less used entities |
|
| 45 | - foreach ( |
|
| 46 | - array( |
|
| 47 | - WL_WHEN_RELATION, |
|
| 48 | - WL_WHERE_RELATION, |
|
| 49 | - WL_WHAT_RELATION, |
|
| 50 | - WL_WHO_RELATION, |
|
| 51 | - ) as $predicate |
|
| 52 | - ) { |
|
| 42 | + // Get the related entities, ordering them by WHO, WHAT, WHERE, WHEN |
|
| 43 | + // TODO Replace with a single query if it is possible |
|
| 44 | + // We select in inverse order to give priority to less used entities |
|
| 45 | + foreach ( |
|
| 46 | + array( |
|
| 47 | + WL_WHEN_RELATION, |
|
| 48 | + WL_WHERE_RELATION, |
|
| 49 | + WL_WHAT_RELATION, |
|
| 50 | + WL_WHO_RELATION, |
|
| 51 | + ) as $predicate |
|
| 52 | + ) { |
|
| 53 | 53 | |
| 54 | - $related_entities = array_merge( $related_entities, |
|
| 55 | - $relation_service->get_objects( $current_post_id, '*', $predicate, 'publish' ) |
|
| 54 | + $related_entities = array_merge( $related_entities, |
|
| 55 | + $relation_service->get_objects( $current_post_id, '*', $predicate, 'publish' ) |
|
| 56 | 56 | // wl_core_get_related_entities( $current_post_id, array( |
| 57 | 57 | // 'predicate' => $predicate, |
| 58 | 58 | // 'status' => 'publish', |
| 59 | 59 | // ) |
| 60 | - ); |
|
| 61 | - |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - foreach ( $related_entities as $related_entity ) { |
|
| 65 | - |
|
| 66 | - // take the id of posts referencing the entity |
|
| 67 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( $related_entity->ID, '*', null, 'publish' ); |
|
| 68 | - |
|
| 69 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 70 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 71 | - |
|
| 72 | - if ( ! in_array( $referencing_post->ID, $blacklist_ids ) ) { |
|
| 73 | - |
|
| 74 | - $blacklist_ids[] = $referencing_post->ID; |
|
| 75 | - $serialized_entity = wl_serialize_entity( $related_entity ); |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Use the thumbnail. |
|
| 79 | - * |
|
| 80 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 81 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 82 | - * |
|
| 83 | - * @since 3.19.3 We're using the medium size image. |
|
| 84 | - */ |
|
| 85 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 86 | - |
|
| 87 | - if ( $thumbnail ) { |
|
| 88 | - |
|
| 89 | - $results[] = array( |
|
| 90 | - 'post' => array( |
|
| 91 | - 'permalink' => get_post_permalink( $referencing_post->ID ), |
|
| 92 | - 'title' => $referencing_post->post_title, |
|
| 93 | - 'thumbnail' => $thumbnail, |
|
| 94 | - ), |
|
| 95 | - 'entity' => array( |
|
| 96 | - 'label' => $serialized_entity['label'], |
|
| 97 | - 'mainType' => $serialized_entity['mainType'], |
|
| 98 | - 'permalink' => get_post_permalink( $related_entity->ID ), |
|
| 99 | - ), |
|
| 100 | - ); |
|
| 101 | - |
|
| 102 | - // Be sure no more than 1 post for entity is returned |
|
| 103 | - break; |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // Return first 4 results in json accordingly to 4 columns layout |
|
| 110 | - return array_slice( array_reverse( $results ), 0, 4 ); |
|
| 60 | + ); |
|
| 61 | + |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + foreach ( $related_entities as $related_entity ) { |
|
| 65 | + |
|
| 66 | + // take the id of posts referencing the entity |
|
| 67 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( $related_entity->ID, '*', null, 'publish' ); |
|
| 68 | + |
|
| 69 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 70 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 71 | + |
|
| 72 | + if ( ! in_array( $referencing_post->ID, $blacklist_ids ) ) { |
|
| 73 | + |
|
| 74 | + $blacklist_ids[] = $referencing_post->ID; |
|
| 75 | + $serialized_entity = wl_serialize_entity( $related_entity ); |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Use the thumbnail. |
|
| 79 | + * |
|
| 80 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 81 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 82 | + * |
|
| 83 | + * @since 3.19.3 We're using the medium size image. |
|
| 84 | + */ |
|
| 85 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 86 | + |
|
| 87 | + if ( $thumbnail ) { |
|
| 88 | + |
|
| 89 | + $results[] = array( |
|
| 90 | + 'post' => array( |
|
| 91 | + 'permalink' => get_post_permalink( $referencing_post->ID ), |
|
| 92 | + 'title' => $referencing_post->post_title, |
|
| 93 | + 'thumbnail' => $thumbnail, |
|
| 94 | + ), |
|
| 95 | + 'entity' => array( |
|
| 96 | + 'label' => $serialized_entity['label'], |
|
| 97 | + 'mainType' => $serialized_entity['mainType'], |
|
| 98 | + 'permalink' => get_post_permalink( $related_entity->ID ), |
|
| 99 | + ), |
|
| 100 | + ); |
|
| 101 | + |
|
| 102 | + // Be sure no more than 1 post for entity is returned |
|
| 103 | + break; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // Return first 4 results in json accordingly to 4 columns layout |
|
| 110 | + return array_slice( array_reverse( $results ), 0, 4 ); |
|
| 111 | 111 | |
| 112 | 112 | } |
| 113 | 113 | |
@@ -118,8 +118,8 @@ discard block |
||
| 118 | 118 | */ |
| 119 | 119 | function wl_shortcode_navigator_ajax() { |
| 120 | 120 | |
| 121 | - $results = wl_shortcode_navigator_data(); |
|
| 122 | - wl_core_send_json($results); |
|
| 121 | + $results = wl_shortcode_navigator_data(); |
|
| 122 | + wl_core_send_json($results); |
|
| 123 | 123 | |
| 124 | 124 | } |
| 125 | 125 | |
@@ -131,15 +131,15 @@ discard block |
||
| 131 | 131 | */ |
| 132 | 132 | function wl_shortcode_navigator_wp_json() { |
| 133 | 133 | |
| 134 | - $results = wl_shortcode_navigator_data(); |
|
| 135 | - if ( ob_get_contents() ) { |
|
| 136 | - ob_clean(); |
|
| 137 | - } |
|
| 138 | - return array( |
|
| 139 | - 'items' => array( |
|
| 140 | - array('values' => $results) |
|
| 141 | - ) |
|
| 142 | - ); |
|
| 134 | + $results = wl_shortcode_navigator_data(); |
|
| 135 | + if ( ob_get_contents() ) { |
|
| 136 | + ob_clean(); |
|
| 137 | + } |
|
| 138 | + return array( |
|
| 139 | + 'items' => array( |
|
| 140 | + array('values' => $results) |
|
| 141 | + ) |
|
| 142 | + ); |
|
| 143 | 143 | |
| 144 | 144 | } |
| 145 | 145 | |
@@ -147,43 +147,43 @@ discard block |
||
| 147 | 147 | * Adding `rest_api_init` action for amp backend of navigator |
| 148 | 148 | */ |
| 149 | 149 | add_action( 'rest_api_init', function () { |
| 150 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 151 | - 'methods' => 'GET', |
|
| 152 | - 'callback' => 'wl_shortcode_navigator_wp_json', |
|
| 153 | - ) ); |
|
| 150 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 151 | + 'methods' => 'GET', |
|
| 152 | + 'callback' => 'wl_shortcode_navigator_wp_json', |
|
| 153 | + ) ); |
|
| 154 | 154 | } ); |
| 155 | 155 | |
| 156 | 156 | /** |
| 157 | 157 | * register_block_type for Gutenberg blocks |
| 158 | 158 | */ |
| 159 | 159 | add_action( 'init', function() { |
| 160 | - // Bail out if the `register_block_type` function isn't available. |
|
| 161 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 162 | - return; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - register_block_type('wordlift/navigator', array( |
|
| 166 | - 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 167 | - 'render_callback' => function($attributes){ |
|
| 168 | - $attr_code = ''; |
|
| 169 | - foreach ($attributes as $key => $value) { |
|
| 170 | - $attr_code .= $key.'="'.$value.'" '; |
|
| 171 | - } |
|
| 172 | - return '[wl_navigator '.$attr_code.']'; |
|
| 173 | - }, |
|
| 174 | - 'attributes' => array( |
|
| 175 | - 'title' => array( |
|
| 176 | - 'type' => 'string', |
|
| 177 | - 'default' => __( 'Related articles', 'wordlift' ) |
|
| 178 | - ), |
|
| 179 | - 'with_carousel' => array( |
|
| 180 | - 'type' => 'bool', |
|
| 181 | - 'default' => true |
|
| 182 | - ), |
|
| 183 | - 'squared_thumbs' => array( |
|
| 184 | - 'type' => 'bool', |
|
| 185 | - 'default' => false |
|
| 186 | - ) |
|
| 187 | - ) |
|
| 188 | - )); |
|
| 160 | + // Bail out if the `register_block_type` function isn't available. |
|
| 161 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 162 | + return; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + register_block_type('wordlift/navigator', array( |
|
| 166 | + 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 167 | + 'render_callback' => function($attributes){ |
|
| 168 | + $attr_code = ''; |
|
| 169 | + foreach ($attributes as $key => $value) { |
|
| 170 | + $attr_code .= $key.'="'.$value.'" '; |
|
| 171 | + } |
|
| 172 | + return '[wl_navigator '.$attr_code.']'; |
|
| 173 | + }, |
|
| 174 | + 'attributes' => array( |
|
| 175 | + 'title' => array( |
|
| 176 | + 'type' => 'string', |
|
| 177 | + 'default' => __( 'Related articles', 'wordlift' ) |
|
| 178 | + ), |
|
| 179 | + 'with_carousel' => array( |
|
| 180 | + 'type' => 'bool', |
|
| 181 | + 'default' => true |
|
| 182 | + ), |
|
| 183 | + 'squared_thumbs' => array( |
|
| 184 | + 'type' => 'bool', |
|
| 185 | + 'default' => false |
|
| 186 | + ) |
|
| 187 | + ) |
|
| 188 | + )); |
|
| 189 | 189 | } ); |
@@ -15,25 +15,25 @@ discard block |
||
| 15 | 15 | function wl_shortcode_navigator_data() { |
| 16 | 16 | |
| 17 | 17 | // Post ID must be defined |
| 18 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 19 | - wp_die( 'No post_id given' ); |
|
| 18 | + if ( ! isset($_GET['post_id'])) { |
|
| 19 | + wp_die('No post_id given'); |
|
| 20 | 20 | |
| 21 | 21 | return; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | $current_post_id = $_GET['post_id']; |
| 25 | - $current_post = get_post( $current_post_id ); |
|
| 25 | + $current_post = get_post($current_post_id); |
|
| 26 | 26 | |
| 27 | 27 | // Post ID has to match an existing item |
| 28 | - if ( null === $current_post ) { |
|
| 29 | - wp_die( 'No valid post_id given' ); |
|
| 28 | + if (null === $current_post) { |
|
| 29 | + wp_die('No valid post_id given'); |
|
| 30 | 30 | |
| 31 | 31 | return; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | // prepare structures to memorize other related posts |
| 35 | 35 | $results = array(); |
| 36 | - $blacklist_ids = array( $current_post_id ); |
|
| 36 | + $blacklist_ids = array($current_post_id); |
|
| 37 | 37 | $related_entities = array(); |
| 38 | 38 | |
| 39 | 39 | |
@@ -51,8 +51,8 @@ discard block |
||
| 51 | 51 | ) as $predicate |
| 52 | 52 | ) { |
| 53 | 53 | |
| 54 | - $related_entities = array_merge( $related_entities, |
|
| 55 | - $relation_service->get_objects( $current_post_id, '*', $predicate, 'publish' ) |
|
| 54 | + $related_entities = array_merge($related_entities, |
|
| 55 | + $relation_service->get_objects($current_post_id, '*', $predicate, 'publish') |
|
| 56 | 56 | // wl_core_get_related_entities( $current_post_id, array( |
| 57 | 57 | // 'predicate' => $predicate, |
| 58 | 58 | // 'status' => 'publish', |
@@ -61,18 +61,18 @@ discard block |
||
| 61 | 61 | |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - foreach ( $related_entities as $related_entity ) { |
|
| 64 | + foreach ($related_entities as $related_entity) { |
|
| 65 | 65 | |
| 66 | 66 | // take the id of posts referencing the entity |
| 67 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( $related_entity->ID, '*', null, 'publish' ); |
|
| 67 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects($related_entity->ID, '*', null, 'publish'); |
|
| 68 | 68 | |
| 69 | 69 | // loop over them and take the first one which is not already in the $related_posts |
| 70 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 70 | + foreach ($referencing_posts as $referencing_post) { |
|
| 71 | 71 | |
| 72 | - if ( ! in_array( $referencing_post->ID, $blacklist_ids ) ) { |
|
| 72 | + if ( ! in_array($referencing_post->ID, $blacklist_ids)) { |
|
| 73 | 73 | |
| 74 | 74 | $blacklist_ids[] = $referencing_post->ID; |
| 75 | - $serialized_entity = wl_serialize_entity( $related_entity ); |
|
| 75 | + $serialized_entity = wl_serialize_entity($related_entity); |
|
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | 78 | * Use the thumbnail. |
@@ -82,20 +82,20 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @since 3.19.3 We're using the medium size image. |
| 84 | 84 | */ |
| 85 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 85 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 86 | 86 | |
| 87 | - if ( $thumbnail ) { |
|
| 87 | + if ($thumbnail) { |
|
| 88 | 88 | |
| 89 | 89 | $results[] = array( |
| 90 | 90 | 'post' => array( |
| 91 | - 'permalink' => get_post_permalink( $referencing_post->ID ), |
|
| 91 | + 'permalink' => get_post_permalink($referencing_post->ID), |
|
| 92 | 92 | 'title' => $referencing_post->post_title, |
| 93 | 93 | 'thumbnail' => $thumbnail, |
| 94 | 94 | ), |
| 95 | 95 | 'entity' => array( |
| 96 | 96 | 'label' => $serialized_entity['label'], |
| 97 | 97 | 'mainType' => $serialized_entity['mainType'], |
| 98 | - 'permalink' => get_post_permalink( $related_entity->ID ), |
|
| 98 | + 'permalink' => get_post_permalink($related_entity->ID), |
|
| 99 | 99 | ), |
| 100 | 100 | ); |
| 101 | 101 | |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | // Return first 4 results in json accordingly to 4 columns layout |
| 110 | - return array_slice( array_reverse( $results ), 0, 4 ); |
|
| 110 | + return array_slice(array_reverse($results), 0, 4); |
|
| 111 | 111 | |
| 112 | 112 | } |
| 113 | 113 | |
@@ -123,8 +123,8 @@ discard block |
||
| 123 | 123 | |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 127 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 126 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 127 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 128 | 128 | |
| 129 | 129 | /** |
| 130 | 130 | * wp-json call for the navigator widget |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | function wl_shortcode_navigator_wp_json() { |
| 133 | 133 | |
| 134 | 134 | $results = wl_shortcode_navigator_data(); |
| 135 | - if ( ob_get_contents() ) { |
|
| 135 | + if (ob_get_contents()) { |
|
| 136 | 136 | ob_clean(); |
| 137 | 137 | } |
| 138 | 138 | return array( |
@@ -146,25 +146,25 @@ discard block |
||
| 146 | 146 | /** |
| 147 | 147 | * Adding `rest_api_init` action for amp backend of navigator |
| 148 | 148 | */ |
| 149 | -add_action( 'rest_api_init', function () { |
|
| 150 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 149 | +add_action('rest_api_init', function() { |
|
| 150 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 151 | 151 | 'methods' => 'GET', |
| 152 | 152 | 'callback' => 'wl_shortcode_navigator_wp_json', |
| 153 | - ) ); |
|
| 153 | + )); |
|
| 154 | 154 | } ); |
| 155 | 155 | |
| 156 | 156 | /** |
| 157 | 157 | * register_block_type for Gutenberg blocks |
| 158 | 158 | */ |
| 159 | -add_action( 'init', function() { |
|
| 159 | +add_action('init', function() { |
|
| 160 | 160 | // Bail out if the `register_block_type` function isn't available. |
| 161 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 161 | + if ( ! function_exists('register_block_type')) { |
|
| 162 | 162 | return; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | register_block_type('wordlift/navigator', array( |
| 166 | 166 | 'editor_script' => 'wordlift-admin-edit-gutenberg', |
| 167 | - 'render_callback' => function($attributes){ |
|
| 167 | + 'render_callback' => function($attributes) { |
|
| 168 | 168 | $attr_code = ''; |
| 169 | 169 | foreach ($attributes as $key => $value) { |
| 170 | 170 | $attr_code .= $key.'="'.$value.'" '; |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | 'attributes' => array( |
| 175 | 175 | 'title' => array( |
| 176 | 176 | 'type' => 'string', |
| 177 | - 'default' => __( 'Related articles', 'wordlift' ) |
|
| 177 | + 'default' => __('Related articles', 'wordlift') |
|
| 178 | 178 | ), |
| 179 | 179 | 'with_carousel' => array( |
| 180 | 180 | 'type' => 'bool', |
@@ -16,114 +16,114 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | function wl_shortcode_faceted_search_data_ajax( $http_raw_data = null ) { |
| 18 | 18 | |
| 19 | - // Post ID must be defined. |
|
| 20 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 21 | - wp_die( 'No post_id given' ); |
|
| 22 | - |
|
| 23 | - return; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - // Extract filtering conditions. |
|
| 27 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 28 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 29 | - |
|
| 30 | - $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 31 | - $current_post = get_post( $current_post_id ); |
|
| 32 | - |
|
| 33 | - // Post ID has to match an existing item. |
|
| 34 | - if ( null === $current_post ) { |
|
| 35 | - wp_die( 'No valid post_id given' ); |
|
| 36 | - |
|
| 37 | - return; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - // If the current post is an entity, |
|
| 41 | - // the current post is used as main entity. |
|
| 42 | - // Otherwise, current post related entities are used. |
|
| 43 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 44 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 45 | - array( $current_post->ID ) : |
|
| 46 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 47 | - |
|
| 48 | - // If there are no entities we cannot render the widget. |
|
| 49 | - if ( 0 === count( $entity_ids ) ) { |
|
| 50 | - wp_die( 'No entities available' ); |
|
| 51 | - |
|
| 52 | - return; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - // Retrieve requested type |
|
| 56 | - $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 57 | - |
|
| 58 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 59 | - |
|
| 60 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 61 | - $entity_ids, |
|
| 62 | - '*', |
|
| 63 | - null, |
|
| 64 | - 'publish', |
|
| 65 | - array( $current_post_id ), |
|
| 66 | - $limit |
|
| 67 | - ); |
|
| 68 | - |
|
| 69 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 70 | - return $p->ID; |
|
| 71 | - }, $referencing_posts ); |
|
| 72 | - $results = array(); |
|
| 73 | - |
|
| 74 | - if ( 'posts' === $required_type ) { |
|
| 75 | - |
|
| 76 | - // Required filtered posts. |
|
| 77 | - wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 78 | - |
|
| 79 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 80 | - $referencing_posts : |
|
| 81 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 82 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 83 | - '*', |
|
| 84 | - null, |
|
| 85 | - null, |
|
| 86 | - array(), |
|
| 87 | - null, |
|
| 88 | - $referencing_post_ids |
|
| 89 | - ); |
|
| 90 | - |
|
| 91 | - if ( $filtered_posts ) { |
|
| 92 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Use the thumbnail. |
|
| 96 | - * |
|
| 97 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 98 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 99 | - * |
|
| 100 | - * @since 3.19.3 We're using the medium size image. |
|
| 101 | - */ |
|
| 102 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 103 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 104 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 105 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 106 | - |
|
| 107 | - $results[] = $post_obj; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - } else { |
|
| 111 | - |
|
| 112 | - global $wpdb; |
|
| 113 | - |
|
| 114 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 115 | - |
|
| 116 | - // Retrieve Wordlift relation instances table name. |
|
| 117 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 118 | - |
|
| 119 | - /* |
|
| 19 | + // Post ID must be defined. |
|
| 20 | + if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 21 | + wp_die( 'No post_id given' ); |
|
| 22 | + |
|
| 23 | + return; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + // Extract filtering conditions. |
|
| 27 | + $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 28 | + $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 29 | + |
|
| 30 | + $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 31 | + $current_post = get_post( $current_post_id ); |
|
| 32 | + |
|
| 33 | + // Post ID has to match an existing item. |
|
| 34 | + if ( null === $current_post ) { |
|
| 35 | + wp_die( 'No valid post_id given' ); |
|
| 36 | + |
|
| 37 | + return; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + // If the current post is an entity, |
|
| 41 | + // the current post is used as main entity. |
|
| 42 | + // Otherwise, current post related entities are used. |
|
| 43 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 44 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 45 | + array( $current_post->ID ) : |
|
| 46 | + wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 47 | + |
|
| 48 | + // If there are no entities we cannot render the widget. |
|
| 49 | + if ( 0 === count( $entity_ids ) ) { |
|
| 50 | + wp_die( 'No entities available' ); |
|
| 51 | + |
|
| 52 | + return; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + // Retrieve requested type |
|
| 56 | + $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 57 | + |
|
| 58 | + $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 59 | + |
|
| 60 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 61 | + $entity_ids, |
|
| 62 | + '*', |
|
| 63 | + null, |
|
| 64 | + 'publish', |
|
| 65 | + array( $current_post_id ), |
|
| 66 | + $limit |
|
| 67 | + ); |
|
| 68 | + |
|
| 69 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 70 | + return $p->ID; |
|
| 71 | + }, $referencing_posts ); |
|
| 72 | + $results = array(); |
|
| 73 | + |
|
| 74 | + if ( 'posts' === $required_type ) { |
|
| 75 | + |
|
| 76 | + // Required filtered posts. |
|
| 77 | + wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 78 | + |
|
| 79 | + $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 80 | + $referencing_posts : |
|
| 81 | + Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 82 | + wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 83 | + '*', |
|
| 84 | + null, |
|
| 85 | + null, |
|
| 86 | + array(), |
|
| 87 | + null, |
|
| 88 | + $referencing_post_ids |
|
| 89 | + ); |
|
| 90 | + |
|
| 91 | + if ( $filtered_posts ) { |
|
| 92 | + foreach ( $filtered_posts as $post_obj ) { |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Use the thumbnail. |
|
| 96 | + * |
|
| 97 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 98 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 99 | + * |
|
| 100 | + * @since 3.19.3 We're using the medium size image. |
|
| 101 | + */ |
|
| 102 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 103 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 104 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 105 | + $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 106 | + |
|
| 107 | + $results[] = $post_obj; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + } else { |
|
| 111 | + |
|
| 112 | + global $wpdb; |
|
| 113 | + |
|
| 114 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 115 | + |
|
| 116 | + // Retrieve Wordlift relation instances table name. |
|
| 117 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 118 | + |
|
| 119 | + /* |
|
| 120 | 120 | * Make sure we have some referenced post, otherwise the IN parts of |
| 121 | 121 | * the SQL will produce an SQL error. |
| 122 | 122 | */ |
| 123 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 124 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 123 | + if ( ! empty( $referencing_post_ids ) ) { |
|
| 124 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 125 | 125 | |
| 126 | - $query = " |
|
| 126 | + $query = " |
|
| 127 | 127 | SELECT |
| 128 | 128 | object_id AS ID, |
| 129 | 129 | count( object_id ) AS counter |
@@ -135,30 +135,30 @@ discard block |
||
| 135 | 135 | LIMIT $limit; |
| 136 | 136 | "; |
| 137 | 137 | |
| 138 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 138 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 139 | 139 | |
| 140 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 140 | + $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 141 | 141 | |
| 142 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 142 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 143 | 143 | |
| 144 | - foreach ( $entities as $obj ) { |
|
| 144 | + foreach ( $entities as $obj ) { |
|
| 145 | 145 | |
| 146 | - $entity = get_post( $obj->ID ); |
|
| 146 | + $entity = get_post( $obj->ID ); |
|
| 147 | 147 | |
| 148 | - // Ensure only valid and published entities are returned. |
|
| 149 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 148 | + // Ensure only valid and published entities are returned. |
|
| 149 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 150 | 150 | |
| 151 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 152 | - $serialized_entity['counter'] = $obj->counter; |
|
| 153 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
| 151 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
| 152 | + $serialized_entity['counter'] = $obj->counter; |
|
| 153 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
| 154 | 154 | |
| 155 | - $results[] = $serialized_entity; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - } |
|
| 155 | + $results[] = $serialized_entity; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - return $results; |
|
| 161 | + return $results; |
|
| 162 | 162 | |
| 163 | 163 | } |
| 164 | 164 | |
@@ -170,148 +170,148 @@ discard block |
||
| 170 | 170 | */ |
| 171 | 171 | function wl_shortcode_faceted_search_data_wp_json( $http_raw_data = null ) { |
| 172 | 172 | |
| 173 | - // Post ID must be defined. |
|
| 174 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 175 | - wp_die( 'No post_id given' ); |
|
| 176 | - |
|
| 177 | - return; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // Extract filtering conditions. |
|
| 181 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 182 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 183 | - |
|
| 184 | - $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 185 | - $current_post = get_post( $current_post_id ); |
|
| 186 | - |
|
| 187 | - // Post ID has to match an existing item. |
|
| 188 | - if ( null === $current_post ) { |
|
| 189 | - wp_die( 'No valid post_id given' ); |
|
| 190 | - |
|
| 191 | - return; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - // If the current post is an entity, |
|
| 195 | - // the current post is used as main entity. |
|
| 196 | - // Otherwise, current post related entities are used. |
|
| 197 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 198 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 199 | - array( $current_post->ID ) : |
|
| 200 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 201 | - |
|
| 202 | - // If there are no entities we cannot render the widget. |
|
| 203 | - if ( 0 === count( $entity_ids ) ) { |
|
| 204 | - wp_die( 'No entities available' ); |
|
| 205 | - |
|
| 206 | - return; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // Retrieve requested type |
|
| 210 | - $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 211 | - |
|
| 212 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 213 | - |
|
| 214 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 215 | - $entity_ids, |
|
| 216 | - '*', |
|
| 217 | - null, |
|
| 218 | - 'publish', |
|
| 219 | - array( $current_post_id ), |
|
| 220 | - $limit |
|
| 221 | - ); |
|
| 222 | - |
|
| 223 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 224 | - return $p->ID; |
|
| 225 | - }, $referencing_posts ); |
|
| 226 | - $results = array(); |
|
| 227 | - |
|
| 228 | - if ( 'posts' === $required_type ) { |
|
| 229 | - |
|
| 230 | - // Required filtered posts. |
|
| 231 | - wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 232 | - |
|
| 233 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 234 | - $referencing_posts : |
|
| 235 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 236 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 237 | - '*', |
|
| 238 | - null, |
|
| 239 | - null, |
|
| 240 | - array(), |
|
| 241 | - null, |
|
| 242 | - $referencing_post_ids |
|
| 243 | - ); |
|
| 244 | - |
|
| 245 | - if ( $filtered_posts ) { |
|
| 246 | - foreach ( $filtered_posts as $i => $post_obj ) { |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Use the thumbnail. |
|
| 250 | - * |
|
| 251 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 252 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 253 | - * |
|
| 254 | - * @since 3.19.3 We're using the medium size image. |
|
| 255 | - */ |
|
| 256 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 257 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 258 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 259 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 260 | - |
|
| 261 | - $results[$i] = $post_obj; |
|
| 262 | - |
|
| 263 | - // Get Entity URLs needed for client side filtering in amp |
|
| 264 | - foreach( Wordlift_Relation_Service::get_instance()->get_objects( $post_obj->ID, 'ids' ) as $entity_id ){ |
|
| 265 | - $results[$i]->entities[] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id ); |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - return array( |
|
| 271 | - array('values' => $results) |
|
| 272 | - ); |
|
| 273 | - |
|
| 274 | - } else { |
|
| 275 | - |
|
| 276 | - global $wpdb; |
|
| 277 | - |
|
| 278 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 279 | - |
|
| 280 | - // Retrieve Wordlift relation instances table name. |
|
| 281 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 282 | - |
|
| 283 | - // Response interface with l10n strings, grouped entities and empty data array |
|
| 284 | - $serialized_entity_groups = array( |
|
| 285 | - array( |
|
| 286 | - 'l10n' => $_GET['l10n'] ? $_GET['l10n']['what'] : 'what', |
|
| 287 | - 'entities' => array('thing', 'creative-work', 'recipe'), |
|
| 288 | - 'data' => array() |
|
| 289 | - ), |
|
| 290 | - array( |
|
| 291 | - 'l10n' => $_GET['l10n'] ? $_GET['l10n']['who'] : 'who', |
|
| 292 | - 'entities' => array('person', 'organization', 'local-business'), |
|
| 293 | - 'data' => array() |
|
| 294 | - ), |
|
| 295 | - array( |
|
| 296 | - 'l10n' => $_GET['l10n'] ? $_GET['l10n']['where'] : 'where', |
|
| 297 | - 'entities' => array('place'), |
|
| 298 | - 'data' => array() |
|
| 299 | - ), |
|
| 300 | - array( |
|
| 301 | - 'l10n' => $_GET['l10n'] ? $_GET['l10n']['when'] : 'when', |
|
| 302 | - 'entities' => array('event'), |
|
| 303 | - 'data' => array() |
|
| 304 | - ) |
|
| 305 | - ); |
|
| 306 | - |
|
| 307 | - /* |
|
| 173 | + // Post ID must be defined. |
|
| 174 | + if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 175 | + wp_die( 'No post_id given' ); |
|
| 176 | + |
|
| 177 | + return; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // Extract filtering conditions. |
|
| 181 | + $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 182 | + $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 183 | + |
|
| 184 | + $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 185 | + $current_post = get_post( $current_post_id ); |
|
| 186 | + |
|
| 187 | + // Post ID has to match an existing item. |
|
| 188 | + if ( null === $current_post ) { |
|
| 189 | + wp_die( 'No valid post_id given' ); |
|
| 190 | + |
|
| 191 | + return; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + // If the current post is an entity, |
|
| 195 | + // the current post is used as main entity. |
|
| 196 | + // Otherwise, current post related entities are used. |
|
| 197 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 198 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 199 | + array( $current_post->ID ) : |
|
| 200 | + wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 201 | + |
|
| 202 | + // If there are no entities we cannot render the widget. |
|
| 203 | + if ( 0 === count( $entity_ids ) ) { |
|
| 204 | + wp_die( 'No entities available' ); |
|
| 205 | + |
|
| 206 | + return; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // Retrieve requested type |
|
| 210 | + $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 211 | + |
|
| 212 | + $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 213 | + |
|
| 214 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 215 | + $entity_ids, |
|
| 216 | + '*', |
|
| 217 | + null, |
|
| 218 | + 'publish', |
|
| 219 | + array( $current_post_id ), |
|
| 220 | + $limit |
|
| 221 | + ); |
|
| 222 | + |
|
| 223 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 224 | + return $p->ID; |
|
| 225 | + }, $referencing_posts ); |
|
| 226 | + $results = array(); |
|
| 227 | + |
|
| 228 | + if ( 'posts' === $required_type ) { |
|
| 229 | + |
|
| 230 | + // Required filtered posts. |
|
| 231 | + wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 232 | + |
|
| 233 | + $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 234 | + $referencing_posts : |
|
| 235 | + Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 236 | + wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 237 | + '*', |
|
| 238 | + null, |
|
| 239 | + null, |
|
| 240 | + array(), |
|
| 241 | + null, |
|
| 242 | + $referencing_post_ids |
|
| 243 | + ); |
|
| 244 | + |
|
| 245 | + if ( $filtered_posts ) { |
|
| 246 | + foreach ( $filtered_posts as $i => $post_obj ) { |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Use the thumbnail. |
|
| 250 | + * |
|
| 251 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 252 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 253 | + * |
|
| 254 | + * @since 3.19.3 We're using the medium size image. |
|
| 255 | + */ |
|
| 256 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 257 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 258 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 259 | + $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 260 | + |
|
| 261 | + $results[$i] = $post_obj; |
|
| 262 | + |
|
| 263 | + // Get Entity URLs needed for client side filtering in amp |
|
| 264 | + foreach( Wordlift_Relation_Service::get_instance()->get_objects( $post_obj->ID, 'ids' ) as $entity_id ){ |
|
| 265 | + $results[$i]->entities[] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id ); |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + return array( |
|
| 271 | + array('values' => $results) |
|
| 272 | + ); |
|
| 273 | + |
|
| 274 | + } else { |
|
| 275 | + |
|
| 276 | + global $wpdb; |
|
| 277 | + |
|
| 278 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 279 | + |
|
| 280 | + // Retrieve Wordlift relation instances table name. |
|
| 281 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 282 | + |
|
| 283 | + // Response interface with l10n strings, grouped entities and empty data array |
|
| 284 | + $serialized_entity_groups = array( |
|
| 285 | + array( |
|
| 286 | + 'l10n' => $_GET['l10n'] ? $_GET['l10n']['what'] : 'what', |
|
| 287 | + 'entities' => array('thing', 'creative-work', 'recipe'), |
|
| 288 | + 'data' => array() |
|
| 289 | + ), |
|
| 290 | + array( |
|
| 291 | + 'l10n' => $_GET['l10n'] ? $_GET['l10n']['who'] : 'who', |
|
| 292 | + 'entities' => array('person', 'organization', 'local-business'), |
|
| 293 | + 'data' => array() |
|
| 294 | + ), |
|
| 295 | + array( |
|
| 296 | + 'l10n' => $_GET['l10n'] ? $_GET['l10n']['where'] : 'where', |
|
| 297 | + 'entities' => array('place'), |
|
| 298 | + 'data' => array() |
|
| 299 | + ), |
|
| 300 | + array( |
|
| 301 | + 'l10n' => $_GET['l10n'] ? $_GET['l10n']['when'] : 'when', |
|
| 302 | + 'entities' => array('event'), |
|
| 303 | + 'data' => array() |
|
| 304 | + ) |
|
| 305 | + ); |
|
| 306 | + |
|
| 307 | + /* |
|
| 308 | 308 | * Make sure we have some referenced post, otherwise the IN parts of |
| 309 | 309 | * the SQL will produce an SQL error. |
| 310 | 310 | */ |
| 311 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 312 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 311 | + if ( ! empty( $referencing_post_ids ) ) { |
|
| 312 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 313 | 313 | |
| 314 | - $query = " |
|
| 314 | + $query = " |
|
| 315 | 315 | SELECT |
| 316 | 316 | object_id AS ID, |
| 317 | 317 | count( object_id ) AS counter |
@@ -323,45 +323,45 @@ discard block |
||
| 323 | 323 | LIMIT $limit; |
| 324 | 324 | "; |
| 325 | 325 | |
| 326 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 326 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 327 | 327 | |
| 328 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 328 | + $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 329 | 329 | |
| 330 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 330 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 331 | 331 | |
| 332 | - foreach ( $entities as $obj ) { |
|
| 332 | + foreach ( $entities as $obj ) { |
|
| 333 | 333 | |
| 334 | - $entity = get_post( $obj->ID ); |
|
| 334 | + $entity = get_post( $obj->ID ); |
|
| 335 | 335 | |
| 336 | - // Ensure only valid and published entities are returned. |
|
| 337 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 336 | + // Ensure only valid and published entities are returned. |
|
| 337 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 338 | 338 | |
| 339 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 340 | - $serialized_entity['counter'] = $obj->counter; |
|
| 341 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
| 339 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
| 340 | + $serialized_entity['counter'] = $obj->counter; |
|
| 341 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
| 342 | 342 | |
| 343 | - // Populate $serialized_entity_groups maintaining array sequence |
|
| 344 | - foreach ( $serialized_entity_groups as $seg_key => $seg_value ) { |
|
| 345 | - if ( in_array( $serialized_entity['mainType'], $seg_value['entities'] ) ) { |
|
| 346 | - $serialized_entity_groups[$seg_key]['data'][] = $serialized_entity; |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - } |
|
| 343 | + // Populate $serialized_entity_groups maintaining array sequence |
|
| 344 | + foreach ( $serialized_entity_groups as $seg_key => $seg_value ) { |
|
| 345 | + if ( in_array( $serialized_entity['mainType'], $seg_value['entities'] ) ) { |
|
| 346 | + $serialized_entity_groups[$seg_key]['data'][] = $serialized_entity; |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | 351 | |
| 352 | - // Clean-up $serialized_entity_groups by removing empty items and entities |
|
| 353 | - foreach ( $serialized_entity_groups as $seg_key => $seg_value ) { |
|
| 354 | - if( empty($seg_value['data']) ) { |
|
| 355 | - unset($serialized_entity_groups[$seg_key]); |
|
| 356 | - } |
|
| 357 | - unset($serialized_entity_groups[$seg_key]['entities']); |
|
| 358 | - } |
|
| 352 | + // Clean-up $serialized_entity_groups by removing empty items and entities |
|
| 353 | + foreach ( $serialized_entity_groups as $seg_key => $seg_value ) { |
|
| 354 | + if( empty($seg_value['data']) ) { |
|
| 355 | + unset($serialized_entity_groups[$seg_key]); |
|
| 356 | + } |
|
| 357 | + unset($serialized_entity_groups[$seg_key]['entities']); |
|
| 358 | + } |
|
| 359 | 359 | |
| 360 | - $results = $serialized_entity_groups; |
|
| 361 | - } |
|
| 360 | + $results = $serialized_entity_groups; |
|
| 361 | + } |
|
| 362 | 362 | |
| 363 | - return $results; |
|
| 364 | - } |
|
| 363 | + return $results; |
|
| 364 | + } |
|
| 365 | 365 | |
| 366 | 366 | } |
| 367 | 367 | |
@@ -370,8 +370,8 @@ discard block |
||
| 370 | 370 | */ |
| 371 | 371 | function wl_shortcode_faceted_search_ajax( $http_raw_data = null ) { |
| 372 | 372 | |
| 373 | - $results = wl_shortcode_faceted_search_data_ajax( $http_raw_data ); |
|
| 374 | - wl_core_send_json( $results ); |
|
| 373 | + $results = wl_shortcode_faceted_search_data_ajax( $http_raw_data ); |
|
| 374 | + wl_core_send_json( $results ); |
|
| 375 | 375 | |
| 376 | 376 | } |
| 377 | 377 | |
@@ -386,13 +386,13 @@ discard block |
||
| 386 | 386 | */ |
| 387 | 387 | function wl_shortcode_faceted_search_wp_json( $http_raw_data = null ) { |
| 388 | 388 | |
| 389 | - $results = wl_shortcode_faceted_search_data_wp_json( $http_raw_data ); |
|
| 390 | - if ( ob_get_contents() ) { |
|
| 391 | - ob_clean(); |
|
| 392 | - } |
|
| 393 | - return array( |
|
| 394 | - 'items' => $results |
|
| 395 | - ); |
|
| 389 | + $results = wl_shortcode_faceted_search_data_wp_json( $http_raw_data ); |
|
| 390 | + if ( ob_get_contents() ) { |
|
| 391 | + ob_clean(); |
|
| 392 | + } |
|
| 393 | + return array( |
|
| 394 | + 'items' => $results |
|
| 395 | + ); |
|
| 396 | 396 | |
| 397 | 397 | } |
| 398 | 398 | |
@@ -400,51 +400,51 @@ discard block |
||
| 400 | 400 | * Adding `rest_api_init` action for amp backend of faceted-search |
| 401 | 401 | */ |
| 402 | 402 | add_action( 'rest_api_init', function () { |
| 403 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 404 | - 'methods' => 'GET', |
|
| 405 | - 'callback' => 'wl_shortcode_faceted_search_wp_json', |
|
| 406 | - ) ); |
|
| 403 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 404 | + 'methods' => 'GET', |
|
| 405 | + 'callback' => 'wl_shortcode_faceted_search_wp_json', |
|
| 406 | + ) ); |
|
| 407 | 407 | } ); |
| 408 | 408 | |
| 409 | 409 | /** |
| 410 | 410 | * register_block_type for Gutenberg blocks |
| 411 | 411 | */ |
| 412 | 412 | add_action( 'init', function() { |
| 413 | - // Bail out if the `register_block_type` function isn't available. |
|
| 414 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 415 | - return; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - register_block_type('wordlift/faceted-search', array( |
|
| 419 | - 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 420 | - 'render_callback' => function($attributes){ |
|
| 421 | - $attr_code = ''; |
|
| 422 | - foreach ($attributes as $key => $value) { |
|
| 423 | - $attr_code .= $key.'="'.$value.'" '; |
|
| 424 | - } |
|
| 425 | - return '[wl_faceted_search '.$attr_code.']'; |
|
| 426 | - }, |
|
| 427 | - 'attributes' => array( |
|
| 428 | - 'title' => array( |
|
| 429 | - 'type' => 'string', |
|
| 430 | - 'default' => __( 'Related articles', 'wordlift' ) |
|
| 431 | - ), |
|
| 432 | - 'show_facets' => array( |
|
| 433 | - 'type' => 'bool', |
|
| 434 | - 'default' => true |
|
| 435 | - ), |
|
| 436 | - 'with_carousel' => array( |
|
| 437 | - 'type' => 'bool', |
|
| 438 | - 'default' => true |
|
| 439 | - ), |
|
| 440 | - 'squared_thumbs' => array( |
|
| 441 | - 'type' => 'bool', |
|
| 442 | - 'default' => false |
|
| 443 | - ), |
|
| 444 | - 'limit' => array( |
|
| 445 | - 'type' => 'number', |
|
| 446 | - 'default' => 20 |
|
| 447 | - ) |
|
| 448 | - ) |
|
| 449 | - )); |
|
| 413 | + // Bail out if the `register_block_type` function isn't available. |
|
| 414 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 415 | + return; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + register_block_type('wordlift/faceted-search', array( |
|
| 419 | + 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 420 | + 'render_callback' => function($attributes){ |
|
| 421 | + $attr_code = ''; |
|
| 422 | + foreach ($attributes as $key => $value) { |
|
| 423 | + $attr_code .= $key.'="'.$value.'" '; |
|
| 424 | + } |
|
| 425 | + return '[wl_faceted_search '.$attr_code.']'; |
|
| 426 | + }, |
|
| 427 | + 'attributes' => array( |
|
| 428 | + 'title' => array( |
|
| 429 | + 'type' => 'string', |
|
| 430 | + 'default' => __( 'Related articles', 'wordlift' ) |
|
| 431 | + ), |
|
| 432 | + 'show_facets' => array( |
|
| 433 | + 'type' => 'bool', |
|
| 434 | + 'default' => true |
|
| 435 | + ), |
|
| 436 | + 'with_carousel' => array( |
|
| 437 | + 'type' => 'bool', |
|
| 438 | + 'default' => true |
|
| 439 | + ), |
|
| 440 | + 'squared_thumbs' => array( |
|
| 441 | + 'type' => 'bool', |
|
| 442 | + 'default' => false |
|
| 443 | + ), |
|
| 444 | + 'limit' => array( |
|
| 445 | + 'type' => 'number', |
|
| 446 | + 'default' => 20 |
|
| 447 | + ) |
|
| 448 | + ) |
|
| 449 | + )); |
|
| 450 | 450 | } ); |
@@ -14,25 +14,25 @@ discard block |
||
| 14 | 14 | * @since 3.20.0 |
| 15 | 15 | * @return array $results |
| 16 | 16 | */ |
| 17 | -function wl_shortcode_faceted_search_data_ajax( $http_raw_data = null ) { |
|
| 17 | +function wl_shortcode_faceted_search_data_ajax($http_raw_data = null) { |
|
| 18 | 18 | |
| 19 | 19 | // Post ID must be defined. |
| 20 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 21 | - wp_die( 'No post_id given' ); |
|
| 20 | + if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
| 21 | + wp_die('No post_id given'); |
|
| 22 | 22 | |
| 23 | 23 | return; |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | // Extract filtering conditions. |
| 27 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 28 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 27 | + $filtering_entity_uris = (null == $http_raw_data) ? file_get_contents('php://input') : $http_raw_data; |
|
| 28 | + $filtering_entity_uris = json_decode($filtering_entity_uris); |
|
| 29 | 29 | |
| 30 | 30 | $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
| 31 | - $current_post = get_post( $current_post_id ); |
|
| 31 | + $current_post = get_post($current_post_id); |
|
| 32 | 32 | |
| 33 | 33 | // Post ID has to match an existing item. |
| 34 | - if ( null === $current_post ) { |
|
| 35 | - wp_die( 'No valid post_id given' ); |
|
| 34 | + if (null === $current_post) { |
|
| 35 | + wp_die('No valid post_id given'); |
|
| 36 | 36 | |
| 37 | 37 | return; |
| 38 | 38 | } |
@@ -41,45 +41,43 @@ discard block |
||
| 41 | 41 | // the current post is used as main entity. |
| 42 | 42 | // Otherwise, current post related entities are used. |
| 43 | 43 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 44 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 45 | - array( $current_post->ID ) : |
|
| 46 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 44 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 45 | + array($current_post->ID) : wl_core_get_related_entity_ids($current_post->ID); |
|
| 47 | 46 | |
| 48 | 47 | // If there are no entities we cannot render the widget. |
| 49 | - if ( 0 === count( $entity_ids ) ) { |
|
| 50 | - wp_die( 'No entities available' ); |
|
| 48 | + if (0 === count($entity_ids)) { |
|
| 49 | + wp_die('No entities available'); |
|
| 51 | 50 | |
| 52 | 51 | return; |
| 53 | 52 | } |
| 54 | 53 | |
| 55 | 54 | // Retrieve requested type |
| 56 | - $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 55 | + $required_type = (isset($_GET['type'])) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 57 | 56 | |
| 58 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 57 | + $limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 59 | 58 | |
| 60 | 59 | $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
| 61 | 60 | $entity_ids, |
| 62 | 61 | '*', |
| 63 | 62 | null, |
| 64 | 63 | 'publish', |
| 65 | - array( $current_post_id ), |
|
| 64 | + array($current_post_id), |
|
| 66 | 65 | $limit |
| 67 | 66 | ); |
| 68 | 67 | |
| 69 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 68 | + $referencing_post_ids = array_map(function($p) { |
|
| 70 | 69 | return $p->ID; |
| 71 | - }, $referencing_posts ); |
|
| 70 | + }, $referencing_posts); |
|
| 72 | 71 | $results = array(); |
| 73 | 72 | |
| 74 | - if ( 'posts' === $required_type ) { |
|
| 73 | + if ('posts' === $required_type) { |
|
| 75 | 74 | |
| 76 | 75 | // Required filtered posts. |
| 77 | - wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 76 | + wl_write_log("Going to find related posts for the current post [ post ID :: $current_post_id ]"); |
|
| 78 | 77 | |
| 79 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 80 | - $referencing_posts : |
|
| 81 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 82 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 78 | + $filtered_posts = (empty($filtering_entity_uris)) ? |
|
| 79 | + $referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 80 | + wl_get_entity_post_ids_by_uris($filtering_entity_uris), |
|
| 83 | 81 | '*', |
| 84 | 82 | null, |
| 85 | 83 | null, |
@@ -88,8 +86,8 @@ discard block |
||
| 88 | 86 | $referencing_post_ids |
| 89 | 87 | ); |
| 90 | 88 | |
| 91 | - if ( $filtered_posts ) { |
|
| 92 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 89 | + if ($filtered_posts) { |
|
| 90 | + foreach ($filtered_posts as $post_obj) { |
|
| 93 | 91 | |
| 94 | 92 | /** |
| 95 | 93 | * Use the thumbnail. |
@@ -99,10 +97,10 @@ discard block |
||
| 99 | 97 | * |
| 100 | 98 | * @since 3.19.3 We're using the medium size image. |
| 101 | 99 | */ |
| 102 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 103 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 100 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 101 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 104 | 102 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 105 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 103 | + $post_obj->permalink = get_post_permalink($post_obj->ID); |
|
| 106 | 104 | |
| 107 | 105 | $results[] = $post_obj; |
| 108 | 106 | } |
@@ -111,7 +109,7 @@ discard block |
||
| 111 | 109 | |
| 112 | 110 | global $wpdb; |
| 113 | 111 | |
| 114 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 112 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ]"); |
|
| 115 | 113 | |
| 116 | 114 | // Retrieve Wordlift relation instances table name. |
| 117 | 115 | $table_name = wl_core_get_relation_instances_table_name(); |
@@ -120,8 +118,8 @@ discard block |
||
| 120 | 118 | * Make sure we have some referenced post, otherwise the IN parts of |
| 121 | 119 | * the SQL will produce an SQL error. |
| 122 | 120 | */ |
| 123 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 124 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 121 | + if ( ! empty($referencing_post_ids)) { |
|
| 122 | + $subject_ids = implode(',', $referencing_post_ids); |
|
| 125 | 123 | |
| 126 | 124 | $query = " |
| 127 | 125 | SELECT |
@@ -135,20 +133,20 @@ discard block |
||
| 135 | 133 | LIMIT $limit; |
| 136 | 134 | "; |
| 137 | 135 | |
| 138 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 136 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
| 139 | 137 | |
| 140 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 138 | + $entities = $wpdb->get_results($query, OBJECT); // No cache ok. |
|
| 141 | 139 | |
| 142 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 140 | + wl_write_log('Entities found '.count($entities)); |
|
| 143 | 141 | |
| 144 | - foreach ( $entities as $obj ) { |
|
| 142 | + foreach ($entities as $obj) { |
|
| 145 | 143 | |
| 146 | - $entity = get_post( $obj->ID ); |
|
| 144 | + $entity = get_post($obj->ID); |
|
| 147 | 145 | |
| 148 | 146 | // Ensure only valid and published entities are returned. |
| 149 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 147 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
| 150 | 148 | |
| 151 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 149 | + $serialized_entity = wl_serialize_entity($entity); |
|
| 152 | 150 | $serialized_entity['counter'] = $obj->counter; |
| 153 | 151 | $serialized_entity['createdAt'] = $entity->post_date; |
| 154 | 152 | |
@@ -168,25 +166,25 @@ discard block |
||
| 168 | 166 | * @since 3.20.0 |
| 169 | 167 | * @return array $results |
| 170 | 168 | */ |
| 171 | -function wl_shortcode_faceted_search_data_wp_json( $http_raw_data = null ) { |
|
| 169 | +function wl_shortcode_faceted_search_data_wp_json($http_raw_data = null) { |
|
| 172 | 170 | |
| 173 | 171 | // Post ID must be defined. |
| 174 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 175 | - wp_die( 'No post_id given' ); |
|
| 172 | + if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
| 173 | + wp_die('No post_id given'); |
|
| 176 | 174 | |
| 177 | 175 | return; |
| 178 | 176 | } |
| 179 | 177 | |
| 180 | 178 | // Extract filtering conditions. |
| 181 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 182 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 179 | + $filtering_entity_uris = (null == $http_raw_data) ? file_get_contents('php://input') : $http_raw_data; |
|
| 180 | + $filtering_entity_uris = json_decode($filtering_entity_uris); |
|
| 183 | 181 | |
| 184 | 182 | $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
| 185 | - $current_post = get_post( $current_post_id ); |
|
| 183 | + $current_post = get_post($current_post_id); |
|
| 186 | 184 | |
| 187 | 185 | // Post ID has to match an existing item. |
| 188 | - if ( null === $current_post ) { |
|
| 189 | - wp_die( 'No valid post_id given' ); |
|
| 186 | + if (null === $current_post) { |
|
| 187 | + wp_die('No valid post_id given'); |
|
| 190 | 188 | |
| 191 | 189 | return; |
| 192 | 190 | } |
@@ -195,45 +193,43 @@ discard block |
||
| 195 | 193 | // the current post is used as main entity. |
| 196 | 194 | // Otherwise, current post related entities are used. |
| 197 | 195 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 198 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 199 | - array( $current_post->ID ) : |
|
| 200 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 196 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 197 | + array($current_post->ID) : wl_core_get_related_entity_ids($current_post->ID); |
|
| 201 | 198 | |
| 202 | 199 | // If there are no entities we cannot render the widget. |
| 203 | - if ( 0 === count( $entity_ids ) ) { |
|
| 204 | - wp_die( 'No entities available' ); |
|
| 200 | + if (0 === count($entity_ids)) { |
|
| 201 | + wp_die('No entities available'); |
|
| 205 | 202 | |
| 206 | 203 | return; |
| 207 | 204 | } |
| 208 | 205 | |
| 209 | 206 | // Retrieve requested type |
| 210 | - $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 207 | + $required_type = (isset($_GET['type'])) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 211 | 208 | |
| 212 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 209 | + $limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 213 | 210 | |
| 214 | 211 | $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
| 215 | 212 | $entity_ids, |
| 216 | 213 | '*', |
| 217 | 214 | null, |
| 218 | 215 | 'publish', |
| 219 | - array( $current_post_id ), |
|
| 216 | + array($current_post_id), |
|
| 220 | 217 | $limit |
| 221 | 218 | ); |
| 222 | 219 | |
| 223 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 220 | + $referencing_post_ids = array_map(function($p) { |
|
| 224 | 221 | return $p->ID; |
| 225 | - }, $referencing_posts ); |
|
| 222 | + }, $referencing_posts); |
|
| 226 | 223 | $results = array(); |
| 227 | 224 | |
| 228 | - if ( 'posts' === $required_type ) { |
|
| 225 | + if ('posts' === $required_type) { |
|
| 229 | 226 | |
| 230 | 227 | // Required filtered posts. |
| 231 | - wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 228 | + wl_write_log("Going to find related posts for the current post [ post ID :: $current_post_id ]"); |
|
| 232 | 229 | |
| 233 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 234 | - $referencing_posts : |
|
| 235 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 236 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 230 | + $filtered_posts = (empty($filtering_entity_uris)) ? |
|
| 231 | + $referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 232 | + wl_get_entity_post_ids_by_uris($filtering_entity_uris), |
|
| 237 | 233 | '*', |
| 238 | 234 | null, |
| 239 | 235 | null, |
@@ -242,8 +238,8 @@ discard block |
||
| 242 | 238 | $referencing_post_ids |
| 243 | 239 | ); |
| 244 | 240 | |
| 245 | - if ( $filtered_posts ) { |
|
| 246 | - foreach ( $filtered_posts as $i => $post_obj ) { |
|
| 241 | + if ($filtered_posts) { |
|
| 242 | + foreach ($filtered_posts as $i => $post_obj) { |
|
| 247 | 243 | |
| 248 | 244 | /** |
| 249 | 245 | * Use the thumbnail. |
@@ -253,16 +249,16 @@ discard block |
||
| 253 | 249 | * |
| 254 | 250 | * @since 3.19.3 We're using the medium size image. |
| 255 | 251 | */ |
| 256 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 257 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 252 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 253 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 258 | 254 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 259 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 255 | + $post_obj->permalink = get_post_permalink($post_obj->ID); |
|
| 260 | 256 | |
| 261 | 257 | $results[$i] = $post_obj; |
| 262 | 258 | |
| 263 | 259 | // Get Entity URLs needed for client side filtering in amp |
| 264 | - foreach( Wordlift_Relation_Service::get_instance()->get_objects( $post_obj->ID, 'ids' ) as $entity_id ){ |
|
| 265 | - $results[$i]->entities[] = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id ); |
|
| 260 | + foreach (Wordlift_Relation_Service::get_instance()->get_objects($post_obj->ID, 'ids') as $entity_id) { |
|
| 261 | + $results[$i]->entities[] = Wordlift_Entity_Service::get_instance()->get_uri($entity_id); |
|
| 266 | 262 | } |
| 267 | 263 | } |
| 268 | 264 | } |
@@ -275,7 +271,7 @@ discard block |
||
| 275 | 271 | |
| 276 | 272 | global $wpdb; |
| 277 | 273 | |
| 278 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 274 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ]"); |
|
| 279 | 275 | |
| 280 | 276 | // Retrieve Wordlift relation instances table name. |
| 281 | 277 | $table_name = wl_core_get_relation_instances_table_name(); |
@@ -308,8 +304,8 @@ discard block |
||
| 308 | 304 | * Make sure we have some referenced post, otherwise the IN parts of |
| 309 | 305 | * the SQL will produce an SQL error. |
| 310 | 306 | */ |
| 311 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 312 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 307 | + if ( ! empty($referencing_post_ids)) { |
|
| 308 | + $subject_ids = implode(',', $referencing_post_ids); |
|
| 313 | 309 | |
| 314 | 310 | $query = " |
| 315 | 311 | SELECT |
@@ -323,26 +319,26 @@ discard block |
||
| 323 | 319 | LIMIT $limit; |
| 324 | 320 | "; |
| 325 | 321 | |
| 326 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 322 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
| 327 | 323 | |
| 328 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 324 | + $entities = $wpdb->get_results($query, OBJECT); // No cache ok. |
|
| 329 | 325 | |
| 330 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 326 | + wl_write_log('Entities found '.count($entities)); |
|
| 331 | 327 | |
| 332 | - foreach ( $entities as $obj ) { |
|
| 328 | + foreach ($entities as $obj) { |
|
| 333 | 329 | |
| 334 | - $entity = get_post( $obj->ID ); |
|
| 330 | + $entity = get_post($obj->ID); |
|
| 335 | 331 | |
| 336 | 332 | // Ensure only valid and published entities are returned. |
| 337 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 333 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
| 338 | 334 | |
| 339 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 335 | + $serialized_entity = wl_serialize_entity($entity); |
|
| 340 | 336 | $serialized_entity['counter'] = $obj->counter; |
| 341 | 337 | $serialized_entity['createdAt'] = $entity->post_date; |
| 342 | 338 | |
| 343 | 339 | // Populate $serialized_entity_groups maintaining array sequence |
| 344 | - foreach ( $serialized_entity_groups as $seg_key => $seg_value ) { |
|
| 345 | - if ( in_array( $serialized_entity['mainType'], $seg_value['entities'] ) ) { |
|
| 340 | + foreach ($serialized_entity_groups as $seg_key => $seg_value) { |
|
| 341 | + if (in_array($serialized_entity['mainType'], $seg_value['entities'])) { |
|
| 346 | 342 | $serialized_entity_groups[$seg_key]['data'][] = $serialized_entity; |
| 347 | 343 | } |
| 348 | 344 | } |
@@ -350,8 +346,8 @@ discard block |
||
| 350 | 346 | } |
| 351 | 347 | |
| 352 | 348 | // Clean-up $serialized_entity_groups by removing empty items and entities |
| 353 | - foreach ( $serialized_entity_groups as $seg_key => $seg_value ) { |
|
| 354 | - if( empty($seg_value['data']) ) { |
|
| 349 | + foreach ($serialized_entity_groups as $seg_key => $seg_value) { |
|
| 350 | + if (empty($seg_value['data'])) { |
|
| 355 | 351 | unset($serialized_entity_groups[$seg_key]); |
| 356 | 352 | } |
| 357 | 353 | unset($serialized_entity_groups[$seg_key]['entities']); |
@@ -368,26 +364,26 @@ discard block |
||
| 368 | 364 | /** |
| 369 | 365 | * Ajax call for the faceted search widget |
| 370 | 366 | */ |
| 371 | -function wl_shortcode_faceted_search_ajax( $http_raw_data = null ) { |
|
| 367 | +function wl_shortcode_faceted_search_ajax($http_raw_data = null) { |
|
| 372 | 368 | |
| 373 | - $results = wl_shortcode_faceted_search_data_ajax( $http_raw_data ); |
|
| 374 | - wl_core_send_json( $results ); |
|
| 369 | + $results = wl_shortcode_faceted_search_data_ajax($http_raw_data); |
|
| 370 | + wl_core_send_json($results); |
|
| 375 | 371 | |
| 376 | 372 | } |
| 377 | 373 | |
| 378 | 374 | /** |
| 379 | 375 | * Adding `wp_ajax` and `wp_ajax_nopriv` action for web backend of faceted-search |
| 380 | 376 | */ |
| 381 | -add_action( 'wp_ajax_wl_faceted_search', 'wl_shortcode_faceted_search_ajax' ); |
|
| 382 | -add_action( 'wp_ajax_nopriv_wl_faceted_search', 'wl_shortcode_faceted_search_ajax' ); |
|
| 377 | +add_action('wp_ajax_wl_faceted_search', 'wl_shortcode_faceted_search_ajax'); |
|
| 378 | +add_action('wp_ajax_nopriv_wl_faceted_search', 'wl_shortcode_faceted_search_ajax'); |
|
| 383 | 379 | |
| 384 | 380 | /** |
| 385 | 381 | * wp-json call for the faceted search widget |
| 386 | 382 | */ |
| 387 | -function wl_shortcode_faceted_search_wp_json( $http_raw_data = null ) { |
|
| 383 | +function wl_shortcode_faceted_search_wp_json($http_raw_data = null) { |
|
| 388 | 384 | |
| 389 | - $results = wl_shortcode_faceted_search_data_wp_json( $http_raw_data ); |
|
| 390 | - if ( ob_get_contents() ) { |
|
| 385 | + $results = wl_shortcode_faceted_search_data_wp_json($http_raw_data); |
|
| 386 | + if (ob_get_contents()) { |
|
| 391 | 387 | ob_clean(); |
| 392 | 388 | } |
| 393 | 389 | return array( |
@@ -399,25 +395,25 @@ discard block |
||
| 399 | 395 | /** |
| 400 | 396 | * Adding `rest_api_init` action for amp backend of faceted-search |
| 401 | 397 | */ |
| 402 | -add_action( 'rest_api_init', function () { |
|
| 403 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 398 | +add_action('rest_api_init', function() { |
|
| 399 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 404 | 400 | 'methods' => 'GET', |
| 405 | 401 | 'callback' => 'wl_shortcode_faceted_search_wp_json', |
| 406 | - ) ); |
|
| 402 | + )); |
|
| 407 | 403 | } ); |
| 408 | 404 | |
| 409 | 405 | /** |
| 410 | 406 | * register_block_type for Gutenberg blocks |
| 411 | 407 | */ |
| 412 | -add_action( 'init', function() { |
|
| 408 | +add_action('init', function() { |
|
| 413 | 409 | // Bail out if the `register_block_type` function isn't available. |
| 414 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 410 | + if ( ! function_exists('register_block_type')) { |
|
| 415 | 411 | return; |
| 416 | 412 | } |
| 417 | 413 | |
| 418 | 414 | register_block_type('wordlift/faceted-search', array( |
| 419 | 415 | 'editor_script' => 'wordlift-admin-edit-gutenberg', |
| 420 | - 'render_callback' => function($attributes){ |
|
| 416 | + 'render_callback' => function($attributes) { |
|
| 421 | 417 | $attr_code = ''; |
| 422 | 418 | foreach ($attributes as $key => $value) { |
| 423 | 419 | $attr_code .= $key.'="'.$value.'" '; |
@@ -427,7 +423,7 @@ discard block |
||
| 427 | 423 | 'attributes' => array( |
| 428 | 424 | 'title' => array( |
| 429 | 425 | 'type' => 'string', |
| 430 | - 'default' => __( 'Related articles', 'wordlift' ) |
|
| 426 | + 'default' => __('Related articles', 'wordlift') |
|
| 431 | 427 | ), |
| 432 | 428 | 'show_facets' => array( |
| 433 | 429 | 'type' => 'bool', |
@@ -18,58 +18,58 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | function wl_shortcode_geomap_get_places( $post_id = null ) { |
| 20 | 20 | |
| 21 | - // If $post_id is null or is not numeric it means this is a global geomap |
|
| 22 | - $is_global = is_null( $post_id ) || ! is_numeric( $post_id ); |
|
| 21 | + // If $post_id is null or is not numeric it means this is a global geomap |
|
| 22 | + $is_global = is_null( $post_id ) || ! is_numeric( $post_id ); |
|
| 23 | 23 | |
| 24 | - // If the current one is not a global geomap, retrieve related entities ids |
|
| 25 | - if ( $is_global ) { |
|
| 26 | - $related_ids = array(); |
|
| 27 | - } else { |
|
| 28 | - $related_ids = wl_core_get_related_entity_ids( $post_id, array( |
|
| 29 | - 'status' => 'publish', |
|
| 30 | - ) ); |
|
| 24 | + // If the current one is not a global geomap, retrieve related entities ids |
|
| 25 | + if ( $is_global ) { |
|
| 26 | + $related_ids = array(); |
|
| 27 | + } else { |
|
| 28 | + $related_ids = wl_core_get_related_entity_ids( $post_id, array( |
|
| 29 | + 'status' => 'publish', |
|
| 30 | + ) ); |
|
| 31 | 31 | |
| 32 | - // Also include current entity |
|
| 33 | - if ( Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 34 | - $related_ids[] = $post_id; |
|
| 35 | - } |
|
| 36 | - } |
|
| 32 | + // Also include current entity |
|
| 33 | + if ( Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 34 | + $related_ids[] = $post_id; |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - // If is not a global geomap, an empty $related_ids means that no entities are related to the post |
|
| 39 | - // An empty array can be returned in this case |
|
| 40 | - if ( ! $is_global && empty( $related_ids ) ) { |
|
| 41 | - return array(); |
|
| 42 | - } |
|
| 38 | + // If is not a global geomap, an empty $related_ids means that no entities are related to the post |
|
| 39 | + // An empty array can be returned in this case |
|
| 40 | + if ( ! $is_global && empty( $related_ids ) ) { |
|
| 41 | + return array(); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - // Retrieve all 'published' places with geo coordinates defined |
|
| 45 | - // If $related_ids is not empty, it's used to limit query results to the current post related places |
|
| 46 | - // Please note that when $place_ids is an empty array, the 'post__in' parameter is not considered in the query |
|
| 47 | - return get_posts( array( |
|
| 48 | - 'post__in' => $related_ids, |
|
| 49 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 50 | - 'nopaging' => true, |
|
| 51 | - 'post_status' => 'publish', |
|
| 52 | - 'meta_query' => array( |
|
| 53 | - 'relation' => 'AND', |
|
| 54 | - array( |
|
| 55 | - 'key' => Wordlift_Schema_Service::FIELD_GEO_LATITUDE, |
|
| 56 | - 'value' => null, |
|
| 57 | - 'compare' => '!=', |
|
| 58 | - ), |
|
| 59 | - array( |
|
| 60 | - 'key' => Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, |
|
| 61 | - 'value' => null, |
|
| 62 | - 'compare' => '!=', |
|
| 63 | - ), |
|
| 64 | - ), |
|
| 65 | - 'tax_query' => array( |
|
| 66 | - array( |
|
| 67 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 68 | - 'field' => 'slug', |
|
| 69 | - 'terms' => 'place', |
|
| 70 | - ), |
|
| 71 | - ), |
|
| 72 | - ) ); |
|
| 44 | + // Retrieve all 'published' places with geo coordinates defined |
|
| 45 | + // If $related_ids is not empty, it's used to limit query results to the current post related places |
|
| 46 | + // Please note that when $place_ids is an empty array, the 'post__in' parameter is not considered in the query |
|
| 47 | + return get_posts( array( |
|
| 48 | + 'post__in' => $related_ids, |
|
| 49 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 50 | + 'nopaging' => true, |
|
| 51 | + 'post_status' => 'publish', |
|
| 52 | + 'meta_query' => array( |
|
| 53 | + 'relation' => 'AND', |
|
| 54 | + array( |
|
| 55 | + 'key' => Wordlift_Schema_Service::FIELD_GEO_LATITUDE, |
|
| 56 | + 'value' => null, |
|
| 57 | + 'compare' => '!=', |
|
| 58 | + ), |
|
| 59 | + array( |
|
| 60 | + 'key' => Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, |
|
| 61 | + 'value' => null, |
|
| 62 | + 'compare' => '!=', |
|
| 63 | + ), |
|
| 64 | + ), |
|
| 65 | + 'tax_query' => array( |
|
| 66 | + array( |
|
| 67 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 68 | + 'field' => 'slug', |
|
| 69 | + 'terms' => 'place', |
|
| 70 | + ), |
|
| 71 | + ), |
|
| 72 | + ) ); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -84,91 +84,91 @@ discard block |
||
| 84 | 84 | */ |
| 85 | 85 | function wl_shortcode_geomap_prepare_map( $places ) { |
| 86 | 86 | |
| 87 | - // Prepare for min/max lat/long in case we need to define a view boundary for the client JavaScript. |
|
| 88 | - $min_latitude = PHP_INT_MAX; |
|
| 89 | - $min_longitude = PHP_INT_MAX; |
|
| 90 | - $max_latitude = ~PHP_INT_MAX; |
|
| 91 | - $max_longitude = ~PHP_INT_MAX; |
|
| 87 | + // Prepare for min/max lat/long in case we need to define a view boundary for the client JavaScript. |
|
| 88 | + $min_latitude = PHP_INT_MAX; |
|
| 89 | + $min_longitude = PHP_INT_MAX; |
|
| 90 | + $max_latitude = ~PHP_INT_MAX; |
|
| 91 | + $max_longitude = ~PHP_INT_MAX; |
|
| 92 | 92 | |
| 93 | - // Prepare an empty array of POIs in geoJSON format. |
|
| 94 | - $pois = array(); |
|
| 95 | - // And store list of points to allow Leaflet compute the optimal bounding box. |
|
| 96 | - // The main reason for this is that geoJSON has swapped coordinates (lon. lat) |
|
| 97 | - $boundaries = array(); |
|
| 93 | + // Prepare an empty array of POIs in geoJSON format. |
|
| 94 | + $pois = array(); |
|
| 95 | + // And store list of points to allow Leaflet compute the optimal bounding box. |
|
| 96 | + // The main reason for this is that geoJSON has swapped coordinates (lon. lat) |
|
| 97 | + $boundaries = array(); |
|
| 98 | 98 | |
| 99 | - // Add a POI for each entity that has coordinates. |
|
| 100 | - foreach ( $places as $entity ) { |
|
| 99 | + // Add a POI for each entity that has coordinates. |
|
| 100 | + foreach ( $places as $entity ) { |
|
| 101 | 101 | |
| 102 | - // Get the coordinates. |
|
| 103 | - $coordinates = wl_get_coordinates( $entity->ID ); |
|
| 102 | + // Get the coordinates. |
|
| 103 | + $coordinates = wl_get_coordinates( $entity->ID ); |
|
| 104 | 104 | |
| 105 | - // Don't show the widget if the coordinates aren't set. |
|
| 106 | - if ( $coordinates['latitude'] == 0 || $coordinates['longitude'] == 0 ) { |
|
| 107 | - continue; |
|
| 108 | - } |
|
| 105 | + // Don't show the widget if the coordinates aren't set. |
|
| 106 | + if ( $coordinates['latitude'] == 0 || $coordinates['longitude'] == 0 ) { |
|
| 107 | + continue; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - // TODO Map html rendering should be delegated to the wordlift js ui layer |
|
| 111 | - // This function should be focused on returning pure data instead |
|
| 110 | + // TODO Map html rendering should be delegated to the wordlift js ui layer |
|
| 111 | + // This function should be focused on returning pure data instead |
|
| 112 | 112 | |
| 113 | - // Get the title, URL and thumb of the entity. |
|
| 114 | - $title = esc_attr( $entity->post_title ); |
|
| 115 | - $link = esc_attr( get_permalink( $entity->ID ) ); |
|
| 116 | - if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $entity->ID ) ) && |
|
| 117 | - false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 118 | - ) { |
|
| 119 | - $img_src = esc_attr( $attachment[0] ); |
|
| 120 | - } |
|
| 113 | + // Get the title, URL and thumb of the entity. |
|
| 114 | + $title = esc_attr( $entity->post_title ); |
|
| 115 | + $link = esc_attr( get_permalink( $entity->ID ) ); |
|
| 116 | + if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $entity->ID ) ) && |
|
| 117 | + false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 118 | + ) { |
|
| 119 | + $img_src = esc_attr( $attachment[0] ); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - // Build HTML popup. TODO: move thumb width in css |
|
| 123 | - $content = "<a href=$link><h6>$title</h6>"; |
|
| 124 | - if ( isset( $img_src ) ) { |
|
| 125 | - $content = $content . "<img src=$img_src style='width:100%'/>"; |
|
| 126 | - } |
|
| 127 | - $content = $content . "</a><ul>"; |
|
| 128 | - // Get the related posts (published) and print them in the popup. |
|
| 129 | - $related_posts = wl_core_get_related_post_ids( $entity->ID, array( |
|
| 130 | - 'status' => 'publish', |
|
| 131 | - ) ); |
|
| 132 | - foreach ( $related_posts as $rp_id ) { |
|
| 122 | + // Build HTML popup. TODO: move thumb width in css |
|
| 123 | + $content = "<a href=$link><h6>$title</h6>"; |
|
| 124 | + if ( isset( $img_src ) ) { |
|
| 125 | + $content = $content . "<img src=$img_src style='width:100%'/>"; |
|
| 126 | + } |
|
| 127 | + $content = $content . "</a><ul>"; |
|
| 128 | + // Get the related posts (published) and print them in the popup. |
|
| 129 | + $related_posts = wl_core_get_related_post_ids( $entity->ID, array( |
|
| 130 | + 'status' => 'publish', |
|
| 131 | + ) ); |
|
| 132 | + foreach ( $related_posts as $rp_id ) { |
|
| 133 | 133 | |
| 134 | - $rp = get_post( $rp_id ); |
|
| 135 | - $title = esc_attr( $rp->post_title ); |
|
| 136 | - $link = esc_attr( get_permalink( $rp->ID ) ); |
|
| 137 | - $content = $content . "<li><a href=$link>$title</a></li>"; |
|
| 138 | - } |
|
| 139 | - $content = $content . "</ul>"; |
|
| 134 | + $rp = get_post( $rp_id ); |
|
| 135 | + $title = esc_attr( $rp->post_title ); |
|
| 136 | + $link = esc_attr( get_permalink( $rp->ID ) ); |
|
| 137 | + $content = $content . "<li><a href=$link>$title</a></li>"; |
|
| 138 | + } |
|
| 139 | + $content = $content . "</ul>"; |
|
| 140 | 140 | |
| 141 | - // Formatting POI in geoJSON. |
|
| 142 | - // http://leafletjs.com/examples/geojson.html |
|
| 143 | - $poi = array( |
|
| 144 | - 'type' => 'Feature', |
|
| 145 | - 'properties' => array( 'popupContent' => $content ), |
|
| 146 | - 'geometry' => array( |
|
| 147 | - 'type' => 'Point', |
|
| 148 | - 'coordinates' => array( |
|
| 149 | - // Leaflet geoJSON wants them swapped |
|
| 150 | - $coordinates['longitude'], |
|
| 151 | - $coordinates['latitude'], |
|
| 152 | - ), |
|
| 153 | - ), |
|
| 154 | - ); |
|
| 141 | + // Formatting POI in geoJSON. |
|
| 142 | + // http://leafletjs.com/examples/geojson.html |
|
| 143 | + $poi = array( |
|
| 144 | + 'type' => 'Feature', |
|
| 145 | + 'properties' => array( 'popupContent' => $content ), |
|
| 146 | + 'geometry' => array( |
|
| 147 | + 'type' => 'Point', |
|
| 148 | + 'coordinates' => array( |
|
| 149 | + // Leaflet geoJSON wants them swapped |
|
| 150 | + $coordinates['longitude'], |
|
| 151 | + $coordinates['latitude'], |
|
| 152 | + ), |
|
| 153 | + ), |
|
| 154 | + ); |
|
| 155 | 155 | |
| 156 | - $pois[] = $poi; |
|
| 156 | + $pois[] = $poi; |
|
| 157 | 157 | |
| 158 | - // Formatting boundaries in a Leaflet-like format (see LatLngBounds). |
|
| 159 | - // http://leafletjs.com/reference.html#latlngbounds |
|
| 160 | - $boundaries[] = array( |
|
| 161 | - $coordinates['latitude'], |
|
| 162 | - $coordinates['longitude'], |
|
| 163 | - ); |
|
| 158 | + // Formatting boundaries in a Leaflet-like format (see LatLngBounds). |
|
| 159 | + // http://leafletjs.com/reference.html#latlngbounds |
|
| 160 | + $boundaries[] = array( |
|
| 161 | + $coordinates['latitude'], |
|
| 162 | + $coordinates['longitude'], |
|
| 163 | + ); |
|
| 164 | 164 | |
| 165 | - } |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - $map_data = array(); |
|
| 168 | - $map_data['features'] = $pois; |
|
| 169 | - $map_data['boundaries'] = $boundaries; |
|
| 167 | + $map_data = array(); |
|
| 168 | + $map_data['features'] = $pois; |
|
| 169 | + $map_data['boundaries'] = $boundaries; |
|
| 170 | 170 | |
| 171 | - return $map_data; |
|
| 171 | + return $map_data; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -182,13 +182,13 @@ discard block |
||
| 182 | 182 | * @return array An array of place posts. |
| 183 | 183 | */ |
| 184 | 184 | function wl_shortcode_geomap_ajax() { |
| 185 | - // Get the post Id. |
|
| 186 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null ); |
|
| 185 | + // Get the post Id. |
|
| 186 | + $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null ); |
|
| 187 | 187 | |
| 188 | - $places = wl_shortcode_geomap_get_places( $post_id ); |
|
| 189 | - $map_data = wl_shortcode_geomap_prepare_map( $places ); |
|
| 188 | + $places = wl_shortcode_geomap_get_places( $post_id ); |
|
| 189 | + $map_data = wl_shortcode_geomap_prepare_map( $places ); |
|
| 190 | 190 | |
| 191 | - wl_core_send_json( $map_data ); |
|
| 191 | + wl_core_send_json( $map_data ); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | add_action( 'wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax' ); |
@@ -199,35 +199,35 @@ discard block |
||
| 199 | 199 | */ |
| 200 | 200 | add_action( 'init', function() { |
| 201 | 201 | |
| 202 | - // Bail out if the `register_block_type` function isn't available. |
|
| 203 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 204 | - return; |
|
| 205 | - } |
|
| 202 | + // Bail out if the `register_block_type` function isn't available. |
|
| 203 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 204 | + return; |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - register_block_type('wordlift/geomap', array( |
|
| 208 | - 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 209 | - 'render_callback' => function($attributes){ |
|
| 210 | - $attr_code = ''; |
|
| 211 | - foreach ($attributes as $key => $value) { |
|
| 212 | - $attr_code .= $key.'="'.$value.'" '; |
|
| 213 | - } |
|
| 214 | - return '[wl_geomap '.$attr_code.']'; |
|
| 215 | - }, |
|
| 216 | - 'attributes' => array( |
|
| 217 | - 'width' => array( |
|
| 218 | - 'type' => 'string', |
|
| 219 | - 'default' => '100%' |
|
| 220 | - ), |
|
| 221 | - 'height' => array( |
|
| 222 | - 'type' => 'string', |
|
| 223 | - 'default' => '300px' |
|
| 224 | - ), |
|
| 225 | - 'global' => array( |
|
| 226 | - 'type' => 'bool', |
|
| 227 | - 'default' => false |
|
| 228 | - ), |
|
| 229 | - ) |
|
| 230 | - )); |
|
| 207 | + register_block_type('wordlift/geomap', array( |
|
| 208 | + 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 209 | + 'render_callback' => function($attributes){ |
|
| 210 | + $attr_code = ''; |
|
| 211 | + foreach ($attributes as $key => $value) { |
|
| 212 | + $attr_code .= $key.'="'.$value.'" '; |
|
| 213 | + } |
|
| 214 | + return '[wl_geomap '.$attr_code.']'; |
|
| 215 | + }, |
|
| 216 | + 'attributes' => array( |
|
| 217 | + 'width' => array( |
|
| 218 | + 'type' => 'string', |
|
| 219 | + 'default' => '100%' |
|
| 220 | + ), |
|
| 221 | + 'height' => array( |
|
| 222 | + 'type' => 'string', |
|
| 223 | + 'default' => '300px' |
|
| 224 | + ), |
|
| 225 | + 'global' => array( |
|
| 226 | + 'type' => 'bool', |
|
| 227 | + 'default' => false |
|
| 228 | + ), |
|
| 229 | + ) |
|
| 230 | + )); |
|
| 231 | 231 | } ); |
| 232 | 232 | |
| 233 | 233 | ///** |
@@ -16,35 +16,35 @@ discard block |
||
| 16 | 16 | * |
| 17 | 17 | * @return array An array of place posts. |
| 18 | 18 | */ |
| 19 | -function wl_shortcode_geomap_get_places( $post_id = null ) { |
|
| 19 | +function wl_shortcode_geomap_get_places($post_id = null) { |
|
| 20 | 20 | |
| 21 | 21 | // If $post_id is null or is not numeric it means this is a global geomap |
| 22 | - $is_global = is_null( $post_id ) || ! is_numeric( $post_id ); |
|
| 22 | + $is_global = is_null($post_id) || ! is_numeric($post_id); |
|
| 23 | 23 | |
| 24 | 24 | // If the current one is not a global geomap, retrieve related entities ids |
| 25 | - if ( $is_global ) { |
|
| 25 | + if ($is_global) { |
|
| 26 | 26 | $related_ids = array(); |
| 27 | 27 | } else { |
| 28 | - $related_ids = wl_core_get_related_entity_ids( $post_id, array( |
|
| 28 | + $related_ids = wl_core_get_related_entity_ids($post_id, array( |
|
| 29 | 29 | 'status' => 'publish', |
| 30 | - ) ); |
|
| 30 | + )); |
|
| 31 | 31 | |
| 32 | 32 | // Also include current entity |
| 33 | - if ( Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) { |
|
| 33 | + if (Wordlift_Entity_Service::get_instance()->is_entity($post_id)) { |
|
| 34 | 34 | $related_ids[] = $post_id; |
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | // If is not a global geomap, an empty $related_ids means that no entities are related to the post |
| 39 | 39 | // An empty array can be returned in this case |
| 40 | - if ( ! $is_global && empty( $related_ids ) ) { |
|
| 40 | + if ( ! $is_global && empty($related_ids)) { |
|
| 41 | 41 | return array(); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | // Retrieve all 'published' places with geo coordinates defined |
| 45 | 45 | // If $related_ids is not empty, it's used to limit query results to the current post related places |
| 46 | 46 | // Please note that when $place_ids is an empty array, the 'post__in' parameter is not considered in the query |
| 47 | - return get_posts( array( |
|
| 47 | + return get_posts(array( |
|
| 48 | 48 | 'post__in' => $related_ids, |
| 49 | 49 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
| 50 | 50 | 'nopaging' => true, |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | 'terms' => 'place', |
| 70 | 70 | ), |
| 71 | 71 | ), |
| 72 | - ) ); |
|
| 72 | + )); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @return array An array of markers and boundaries for Leaflet. |
| 84 | 84 | */ |
| 85 | -function wl_shortcode_geomap_prepare_map( $places ) { |
|
| 85 | +function wl_shortcode_geomap_prepare_map($places) { |
|
| 86 | 86 | |
| 87 | 87 | // Prepare for min/max lat/long in case we need to define a view boundary for the client JavaScript. |
| 88 | 88 | $min_latitude = PHP_INT_MAX; |
@@ -97,13 +97,13 @@ discard block |
||
| 97 | 97 | $boundaries = array(); |
| 98 | 98 | |
| 99 | 99 | // Add a POI for each entity that has coordinates. |
| 100 | - foreach ( $places as $entity ) { |
|
| 100 | + foreach ($places as $entity) { |
|
| 101 | 101 | |
| 102 | 102 | // Get the coordinates. |
| 103 | - $coordinates = wl_get_coordinates( $entity->ID ); |
|
| 103 | + $coordinates = wl_get_coordinates($entity->ID); |
|
| 104 | 104 | |
| 105 | 105 | // Don't show the widget if the coordinates aren't set. |
| 106 | - if ( $coordinates['latitude'] == 0 || $coordinates['longitude'] == 0 ) { |
|
| 106 | + if ($coordinates['latitude'] == 0 || $coordinates['longitude'] == 0) { |
|
| 107 | 107 | continue; |
| 108 | 108 | } |
| 109 | 109 | |
@@ -111,38 +111,38 @@ discard block |
||
| 111 | 111 | // This function should be focused on returning pure data instead |
| 112 | 112 | |
| 113 | 113 | // Get the title, URL and thumb of the entity. |
| 114 | - $title = esc_attr( $entity->post_title ); |
|
| 115 | - $link = esc_attr( get_permalink( $entity->ID ) ); |
|
| 116 | - if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $entity->ID ) ) && |
|
| 117 | - false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 114 | + $title = esc_attr($entity->post_title); |
|
| 115 | + $link = esc_attr(get_permalink($entity->ID)); |
|
| 116 | + if ('' !== ($thumbnail_id = get_post_thumbnail_id($entity->ID)) && |
|
| 117 | + false !== ($attachment = wp_get_attachment_image_src($thumbnail_id)) |
|
| 118 | 118 | ) { |
| 119 | - $img_src = esc_attr( $attachment[0] ); |
|
| 119 | + $img_src = esc_attr($attachment[0]); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | // Build HTML popup. TODO: move thumb width in css |
| 123 | 123 | $content = "<a href=$link><h6>$title</h6>"; |
| 124 | - if ( isset( $img_src ) ) { |
|
| 125 | - $content = $content . "<img src=$img_src style='width:100%'/>"; |
|
| 124 | + if (isset($img_src)) { |
|
| 125 | + $content = $content."<img src=$img_src style='width:100%'/>"; |
|
| 126 | 126 | } |
| 127 | - $content = $content . "</a><ul>"; |
|
| 127 | + $content = $content."</a><ul>"; |
|
| 128 | 128 | // Get the related posts (published) and print them in the popup. |
| 129 | - $related_posts = wl_core_get_related_post_ids( $entity->ID, array( |
|
| 129 | + $related_posts = wl_core_get_related_post_ids($entity->ID, array( |
|
| 130 | 130 | 'status' => 'publish', |
| 131 | - ) ); |
|
| 132 | - foreach ( $related_posts as $rp_id ) { |
|
| 131 | + )); |
|
| 132 | + foreach ($related_posts as $rp_id) { |
|
| 133 | 133 | |
| 134 | - $rp = get_post( $rp_id ); |
|
| 135 | - $title = esc_attr( $rp->post_title ); |
|
| 136 | - $link = esc_attr( get_permalink( $rp->ID ) ); |
|
| 137 | - $content = $content . "<li><a href=$link>$title</a></li>"; |
|
| 134 | + $rp = get_post($rp_id); |
|
| 135 | + $title = esc_attr($rp->post_title); |
|
| 136 | + $link = esc_attr(get_permalink($rp->ID)); |
|
| 137 | + $content = $content."<li><a href=$link>$title</a></li>"; |
|
| 138 | 138 | } |
| 139 | - $content = $content . "</ul>"; |
|
| 139 | + $content = $content."</ul>"; |
|
| 140 | 140 | |
| 141 | 141 | // Formatting POI in geoJSON. |
| 142 | 142 | // http://leafletjs.com/examples/geojson.html |
| 143 | 143 | $poi = array( |
| 144 | 144 | 'type' => 'Feature', |
| 145 | - 'properties' => array( 'popupContent' => $content ), |
|
| 145 | + 'properties' => array('popupContent' => $content), |
|
| 146 | 146 | 'geometry' => array( |
| 147 | 147 | 'type' => 'Point', |
| 148 | 148 | 'coordinates' => array( |
@@ -183,30 +183,30 @@ discard block |
||
| 183 | 183 | */ |
| 184 | 184 | function wl_shortcode_geomap_ajax() { |
| 185 | 185 | // Get the post Id. |
| 186 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null ); |
|
| 186 | + $post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : null); |
|
| 187 | 187 | |
| 188 | - $places = wl_shortcode_geomap_get_places( $post_id ); |
|
| 189 | - $map_data = wl_shortcode_geomap_prepare_map( $places ); |
|
| 188 | + $places = wl_shortcode_geomap_get_places($post_id); |
|
| 189 | + $map_data = wl_shortcode_geomap_prepare_map($places); |
|
| 190 | 190 | |
| 191 | - wl_core_send_json( $map_data ); |
|
| 191 | + wl_core_send_json($map_data); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | -add_action( 'wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax' ); |
|
| 195 | -add_action( 'wp_ajax_nopriv_wl_geomap', 'wl_shortcode_geomap_ajax' ); |
|
| 194 | +add_action('wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax'); |
|
| 195 | +add_action('wp_ajax_nopriv_wl_geomap', 'wl_shortcode_geomap_ajax'); |
|
| 196 | 196 | |
| 197 | 197 | /** |
| 198 | 198 | * register_block_type for Gutenberg blocks |
| 199 | 199 | */ |
| 200 | -add_action( 'init', function() { |
|
| 200 | +add_action('init', function() { |
|
| 201 | 201 | |
| 202 | 202 | // Bail out if the `register_block_type` function isn't available. |
| 203 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 203 | + if ( ! function_exists('register_block_type')) { |
|
| 204 | 204 | return; |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | register_block_type('wordlift/geomap', array( |
| 208 | 208 | 'editor_script' => 'wordlift-admin-edit-gutenberg', |
| 209 | - 'render_callback' => function($attributes){ |
|
| 209 | + 'render_callback' => function($attributes) { |
|
| 210 | 210 | $attr_code = ''; |
| 211 | 211 | foreach ($attributes as $key => $value) { |
| 212 | 212 | $attr_code .= $key.'="'.$value.'" '; |
@@ -15,45 +15,45 @@ discard block |
||
| 15 | 15 | * @return mixed |
| 16 | 16 | */ |
| 17 | 17 | function wl_shortcode_chord_most_referenced_entity_id() { |
| 18 | - // Get the last 20 articles by post date. |
|
| 19 | - // For each article get the entities they reference. |
|
| 20 | - $post_ids = get_posts( array( |
|
| 21 | - 'numberposts' => 20, |
|
| 22 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 23 | - 'fields' => 'ids', // Only get post IDs. |
|
| 24 | - 'post_status' => 'publish', |
|
| 25 | - 'tax_query' => array( |
|
| 26 | - 'relation' => 'OR', |
|
| 27 | - array( |
|
| 28 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 29 | - 'operator' => 'NOT EXISTS', |
|
| 30 | - ), |
|
| 31 | - array( |
|
| 32 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 33 | - 'field' => 'slug', |
|
| 34 | - 'terms' => 'article', |
|
| 35 | - ), |
|
| 36 | - ), |
|
| 37 | - 'orderby' => 'post_date', |
|
| 38 | - 'order' => 'DESC', |
|
| 39 | - ) ); |
|
| 40 | - |
|
| 41 | - if ( empty( $post_ids ) ) { |
|
| 42 | - return null; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - $entities = array(); |
|
| 46 | - foreach ( $post_ids as $id ) { |
|
| 47 | - $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - $famous_entities = array_count_values( $entities ); |
|
| 51 | - arsort( $famous_entities ); |
|
| 52 | - if ( sizeof( $famous_entities ) >= 1 ) { |
|
| 53 | - return key( $famous_entities ); |
|
| 54 | - } else { |
|
| 55 | - return $post_ids[0]; |
|
| 56 | - } |
|
| 18 | + // Get the last 20 articles by post date. |
|
| 19 | + // For each article get the entities they reference. |
|
| 20 | + $post_ids = get_posts( array( |
|
| 21 | + 'numberposts' => 20, |
|
| 22 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 23 | + 'fields' => 'ids', // Only get post IDs. |
|
| 24 | + 'post_status' => 'publish', |
|
| 25 | + 'tax_query' => array( |
|
| 26 | + 'relation' => 'OR', |
|
| 27 | + array( |
|
| 28 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 29 | + 'operator' => 'NOT EXISTS', |
|
| 30 | + ), |
|
| 31 | + array( |
|
| 32 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 33 | + 'field' => 'slug', |
|
| 34 | + 'terms' => 'article', |
|
| 35 | + ), |
|
| 36 | + ), |
|
| 37 | + 'orderby' => 'post_date', |
|
| 38 | + 'order' => 'DESC', |
|
| 39 | + ) ); |
|
| 40 | + |
|
| 41 | + if ( empty( $post_ids ) ) { |
|
| 42 | + return null; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + $entities = array(); |
|
| 46 | + foreach ( $post_ids as $id ) { |
|
| 47 | + $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + $famous_entities = array_count_values( $entities ); |
|
| 51 | + arsort( $famous_entities ); |
|
| 52 | + if ( sizeof( $famous_entities ) >= 1 ) { |
|
| 53 | + return key( $famous_entities ); |
|
| 54 | + } else { |
|
| 55 | + return $post_ids[0]; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
| 59 | 59 | |
@@ -71,60 +71,60 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) { |
| 73 | 73 | |
| 74 | - if ( ! is_null( $related ) ) { |
|
| 75 | - if ( 0 === $depth ) { |
|
| 76 | - return $related; |
|
| 77 | - } |
|
| 78 | - } |
|
| 74 | + if ( ! is_null( $related ) ) { |
|
| 75 | + if ( 0 === $depth ) { |
|
| 76 | + return $related; |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" ); |
|
| 80 | + wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" ); |
|
| 81 | 81 | |
| 82 | - // Create a related array which will hold entities and relations. |
|
| 83 | - if ( is_null( $related ) ) { |
|
| 84 | - $related = array( |
|
| 85 | - 'entities' => array( $entity_id ), |
|
| 86 | - 'relations' => array(), |
|
| 87 | - ); |
|
| 88 | - } |
|
| 82 | + // Create a related array which will hold entities and relations. |
|
| 83 | + if ( is_null( $related ) ) { |
|
| 84 | + $related = array( |
|
| 85 | + 'entities' => array( $entity_id ), |
|
| 86 | + 'relations' => array(), |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - // Get related entities |
|
| 91 | - $related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array( |
|
| 92 | - 'status' => 'publish', |
|
| 93 | - ) ); |
|
| 90 | + // Get related entities |
|
| 91 | + $related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array( |
|
| 92 | + 'status' => 'publish', |
|
| 93 | + ) ); |
|
| 94 | 94 | |
| 95 | - // If the current node is an entity, add related posts too |
|
| 96 | - $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 97 | - ->is_entity( $entity_id ) ) ? |
|
| 98 | - wl_core_get_related_post_ids( $entity_id, array( |
|
| 99 | - 'status' => 'publish', |
|
| 100 | - ) ) : |
|
| 101 | - array(); |
|
| 95 | + // If the current node is an entity, add related posts too |
|
| 96 | + $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 97 | + ->is_entity( $entity_id ) ) ? |
|
| 98 | + wl_core_get_related_post_ids( $entity_id, array( |
|
| 99 | + 'status' => 'publish', |
|
| 100 | + ) ) : |
|
| 101 | + array(); |
|
| 102 | 102 | |
| 103 | - // Merge results and remove duplicated entries |
|
| 104 | - $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 103 | + // Merge results and remove duplicated entries |
|
| 104 | + $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 105 | 105 | |
| 106 | - // TODO: List of entities ($rel) should be ordered by interest factors. |
|
| 107 | - shuffle( $related_ids ); |
|
| 106 | + // TODO: List of entities ($rel) should be ordered by interest factors. |
|
| 107 | + shuffle( $related_ids ); |
|
| 108 | 108 | |
| 109 | - // Now we have all the related IDs. |
|
| 110 | - foreach ( $related_ids as $related_id ) { |
|
| 109 | + // Now we have all the related IDs. |
|
| 110 | + foreach ( $related_ids as $related_id ) { |
|
| 111 | 111 | |
| 112 | - if ( count( $related['entities'] ) >= $max_size ) { |
|
| 113 | - return $related; |
|
| 114 | - } |
|
| 112 | + if ( count( $related['entities'] ) >= $max_size ) { |
|
| 113 | + return $related; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - $related['relations'][] = array( $entity_id, $related_id ); |
|
| 116 | + $related['relations'][] = array( $entity_id, $related_id ); |
|
| 117 | 117 | |
| 118 | - if ( ! in_array( $related_id, $related['entities'] ) ) { |
|
| 119 | - // Found new related entity! |
|
| 120 | - $related['entities'][] = $related_id; |
|
| 118 | + if ( ! in_array( $related_id, $related['entities'] ) ) { |
|
| 119 | + // Found new related entity! |
|
| 120 | + $related['entities'][] = $related_id; |
|
| 121 | 121 | |
| 122 | - $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 123 | - } |
|
| 124 | - } |
|
| 122 | + $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - // End condition 2: no more entities to search for. |
|
| 127 | - return $related; |
|
| 126 | + // End condition 2: no more entities to search for. |
|
| 127 | + return $related; |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -138,57 +138,57 @@ discard block |
||
| 138 | 138 | */ |
| 139 | 139 | function wl_shortcode_chord_get_graph( $data ) { |
| 140 | 140 | |
| 141 | - // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
|
| 142 | - array_walk( $data['entities'], function ( &$item ) { |
|
| 143 | - $post = get_post( $item ); |
|
| 144 | - |
|
| 145 | - // Skip non-existing posts. |
|
| 146 | - if ( is_null( $post ) ) { |
|
| 147 | - wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 148 | - |
|
| 149 | - return $item; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
|
| 153 | - $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 154 | - |
|
| 155 | - // The following log may create a circular loop. |
|
| 156 | - // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
|
| 157 | - |
|
| 158 | - // TODO: get all images |
|
| 159 | - $thumbnail = null; |
|
| 160 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 161 | - if ( '' !== $thumbnail_id ) { |
|
| 162 | - $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 163 | - if ( false !== $attachment ) { |
|
| 164 | - $thumbnail = esc_attr( $attachment[0] ); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $entity = array( |
|
| 169 | - 'uri' => wl_get_entity_uri( $item ), |
|
| 170 | - 'url' => get_permalink( $item ), |
|
| 171 | - 'label' => $post->post_title, |
|
| 172 | - 'type' => $post->post_type, |
|
| 173 | - 'thumbnails' => array( $thumbnail ), |
|
| 174 | - 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 175 | - ); |
|
| 176 | - |
|
| 177 | - $item = $entity; |
|
| 178 | - } ); |
|
| 179 | - |
|
| 180 | - // Refactor the relations. |
|
| 181 | - array_walk( $data['relations'], function ( &$item ) { |
|
| 182 | - $relation = array( |
|
| 183 | - 's' => wl_get_entity_uri( $item[0] ), |
|
| 184 | - 'o' => wl_get_entity_uri( $item[1] ), |
|
| 185 | - ); |
|
| 186 | - |
|
| 187 | - $item = $relation; |
|
| 188 | - } ); |
|
| 189 | - |
|
| 190 | - // Return the JSON representation. |
|
| 191 | - return $data; |
|
| 141 | + // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
|
| 142 | + array_walk( $data['entities'], function ( &$item ) { |
|
| 143 | + $post = get_post( $item ); |
|
| 144 | + |
|
| 145 | + // Skip non-existing posts. |
|
| 146 | + if ( is_null( $post ) ) { |
|
| 147 | + wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 148 | + |
|
| 149 | + return $item; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
|
| 153 | + $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 154 | + |
|
| 155 | + // The following log may create a circular loop. |
|
| 156 | + // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
|
| 157 | + |
|
| 158 | + // TODO: get all images |
|
| 159 | + $thumbnail = null; |
|
| 160 | + $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 161 | + if ( '' !== $thumbnail_id ) { |
|
| 162 | + $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 163 | + if ( false !== $attachment ) { |
|
| 164 | + $thumbnail = esc_attr( $attachment[0] ); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $entity = array( |
|
| 169 | + 'uri' => wl_get_entity_uri( $item ), |
|
| 170 | + 'url' => get_permalink( $item ), |
|
| 171 | + 'label' => $post->post_title, |
|
| 172 | + 'type' => $post->post_type, |
|
| 173 | + 'thumbnails' => array( $thumbnail ), |
|
| 174 | + 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 175 | + ); |
|
| 176 | + |
|
| 177 | + $item = $entity; |
|
| 178 | + } ); |
|
| 179 | + |
|
| 180 | + // Refactor the relations. |
|
| 181 | + array_walk( $data['relations'], function ( &$item ) { |
|
| 182 | + $relation = array( |
|
| 183 | + 's' => wl_get_entity_uri( $item[0] ), |
|
| 184 | + 'o' => wl_get_entity_uri( $item[1] ), |
|
| 185 | + ); |
|
| 186 | + |
|
| 187 | + $item = $relation; |
|
| 188 | + } ); |
|
| 189 | + |
|
| 190 | + // Return the JSON representation. |
|
| 191 | + return $data; |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
@@ -199,13 +199,13 @@ discard block |
||
| 199 | 199 | */ |
| 200 | 200 | function wl_shortcode_chord_ajax() { |
| 201 | 201 | |
| 202 | - $post_id = $_REQUEST['post_id']; |
|
| 203 | - $depth = $_REQUEST['depth']; |
|
| 202 | + $post_id = $_REQUEST['post_id']; |
|
| 203 | + $depth = $_REQUEST['depth']; |
|
| 204 | 204 | |
| 205 | - $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 206 | - $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 205 | + $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 206 | + $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 207 | 207 | |
| 208 | - wl_core_send_json( $graph ); |
|
| 208 | + wl_core_send_json( $graph ); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' ); |
@@ -216,42 +216,42 @@ discard block |
||
| 216 | 216 | */ |
| 217 | 217 | add_action( 'init', function () { |
| 218 | 218 | |
| 219 | - // Bail out if the `register_block_type` function isn't available. |
|
| 220 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 221 | - return; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - register_block_type( 'wordlift/chord', array( |
|
| 225 | - 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 226 | - 'render_callback' => function ( $attributes ) { |
|
| 227 | - $attr_code = ''; |
|
| 228 | - foreach ( $attributes as $key => $value ) { |
|
| 229 | - $attr_code .= $key . '="' . $value . '" '; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - return '[wl_chord ' . $attr_code . ']'; |
|
| 233 | - }, |
|
| 234 | - 'attributes' => array( |
|
| 235 | - 'width' => array( |
|
| 236 | - 'type' => 'string', |
|
| 237 | - 'default' => '100%', |
|
| 238 | - ), |
|
| 239 | - 'height' => array( |
|
| 240 | - 'type' => 'string', |
|
| 241 | - 'default' => '500px', |
|
| 242 | - ), |
|
| 243 | - 'main_color' => array( |
|
| 244 | - 'type' => 'string', |
|
| 245 | - 'default' => '000', |
|
| 246 | - ), |
|
| 247 | - 'depth' => array( |
|
| 248 | - 'type' => 'number', |
|
| 249 | - 'default' => 2, |
|
| 250 | - ), |
|
| 251 | - 'global' => array( |
|
| 252 | - 'type' => 'bool', |
|
| 253 | - 'default' => false, |
|
| 254 | - ), |
|
| 255 | - ), |
|
| 256 | - ) ); |
|
| 219 | + // Bail out if the `register_block_type` function isn't available. |
|
| 220 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 221 | + return; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + register_block_type( 'wordlift/chord', array( |
|
| 225 | + 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 226 | + 'render_callback' => function ( $attributes ) { |
|
| 227 | + $attr_code = ''; |
|
| 228 | + foreach ( $attributes as $key => $value ) { |
|
| 229 | + $attr_code .= $key . '="' . $value . '" '; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + return '[wl_chord ' . $attr_code . ']'; |
|
| 233 | + }, |
|
| 234 | + 'attributes' => array( |
|
| 235 | + 'width' => array( |
|
| 236 | + 'type' => 'string', |
|
| 237 | + 'default' => '100%', |
|
| 238 | + ), |
|
| 239 | + 'height' => array( |
|
| 240 | + 'type' => 'string', |
|
| 241 | + 'default' => '500px', |
|
| 242 | + ), |
|
| 243 | + 'main_color' => array( |
|
| 244 | + 'type' => 'string', |
|
| 245 | + 'default' => '000', |
|
| 246 | + ), |
|
| 247 | + 'depth' => array( |
|
| 248 | + 'type' => 'number', |
|
| 249 | + 'default' => 2, |
|
| 250 | + ), |
|
| 251 | + 'global' => array( |
|
| 252 | + 'type' => 'bool', |
|
| 253 | + 'default' => false, |
|
| 254 | + ), |
|
| 255 | + ), |
|
| 256 | + ) ); |
|
| 257 | 257 | } ); |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | function wl_shortcode_chord_most_referenced_entity_id() { |
| 18 | 18 | // Get the last 20 articles by post date. |
| 19 | 19 | // For each article get the entities they reference. |
| 20 | - $post_ids = get_posts( array( |
|
| 20 | + $post_ids = get_posts(array( |
|
| 21 | 21 | 'numberposts' => 20, |
| 22 | 22 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
| 23 | 23 | 'fields' => 'ids', // Only get post IDs. |
@@ -36,21 +36,21 @@ discard block |
||
| 36 | 36 | ), |
| 37 | 37 | 'orderby' => 'post_date', |
| 38 | 38 | 'order' => 'DESC', |
| 39 | - ) ); |
|
| 39 | + )); |
|
| 40 | 40 | |
| 41 | - if ( empty( $post_ids ) ) { |
|
| 41 | + if (empty($post_ids)) { |
|
| 42 | 42 | return null; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | $entities = array(); |
| 46 | - foreach ( $post_ids as $id ) { |
|
| 47 | - $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 46 | + foreach ($post_ids as $id) { |
|
| 47 | + $entities = array_merge($entities, wl_core_get_related_entity_ids($id)); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - $famous_entities = array_count_values( $entities ); |
|
| 51 | - arsort( $famous_entities ); |
|
| 52 | - if ( sizeof( $famous_entities ) >= 1 ) { |
|
| 53 | - return key( $famous_entities ); |
|
| 50 | + $famous_entities = array_count_values($entities); |
|
| 51 | + arsort($famous_entities); |
|
| 52 | + if (sizeof($famous_entities) >= 1) { |
|
| 53 | + return key($famous_entities); |
|
| 54 | 54 | } else { |
| 55 | 55 | return $post_ids[0]; |
| 56 | 56 | } |
@@ -69,57 +69,56 @@ discard block |
||
| 69 | 69 | * @uses wl_core_get_related_post_ids() to get the list of post ids that reference an entity. |
| 70 | 70 | * |
| 71 | 71 | */ |
| 72 | -function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) { |
|
| 72 | +function wl_shortcode_chord_get_relations($entity_id, $depth = 2, $related = null, $max_size = 9) { |
|
| 73 | 73 | |
| 74 | - if ( ! is_null( $related ) ) { |
|
| 75 | - if ( 0 === $depth ) { |
|
| 74 | + if ( ! is_null($related)) { |
|
| 75 | + if (0 === $depth) { |
|
| 76 | 76 | return $related; |
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" ); |
|
| 80 | + wl_write_log("wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: ".(is_null($related) ? 'yes' : 'no')." ]"); |
|
| 81 | 81 | |
| 82 | 82 | // Create a related array which will hold entities and relations. |
| 83 | - if ( is_null( $related ) ) { |
|
| 83 | + if (is_null($related)) { |
|
| 84 | 84 | $related = array( |
| 85 | - 'entities' => array( $entity_id ), |
|
| 85 | + 'entities' => array($entity_id), |
|
| 86 | 86 | 'relations' => array(), |
| 87 | 87 | ); |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | // Get related entities |
| 91 | - $related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array( |
|
| 91 | + $related_entity_ids = wl_core_get_related_entity_ids($entity_id, array( |
|
| 92 | 92 | 'status' => 'publish', |
| 93 | - ) ); |
|
| 93 | + )); |
|
| 94 | 94 | |
| 95 | 95 | // If the current node is an entity, add related posts too |
| 96 | - $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 97 | - ->is_entity( $entity_id ) ) ? |
|
| 98 | - wl_core_get_related_post_ids( $entity_id, array( |
|
| 96 | + $related_post_ids = (Wordlift_Entity_Service::get_instance() |
|
| 97 | + ->is_entity($entity_id)) ? |
|
| 98 | + wl_core_get_related_post_ids($entity_id, array( |
|
| 99 | 99 | 'status' => 'publish', |
| 100 | - ) ) : |
|
| 101 | - array(); |
|
| 100 | + )) : array(); |
|
| 102 | 101 | |
| 103 | 102 | // Merge results and remove duplicated entries |
| 104 | - $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 103 | + $related_ids = array_unique(array_merge($related_post_ids, $related_entity_ids)); |
|
| 105 | 104 | |
| 106 | 105 | // TODO: List of entities ($rel) should be ordered by interest factors. |
| 107 | - shuffle( $related_ids ); |
|
| 106 | + shuffle($related_ids); |
|
| 108 | 107 | |
| 109 | 108 | // Now we have all the related IDs. |
| 110 | - foreach ( $related_ids as $related_id ) { |
|
| 109 | + foreach ($related_ids as $related_id) { |
|
| 111 | 110 | |
| 112 | - if ( count( $related['entities'] ) >= $max_size ) { |
|
| 111 | + if (count($related['entities']) >= $max_size) { |
|
| 113 | 112 | return $related; |
| 114 | 113 | } |
| 115 | 114 | |
| 116 | - $related['relations'][] = array( $entity_id, $related_id ); |
|
| 115 | + $related['relations'][] = array($entity_id, $related_id); |
|
| 117 | 116 | |
| 118 | - if ( ! in_array( $related_id, $related['entities'] ) ) { |
|
| 117 | + if ( ! in_array($related_id, $related['entities'])) { |
|
| 119 | 118 | // Found new related entity! |
| 120 | 119 | $related['entities'][] = $related_id; |
| 121 | 120 | |
| 122 | - $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 121 | + $related = wl_shortcode_chord_get_relations($related_id, ($depth - 1), $related, $max_size); |
|
| 123 | 122 | } |
| 124 | 123 | } |
| 125 | 124 | |
@@ -136,52 +135,52 @@ discard block |
||
| 136 | 135 | * |
| 137 | 136 | * @return mixed|string |
| 138 | 137 | */ |
| 139 | -function wl_shortcode_chord_get_graph( $data ) { |
|
| 138 | +function wl_shortcode_chord_get_graph($data) { |
|
| 140 | 139 | |
| 141 | 140 | // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
| 142 | - array_walk( $data['entities'], function ( &$item ) { |
|
| 143 | - $post = get_post( $item ); |
|
| 141 | + array_walk($data['entities'], function(&$item) { |
|
| 142 | + $post = get_post($item); |
|
| 144 | 143 | |
| 145 | 144 | // Skip non-existing posts. |
| 146 | - if ( is_null( $post ) ) { |
|
| 147 | - wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 145 | + if (is_null($post)) { |
|
| 146 | + wl_write_log("wl_shortcode_chord_get_graph : post not found [ post id :: $item ]"); |
|
| 148 | 147 | |
| 149 | 148 | return $item; |
| 150 | 149 | } |
| 151 | 150 | |
| 152 | 151 | // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
| 153 | - $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 152 | + $term = Wordlift_Entity_Type_Service::get_instance()->get($item); |
|
| 154 | 153 | |
| 155 | 154 | // The following log may create a circular loop. |
| 156 | 155 | // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
| 157 | 156 | |
| 158 | 157 | // TODO: get all images |
| 159 | 158 | $thumbnail = null; |
| 160 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 161 | - if ( '' !== $thumbnail_id ) { |
|
| 162 | - $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 163 | - if ( false !== $attachment ) { |
|
| 164 | - $thumbnail = esc_attr( $attachment[0] ); |
|
| 159 | + $thumbnail_id = get_post_thumbnail_id($post->ID); |
|
| 160 | + if ('' !== $thumbnail_id) { |
|
| 161 | + $attachment = wp_get_attachment_image_src($thumbnail_id); |
|
| 162 | + if (false !== $attachment) { |
|
| 163 | + $thumbnail = esc_attr($attachment[0]); |
|
| 165 | 164 | } |
| 166 | 165 | } |
| 167 | 166 | |
| 168 | 167 | $entity = array( |
| 169 | - 'uri' => wl_get_entity_uri( $item ), |
|
| 170 | - 'url' => get_permalink( $item ), |
|
| 168 | + 'uri' => wl_get_entity_uri($item), |
|
| 169 | + 'url' => get_permalink($item), |
|
| 171 | 170 | 'label' => $post->post_title, |
| 172 | 171 | 'type' => $post->post_type, |
| 173 | - 'thumbnails' => array( $thumbnail ), |
|
| 174 | - 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 172 | + 'thumbnails' => array($thumbnail), |
|
| 173 | + 'css_class' => (isset($term['css_class']) ? $term['css_class'] : ''), |
|
| 175 | 174 | ); |
| 176 | 175 | |
| 177 | 176 | $item = $entity; |
| 178 | 177 | } ); |
| 179 | 178 | |
| 180 | 179 | // Refactor the relations. |
| 181 | - array_walk( $data['relations'], function ( &$item ) { |
|
| 180 | + array_walk($data['relations'], function(&$item) { |
|
| 182 | 181 | $relation = array( |
| 183 | - 's' => wl_get_entity_uri( $item[0] ), |
|
| 184 | - 'o' => wl_get_entity_uri( $item[1] ), |
|
| 182 | + 's' => wl_get_entity_uri($item[0]), |
|
| 183 | + 'o' => wl_get_entity_uri($item[1]), |
|
| 185 | 184 | ); |
| 186 | 185 | |
| 187 | 186 | $item = $relation; |
@@ -202,34 +201,34 @@ discard block |
||
| 202 | 201 | $post_id = $_REQUEST['post_id']; |
| 203 | 202 | $depth = $_REQUEST['depth']; |
| 204 | 203 | |
| 205 | - $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 206 | - $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 204 | + $relations = wl_shortcode_chord_get_relations($post_id, $depth); |
|
| 205 | + $graph = wl_shortcode_chord_get_graph($relations); |
|
| 207 | 206 | |
| 208 | - wl_core_send_json( $graph ); |
|
| 207 | + wl_core_send_json($graph); |
|
| 209 | 208 | } |
| 210 | 209 | |
| 211 | -add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' ); |
|
| 212 | -add_action( 'wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax' ); |
|
| 210 | +add_action('wp_ajax_wl_chord', 'wl_shortcode_chord_ajax'); |
|
| 211 | +add_action('wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax'); |
|
| 213 | 212 | |
| 214 | 213 | /** |
| 215 | 214 | * register_block_type for Gutenberg blocks |
| 216 | 215 | */ |
| 217 | -add_action( 'init', function () { |
|
| 216 | +add_action('init', function() { |
|
| 218 | 217 | |
| 219 | 218 | // Bail out if the `register_block_type` function isn't available. |
| 220 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 219 | + if ( ! function_exists('register_block_type')) { |
|
| 221 | 220 | return; |
| 222 | 221 | } |
| 223 | 222 | |
| 224 | - register_block_type( 'wordlift/chord', array( |
|
| 223 | + register_block_type('wordlift/chord', array( |
|
| 225 | 224 | 'editor_script' => 'wordlift-admin-edit-gutenberg', |
| 226 | - 'render_callback' => function ( $attributes ) { |
|
| 225 | + 'render_callback' => function($attributes) { |
|
| 227 | 226 | $attr_code = ''; |
| 228 | - foreach ( $attributes as $key => $value ) { |
|
| 229 | - $attr_code .= $key . '="' . $value . '" '; |
|
| 227 | + foreach ($attributes as $key => $value) { |
|
| 228 | + $attr_code .= $key.'="'.$value.'" '; |
|
| 230 | 229 | } |
| 231 | 230 | |
| 232 | - return '[wl_chord ' . $attr_code . ']'; |
|
| 231 | + return '[wl_chord '.$attr_code.']'; |
|
| 233 | 232 | }, |
| 234 | 233 | 'attributes' => array( |
| 235 | 234 | 'width' => array( |
@@ -253,5 +252,5 @@ discard block |
||
| 253 | 252 | 'default' => false, |
| 254 | 253 | ), |
| 255 | 254 | ), |
| 256 | - ) ); |
|
| 255 | + )); |
|
| 257 | 256 | } ); |