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