PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / lib / abstract-main.php

abstract-main.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at lib/abstract-main.php

174 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\Lib;
4
5 use Exception;
6 use WPSEO_Utils;
7 use Yoast\WP\Lib\Dependency_Injection\Container_Registry;
8 use Yoast\WP\SEO\Exceptions\Forbidden_Property_Mutation_Exception;
9 use Yoast\WP\SEO\Loader;
10 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13 * Abstract class to extend for the main class in a plugin.
14 */
15 abstract class Abstract_Main {
16
17 /**
18 * The DI container.
19 *
20 * @var ContainerInterface|null
21 */
22 protected $container;
23
24 /**
25 * A cache for previously requested and constructed surfaces.
26 *
27 * @var mixed[]
28 */
29 private $cached_surfaces = [];
30
31 /**
32 * Loads the plugin.
33 *
34 * @return void
35 *
36 * @throws Exception If loading fails and YOAST_ENVIRONMENT is development.
37 */
38 public function load() {
39 if ( $this->container ) {
40 return;
41 }
42
43 try {
44 $this->container = $this->get_container();
45 Container_Registry::register( $this->get_name(), $this->container );
46
47 if ( ! $this->container ) {
48 return;
49 }
50 if ( ! $this->container->has( Loader::class ) ) {
51 return;
52 }
53
54 $this->container->get( Loader::class )->load();
55 } catch ( Exception $e ) {
56 if ( $this->is_development() ) {
57 throw $e;
58 }
59 // Don't crash the entire site, simply don't load.
60 }
61 }
62
63 /**
64 * Magic getter for retrieving a property from a surface.
65 *
66 * @param string $property The property to retrieve.
67 *
68 * @return mixed The value of the property.
69 *
70 * @throws Exception When the property doesn't exist.
71 */
72 public function __get( $property ) {
73 if ( \array_key_exists( $property, $this->cached_surfaces ) ) {
74 return $this->cached_surfaces[ $property ];
75 }
76
77 $surfaces = $this->get_surfaces();
78
79 if ( isset( $surfaces[ $property ] ) ) {
80 $this->cached_surfaces[ $property ] = $this->container->get( $surfaces[ $property ] );
81
82 return $this->cached_surfaces[ $property ];
83 }
84 throw new Exception( \sprintf( 'Property $%s does not exist.', $property ) );
85 }
86
87 /**
88 * Checks if the given property exists as a surface.
89 *
90 * @param string $property The property to retrieve.
91 *
92 * @return bool True when property is set.
93 */
94 public function __isset( $property ) {
95 if ( \array_key_exists( $property, $this->cached_surfaces ) ) {
96 return true;
97 }
98
99 $surfaces = $this->get_surfaces();
100
101 if ( ! isset( $surfaces[ $property ] ) ) {
102 return false;
103 }
104
105 return $this->container->has( $surfaces[ $property ] );
106 }
107
108 /**
109 * Prevents setting dynamic properties and unsetting declared properties
110 * from an inaccessible context.
111 *
112 * @param string $name The property name.
113 * @param mixed $value The property value.
114 *
115 * @return void
116 *
117 * @throws Forbidden_Property_Mutation_Exception Set is never meant to be called.
118 */
119 public function __set( $name, $value ) { // @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- __set must have a name and value - PHPCS #3715.
120 throw Forbidden_Property_Mutation_Exception::cannot_set_because_property_is_immutable( $name );
121 }
122
123 /**
124 * Prevents unsetting dynamic properties and unsetting declared properties
125 * from an inaccessible context.
126 *
127 * @param string $name The property name.
128 *
129 * @return void
130 *
131 * @throws Forbidden_Property_Mutation_Exception Unset is never meant to be called.
132 */
133 public function __unset( $name ) {
134 throw Forbidden_Property_Mutation_Exception::cannot_unset_because_property_is_immutable( $name );
135 }
136
137 /**
138 * Loads the DI container.
139 *
140 * @return ContainerInterface|null The DI container.
141 *
142 * @throws Exception If something goes wrong generating the DI container.
143 */
144 abstract protected function get_container();
145
146 /**
147 * Gets the name of the plugin.
148 *
149 * @return string The name.
150 */
151 abstract protected function get_name();
152
153 /**
154 * Gets the surfaces of this plugin.
155 *
156 * @return array A mapping of surface name to the responsible class.
157 */
158 abstract protected function get_surfaces();
159
160 /**
161 * Returns whether or not we're in an environment for Yoast development.
162 *
163 * @return bool Whether or not to load in development mode.
164 */
165 protected function is_development() {
166 try {
167 return WPSEO_Utils::is_development_mode();
168 } catch ( Exception $exception ) {
169 // E.g. when WordPress and/or WordPress SEO are not loaded.
170 return \defined( 'YOAST_ENVIRONMENT' ) && \YOAST_ENVIRONMENT === 'development';
171 }
172 }
173 }
174