| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Optml_shortcode_ultimate. |
| 5 |
* |
| 6 |
* @reason Shortcode Ultimate uses a strange image resizing feature |
| 7 |
* which has by default the hard cropping on. As we are following the WordPress |
| 8 |
* image size defaults, we need to change the default cropping |
| 9 |
* for shortcode's output. |
| 10 |
*/ |
| 11 |
class Optml_shortcode_ultimate extends Optml_compatibility { |
| 12 |
/** |
| 13 |
* Tags where we subscribe the compatibility. |
| 14 |
* |
| 15 |
* @var array Allowed tags. |
| 16 |
*/ |
| 17 |
private $allowed_tags = [ |
| 18 |
'su_slider' => true, |
| 19 |
'su_carousel' => true, |
| 20 |
'su_custom_gallery' => true, |
| 21 |
]; |
| 22 |
|
| 23 |
/** |
| 24 |
* Should we load the integration logic. |
| 25 |
* |
| 26 |
* @return bool Should we load. |
| 27 |
*/ |
| 28 |
function should_load() { |
| 29 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 30 |
|
| 31 |
return is_plugin_active( 'shortcodes-ultimate/shortcodes-ultimate.php' ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Register integration details. |
| 36 |
*/ |
| 37 |
public function register() { |
| 38 |
add_filter( 'do_shortcode_tag', [ $this, 'alter_shortcode_output' ], 10, 3 ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Alter shortcode output by replacing the image urls. |
| 43 |
* |
| 44 |
* @param string $output Previous shortcode output. |
| 45 |
* @param string $tag Shortcode tag. |
| 46 |
* @param array $attr Shortcode attrs. |
| 47 |
* |
| 48 |
* @return mixed New output. |
| 49 |
*/ |
| 50 |
function alter_shortcode_output( $output, $tag, $attr ) { |
| 51 |
|
| 52 |
if ( ! isset( $this->allowed_tags[ $tag ] ) ) { |
| 53 |
return $output; |
| 54 |
} |
| 55 |
|
| 56 |
add_filter( 'optml_default_crop', [ $this, 'change_default_crop' ] ); |
| 57 |
add_filter( 'optml_parse_resize_from_tag', [ $this, 'change_default_crop' ] ); |
| 58 |
|
| 59 |
$output = Optml_Main::instance()->manager->process_images_from_content( $output ); |
| 60 |
|
| 61 |
remove_filter( 'optml_default_crop', [ $this, 'change_default_crop' ] ); |
| 62 |
remove_filter( 'optml_parse_resize_from_tag', [ $this, 'change_default_crop' ] ); |
| 63 |
|
| 64 |
return $output; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Change default crop. |
| 69 |
* |
| 70 |
* @return array New default cropping. |
| 71 |
*/ |
| 72 |
public function change_default_crop() { |
| 73 |
return [ |
| 74 |
'type' => Optml_Resize::RESIZE_FILL, |
| 75 |
'gravity' => Optml_Resize::GRAVITY_CENTER, |
| 76 |
]; |
| 77 |
} |
| 78 |
} |
| 79 |
|