Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //BEGIN - TOOLBOX FUNCTION FOR IMAGE ASPECT RATIO TO WORK WITH BEAVER BUILDER
- add_filter( 'toolbox/helpers/settings/type=image_aspect_ratio_crop' , 'foo39_crop_image_settings' , 10, 2 );
- add_filter( 'toolbox/helpers/sc_attr/type=image_aspect_ratio_crop' , 'toolbox_acf::return_shortcode_attr_default_image' , 10, 1 );
- add_filter( 'toolbox/helpers/get_acf_field/type=image_aspect_ratio_crop' , 'foo39_crop_image_getter' , 10, 5 );
- add_filter( 'toolbox_condlogic_fieldtypes' , 'foo123_add_custom_fieldtype' , 10, 1 );
- add_filter( 'toolbox_condlogic_operators', 'foo123_add_custom_fieldtype_operator' , 10, 1 );
- /**
- * Settings panel options
- *
- * @type function
- * @since 1.0.0
- * @param [type] $settings [description]
- * @return [type] [description]
- */
- function foo39_crop_image_settings( $settings , $type = '' ) {
- $new_settings = array(
- 'size' => array(
- 'type' => 'select',
- 'label' => __( 'Image Size', 'toolbox' ),
- 'default' => 'medium',
- 'options' => apply_filters( 'toolbox/helpers/imagesizes', array() ),
- ),
- 'tag' => array(
- 'type' => 'select',
- 'label' => 'Tag',
- 'default' => 'false',
- 'options' => array(
- 'true' => __( 'Yes', 'toolbox' ),
- 'false' => __( 'No', 'toolbox' ),
- ),
- 'help' => __( 'Output with a src-tag? (set to false on image-fields)', 'toolbox' ),
- ),
- 'linked' => array(
- 'type' => 'select',
- 'label' => 'Linked to postid?',
- 'default' => 'false',
- 'options' => array(
- 'true' => __( 'Yes', 'toolbox' ),
- 'false' => __( 'No', 'toolbox' ),
- ),
- 'help' => __( 'Output with a src-tag? (set to false on image-fields)', 'toolbox' ),
- ),
- 'return_as_photo' => array(
- 'type' => 'select',
- 'label' => __( 'Return as Photo', 'textdomain' ),
- 'default' => false,
- 'options' => array(
- false => __( 'No', 'textdomain' ),
- true => __( 'Yes', 'textdomain' ),
- ),
- 'multi-select' => false,
- ),
- );
- return array_merge($settings, $new_settings);
- }
- function foo39_crop_image_getter( $string , $field_object , $value , $atts = null , $postid = null ) {
- $image_url = '';
- $image_title = '';
- // get the correct fieldname for version 4 or 5 of acf
- $acf_format = toolboxUtils_acf::acf_format();
- $classes = apply_filters( 'toolbox/helpers/set_acfimage_classes' , array() , $atts );
- /**
- * return_format or save_format is ARRAY or OBJECT
- */
- if ( 'array' == $field_object[ $acf_format ] || 'object' == $field_object[ $acf_format ] ) { // v5 array, v4 object
- $image_url = $value[ 'sizes' ][ $atts [ 'size' ] ];
- if ( 'full' == $atts[ 'size' ] ) $image_url = $value[ 'url' ];
- /**
- * return_format or save_format is ID
- */
- } elseif ( 'id' == $field_object[ $acf_format ] ) {
- $image_url = wp_get_attachment_image_url( $value , $atts[ 'size' ] );
- $image_title = get_post_meta( $value , '_wp_attachment_image_alt', TRUE );
- /**
- * return_format or save_format is full URL
- */
- } else {
- $image_url = ( $value !== null ) ? $value : "http://placehold.it/800?text=Not%20An%20Image" ;
- }
- if ( $atts[ 'tag' ] === 'true' ) {
- // return img-tag, if no image is set don't output the tag
- $string = ( $image_url == '' ) ? $string : $string . sprintf( '<img src="%s" alt="%s" title="%s" class="%s" />',$image_url , $image_title , $image_title , implode(' ', $classes) );
- if ( $atts['linked'] == 'true' ) {
- $string = "<a href=\"" . get_permalink( $postid, false ) . "\">". $string . "</a>";
- }
- return $string;
- } else {
- if ( isset( $atts[ 'return_as_photo' ] ) && $atts[ 'return_as_photo' ] ) {
- if ( gettype( $value ) == 'array' ) {
- return array( 'id' => $value['ID'] , 'url' => $image_url );
- }
- return array( 'id' => $value , 'url' => $image_url );
- }
- // return just the url for the image ( from array or just the file);
- return $string . $image_url;
- }
- return $string . $image_url;
- }
- function foo123_add_custom_fieldtype( $fieldtypes ) {
- return array_merge( $fieldtypes ,
- array(
- array(
- 'name' => 'image_aspect_ratio_crop',
- 'label'=> 'Aspect Crop'
- ) ,
- )
- );
- }
- function foo123_add_custom_fieldtype_operator( $fieldtypes ) {
- return array_merge( $fieldtypes ,
- array(
- 'image_aspect_ratio_crop' => array(
- 'operators' => [ 'equals', 'does_not_equal' , 'is_empty', 'is_not_empty', 'is_set' , 'is_not_set' ],
- 'hasvalue' => true,
- 'hasend' => false,
- ),
- )
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement