class-main.php 11.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
<?php
/**
 * The main Processor that Link calls. It will then share jobs to sub processors.
 *
 * @link    https://wordpress.org/plugins/broken-link-checker/
 * @since   2.1.0
 *
 * @author  WPMUDEV (https://wpmudev.com)
 * @package WPMUDEV_BLC\App\Broken_Links_Actions
 *
 * @copyright (c) 2022, Incsub (http://incsub.com)
 */

namespace WPMUDEV_BLC\App\Broken_Links_Actions\Processors;

use WPMUDEV_BLC\App\Broken_Links_Actions\Link;
use WPMUDEV_BLC\App\Broken_Links_Actions\Processors\Replace_Link;
use WPMUDEV_BLC\App\Broken_Links_Actions\Processors\Unlink_Link;
use WPMUDEV_BLC\App\Broken_Links_Actions\Processors\Nofollow_Link;
use WPMUDEV_BLC\Core\Utils\Abstracts\Base;
use WPMUDEV_BLC\Core\Utils\Utilities;

/**
 * Class Main
 *
 * @package WPMUDEV_BLC\App\Broken_Links_Actions\Processors
 */
class Main extends Base {
	/**
	 * The object that will execute the specified action (replace|unlink|nofollow)
	 *
	 * @var null
	 */
	protected $processor = null;

	/**
	 * Link object.
	 *
	 * @var null
	 */
	protected $link = null;

	/**
	 * A Store array to store data in order to avoid repetitive queries.
	 *
	 * @var array
	 */
	protected $temp_store = array();

	public function __construct( Link $link = null ) {
		$this->link = $link;
	}

	/**
	 * Replaces url in post's content, meta and comments.
	 * Returns false if link not replaced anywhere, else true.
	 *
	 * @param int|null $post_id
	 *
	 * @return bool
	 */
	public function execute_url_in_post( int $post_id = null ) {
		if ( empty( $post_id ) ) {
			return false;
		}

		//Execute link in post content.
		$completed_in_posts = $this->execute_in_post_content( $post_id );

		//Execute link in post metas.
		$completed_in_meta = $this->execute_in_post_meta_values( $post_id );

		//Execute link in post comments.
		$completed_in_comments = $this->execute_in_post_comments( $post_id );

		// Execute link reusable blocks.
		$completed_in_reusable_blocks = $this->execute_in_post_reusable_blocks( $post_id );

		return $completed_in_posts || $completed_in_meta || $completed_in_comments || $completed_in_reusable_blocks;
	}

	public function execute_in_post_content( int $post_id = null, string $content = null ) {
		if ( empty( $post_id ) ) {
			return false;
		}

		if ( empty( $content ) ) {
			if ( empty( $this->temp_store['posts'][ $post_id ]['content'] ) ) {
				$this->temp_store['posts'][ $post_id ]['content'] = get_post_field( 'post_content', $post_id );
			}

			$content = $this->temp_store['posts'][ $post_id ]['content'];
		}

		$new_content = $this->get_processor()->execute( $content, $this->link->get_link(), $this->link->get_new_link() );

		if ( $content !== $new_content ) {
			// At first, we are creating a new post revision.
			wp_save_post_revision( $post_id );

			// Then update post with new content.
			kses_remove_filters();

			return wp_update_post(
				array(
					'ID'           => $post_id,
					'post_content' => $new_content,
				)
			);
			kses_init_filters();
		}

		return false;
	}

	/**
	 * Executes in all post meta values by post id.
	 *
	 * @param int|null $post_id
	 *
	 * @return false|void
	 */
	public function execute_in_post_meta_values( int $post_id = null ) {
		if ( empty( $post_id ) ) {
			return false;
		}

		// The `wpmudev_blc_process_extensive` filter is returned in `Utilities::process_extensive()`.
		if ( Utilities::process_extensive() ) {
			global $wpdb;

			$query = $wpdb->prepare(
				"SELECT meta_key, meta_value as content FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_value LIKE %s",
				$post_id,
				'%' . $wpdb->esc_like( $this->link->get_link() ) . '%'
			);
			$metas = $wpdb->get_results( $query );

			if ( ! empty( $metas ) ) {
				foreach ( $metas as $meta ) {
					$new_content = $this->get_processor()->execute( $meta->content, $this->link->get_link(), $this->link->get_new_link() );

					if ( $new_content !== $meta->content ) {
						return update_post_meta( $post_id, sanitize_key( $meta->meta_key ), $new_content );
					}
				}
			}
		} else {
			$supported_meta_keys = apply_filters(
				'wpmudev_blc_edit_link_post_meta_keys',
				array()
			);

			if ( empty( $supported_meta_keys ) ) {
				return false;
			}

			foreach ( $supported_meta_keys as $meta_key ) {
				$content     = get_post_meta( $post_id, sanitize_key( $meta_key ), true );
				$new_content = $this->get_processor()->execute( $content, $this->link->get_link(), $this->link->get_new_link() );

				if ( $new_content !== $content ) {
					return update_post_meta( $post_id, sanitize_key( $meta_key ), $new_content );
				}
			}
		}

		return false;
	}

	/**
	 * Executes link action in post's comments.
	 * TODO: Revisit comments links because WP wraps urls with `<a>` tags when comment is displayed. The means that because of that links will be found as broken but not replaced.
	 *
	 * @param int|null $post_id
	 *
	 * @return bool
	 */
	public function execute_in_post_comments( int $post_id = null ) {
		$post_comments = get_comments( array( 'post_id' => $post_id ) );

		if ( ! empty( $post_comments ) ) {
			foreach ( $post_comments as $post_comment ) {
				$content     = $post_comment->comment_content;
				$new_content = $this->get_processor()->execute( $content, $this->link->get_link(), $this->link->get_new_link() );

				if ( $new_content !== $content ) {
					$update = wp_update_comment(
						array(
							'comment_ID'      => intval( $post_comment->comment_ID ),
							'comment_content' => $new_content,
						)
					);

					return $update && ! is_wp_error( $update );
				}
			}
		}

		return false;
	}


	public function execute_in_post_reusable_blocks( int $post_id = null ) {
		if ( ! function_exists( 'register_block_type' ) ) {
			return false;
		}

		if ( empty( $post_id ) ) {
			return false;
		}

		$result = false;

		if ( empty( $this->temp_store['posts'][ $post_id ]['content'] ) ) {
			$this->temp_store['posts'][ $post_id ]['content'] = get_post_field( 'post_content', $post_id );
		}

		$content         = $this->temp_store['posts'][ $post_id ]['content'];
		$reusable_blocks = $this->get_reusable_blocks_from_content( $content );

		if ( ! empty( $reusable_blocks ) ) {
			foreach ( $reusable_blocks as $block_post_id ) {
				if ( $this->execute_in_post_content( $block_post_id ) ) {
					$result = true;
				}
			}
		}

		return $result;
	}

	public function get_reusable_blocks_from_content( string $content = null ) {
		if ( empty( $content ) ) {
			return array();
		}

		$reusable_blocks = get_posts(
			array(
				'posts_per_page' => -1,
				'fields'         => 'ids',
				'post_type'      => 'wp_block',
			)
		);

		if ( ! empty( $reusable_blocks ) ) {
			foreach ( $reusable_blocks as $block_key => $block_id ) {
				$block_name = '<!-- wp:block {"ref":' . $block_id . '} /-->';

				if ( strpos( $content, $block_name ) === false ) {
					unset( $reusable_blocks[ $block_key ] );
				}
			}
		}

		return $reusable_blocks;
	}

	public function get_site_blocks() {

	}

	/**
	 * Executes in users meta table by user id.
	 *
	 * @param int|null $user_id
	 *
	 * @return bool
	 */
	public function execute_in_user_author_meta( int $user_id = null ) {
		if ( is_null( $user_id ) ) {
			return false;
		}

		/*
		 * TODO: Use an global process_extensive flag and use `get_user_meta()` only with $user_id (no key).
		 */
		$content     = get_user_meta( $user_id, 'description', true );
		$new_content = $this->get_processor()->execute( $content, $this->link->get_link(), $this->link->get_new_link() );

		return update_user_meta( $user_id, 'description', $new_content );
	}

	/**
	 * Executes link action in comments by comment id.
	 * TODO: Revisit comments links because WP wraps urls with `<a>` tags when comment is displayed. The means that because of that links will be found as broken but not replaced.
	 *
	 * @param int|null $id comment id
	 *
	 * @return bool
	 */
	public function execute_in_comment( int $id = null, string $content = null ) {
		if ( empty( $content ) ) {
			$comment = get_comment( $id );

			if ( $comment instanceof WP_Comment || $comment instanceof \WP_Comment ) {
				$content = $comment->comment_content;
			}

		}

		$new_content = $this->get_processor()->execute( $content, $this->link->get_link(), $this->link->get_new_link() );

		if ( $new_content !== $content ) {
			$update = wp_update_comment(
				array(
					'comment_ID'      => intval( $id ),
					'comment_content' => $new_content,
				)
			);

			return $update && ! is_wp_error( $update );
		}

		return false;
	}

	/**
	 * Executes in postmeta by meta_id.
	 *
	 * @param int|null $meta_id
	 * @param string|null $content
	 *
	 * @return bool
	 */
	public function execute_in_postmeta( int $meta_id = null, string $content = null ) {
		/*
			  if ( empty( $content ) ) {
				  $meta = get_post_meta_by_id( $id );
				  $content = $meta->meta_value;
			  }

			  if ( ! empty( $content ) ) {
				  $new_content = $this->get_processor()->execute( $content, $this->link->get_link(), $this->link->get_new_link() );

				  if ( $new_content !== $content ) {
					  return update_meta( $id, $meta->meta_key, $new_content );
				  }
			  }

			  return false;
			  */

		return $this->execute_in_meta_table( 'post', $meta_id, $content );
	}

	/**
	 * Executes the action in usermeta table by meta_id.
	 *
	 * @param int|null $meta_id
	 * @param string|null $content
	 *
	 * @return false
	 */
	public function execute_in_usermeta( int $meta_id = null, string $content = null ) {
		/*
			  if ( empty( $content ) ) {
				  $meta = get_metadata_by_mid( 'user', $meta_id );
				  $content = property_exists( $meta, 'meta_value' ) ? $meta->meta_value : null;
			  }

			  if ( empty( $content ) ) {
				  return false;
			  }

			  $new_content = $this->get_processor()->execute( $content, $this->link->get_link(), $this->link->get_new_link() );

			  return update_metadata_by_mid( 'user', $meta_id, $new_content );
			  */

		return $this->execute_in_meta_table( 'user', $meta_id, $content );
	}

	public function execute_in_meta_table( string $type = null, int $meta_id = null, string $content = null ) {
		if ( empty( $meta_id ) || ! in_array( $type, array( 'user', 'post' ) ) ) {
			return false;
		}

		if ( empty( $content ) ) {
			$meta    = get_metadata_by_mid( $type, $meta_id );
			$content = property_exists( $meta, 'meta_value' ) ? $meta->meta_value : null;
		}

		if ( ! empty( $content ) ) {
			$new_content = $this->get_processor()->execute( $content, $this->link->get_link(), $this->link->get_new_link() );

			return update_metadata_by_mid( $type, $meta_id, $new_content );
		}

		return false;
	}

	public function get_processor() {
		if ( empty( $this->processor ) ) {
			$this->processor = $this->determine_processor();
		}

		return $this->processor;
	}

	public function determine_processor() {
		$type = $this->link->get_type();

		if ( empty( $this->actions_map()[ $type ] ) ) {
			return new \WP_Error(
				'blc-link-execution-error',
				esc_html__(
					'Unmapped action',
					'broken-link-checker'
				)
			);
		}

		return $this->actions_map()[ $type ];
	}


	/**
	 * Map with allowed actions and classes that handle link actions.
	 *
	 * @return array
	 */
	public function actions_map() {
		return apply_filters(
			'wpmudev_blc_link_actions_map',
			array(
				'replace'  => Replace_Link::instance(),
				'unlink'   => Unlink_Link::instance(),
				'nofollow' => Nofollow_Link::instance(),
			),
			$this
		);
	}
}