| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Conditionals; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Conditionals\Admin\Post_Conditional; |
| 6 |
use Yoast\WP\SEO\Helpers\Current_Page_Helper; |
| 7 |
use Yoast\WP\SEO\Helpers\Product_Helper; |
| 8 |
|
| 9 |
/** |
| 10 |
* Conditional that is met when the AI editor integration should be active. |
| 11 |
*/ |
| 12 |
class AI_Editor_Conditional implements Conditional { |
| 13 |
|
| 14 |
/** |
| 15 |
* Holds the Post_Conditional. |
| 16 |
* |
| 17 |
* @var Post_Conditional |
| 18 |
*/ |
| 19 |
private $post_conditional; |
| 20 |
|
| 21 |
/** |
| 22 |
* Holds the Current_Page_Helper. |
| 23 |
* |
| 24 |
* @var Current_Page_Helper |
| 25 |
*/ |
| 26 |
private $current_page_helper; |
| 27 |
|
| 28 |
/** |
| 29 |
* Holds the Product_Helper. |
| 30 |
* |
| 31 |
* @var Product_Helper |
| 32 |
*/ |
| 33 |
private $product_helper; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructs Ai_Editor_Conditional. |
| 37 |
* |
| 38 |
* @param Post_Conditional $post_conditional The Post_Conditional. |
| 39 |
* @param Current_Page_Helper $current_page_helper The Current_Page_Helper. |
| 40 |
* @param Product_Helper $product_helper The Product_Helper. |
| 41 |
*/ |
| 42 |
public function __construct( Post_Conditional $post_conditional, Current_Page_Helper $current_page_helper, Product_Helper $product_helper ) { |
| 43 |
$this->post_conditional = $post_conditional; |
| 44 |
$this->current_page_helper = $current_page_helper; |
| 45 |
$this->product_helper = $product_helper; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns `true` when the AI editor integration should be active. |
| 50 |
* |
| 51 |
* @return bool `true` when the AI editor integration should be active. |
| 52 |
*/ |
| 53 |
public function is_met() { |
| 54 |
if ( $this->is_attachment() ) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
if ( $this->is_ai_generator_premium() ) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
return $this->post_conditional->is_met() || $this->is_term() || $this->is_elementor_editor(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns `true` when the page is a term page. |
| 67 |
* |
| 68 |
* @return bool `true` when the page is a term page. |
| 69 |
*/ |
| 70 |
private function is_term() { |
| 71 |
return $this->current_page_helper->get_current_admin_page() === 'term.php'; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns `true` when the page is the Elementor editor. |
| 76 |
* |
| 77 |
* @return bool `true` when the page is the Elementor editor. |
| 78 |
*/ |
| 79 |
private function is_elementor_editor() { |
| 80 |
if ( $this->current_page_helper->get_current_admin_page() !== 'post.php' ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 85 |
if ( isset( $_GET['action'] ) && \is_string( $_GET['action'] ) ) { |
| 86 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information, We are only strictly comparing. |
| 87 |
if ( \wp_unslash( $_GET['action'] ) === 'elementor' ) { |
| 88 |
return true; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Is an attchment post type. |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function is_attachment() { |
| 101 |
return $this->current_page_helper->get_current_post_type() === 'attachment'; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Is premium version containes AI generator. We exclude product post type because it is not supported in premium version before 25.6. |
| 106 |
* |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
public function is_ai_generator_premium() { |
| 110 |
if ( ! $this->product_helper->is_premium() ) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
$premium_version = $this->product_helper->get_premium_version(); |
| 114 |
return \version_compare( $premium_version, '25.6-RC0', '<' ) && $this->current_page_helper->get_current_post_type() !== 'product'; |
| 115 |
} |
| 116 |
} |
| 117 |
|