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