PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/yoimages/vendor/sirulli/yoimages-crop/inc/extend-yoimg-settings.php

https://bitbucket.org/tasshanakagawa/amplifly
PHP | 173 lines | 169 code | 3 blank | 1 comment | 40 complexity | bc0a57dd82e64e72bf313d7479eca815 MD5 | raw file
Possible License(s): GPL-2.0, MIT
  1. <?php
  2. if (! defined ( 'ABSPATH' )) {
  3. die ( 'No script kiddies please!' );
  4. }
  5. function yoimg_crop_extend_settings($settings) {
  6. $crop_settings = array (
  7. 'option' => array (
  8. 'page' => 'yoimages-crop',
  9. 'title' => __ ( 'Crop settings', YOIMG_DOMAIN ),
  10. 'option_group' => 'yoimages-crop-group',
  11. 'option_name' => 'yoimg_crop_settings',
  12. 'sanitize_callback' => 'yoimg_crop_settings_sanitize',
  13. 'sections' => array (
  14. array (
  15. 'id' => 'yoimg_crop_options_section',
  16. 'title' => __ ( 'Crop settings', YOIMG_DOMAIN ),
  17. 'callback' => 'yoimg_crop_settings_section_info',
  18. 'fields' => array (
  19. array (
  20. 'id' => 'cropping_is_active',
  21. 'title' => __ ( 'Enable', YOIMG_DOMAIN ),
  22. 'callback' => 'yoimg_crop_settings_cropping_is_active_callback'
  23. ),
  24. array (
  25. 'id' => 'crop_qualities',
  26. 'title' => __ ( 'Crop qualities', YOIMG_DOMAIN ),
  27. 'callback' => 'yoimg_crop_settings_crop_qualities_callback'
  28. ),
  29. array (
  30. 'id' => 'retina_cropping_is_active',
  31. 'title' => __ ( 'Retina friendly', YOIMG_DOMAIN ),
  32. 'callback' => 'yoimg_crop_settings_retina_cropping_is_active_callback'
  33. ),
  34. array (
  35. 'id' => 'cropping_sizes',
  36. 'title' => __ ( 'Cropping sizes', YOIMG_DOMAIN ),
  37. 'callback' => 'yoimg_crop_settings_cropping_sizes_callback'
  38. ),
  39. array (
  40. 'id' => 'cachebusting_is_active',
  41. 'title' => __ ( 'Cachebust new crops', YOIMG_DOMAIN ),
  42. 'callback' => 'yoimg_crop_settings_cachebust_new_crops_callback'
  43. )
  44. )
  45. )
  46. )
  47. )
  48. );
  49. array_push ( $settings, $crop_settings );
  50. return $settings;
  51. }
  52. add_filter ( 'yoimg_settings', 'yoimg_crop_extend_settings', 10, 1 );
  53. function yoimg_crop_settings_cropping_is_active_callback() {
  54. $crop_options = get_option ( 'yoimg_crop_settings' );
  55. printf ( '<input type="checkbox" id="cropping_is_active" name="yoimg_crop_settings[cropping_is_active]" value="TRUE" %s />
  56. <p class="description">' . __ ( 'If checked cropping is active', YOIMG_DOMAIN ) . '</p>', $crop_options ['cropping_is_active'] ? 'checked="checked"' : (YOIMG_DEFAULT_CROP_ENABLED && ! isset ( $crop_options ['cropping_is_active'] ) ? 'checked="checked"' : '') );
  57. }
  58. function yoimg_crop_settings_crop_qualities_callback() {
  59. $crop_options = get_option ( 'yoimg_crop_settings' );
  60. printf ( '<input type="text" id="crop_qualities" name="yoimg_crop_settings[crop_qualities]" value="%s" class="cropping_is_active-dep" />
  61. <p class="description">' . __ ( 'Comma separated list of crop quality values (100 best to 50 medium)', YOIMG_DOMAIN ) . '</p>', ! empty ( $crop_options ['crop_qualities'] ) ? esc_attr ( implode ( ',', $crop_options ['crop_qualities'] ) ) : implode ( ',', unserialize ( YOIMG_DEFAULT_CROP_QUALITIES ) ) );
  62. }
  63. function yoimg_crop_settings_retina_cropping_is_active_callback() {
  64. $crop_options = get_option ( 'yoimg_crop_settings' );
  65. printf ( '<input type="checkbox" id="retina_cropping_is_active" class="cropping_is_active-dep" name="yoimg_crop_settings[retina_cropping_is_active]" value="TRUE" %s />
  66. <p class="description">' . __ ( 'Flag to enable (enable this option if you are using a retina plugin that uses @2x as file naming convention when creating retina images from source - e.g. <a href="https://wordpress.org/plugins/wp-retina-2x/" target="_blank">WP Retina 2x</a>)', YOIMG_DOMAIN ) . '</p>', $crop_options ['retina_cropping_is_active'] ? 'checked="checked"' : (YOIMG_DEFAULT_CROP_RETINA_ENABLED && ! isset ( $crop_options ['retina_cropping_is_active'] ) ? 'checked="checked"' : '') );
  67. }
  68. function yoimg_crop_settings_cropping_sizes_callback() {
  69. $crop_sizes_options = yoimg_get_image_sizes ();
  70. print ( '<table class="wp-list-table widefat fixed striped">
  71. <thead>
  72. <tr>
  73. <td style="width:30%;"><span>' . __ ( 'Size id', YOIMG_DOMAIN ) . '</span></td>
  74. <td style="width:10%;"><span>' . __ ( 'Width', YOIMG_DOMAIN ) . '</span></td>
  75. <td style="width:10%;"><span>' . __ ( 'Height', YOIMG_DOMAIN ) . '</span></td>
  76. <td style="width:40%;"><span>' . __ ( 'User-friendly name', YOIMG_DOMAIN ) . '</span></td>
  77. <td style="width:10%;"><span>' . __ ( 'Active', YOIMG_DOMAIN ) . '</span></td>
  78. </tr>
  79. </thead>' );
  80. foreach ( $crop_sizes_options as $crop_size_id => $crop_size_option ) {
  81. if ( isset( $crop_size_option['active'] ) && $crop_size_option['crop'] == 1 ) {
  82. printf ( '<tr><td>%s</td>', $crop_size_id );
  83. printf ( '<td>%s</td>', $crop_size_option['width'] );
  84. printf ( '<td>%s</td>', $crop_size_option['height'] );
  85. printf ( '<td><input class="cropping_is_active-dep" type="text" name="yoimg_crop_settings[crop_sizes][%s][name]" value="%s" style="width: 100%%;" /></td>', $crop_size_id, $crop_size_option['name'] );
  86. printf ( '<td><input class="cropping_is_active-dep" type="checkbox" name="yoimg_crop_settings[crop_sizes][%s][active]" value="TRUE" %s /></td></tr>', $crop_size_id, isset( $crop_size_option['active'] ) && $crop_size_option['active'] ? 'checked="checked"' : '' );
  87. }
  88. }
  89. print ( '</table>' );
  90. }
  91. // Create the cachebuster setting checkbox
  92. function yoimg_crop_settings_cachebust_new_crops_callback() {
  93. $crop_options = get_option ( 'yoimg_crop_settings' );
  94. $check_value = '';
  95. if( (isset($crop_options['cachebusting_is_active']) && $crop_options['cachebusting_is_active']) || (YOIMG_DEFAULT_CACHEBUSTER_ENABLED && !isset($crop_options['cachebusting_is_active'])) ) {
  96. $check_value = 'checked="checked"';
  97. }
  98. printf ( '<input type="checkbox" id="cachebusting_is_active" class="cropping_is_active-dep" name="yoimg_crop_settings[cachebusting_is_active]" value="TRUE" %s />
  99. <p class="description">' . __ ( 'Generate a new filename after cropping images so that they are updated by external caches and CDNs.', YOIMG_DOMAIN ) . '</p>',
  100. $check_value );
  101. }
  102. function yoimg_crop_settings_section_info() {
  103. print __ ( 'Enter your cropping settings here below', YOIMG_DOMAIN );
  104. }
  105. function yoimg_crop_settings_sanitize($input) {
  106. $new_input = array ();
  107. if (isset ( $input ['cropping_is_active'] ) && ($input ['cropping_is_active'] === 'TRUE' || $input ['cropping_is_active'] === TRUE)) {
  108. $new_input ['cropping_is_active'] = TRUE;
  109. } else {
  110. $new_input ['cropping_is_active'] = FALSE;
  111. }
  112. if (isset ( $input ['crop_qualities'] )) {
  113. if (is_array ( $input ['crop_qualities'] )) {
  114. $crop_qualities = $input ['crop_qualities'];
  115. } else {
  116. $crop_qualities = explode ( ',', $input ['crop_qualities'] );
  117. }
  118. $crop_qualities_count = 0;
  119. foreach ( $crop_qualities as $index => $value ) {
  120. $crop_quality_value = ( int ) $value;
  121. if ($crop_quality_value > 0 && $crop_quality_value <= 100) {
  122. $crop_qualities_arr [$crop_qualities_count] = $crop_quality_value;
  123. $crop_qualities_count ++;
  124. }
  125. }
  126. if (empty ( $crop_qualities_arr )) {
  127. add_settings_error ( 'yoimg_crop_options_group', 'crop_qualities', __ ( 'Crop qualities value is not valid, using default:', YOIMG_DOMAIN ) . ' ' . implode ( ',', unserialize ( YOIMG_DEFAULT_CROP_QUALITIES ) ), 'error' );
  128. $new_input ['crop_qualities'] = unserialize ( YOIMG_DEFAULT_CROP_QUALITIES );
  129. } else {
  130. $crop_qualities_arr = array_unique ( $crop_qualities_arr );
  131. rsort ( $crop_qualities_arr );
  132. $new_input ['crop_qualities'] = $crop_qualities_arr;
  133. }
  134. } else {
  135. $new_input ['crop_qualities'] = unserialize ( YOIMG_DEFAULT_CROP_QUALITIES );
  136. }
  137. if (isset ( $input ['retina_cropping_is_active'] ) && ($input ['retina_cropping_is_active'] === 'TRUE' || $input ['retina_cropping_is_active'] === TRUE)) {
  138. $new_input ['retina_cropping_is_active'] = TRUE;
  139. } else {
  140. $new_input ['retina_cropping_is_active'] = FALSE;
  141. }
  142. if (isset ( $input ['cachebusting_is_active'] ) && ($input ['cachebusting_is_active'] === 'TRUE' || $input ['cachebusting_is_active'] === TRUE)) {
  143. $new_input ['cachebusting_is_active'] = TRUE;
  144. } else {
  145. $new_input ['cachebusting_is_active'] = FALSE;
  146. }
  147. if (isset ( $input ['crop_sizes'] )) {
  148. $there_is_one_manual_crop_active = FALSE;
  149. foreach ( $input['crop_sizes'] as $crop_size_id => $crop_size_option ) {
  150. if (isset ( $crop_size_option ['active'] ) && ($crop_size_option ['active'] === 'TRUE' || $crop_size_option ['active'] === TRUE)) {
  151. $new_input ['crop_sizes'][$crop_size_id]['active'] = TRUE;
  152. $there_is_one_manual_crop_active = TRUE;
  153. } else {
  154. $new_input ['crop_sizes'][$crop_size_id]['active'] = FALSE;
  155. }
  156. if ( isset( $crop_size_option ['name'] ) && ! empty( $crop_size_option ['name'] ) ) {
  157. $new_input ['crop_sizes'][$crop_size_id]['name'] = $crop_size_option ['name'];
  158. } else {
  159. $new_input ['crop_sizes'][$crop_size_id]['name'] = $crop_size_id;
  160. }
  161. }
  162. if ( ! $there_is_one_manual_crop_active ) {
  163. add_settings_error ( 'yoimg_crop_options_group', 'crop_sizes', __ ( 'There should be at least one manual crop activated. If you don\'t want any manual crop to be active, please disable the whole cropping using the first checkbox here below.', YOIMG_DOMAIN ), 'error' );
  164. foreach ( $input['crop_sizes'] as $crop_size_id => $crop_size_option ) {
  165. $new_input ['crop_sizes'][$crop_size_id]['active'] = TRUE;
  166. }
  167. }
  168. }
  169. return $new_input;
  170. }