Advertisement
DidouS

Add image_aspect_ratio_crop

Jan 26th, 2021
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.43 KB | None | 0 0
  1. //BEGIN - TOOLBOX FUNCTION FOR IMAGE ASPECT RATIO TO WORK WITH BEAVER BUILDER
  2.  
  3. add_filter( 'toolbox/helpers/settings/type=image_aspect_ratio_crop' , 'foo39_crop_image_settings' , 10, 2  );
  4. add_filter( 'toolbox/helpers/sc_attr/type=image_aspect_ratio_crop' , 'toolbox_acf::return_shortcode_attr_default_image' , 10, 1 );
  5. add_filter( 'toolbox/helpers/get_acf_field/type=image_aspect_ratio_crop' , 'foo39_crop_image_getter' , 10, 5  );
  6.  
  7. add_filter( 'toolbox_condlogic_fieldtypes' , 'foo123_add_custom_fieldtype' , 10, 1 );
  8. add_filter( 'toolbox_condlogic_operators', 'foo123_add_custom_fieldtype_operator' , 10, 1 );
  9.  
  10. /**
  11.  * Settings panel options
  12.  *
  13.  * @type   function
  14.  * @since  1.0.0
  15.  * @param  [type] $settings [description]
  16.  * @return [type]           [description]
  17.  */
  18. function foo39_crop_image_settings( $settings , $type = '' ) {
  19.  
  20.     $new_settings = array(
  21.         'size' => array(
  22.             'type'        => 'select',
  23.             'label'       => __( 'Image Size', 'toolbox' ),
  24.             'default'     => 'medium',
  25.             'options'     => apply_filters( 'toolbox/helpers/imagesizes', array() ),
  26.         ),
  27.         'tag' => array(
  28.             'type'        => 'select',
  29.             'label'       => 'Tag',
  30.             'default'       => 'false',
  31.             'options'       => array(
  32.                 'true'      => __( 'Yes', 'toolbox' ),
  33.                 'false'     => __( 'No', 'toolbox' ),
  34.             ),
  35.             'help'       => __( 'Output with a src-tag? (set to false on image-fields)', 'toolbox' ),
  36.         ),
  37.         'linked' => array(
  38.             'type'        => 'select',
  39.             'label'       => 'Linked to postid?',
  40.             'default'       => 'false',
  41.             'options'       => array(
  42.                 'true'      => __( 'Yes', 'toolbox' ),
  43.                 'false'     => __( 'No', 'toolbox' ),
  44.             ),
  45.             'help'       => __( 'Output with a src-tag? (set to false on image-fields)', 'toolbox' ),
  46.         ),
  47.         'return_as_photo'   => array(
  48.             'type'          => 'select',
  49.             'label'         => __( 'Return as Photo', 'textdomain' ),
  50.             'default'       => false,
  51.             'options'       => array(
  52.                 false      => __( 'No', 'textdomain' ),
  53.                 true      => __( 'Yes', 'textdomain' ),
  54.             ),
  55.             'multi-select'  => false,
  56.         ),
  57.  
  58.     );
  59.  
  60.     return array_merge($settings, $new_settings);
  61. }
  62.  
  63. function foo39_crop_image_getter( $string , $field_object , $value , $atts = null , $postid = null ) {
  64.  
  65.     $image_url = '';
  66.     $image_title = '';
  67.  
  68.     // get the correct fieldname for version 4 or 5 of acf
  69.     $acf_format = toolboxUtils_acf::acf_format();
  70.  
  71.     $classes = apply_filters( 'toolbox/helpers/set_acfimage_classes' , array() , $atts );
  72.  
  73.     /**
  74.      * return_format or save_format is ARRAY or OBJECT
  75.      */
  76.     if ( 'array' == $field_object[ $acf_format ] || 'object' == $field_object[ $acf_format ] ) {       // v5 array, v4 object
  77.  
  78.         $image_url = $value[ 'sizes' ][ $atts [ 'size' ] ];
  79.         if ( 'full' == $atts[ 'size' ] ) $image_url = $value[ 'url' ];
  80.  
  81.  
  82.     /**
  83.      * return_format or save_format is ID
  84.      */
  85.     } elseif ( 'id' == $field_object[ $acf_format ] ) {
  86.  
  87.         $image_url = wp_get_attachment_image_url( $value , $atts[ 'size' ] );
  88.  
  89.         $image_title = get_post_meta( $value , '_wp_attachment_image_alt', TRUE );
  90.  
  91.     /**
  92.      * return_format or save_format is full URL
  93.      */
  94.     } else {
  95.  
  96.         $image_url = ( $value !== null ) ? $value : "http://placehold.it/800?text=Not%20An%20Image" ;
  97.  
  98.     }
  99.  
  100.     if (  $atts[ 'tag' ] === 'true' ) {
  101.  
  102.         // return img-tag, if no image is set don't output the tag
  103.         $string = ( $image_url == '' ) ? $string : $string . sprintf( '<img src="%s" alt="%s" title="%s" class="%s" />',$image_url , $image_title , $image_title , implode(' ', $classes) );
  104.  
  105.         if ( $atts['linked'] == 'true' ) {
  106.  
  107.             $string = "<a href=\"" . get_permalink( $postid, false ) . "\">". $string . "</a>";
  108.         }
  109.  
  110.         return $string;
  111.  
  112.     } else {
  113.  
  114.         if ( isset( $atts[ 'return_as_photo' ] ) && $atts[ 'return_as_photo' ] ) {
  115.  
  116.             if ( gettype( $value ) == 'array' ) {
  117.                 return array( 'id' => $value['ID'] , 'url' => $image_url );
  118.             }
  119.  
  120.             return array( 'id' => $value , 'url' => $image_url );
  121.         }
  122.  
  123.         // return just the url for the image ( from array or just the file);
  124.         return $string . $image_url;
  125.     }
  126.  
  127.  
  128.     return $string . $image_url;
  129.  
  130. }
  131.  
  132.  
  133.  
  134. function foo123_add_custom_fieldtype( $fieldtypes ) {
  135.     return array_merge( $fieldtypes ,
  136.                         array(
  137.                             array(
  138.                                 'name' => 'image_aspect_ratio_crop',
  139.                                 'label'=> 'Aspect Crop'
  140.                             ) ,
  141.                         )
  142.                     );
  143. }
  144.  
  145. function foo123_add_custom_fieldtype_operator( $fieldtypes ) {
  146.     return array_merge( $fieldtypes ,
  147.                         array(
  148.                             'image_aspect_ratio_crop' =>   array(
  149.                                         'operators' => [ 'equals', 'does_not_equal' , 'is_empty', 'is_not_empty', 'is_set' , 'is_not_set' ],
  150.                                         'hasvalue' => true,
  151.                                         'hasend' => false,
  152.                                     ),
  153.                         )
  154.                     );
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement