abilities
2 days ago
admin
2 days ago
compatibility
2 days ago
extensions
2 days ago
foopluginbase
2 days ago
public
2 days ago
thumbs
2 days ago
asset-manifest.php
2 days ago
class-attachment-filters.php
2 days ago
class-command-palette.php
2 days ago
class-foogallery-animated-gif-support.php
2 days ago
class-foogallery-attachment-custom-class.php
2 days ago
class-foogallery-attachment-filename.php
2 days ago
class-foogallery-attachment-type.php
2 days ago
class-foogallery-attachment.php
2 days ago
class-foogallery-cache.php
2 days ago
class-foogallery-common-fields.php
2 days ago
class-foogallery-crop-position.php
2 days ago
class-foogallery-datasource-media_library.php
2 days ago
class-foogallery-debug.php
2 days ago
class-foogallery-delayed-runtime-loader.php
2 days ago
class-foogallery-extensions-compatibility.php
2 days ago
class-foogallery-force-https.php
2 days ago
class-foogallery-lazyload.php
2 days ago
class-foogallery-license-constant-handler.php
2 days ago
class-foogallery-lightbox.php
2 days ago
class-foogallery-no-javascript-fallback.php
2 days ago
class-foogallery-paging.php
2 days ago
class-foogallery-password-protect.php
2 days ago
class-foogallery-sitemaps.php
2 days ago
class-foogallery-widget.php
2 days ago
class-foogallery.php
2 days ago
class-gallery-advanced-settings.php
2 days ago
class-il8n.php
2 days ago
class-override-thumbnail.php
2 days ago
class-posttypes.php
2 days ago
class-previews.php
2 days ago
class-retina.php
2 days ago
class-thumbnail-dimensions.php
2 days ago
class-thumbnails.php
2 days ago
class-version-check.php
2 days ago
constants.php
2 days ago
functions.php
2 days ago
gallery-management-functions.php
2 days ago
includes.php
2 days ago
index.php
2 days ago
render-functions.php
2 days ago
class-foogallery-crop-position.php
234 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Class to provide a way to override the default attachment crop position. Handy if you want to override the default of center,center |
| 9 | * Date: 19/02/2018 |
| 10 | * |
| 11 | * @since 1.4.18 |
| 12 | */ |
| 13 | if ( ! class_exists( 'FooGallery_Crop_Position' ) ) { |
| 14 | |
| 15 | class FooGallery_Crop_Position { |
| 16 | |
| 17 | const CROP_POSITION_META_KEY = 'foogallery_crop_pos'; |
| 18 | const CROP_POSITION_META_KEY_LEGACY = 'wpthumb_crop_pos'; |
| 19 | const CROP_POSITION_DEFAULT = 'center,center'; |
| 20 | |
| 21 | function __construct() { |
| 22 | if ( is_admin() ) { |
| 23 | add_filter( 'foogallery_admin_settings_override', array( $this, 'add_default_crop_position_setting' ) ); |
| 24 | add_action( 'foogallery_admin_settings_custom_type_render_setting', array( $this, 'render_crop_position_setting' ) ); |
| 25 | |
| 26 | add_filter( 'attachment_fields_to_edit', array( $this, 'media_form_crop_position' ), 10, 2 ); |
| 27 | add_filter( 'attachment_fields_to_save', array( $this, 'media_form_crop_position_save' ), 10, 2 ); |
| 28 | } |
| 29 | add_filter( 'foogallery_thumbnail_resize_args_final', array( $this, 'add_crop_position_arguments' ), 10, 3 ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Append crop position arguments if they are saved |
| 34 | * |
| 35 | * @param $args |
| 36 | * @param $original_image_src |
| 37 | * @param $thumbnail_object |
| 38 | * |
| 39 | * @return mixed |
| 40 | */ |
| 41 | function add_crop_position_arguments( $args, $original_image_src, $thumbnail_object ) { |
| 42 | if ( !foogallery_thumb_active_engine()->has_local_cache() ) { |
| 43 | return $args; |
| 44 | } |
| 45 | |
| 46 | if ( isset( $thumbnail_object ) && $thumbnail_object->ID > 0 ) { |
| 47 | $crop_from_position = get_post_meta( $thumbnail_object->ID, self::CROP_POSITION_META_KEY, true ); |
| 48 | |
| 49 | if ( !empty( $crop_from_position ) ) { |
| 50 | $args['crop_from_position'] = $crop_from_position; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return $args; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Adds the crop position setting to the settings array |
| 59 | * |
| 60 | * @param $settings |
| 61 | * |
| 62 | * @return mixed |
| 63 | */ |
| 64 | function add_default_crop_position_setting( $settings ) { |
| 65 | if ( !foogallery_thumb_active_engine()->has_local_cache() ) { |
| 66 | return $settings; |
| 67 | } |
| 68 | |
| 69 | $just_settings = $settings['settings']; |
| 70 | $position = 0; |
| 71 | //find the position of the 'thumb_jpeg_quality' setting |
| 72 | foreach( $just_settings as $setting ) { |
| 73 | $position++; |
| 74 | if ( 'thumb_jpeg_quality' === $setting['id'] ) { |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | $new_settings[] = array( |
| 80 | 'id' => 'default_crop_position', |
| 81 | 'title' => __( 'Default Crop Position', 'foogallery' ), |
| 82 | 'desc' => __( 'The default crop position when resizing thumbnails.', 'foogallery' ), |
| 83 | 'type' => 'crop', |
| 84 | 'default' => self::CROP_POSITION_DEFAULT, |
| 85 | 'tab' => 'thumb' |
| 86 | ); |
| 87 | |
| 88 | array_splice( $just_settings, $position, 0, $new_settings ); |
| 89 | |
| 90 | $settings['settings'] = $just_settings; |
| 91 | |
| 92 | return $settings; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Render the custom crop position to the settings page |
| 97 | * |
| 98 | * @param array $args |
| 99 | */ |
| 100 | function render_crop_position_setting( $args ) { |
| 101 | if ( 'crop' === $args['type'] ) { |
| 102 | $current_position = foogallery_get_setting( $args['id'], $args['default'] ); ?> |
| 103 | <style>.foogallery_crop_pos input { margin: 5px !important; width: auto; }</style> |
| 104 | <div class="foogallery_crop_pos"> |
| 105 | <input type="radio" name="foogallery[<?php echo esc_attr( $args['id'] ); ?>]" value="left,top" title="Left, Top" <?php checked( 'left,top', $current_position ) ?>/> |
| 106 | <input type="radio" name="foogallery[<?php echo esc_attr( $args['id'] ); ?>]" value="center,top" title="Center, Top" <?php checked( 'center,top', $current_position ) ?>/> |
| 107 | <input type="radio" name="foogallery[<?php echo esc_attr( $args['id'] ); ?>]" value="right,top" title="Right, Top" <?php checked( 'right,top', $current_position ) ?>/><br/> |
| 108 | <input type="radio" name="foogallery[<?php echo esc_attr( $args['id'] ); ?>]" value="left,center" title="Left, Center" <?php checked( 'left,center', $current_position ) ?>/> |
| 109 | <input type="radio" name="foogallery[<?php echo esc_attr( $args['id'] ); ?>]" value="center,center" title="Center, Center" <?php checked( 'center,center', $current_position ) ?>/> |
| 110 | <input type="radio" name="foogallery[<?php echo esc_attr( $args['id'] ); ?>]" value="right,center" title="Right, Center" <?php checked( 'right,center', $current_position ) ?>/><br/> |
| 111 | <input type="radio" name="foogallery[<?php echo esc_attr( $args['id'] ); ?>]" value="left,bottom" title="Left, Bottom" <?php checked( 'left,bottom', $current_position ) ?>/> |
| 112 | <input type="radio" name="foogallery[<?php echo esc_attr( $args['id'] ); ?>]" value="center,bottom" title="Center, Bottom" <?php checked( 'center,bottom', $current_position ) ?>/> |
| 113 | <input type="radio" name="foogallery[<?php echo esc_attr( $args['id'] ); ?>]" value="right,bottom" title="Right, Bottom" <?php checked( 'right,bottom', $current_position ) ?>/> |
| 114 | </div> |
| 115 | <?php } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Return the default crop position |
| 120 | * |
| 121 | * @param $default |
| 122 | * |
| 123 | * @return mixed |
| 124 | */ |
| 125 | function default_crop_position() { |
| 126 | $crop_position = foogallery_get_setting( 'default_crop_position', self::CROP_POSITION_DEFAULT ); |
| 127 | return $crop_position; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Adds a back end for selecting the crop position of images. |
| 132 | * |
| 133 | * @access public |
| 134 | * |
| 135 | * @param array $fields |
| 136 | * @param array $post |
| 137 | * @return $post |
| 138 | */ |
| 139 | function media_form_crop_position( $fields, $post ) { |
| 140 | |
| 141 | if ( !foogallery_thumb_active_engine()->has_local_cache() ) { |
| 142 | return $fields; |
| 143 | } |
| 144 | |
| 145 | // Ensure $post is not null before proceeding |
| 146 | if ( ! is_a( $post, 'WP_Post' ) || ! wp_attachment_is_image( $post->ID ) ) { |
| 147 | return $fields; |
| 148 | } |
| 149 | |
| 150 | $crop_position = $this->get_crop_position_from_attachment( $post->ID ); |
| 151 | |
| 152 | $html = '<style>#foogallery_crop_pos { padding: 5px; } #foogallery_crop_pos input { margin: 5px; width: auto; }</style>'; |
| 153 | $html .= '<div id="foogallery_crop_pos">'; |
| 154 | $html .= '<input type="radio" name="attachments[' . $post->ID . '][foogallery_crop_pos]" value="left,top" title="Left, Top" ' . checked( 'left,top', $crop_position, false ) . '/>'; |
| 155 | $html .= '<input type="radio" name="attachments[' . $post->ID . '][foogallery_crop_pos]" value="center,top" title="Center, Top" ' . checked( 'center,top', $crop_position, false ) . '/>'; |
| 156 | $html .= '<input type="radio" name="attachments[' . $post->ID . '][foogallery_crop_pos]" value="right,top" title="Right, Top" ' . checked( 'right,top', $crop_position, false ) . '/><br/>'; |
| 157 | $html .= '<input type="radio" name="attachments[' . $post->ID . '][foogallery_crop_pos]" value="left,center" title="Left, Center" ' . checked( 'left,center', $crop_position, false ) . '/>'; |
| 158 | $html .= '<input type="radio" name="attachments[' . $post->ID . '][foogallery_crop_pos]" value="center,center" title="Center, Center"' . checked( 'center,center', $crop_position, false ) . '/>'; |
| 159 | $html .= '<input type="radio" name="attachments[' . $post->ID . '][foogallery_crop_pos]" value="right,center" title="Right, Center" ' . checked( 'right,center', $crop_position, false ) . '/><br/>'; |
| 160 | $html .= '<input type="radio" name="attachments[' . $post->ID . '][foogallery_crop_pos]" value="left,bottom" title="Left, Bottom" ' . checked( 'left,bottom', $crop_position, false ) . '/>'; |
| 161 | $html .= '<input type="radio" name="attachments[' . $post->ID . '][foogallery_crop_pos]" value="center,bottom" title="Center, Bottom" ' . checked( 'center,bottom', $crop_position, false ) . '/>'; |
| 162 | $html .= '<input type="radio" name="attachments[' . $post->ID . '][foogallery_crop_pos]" value="right,bottom" title="Right, Bottom" ' . checked( 'right,bottom', $crop_position, false ) . '/>'; |
| 163 | $html .= '</div>'; |
| 164 | |
| 165 | $fields['crop-from-position'] = array( |
| 166 | 'label' => __( 'Crop Position', 'foogallery' ), |
| 167 | 'input' => 'html', |
| 168 | 'html' => $html |
| 169 | ); |
| 170 | |
| 171 | return $fields; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Gets the crop position from the attachment post meta, and defaults to what is set in settings |
| 176 | * |
| 177 | * @param $attachment_id |
| 178 | * |
| 179 | * @return mixed|string |
| 180 | */ |
| 181 | private function get_crop_position_from_attachment( $attachment_id ) { |
| 182 | //first try to get the legacy value using the old key |
| 183 | $crop_postion = get_post_meta( $attachment_id, self::CROP_POSITION_META_KEY_LEGACY, true ); |
| 184 | |
| 185 | //check if we have a legacy value saved, so migrate to the new |
| 186 | if ( $crop_postion ) { |
| 187 | //remove the old post meta |
| 188 | delete_post_meta( $attachment_id, self::CROP_POSITION_META_KEY_LEGACY ); |
| 189 | |
| 190 | //add new post meta with the correct key |
| 191 | update_post_meta( $attachment_id, self::CROP_POSITION_META_KEY, $crop_postion ); |
| 192 | } else { |
| 193 | $current_position = get_post_meta( $attachment_id, self::CROP_POSITION_META_KEY, true ); |
| 194 | } |
| 195 | |
| 196 | if ( !$current_position ) { |
| 197 | $current_position = $this->default_crop_position(); |
| 198 | } |
| 199 | |
| 200 | return $current_position; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * wpthumb_media_form_crop_position_save function. |
| 205 | * |
| 206 | * Saves crop position in post meta. |
| 207 | * |
| 208 | * @access public |
| 209 | * |
| 210 | * @param array $post |
| 211 | * @param array $attachment |
| 212 | * @return $post |
| 213 | */ |
| 214 | function media_form_crop_position_save( $post, $attachment ) { |
| 215 | |
| 216 | if ( !foogallery_thumb_active_engine()->has_local_cache() ) { |
| 217 | return $post; |
| 218 | } |
| 219 | |
| 220 | if ( ! isset( $attachment['foogallery_crop_pos'] ) ) { |
| 221 | return $post; |
| 222 | } |
| 223 | |
| 224 | if ( $attachment['foogallery_crop_pos'] == $this->default_crop_position() ) { |
| 225 | delete_post_meta( $post['ID'], self::CROP_POSITION_META_KEY ); |
| 226 | } else { |
| 227 | update_post_meta( $post['ID'], self::CROP_POSITION_META_KEY, $attachment['foogallery_crop_pos'] ); |
| 228 | } |
| 229 | |
| 230 | return $post; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 |