PluginProbe
ElasticPress / 4.2.2
ElasticPress v4.2.2
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / compat.php

compat.php in ElasticPress 4.2.2, at includes/compat.php

230 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ElasticPress backward compat functions
4 *
5 * @since 3.0
6 * @package elasticpress
7 */
8
9 /**
10 * This class was replaced with \ElasticPress\FeatureRequirementsStatus
11 */
12 class EP_Feature_Requirements_Status {
13 /**
14 * Initialize class
15 *
16 * @param int $code Status code.
17 * @param string|array $message Message describing status.
18 * @since 2.2
19 */
20 public function __construct( $code, $message = null ) {
21 _deprecated_function( __CLASS__, '3.0', '\ElasticPress\FeatureRequirementsStatus' );
22
23 $this->code = $code;
24 $this->message = $message;
25 }
26
27 /**
28 * Returns the status of a feature
29 *
30 * 0 is no issues
31 * 1 is usable but there are warnngs
32 * 2 is not usable
33 *
34 * @var int
35 * @since 2.2
36 */
37 public $code;
38
39 /**
40 * Optional message to describe status code
41 *
42 * @var string|array
43 * @since 2.2
44 */
45 public $message;
46 }
47
48 /**
49 * Search Elasticsearch for related content
50 *
51 * @param int $post_id Post ID
52 * @param int $return Number of posts to get
53 * @since 2.1
54 * @return array|bool
55 */
56 function ep_find_related( $post_id, $return = 5 ) {
57 _deprecated_function( __FUNCTION__, '3.0', 'ElasticPress\Features::factory()->get_registered_feature' );
58
59 $feature = \ElasticPress\Features::factory()->get_registered_feature( 'related_posts' );
60
61 return ( ! empty( $feature ) ) ? $feature->find_related( $post_id, $return ) : false;
62 }
63
64 /**
65 * Index a post given an ID
66 *
67 * @param int $post_id Post ID
68 * @return boolean|array
69 */
70 function ep_index_post( $post_id ) {
71 _deprecated_function( __FUNCTION__, '3.0', "ElasticPress\Indexables::factory()->get( 'post' )->index" );
72
73 return \ElasticPress\Indexables::factory()->get( 'post' )->index( $post_id, true );
74 }
75
76 /**
77 * Get index name for the posts index
78 *
79 * @param int $blog_id Blog ID
80 * @since 3.4
81 * @return string
82 */
83 function ep_get_index_name( $blog_id = null ) {
84 _deprecated_function( __FUNCTION__, '3.0', "ElasticPress\Indexables::factory()->get( 'post' )->get_index_name()" );
85
86 return \ElasticPress\Indexables::factory()->get( 'post' )->get_index_name( $blog_id );
87 }
88 /**
89 * Registers a feature for use in ElasticPress
90 *
91 * @param string $slug Unique slug for feature
92 * @param array $args Feature arguments
93 * @since 2.1
94 */
95 function ep_register_feature( $slug, $args ) {
96 _deprecated_function( __FUNCTION__, '3.0', esc_html__( 'Feature registration API', 'elasticpress' ) );
97
98 $callbacks = [
99 'feature_box_summary_cb',
100 'feature_box_long_cb',
101 'setup_cb',
102 'requirements_status_cb',
103 ];
104
105 $resolved_callbacks = [];
106
107 if ( empty( $GLOBALS['ep_legacy_feature_refs'] ) ) {
108 $GLOBALS['ep_legacy_feature_refs'] = [];
109 }
110
111 $GLOBALS['ep_legacy_feature_refs'][ $slug ] = [];
112
113 foreach ( $callbacks as $callback_key ) {
114 if ( empty( $args[ $callback_key ] ) ) {
115 continue;
116 }
117
118 $callback = $args[ $callback_key ];
119
120 if ( is_string( $callback ) ) {
121 $resolved_callbacks[ $callback_key ] = "'" . $callback . "'";
122 } elseif ( is_array( $callback ) ) {
123 if ( is_string( $callback[0] ) ) {
124 $resolved_callbacks[ $callback_key ] = 'array( "' . addcslashes( $callback[0], "'" ) . '", "' . addcslashes( $callback[1], "'" ) . '" )';
125 } else {
126 $GLOBALS['ep_legacy_feature_refs'][ $slug ][ $callback_key ] = $callback[0];
127
128 $resolved_callbacks[ $callback_key ] = 'array( $GLOBALS["ep_legacy_feature_refs"]["' . $slug . '"]["' . $callback_key . '"], "' . addcslashes( $callback[1], "'" ) . '" )';
129 }
130 } else {
131 $GLOBALS['ep_legacy_feature_refs'][ $slug ][ $callback_key ] = $callback;
132
133 $resolved_callbacks[ $callback_key ] = '$GLOBALS["ep_legacy_feature_refs"]["' . $slug . '"]["' . $callback_key . '"]';
134 }
135 }
136
137 $slug = preg_replace( '#[^a-zA-Z0-9\-\_]#is', '', $slug );
138 $title = ( ! empty( $args['title'] ) ) ? addcslashes( $args['title'], "'" ) : false;
139 $requires_install_reindex = ( ! empty( $requires_install_reindex ) ) ? 'true' : 'false';
140
141 $class_name = 'EP' . $slug . 'Feature';
142
143 if ( class_exists( $class_name ) ) {
144 return;
145 }
146
147 // phpcs:disable
148 $code = "
149 class $class_name extends ElasticPress\Feature {
150 /**
151 * Initialize feature
152 *
153 * @since 3.0
154 */
155 public function __construct() {
156 \$this->slug = '$slug';
157 \$this->requires_install_reindex = $requires_install_reindex;
158
159 " . ( ( ! empty( $title ) ) ?
160 "\$this->title = '$title';"
161 :
162 ''
163 ) . '
164 }
165
166 /**
167 * Setup feature
168 *
169 * @since 3.0
170 */
171 public function setup() {
172 ' . ( ( ! empty( $resolved_callbacks['setup_cb'] ) ) ?
173 'call_user_func( ' . $resolved_callbacks['setup_cb'] . ' );'
174 :
175 ''
176 ) . '
177 }
178
179 /**
180 * Output feature box summary
181 *
182 * @since 3.0
183 */
184 public function output_feature_box_summary() {
185 ' . ( ( ! empty( $resolved_callbacks['feature_box_summary_cb'] ) ) ?
186 'call_user_func( ' . $resolved_callbacks['feature_box_summary_cb'] . ' );'
187 :
188 ''
189 ) . '
190 }
191
192 /**
193 * Output feature box long text
194 *
195 * @since 3.0
196 */
197 public function output_feature_box_long() {
198 ' . ( ( ! empty( $resolved_callbacks['feature_box_long_cb'] ) ) ?
199 'call_user_func( ' . $resolved_callbacks['feature_box_long_cb'] . ' );'
200 :
201 ''
202 ) . '
203 }
204
205 /**
206 * Returns requirements status of feature
207 *
208 * @since 3.0
209 */
210 public function requirements_status() {
211 ' . ( ( ! empty( $resolved_callbacks['requirements_status_cb'] ) ) ?
212 "
213 \$status = new \ElasticPress\FeatureRequirementsStatus( 0 );
214 return call_user_func( " . $resolved_callbacks['requirements_status_cb'] . ", \$status );
215 "
216 :
217 'return parent::requirements_status();'
218 ) . '
219 }
220 }
221 ';
222
223 eval( $code );
224 // phpcs:enable
225
226 ElasticPress\Features::factory()->register_feature(
227 new $class_name()
228 );
229 }
230