Complex classes like PodsField_OEmbed often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PodsField_OEmbed, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class PodsField_OEmbed extends PodsField { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * {@inheritdoc} |
||
| 10 | */ |
||
| 11 | public static $group = 'Relationships / Media'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * {@inheritdoc} |
||
| 15 | */ |
||
| 16 | public static $type = 'oembed'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * {@inheritdoc} |
||
| 20 | */ |
||
| 21 | public static $label = 'oEmbed'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * {@inheritdoc} |
||
| 25 | */ |
||
| 26 | public static $prepare = '%s'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Available oEmbed providers |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | * @since 2.7 |
||
| 33 | */ |
||
| 34 | private $providers = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Current embed width |
||
| 38 | * |
||
| 39 | * @var int |
||
| 40 | * @since 2.7 |
||
| 41 | */ |
||
| 42 | private $width = 0; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Current embed height |
||
| 46 | * |
||
| 47 | * @var int |
||
| 48 | * @since 2.7 |
||
| 49 | */ |
||
| 50 | private $height = 0; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | public function setup() { |
||
| 56 | |||
| 57 | self::$label = __( 'oEmbed', 'pods' ); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | public function admin_init() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public function options() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function schema( $options = null ) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
| 175 | |||
| 176 | $options = (array) $options; |
||
| 177 | $form_field_type = PodsForm::$field_type; |
||
| 178 | |||
| 179 | if ( is_array( $value ) ) { |
||
| 180 | $value = implode( ' ', $value ); |
||
| 181 | } |
||
| 182 | |||
| 183 | if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) { |
||
| 184 | if ( pods_v( 'read_only', $options, false ) ) { |
||
| 185 | $options['readonly'] = true; |
||
| 186 | } else { |
||
| 187 | return; |
||
| 188 | } |
||
| 189 | } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
||
| 190 | $options['readonly'] = true; |
||
| 191 | } |
||
| 192 | |||
| 193 | pods_view( PODS_DIR . 'ui/fields/oembed.php', compact( array_keys( get_defined_vars() ) ) ); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
| 200 | |||
| 201 | $errors = array(); |
||
| 202 | |||
| 203 | $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); |
||
| 204 | |||
| 205 | if ( is_array( $check ) ) { |
||
| 206 | $errors = $check; |
||
| 207 | } else { |
||
| 208 | if ( 0 < strlen( $value ) && '' === $check ) { |
||
| 209 | if ( 1 === (int) pods_v( 'required', $options ) ) { |
||
| 210 | $errors[] = __( 'This field is required.', 'pods' ); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | if ( ! empty( $errors ) ) { |
||
| 216 | return $errors; |
||
| 217 | } |
||
| 218 | |||
| 219 | return true; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function strip_html( $value, $options = null ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding. |
||
| 280 | * |
||
| 281 | * @see WP_Embed::autoembed() |
||
| 282 | * @see WP_Embed::autoembed_callback() |
||
| 283 | * |
||
| 284 | * @uses PodsField_OEmbed::autoembed_callback() |
||
| 285 | * |
||
| 286 | * @param string $content The content to be searched. |
||
| 287 | * |
||
| 288 | * @return string Potentially modified $content. |
||
| 289 | * |
||
| 290 | * @since 2.7 |
||
| 291 | */ |
||
| 292 | public function autoembed( $content ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Callback function for {@link WP_Embed::autoembed()}. |
||
| 312 | * |
||
| 313 | * @param array $match A regex match array. |
||
| 314 | * |
||
| 315 | * @return string The embed shortcode |
||
| 316 | * |
||
| 317 | * @since 2.7 |
||
| 318 | */ |
||
| 319 | public function autoembed_callback( $match ) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get a list of available providers from the WP_oEmbed class |
||
| 329 | * |
||
| 330 | * @see wp-includes/class-oembed.php |
||
| 331 | * @return array $providers { |
||
| 332 | * Array of provider data with regex as key |
||
| 333 | * |
||
| 334 | * @type string URL for this provider |
||
| 335 | * @type int |
||
| 336 | * @type string Hostname for this provider |
||
| 337 | * } |
||
| 338 | * |
||
| 339 | * @since 2.7 |
||
| 340 | */ |
||
| 341 | public function get_providers() { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Takes a URL and returns the corresponding oEmbed provider's URL, if there is one. |
||
| 388 | * This function is ripped from WP since Pods has support from 3.8 and in the WP core this function is 4.0+ |
||
| 389 | * We've stripped the autodiscover part from this function to keep it basic |
||
| 390 | * |
||
| 391 | * @since 2.7 |
||
| 392 | * @access public |
||
| 393 | * |
||
| 394 | * @see WP_oEmbed::get_provider() |
||
| 395 | * |
||
| 396 | * @param string $url The URL to the content. |
||
| 397 | * |
||
| 398 | * @return false|string False on failure, otherwise the oEmbed provider URL. |
||
| 399 | */ |
||
| 400 | public function get_provider( $url ) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Validate a value with the enabled oEmbed providers (if required). |
||
| 432 | * |
||
| 433 | * @since 2.7 |
||
| 434 | * |
||
| 435 | * @param string $value Field value. |
||
| 436 | * @param array $options Field options. |
||
| 437 | * |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | public function validate_provider( $value, $options ) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Handle update preview AJAX. |
||
| 479 | * |
||
| 480 | * @since 2.7 |
||
| 481 | */ |
||
| 482 | public function admin_ajax_oembed_update_preview() { |
||
| 525 | |||
| 526 | } |
||
| 527 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.