| 1 |
<?php |
| 2 |
/* |
| 3 |
* Plugin Name: Display Remote Posts Block |
| 4 |
* Description: Block to display recent posts from a WordPress or Blogger blog |
| 5 |
* Author: Webd Ltd |
| 6 |
* Version: 1.1.4 |
| 7 |
* Author URI: https://webd.uk |
| 8 |
* License: GPLv2 or later |
| 9 |
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 10 |
* Text Domain: display-remote-posts-block |
| 11 |
*/ |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
if (!defined('ABSPATH')) { |
| 16 |
exit('This isn\'t the page you\'re looking for. Move along, move along.'); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
if (!class_exists('display_remote_posts_block_class')) { |
| 22 |
|
| 23 |
class display_remote_posts_block_class { |
| 24 |
|
| 25 |
public static $version = '1.1.4'; |
| 26 |
|
| 27 |
function __construct() { |
| 28 |
|
| 29 |
add_action('init', array($this, 'init')); |
| 30 |
add_action('display_remote_posts_block_cron', array($this, 'display_remote_posts_block_cron')); |
| 31 |
|
| 32 |
if (!wp_next_scheduled('display_remote_posts_block_cron')) { |
| 33 |
|
| 34 |
wp_schedule_event(time(), 'hourly', 'display_remote_posts_block_cron'); |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
register_deactivation_hook(__FILE__, array($this, 'display_remote_posts_block_deactivate')); |
| 39 |
|
| 40 |
if (is_admin()) { |
| 41 |
|
| 42 |
add_action('admin_notices', 'drpbCommon::admin_notices'); |
| 43 |
add_action('wp_ajax_dismiss_drpb_notice_handler', 'drpbCommon::ajax_notice_handler'); |
| 44 |
add_filter('plugin_row_meta', 'drpbCommon::plugin_row_meta', 10, 4); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
public function init() { |
| 51 |
|
| 52 |
if (false === self::check_gutenberg()) { |
| 53 |
|
| 54 |
return false; |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
$asset_file = include( plugin_dir_path( __FILE__ ) . 'build/index.asset.php'); |
| 59 |
|
| 60 |
wp_register_script( |
| 61 |
'display-remote-posts-block', |
| 62 |
plugins_url( 'build/index.js', __FILE__ ), |
| 63 |
$asset_file['dependencies'], |
| 64 |
$asset_file['version'] |
| 65 |
); |
| 66 |
|
| 67 |
register_block_type( 'display-remote-posts-block/display-remote-posts', array( |
| 68 |
'apiVersion' => 3, |
| 69 |
'editor_script' => 'display-remote-posts-block', |
| 70 |
'render_callback' => array($this, 'render_display_remote_posts_block'), |
| 71 |
'textdomain' => 'display-remote-posts-block', |
| 72 |
'attributes' => array( |
| 73 |
'url' => array( |
| 74 |
'type' => 'string', |
| 75 |
'default' => '' |
| 76 |
), |
| 77 |
'title' => array( |
| 78 |
'type' => 'boolean', |
| 79 |
'default' => true |
| 80 |
), |
| 81 |
'post_date' => array( |
| 82 |
'type' => 'boolean', |
| 83 |
'default' => false |
| 84 |
), |
| 85 |
'posts' => array( |
| 86 |
'type' => 'number', |
| 87 |
'default' => 1 |
| 88 |
), |
| 89 |
'offset' => array( |
| 90 |
'type' => 'number', |
| 91 |
'default' => 0 |
| 92 |
), |
| 93 |
'featured_images' => array( |
| 94 |
'type' => 'boolean', |
| 95 |
'default' => true |
| 96 |
), |
| 97 |
'excerpts' => array( |
| 98 |
'type' => 'boolean', |
| 99 |
'default' => true |
| 100 |
), |
| 101 |
'target_blank' => array( |
| 102 |
'type' => 'boolean', |
| 103 |
'default' => false |
| 104 |
), |
| 105 |
'max_width' => array( |
| 106 |
'type' => 'number', |
| 107 |
'default' => 0 |
| 108 |
) |
| 109 |
) |
| 110 |
)); |
| 111 |
|
| 112 |
wp_set_script_translations('display-remote-posts-block', 'display-remote-posts-block'); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
public function display_remote_posts_block_cron() { |
| 117 |
|
| 118 |
delete_option('display_remote_posts_block_cache'); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
public function display_remote_posts_block_deactivate() { |
| 123 |
|
| 124 |
$timestamp = wp_next_scheduled('display_remote_posts_block_cron'); |
| 125 |
wp_unschedule_event($timestamp, 'display_remote_posts_block_cron'); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
public function render_display_remote_posts_block($attributes, $content) { |
| 130 |
|
| 131 |
$attributes = shortcode_atts(array( |
| 132 |
'url' => '', |
| 133 |
'title' => true, |
| 134 |
'post_date' => false, |
| 135 |
'posts' => 1, |
| 136 |
'offset' => 0, |
| 137 |
'featured_images' => true, |
| 138 |
'excerpts' => true, |
| 139 |
'target_blank' => false, |
| 140 |
'max_width' => 0 |
| 141 |
), $attributes); |
| 142 |
|
| 143 |
$content = '<div class="wp-block-display-remote-posts wp-block-display-remote-posts__inner-container">'; |
| 144 |
|
| 145 |
$attributes['url'] = trailingslashit(sanitize_url($attributes['url'])); |
| 146 |
|
| 147 |
if ('/' === $attributes['url']) { |
| 148 |
|
| 149 |
$attributes['url'] = get_home_url(); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
if (filter_var($attributes['url'], FILTER_VALIDATE_URL)) { |
| 154 |
|
| 155 |
$cached_data = get_option('display_remote_posts_block_cache', array()); |
| 156 |
|
| 157 |
$url_hash = md5($attributes['url']); |
| 158 |
|
| 159 |
if (isset($cached_data[$url_hash])) { |
| 160 |
|
| 161 |
$blog_data = $cached_data[$url_hash]; |
| 162 |
|
| 163 |
} else { |
| 164 |
|
| 165 |
$blog_data = $this->get_wordpress_com_data($attributes['url']); |
| 166 |
|
| 167 |
if (is_wp_error($blog_data)) { |
| 168 |
|
| 169 |
$blog_data = $this->get_wordpress_org_data($attributes['url']); |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
if (is_wp_error($blog_data)) { |
| 174 |
|
| 175 |
$blog_data = $this->get_blogger_data($attributes['url']); |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
if (!is_wp_error($blog_data)) { |
| 180 |
|
| 181 |
$cached_data[$url_hash] = $blog_data; |
| 182 |
|
| 183 |
update_option('display_remote_posts_block_cache', $cached_data); |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
if (!is_wp_error($blog_data)) { |
| 190 |
|
| 191 |
if ($attributes['title'] && isset($blog_data->name) && $blog_data->name) { |
| 192 |
|
| 193 |
$content .= '<h3>' . esc_html($blog_data->name) . '</h3>' . PHP_EOL; |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
$post_counter = 0; |
| 198 |
|
| 199 |
foreach ($blog_data->posts as $single_post) { |
| 200 |
|
| 201 |
$post_counter++; |
| 202 |
|
| 203 |
if (($post_counter - 1) < absint($attributes['offset'])) { continue; } |
| 204 |
|
| 205 |
if ($post_counter > (absint($attributes['posts']) + absint($attributes['offset']))) { break; } |
| 206 |
|
| 207 |
If (isset($single_post->URL) && $single_post->URL) { |
| 208 |
|
| 209 |
$content .= '<h4><a href="' . esc_url($single_post->URL) . '" title="' . esc_attr( $single_post->title ) . '"' . ($attributes['target_blank'] ? ' target="_blank"': '') . '>' . esc_html($single_post->title) . '</a></h4>' . PHP_EOL; |
| 210 |
|
| 211 |
} else { |
| 212 |
|
| 213 |
$content .= '<h4>' . esc_html($single_post->title) . '</h4>' . PHP_EOL; |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
If ($attributes['post_date'] && isset($single_post->post_date) && absint($single_post->post_date)) { |
| 218 |
|
| 219 |
$content .= '<div class="wp-block-post-date"><time datetime="' . date_i18n('c', absint($single_post->post_date)) . '">' . date_i18n(get_option('date_format'), absint($single_post->post_date)) . '</time></div>' . PHP_EOL; |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
if ($attributes['max_width'] && isset($single_post->srcset) && is_array($single_post->srcset) && $single_post->srcset) { |
| 224 |
|
| 225 |
foreach ($single_post->srcset as $width => $url) { |
| 226 |
|
| 227 |
$single_post->featured_image = $url; |
| 228 |
|
| 229 |
if ($width <= $attributes['max_width']) { |
| 230 |
|
| 231 |
break; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
if (isset($single_post->URL) && $single_post->URL && $attributes['featured_images'] && $single_post->featured_image) { |
| 240 |
|
| 241 |
$content .= '<figure class="wp-block-image"><a title="' . esc_attr( $single_post->title ) . '" href="' . esc_url($single_post->URL) . '"' . ($attributes['target_blank'] ? ' target="_blank"': '') . '><img src="' . esc_url($single_post->featured_image) . '" alt="' . esc_attr($single_post->title ) . '"/></a></figure>' . PHP_EOL; |
| 242 |
|
| 243 |
} elseif ($attributes['featured_images'] && $single_post->featured_image) { |
| 244 |
|
| 245 |
$content .= '<figure class="wp-block-image"><img src="' . esc_url($single_post->featured_image) . '" alt="' . esc_attr($single_post->title ) . '"/></figure>' . PHP_EOL; |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
if ($attributes['excerpts'] && $single_post->excerpt) { |
| 250 |
|
| 251 |
$content .= '<p>' . $single_post->excerpt . '</p>' . PHP_EOL; |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
} else { |
| 258 |
|
| 259 |
if (current_user_can('editor') || current_user_can('administrator')) { |
| 260 |
|
| 261 |
$content .= '<h3>' . __('Display Remote Posts Error', 'display-remote-posts-block') . '</h3> |
| 262 |
<h4>' . __('Error:', 'display-remote-posts-block') . ' ' . $blog_data->errors[$this->array_key_first($blog_data->errors)][0] . '</h4> |
| 263 |
'; |
| 264 |
|
| 265 |
if (isset($blog_data->error_data[$this->array_key_first($blog_data->errors)])) { |
| 266 |
|
| 267 |
$content .= '<p>' . $blog_data->error_data[$this->array_key_first($blog_data->errors)] . '</p> |
| 268 |
'; |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
} elseif ($attributes['url'] && !filter_var($attributes['url'], FILTER_VALIDATE_URL)) { |
| 277 |
|
| 278 |
if(current_user_can('editor') || current_user_can('administrator')) { |
| 279 |
|
| 280 |
$content .= '<h3>' . __('Display Remote Posts Error', 'display-remote-posts-block') . '</h3> |
| 281 |
<h4>' . __('You need to enter a valid blog URL!', 'display-remote-posts-block') . '</h4> |
| 282 |
<p>' . __('Edit the block settings and provide a valid web address for the blog.', 'display-remote-posts-block') . '</p> |
| 283 |
'; |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
} else { |
| 288 |
|
| 289 |
if(current_user_can('editor') || current_user_can('administrator')) { |
| 290 |
|
| 291 |
$content .= '<h3>' . __('Display Remote Posts', 'display-remote-posts-block') . '</h3> |
| 292 |
<h4>' . __('Edit the block settings to get started!', 'display-remote-posts-block') . '</h4> |
| 293 |
<p>' . __('To show posts from another blog, edit the block settings and provide the web address for the blog.', 'display-remote-posts-block') . '</p> |
| 294 |
'; |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
$content .= '</div>'; |
| 301 |
|
| 302 |
return $content; |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
private function array_key_first($array) { |
| 307 |
|
| 308 |
if (is_array($array)) { |
| 309 |
|
| 310 |
if (function_exists('array_key_first')) { |
| 311 |
|
| 312 |
return array_key_first($array); |
| 313 |
|
| 314 |
} else { |
| 315 |
|
| 316 |
foreach ($array as $key => $value) { |
| 317 |
|
| 318 |
return $key; |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
return false; |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
private function get_wordpress_com_data($url) { |
| 331 |
|
| 332 |
$blog_info = $this->remote_get_data('https://public-api.wordpress.com/rest/v1.1/sites/' . urlencode($url) . '?fields=ID,name'); |
| 333 |
|
| 334 |
if (is_wp_error($blog_info)) { return $blog_info; } |
| 335 |
|
| 336 |
$blog_info = json_decode($blog_info); |
| 337 |
|
| 338 |
if (!$blog_info) { |
| 339 |
|
| 340 |
return new WP_Error( |
| 341 |
'no_body', |
| 342 |
__('Invalid remote response.', 'display-remote-posts-block'), |
| 343 |
__('Invalid JSON from remote.', 'display-remote-posts-block') |
| 344 |
); |
| 345 |
|
| 346 |
} |
| 347 |
|
| 348 |
if (isset($blog_info->error)) { |
| 349 |
|
| 350 |
return new WP_Error( |
| 351 |
'remote_error', |
| 352 |
__('It looks like the WordPress site URL is incorrectly configured. Please check it in the block settings.', 'display-remote-posts-block'), |
| 353 |
$blog_posts->error |
| 354 |
); |
| 355 |
|
| 356 |
} |
| 357 |
|
| 358 |
if (!isset($blog_info->ID) && !isset($blog_info->name)) { |
| 359 |
|
| 360 |
return new WP_Error( |
| 361 |
'no_data', |
| 362 |
__('Incorrect data received!', 'display-remote-posts-block'), |
| 363 |
__('No site ID and / or name returned', 'display-remote-posts-block') |
| 364 |
); |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
$blog_info->ID = absint($blog_info->ID); |
| 369 |
$blog_info->name = sanitize_text_field((string) $blog_info->name); |
| 370 |
|
| 371 |
$blog_posts = $this->remote_get_data('https://public-api.wordpress.com/rest/v1.1/sites/' . absint($blog_info->ID) . '/posts/?fields=id,title,date,excerpt,URL,featured_image,content&number=10'); |
| 372 |
|
| 373 |
if (is_wp_error($blog_posts)) { return $blog_posts; } |
| 374 |
|
| 375 |
$blog_posts = json_decode($blog_posts); |
| 376 |
|
| 377 |
if (!$blog_posts) { |
| 378 |
|
| 379 |
return new WP_Error( |
| 380 |
'no_body', |
| 381 |
__('Invalid remote response.', 'display-remote-posts-block'), |
| 382 |
__('Invalid JSON from remote.', 'display-remote-posts-block') |
| 383 |
); |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
if (isset($blog_posts->error)) { |
| 388 |
|
| 389 |
return new WP_Error( |
| 390 |
'remote_error', |
| 391 |
__('It looks like the WordPress site URL is incorrectly configured. Please check it in the block settings.', 'display-remote-posts-block'), |
| 392 |
$blog_posts->error |
| 393 |
); |
| 394 |
|
| 395 |
} |
| 396 |
|
| 397 |
if (!isset($blog_posts->posts)) { |
| 398 |
|
| 399 |
return new WP_Error( |
| 400 |
'no_data', |
| 401 |
__('Incorrect data received!', 'display-remote-posts-block'), |
| 402 |
__('No site ID and / or name returned', 'display-remote-posts-block') |
| 403 |
); |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
$blog_info->posts = $blog_posts->posts; |
| 408 |
|
| 409 |
foreach ($blog_info->posts as $key => $single_post) { |
| 410 |
|
| 411 |
if (!(isset($single_post->title) && sanitize_text_field((string) $single_post->title))) { $blog_info->posts[$key]->title = __('Blog Post', 'display-remote-posts-block'); } else { $blog_info->posts[$key]->title = sanitize_text_field((string) $single_post->title); } |
| 412 |
|
| 413 |
$blog_info->posts[$key]->post_date = (isset($single_post->date) && absint(strtotime($single_post->date))) ? strtotime($single_post->date) : false; |
| 414 |
unset($blog_info->posts[$key]->date); |
| 415 |
$blog_info->posts[$key]->URL = (isset($single_post->URL) && sanitize_url($single_post->URL)) ? sanitize_url($single_post->URL) : false; |
| 416 |
|
| 417 |
if (!(isset($single_post->excerpt) && $single_post->excerpt)) { $blog_info->posts[$key]->excerpt = false; } |
| 418 |
|
| 419 |
$blog_info->posts[$key]->excerpt = wp_strip_all_tags($blog_info->posts[$key]->excerpt); |
| 420 |
|
| 421 |
if (strpos($blog_info->posts[$key]->excerpt, ' […]') === false) { |
| 422 |
|
| 423 |
$blog_info->posts[$key]->excerpt = trim(substr($blog_info->posts[$key]->excerpt, 0, strpos(wp_strip_all_tags($blog_info->posts[$key]->excerpt), '…'))) . ' […]'; |
| 424 |
|
| 425 |
} |
| 426 |
|
| 427 |
$blog_info->posts[$key]->srcset = false; |
| 428 |
|
| 429 |
preg_match_all('/<img.+?srcset=[\'"]([^\'"]+)[\'"].*?>/i', $single_post->content, $post_images); |
| 430 |
|
| 431 |
if (isset($post_images[1][0])) { |
| 432 |
|
| 433 |
$post_images = explode(', ', $post_images[1][0]); |
| 434 |
$post_srcset = array(); |
| 435 |
|
| 436 |
foreach ($post_images as $post_image) { |
| 437 |
|
| 438 |
$post_image = explode(' ', $post_image); |
| 439 |
$post_srcset[absint($post_image[1])] = sanitize_url($post_image[0]); |
| 440 |
|
| 441 |
} |
| 442 |
|
| 443 |
krsort($post_srcset); |
| 444 |
$blog_info->posts[$key]->srcset = $post_srcset; |
| 445 |
|
| 446 |
} |
| 447 |
|
| 448 |
$blog_info->posts[$key]->featured_image = (isset($single_post->featured_image) && sanitize_url($single_post->featured_image)) ? sanitize_url($single_post->featured_image) : false; |
| 449 |
|
| 450 |
} |
| 451 |
|
| 452 |
return $blog_info; |
| 453 |
|
| 454 |
} |
| 455 |
|
| 456 |
private function get_wordpress_org_data($url) { |
| 457 |
|
| 458 |
$blog_data = $this->remote_get_data(untrailingslashit($url) . '/feed/'); |
| 459 |
|
| 460 |
if (is_wp_error($blog_data)) { return $blog_data; } |
| 461 |
|
| 462 |
libxml_use_internal_errors(true); |
| 463 |
|
| 464 |
$blog_data = simplexml_load_string($blog_data); |
| 465 |
|
| 466 |
if (!$blog_data || !isset($blog_data->channel)) { |
| 467 |
|
| 468 |
return new WP_Error( |
| 469 |
'no_data', |
| 470 |
__('No XML data received!', 'display-remote-posts-block'), |
| 471 |
__('The data returned was not an XML feed', 'display-remote-posts-block') |
| 472 |
); |
| 473 |
|
| 474 |
} |
| 475 |
|
| 476 |
$blog_data = $blog_data->channel; |
| 477 |
|
| 478 |
$blog_info = new stdClass(); |
| 479 |
$blog_info->ID = false; |
| 480 |
|
| 481 |
if (isset($blog_data->title) && sanitize_text_field((string) $blog_data->title)) { $blog_info->name = sanitize_text_field((string) $blog_data->title); } else { $blog_info->name = false; } |
| 482 |
|
| 483 |
$blog_info->posts = array(); |
| 484 |
$post_counter = 0; |
| 485 |
|
| 486 |
foreach ($blog_data->item as $single_post) { |
| 487 |
|
| 488 |
$post_counter++; |
| 489 |
|
| 490 |
if ($post_counter > 30) { break; } |
| 491 |
|
| 492 |
$blog_post = new stdClass(); |
| 493 |
$blog_post->title = (isset($single_post->title) && sanitize_text_field((string) $single_post->title)) ? sanitize_text_field((string) $single_post->title) : __('Blog Post', 'display-remote-posts-block'); |
| 494 |
$blog_post->URL = (isset($single_post->link) && sanitize_url($single_post->link)) ? sanitize_url($single_post->link) : false; |
| 495 |
$blog_post->post_date = (isset($single_post->pubDate) && absint(strtotime($single_post->pubDate))) ? strtotime($single_post->pubDate) : false; |
| 496 |
|
| 497 |
if ($single_post->children("content", true)) { |
| 498 |
|
| 499 |
$blog_post->excerpt = $this->get_excerpt($single_post->children("content", true)); |
| 500 |
preg_match_all('/<img.+?srcset=[\'"]([^\'"]+)[\'"].*?>/i', (string) $single_post->children("content", true), $post_images); |
| 501 |
|
| 502 |
if (isset($post_images[1][0])) { |
| 503 |
|
| 504 |
$post_images = explode(', ', $post_images[1][0]); |
| 505 |
$post_srcset = array(); |
| 506 |
|
| 507 |
foreach ($post_images as $post_image) { |
| 508 |
|
| 509 |
$post_image = explode(' ', $post_image); |
| 510 |
$post_srcset[absint($post_image[1])] = sanitize_url($post_image[0]); |
| 511 |
|
| 512 |
} |
| 513 |
|
| 514 |
krsort($post_srcset); |
| 515 |
$blog_post->srcset = $post_srcset; |
| 516 |
$blog_post->featured_image = reset($post_srcset); |
| 517 |
|
| 518 |
} else { |
| 519 |
|
| 520 |
preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', (string) $single_post->children("content", true), $post_images); |
| 521 |
|
| 522 |
if (isset($post_images[1][0])) { |
| 523 |
|
| 524 |
$blog_post->srcset = false; |
| 525 |
$blog_post->featured_image = sanitize_url($post_images[1][0]); |
| 526 |
|
| 527 |
} else { |
| 528 |
|
| 529 |
$blog_post->srcset = false; |
| 530 |
$blog_post->featured_image = false; |
| 531 |
|
| 532 |
} |
| 533 |
|
| 534 |
} |
| 535 |
|
| 536 |
} elseif (isset($single_post->description) && $single_post->description) { |
| 537 |
|
| 538 |
$blog_post->excerpt = $this->get_excerpt($single_post->description); |
| 539 |
$blog_post->srcset = false; |
| 540 |
$blog_post->featured_image = false; |
| 541 |
|
| 542 |
} else { |
| 543 |
|
| 544 |
$blog_post->excerpt = false; |
| 545 |
$blog_post->srcset = false; |
| 546 |
$blog_post->featured_image = false; |
| 547 |
|
| 548 |
} |
| 549 |
|
| 550 |
$blog_info->posts[] = $blog_post; |
| 551 |
|
| 552 |
} |
| 553 |
|
| 554 |
return $blog_info; |
| 555 |
|
| 556 |
} |
| 557 |
|
| 558 |
private function get_blogger_data($url) { |
| 559 |
|
| 560 |
$blog_data = $this->remote_get_data(untrailingslashit($url) . '/feeds/posts/default'); |
| 561 |
|
| 562 |
if (is_wp_error($blog_data)) { return $blog_data; } |
| 563 |
|
| 564 |
libxml_use_internal_errors(true); |
| 565 |
|
| 566 |
$blog_data = simplexml_load_string($blog_data); |
| 567 |
|
| 568 |
if (!$blog_data || !isset($blog_data->entry)) { |
| 569 |
|
| 570 |
return new WP_Error( |
| 571 |
'no_data', |
| 572 |
__('No XML data received!', 'display-remote-posts-block'), |
| 573 |
__('The data returned was not an XML feed', 'display-remote-posts-block') |
| 574 |
); |
| 575 |
|
| 576 |
} |
| 577 |
|
| 578 |
$blog_info = new stdClass(); |
| 579 |
$blog_info->ID = false; |
| 580 |
|
| 581 |
if (isset($blog_data->title) && sanitize_text_field((string) $blog_data->title)) { $blog_info->name = sanitize_text_field((string) $blog_data->title); } else { $blog_info->name = false; } |
| 582 |
|
| 583 |
$blog_info->posts = array(); |
| 584 |
$post_counter = 0; |
| 585 |
|
| 586 |
foreach ($blog_data->entry as $single_post) { |
| 587 |
|
| 588 |
$post_counter++; |
| 589 |
|
| 590 |
if ($post_counter > 30) { break; } |
| 591 |
|
| 592 |
$blog_post = new stdClass(); |
| 593 |
$blog_post->title = (isset($single_post->title) && sanitize_text_field((string) $single_post->title)) ? sanitize_text_field((string) $single_post->title) : __('Blog Post', 'display-remote-posts-block'); |
| 594 |
$blog_post->post_date = (isset($single_post->published) && absint(strtotime($single_post->published))) ? strtotime($single_post->published) : false; |
| 595 |
|
| 596 |
foreach ($single_post->link as $link_feed) { |
| 597 |
|
| 598 |
if ((string) $link_feed['rel'] === 'alternate') { |
| 599 |
|
| 600 |
$blog_post->URL = sanitize_url((string) $link_feed['href']); |
| 601 |
|
| 602 |
break; |
| 603 |
|
| 604 |
} |
| 605 |
|
| 606 |
} |
| 607 |
|
| 608 |
if (!isset($blog_post->URL)) { $blog_post->URL = false; } |
| 609 |
|
| 610 |
if (isset($single_post->content) && $single_post->content) { |
| 611 |
|
| 612 |
$blog_post->excerpt = $this->get_excerpt($single_post->content); |
| 613 |
|
| 614 |
preg_match_all('/<img.+?srcset=[\'"]([^\'"]+)[\'"].*?>/i', (string) $single_post->content, $post_images); |
| 615 |
|
| 616 |
if (isset($post_images[1][0])) { |
| 617 |
|
| 618 |
$post_images = explode(', ', $post_images[1][0]); |
| 619 |
$post_srcset = array(); |
| 620 |
|
| 621 |
foreach ($post_images as $post_image) { |
| 622 |
$post_image = explode(' ', $post_image); |
| 623 |
$post_srcset[absint($post_image[1])] = sanitize_url($post_image[0]); |
| 624 |
} |
| 625 |
|
| 626 |
krsort($post_srcset); |
| 627 |
|
| 628 |
$blog_post->srcset = $post_srcset; |
| 629 |
$blog_post->featured_image = reset($post_srcset); |
| 630 |
|
| 631 |
} else { |
| 632 |
|
| 633 |
preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', (string) $single_post->content, $post_images); |
| 634 |
|
| 635 |
if (isset($post_images[1][0])) { |
| 636 |
|
| 637 |
$blog_post->srcset = false; |
| 638 |
$blog_post->featured_image = sanitize_url($post_images[1][0]); |
| 639 |
|
| 640 |
} else { |
| 641 |
|
| 642 |
$blog_post->srcset = false; |
| 643 |
$blog_post->featured_image = false; |
| 644 |
|
| 645 |
} |
| 646 |
|
| 647 |
} |
| 648 |
|
| 649 |
} else { |
| 650 |
|
| 651 |
$blog_post->excerpt = false; |
| 652 |
$blog_post->srcset = false; |
| 653 |
$blog_post->featured_image = false; |
| 654 |
|
| 655 |
} |
| 656 |
|
| 657 |
$blog_info->posts[] = $blog_post; |
| 658 |
|
| 659 |
} |
| 660 |
|
| 661 |
return $blog_info; |
| 662 |
|
| 663 |
} |
| 664 |
|
| 665 |
private function get_excerpt($text) { |
| 666 |
|
| 667 |
$text = strip_shortcodes($text); |
| 668 |
$text = excerpt_remove_blocks($text); |
| 669 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 670 |
$text = apply_filters('the_content', $text); |
| 671 |
$text = str_replace(']]>', ']]>', $text); |
| 672 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 673 |
$excerpt_length = (int) apply_filters('excerpt_length', 55); |
| 674 |
$text = ltrim(wp_trim_words($text, $excerpt_length, ' […]' ), ' '); |
| 675 |
|
| 676 |
return $text; |
| 677 |
|
| 678 |
} |
| 679 |
|
| 680 |
private function remote_get_data($url) { |
| 681 |
|
| 682 |
$parsed = wp_parse_url($url); |
| 683 |
|
| 684 |
if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) { |
| 685 |
|
| 686 |
return new WP_Error('invalid_scheme', __('Only HTTP and HTTPS URLs are allowed.', 'display-remote-posts-block')); |
| 687 |
|
| 688 |
} |
| 689 |
|
| 690 |
if (!empty($parsed['port']) && !in_array((int)$parsed['port'], [80, 443], true)) { |
| 691 |
|
| 692 |
return new WP_Error('invalid_port', __('Only ports 80 and 443 are allowed.', 'display-remote-posts-block')); |
| 693 |
|
| 694 |
} |
| 695 |
|
| 696 |
if (empty($parsed['host'])) { |
| 697 |
|
| 698 |
return new WP_Error('invalid_host', __('Missing or invalid host in URL.', 'display-remote-posts-block')); |
| 699 |
|
| 700 |
} |
| 701 |
|
| 702 |
$host_ip = gethostbyname($parsed['host']); |
| 703 |
|
| 704 |
|
| 705 |
if ($host_ip === $parsed['host']) { |
| 706 |
|
| 707 |
return new WP_Error('invalid_host', __('Hostname will not resolve.', 'display-remote-posts-block')); |
| 708 |
|
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
if (!filter_var($host_ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
| 713 |
|
| 714 |
return new WP_Error('blocked_ip', __('Access to private or local network resources is not allowed.', 'display-remote-posts-block')); |
| 715 |
|
| 716 |
} |
| 717 |
|
| 718 |
$response = wp_safe_remote_get($url, [ |
| 719 |
'timeout' => 10, |
| 720 |
'redirection' => 0, |
| 721 |
'headers' => [ 'User-Agent' => 'WordPress-RemoteFetcher' ], |
| 722 |
'limit_response_size' => 1024 * 1024 |
| 723 |
]); |
| 724 |
|
| 725 |
if (is_wp_error($response)) { |
| 726 |
|
| 727 |
return $response; |
| 728 |
|
| 729 |
} |
| 730 |
|
| 731 |
if ( |
| 732 |
302 === wp_remote_retrieve_response_code($response) && |
| 733 |
isset($response['headers']['location']) && |
| 734 |
'https://feeds.feedburner.com/' === substr($response['headers']['location'], 0, strlen('https://feeds.feedburner.com/')) && |
| 735 |
$url !== $response['headers']['location'] |
| 736 |
) { |
| 737 |
|
| 738 |
$response_body = $this->remote_get_data($response['headers']['location']); |
| 739 |
|
| 740 |
if (is_wp_error($response_body)) { |
| 741 |
|
| 742 |
return $response_body; |
| 743 |
|
| 744 |
} |
| 745 |
|
| 746 |
} else { |
| 747 |
|
| 748 |
if (200 !== wp_remote_retrieve_response_code($response)) { |
| 749 |
|
| 750 |
return new WP_Error( |
| 751 |
'http_error', |
| 752 |
__('An error occurred fetching the remote data.', 'display-remote-posts-block'), |
| 753 |
wp_remote_retrieve_response_message($response) |
| 754 |
); |
| 755 |
|
| 756 |
} |
| 757 |
|
| 758 |
$response_body = wp_remote_retrieve_body($response); |
| 759 |
|
| 760 |
} |
| 761 |
|
| 762 |
if (!$response_body) { |
| 763 |
|
| 764 |
return new WP_Error( |
| 765 |
'no_body', |
| 766 |
__('Invalid remote response.', 'display-remote-posts-block'), |
| 767 |
__('No body in response.', 'display-remote-posts-block') |
| 768 |
); |
| 769 |
} |
| 770 |
|
| 771 |
return $response_body; |
| 772 |
|
| 773 |
} |
| 774 |
|
| 775 |
static function check_gutenberg() { |
| 776 |
|
| 777 |
if (false === defined('GUTENBERG_VERSION') && false === version_compare(get_bloginfo('version'), '5.0', '>=')) { |
| 778 |
|
| 779 |
add_action('admin_notices', array(__CLASS__, 'notice_gutenberg_missing')); |
| 780 |
|
| 781 |
return false; |
| 782 |
|
| 783 |
} |
| 784 |
|
| 785 |
} |
| 786 |
|
| 787 |
static function notice_gutenberg_missing() { |
| 788 |
|
| 789 |
echo '<div class="error"><p><b>' . esc_html(__('Display Remote Posts Block', 'display-remote-posts-block')) . '</b> ' . esc_html(__('plugin requires the Gutenberg plugin to work. It is after all a block for Gutenberg ;)', 'display-remote-posts-block')) . '<br>' . esc_html(__('Install the', 'display-remote-posts-block')) . ' <a href="https://wordpress.org/plugins/gutenberg/" target="_blank">' . esc_html(__('Gutenberg plugin', 'display-remote-posts-block')) . '</a> ' . esc_html(__('and this notice will go away.', 'display-remote-posts-block')) . '</p></div>'; |
| 790 |
|
| 791 |
} |
| 792 |
|
| 793 |
} |
| 794 |
|
| 795 |
if (!class_exists('drpbCommon')) { |
| 796 |
|
| 797 |
require_once(dirname(__FILE__) . '/includes/class-drpb-common.php'); |
| 798 |
|
| 799 |
} |
| 800 |
|
| 801 |
$display_remote_posts_block_object= new display_remote_posts_block_class(); |
| 802 |
|
| 803 |
} |
| 804 |
|
| 805 |
?> |
| 806 |
|