PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.3.0
Gallery : FooGallery v3.3.0
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / thumbs / default / class-foogallery-thumb-engine-default.php
foogallery / includes / thumbs / default Last commit date
class-foogallery-thumb-engine-default.php 1 week ago class-foogallery-thumb-generator-background-fill.php 1 week ago class-foogallery-thumb-generator.php 1 week ago
class-foogallery-thumb-engine-default.php
208 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * Class for the default thumbnail engine in FooGallery
9 */
10 if ( ! class_exists( 'FooGallery_Thumb_Engine_Default' ) ) {
11
12 class FooGallery_Thumb_Engine_Default extends FooGallery_Thumb_Engine {
13
14 /**
15 * The last error that was encounted
16 * @var mixed
17 */
18 private $last_error;
19
20 public function init() {
21
22 add_action( 'deleted_post', array( $this, 'delete_cache_folder_for_attachment' ), 10, 1 );
23 add_action( 'foogallery_admin_menu_after', array( $this, 'add_test_thumb_menu' ) );
24
25 //add background fill functionality
26 new FooGallery_Thumb_Generator_Background_Fill();
27 }
28
29 /**
30 * Registers the test thumb menu and page
31 */
32 function add_test_thumb_menu() {
33 //register the menu and page
34 foogallery_add_submenu_page( 'Thumbnail Generation Tests', 'manage_options', 'foogallery_thumb_test', array( $this, 'render_thumb_test_page' ) );
35
36 //hide the menu, but still keep the page registered so it can be rendered
37 remove_submenu_page( foogallery_admin_menu_parent_slug(), 'foogallery_thumb_test' );
38 }
39
40 /**
41 * renders a bunch of thumb tests
42 */
43 function render_thumb_test_page() {
44 global $foogallery_thumbnail_generation_test_errors;
45
46 echo '<h2>Thumbnail Test Page</h2>';
47
48 $this->render_thumb_test_html( FOOGALLERY_URL . 'includes/thumbs/default/tests/test3', 'PNG+No extension Resize to 30x30', 30, 30 );
49 $this->render_thumb_test_html( FOOGALLERY_URL . 'includes/thumbs/default/tests/test1.png', 'PNG Resize to 50x50' );
50 $this->render_thumb_test_html( FOOGALLERY_URL . 'includes/thumbs/default/tests/test2.png?test=1&another=true', 'PNG+Querystring Resize to 40x40', 40, 40 );
51
52 $this->render_thumb_test_html( FOOGALLERY_URL . 'includes/thumbs/default/tests/test4.gif', 'GIF Resize to 50x50' );
53 $this->render_thumb_test_html( FOOGALLERY_URL . 'includes/thumbs/default/tests/test5.jpg', 'JPG Resize to 50x50' );
54 $this->render_thumb_test_html( FOOGALLERY_URL . 'includes/thumbs/default/tests/test6.bmp', 'BMP Resize to 50x50' );
55
56 $this->render_thumb_test_html( 'https://assets.fooplugins.com/test.jpg', 'Remote Resize to 50x50' );
57 $this->render_thumb_test_html( FooGallery_Thumbnails::find_first_image_in_media_library(), 'Media Resize to 50x50' );
58
59 $this->render_thumb_test_html( 'https://fooplugins.s3.amazonaws.com/test.php', 'Remote test for non image', 50, 50, true );
60
61 echo '<h2>ERRORS</h2>';
62 echo '<textarea style="width: 100%;font-family: \'courier new\'; height: 500px;">';
63
64 if ( is_array( $foogallery_thumbnail_generation_test_errors) ) {
65 foreach ( $foogallery_thumbnail_generation_test_errors as $error ) {
66 echo esc_html( $error['title'] ) . '
67 =======================================
68 URL : ' . esc_url( $error['url'] ) . '
69 Error : ';
70 print_r( $error['error'] );
71 echo "\n";
72 echo "\n";
73 }
74
75 } else {
76 echo 'None :)';
77 }
78 echo '</textarea>';
79 }
80
81 /**
82 * Renders a single thumb test
83 *
84 * @param $url
85 * @param $title
86 * @param int $width
87 * @param int $height
88 */
89 function render_thumb_test_html( $url, $title, $width = 50, $height = 50, $expect_error = false ) {
90 global $foogallery_thumbnail_generation_test_errors;
91
92 if ( $url === false ) {
93 return;
94 }
95
96 $engine = foogallery_thumb_active_engine();
97
98 //always clear the cache for the file
99 $engine->clear_local_cache_for_file( $url );
100
101 $resize_url = $engine->generate( $url, array(
102 'width' => $width,
103 'height' => $height,
104 'crop' => true
105 ) );
106
107 echo '<h3>' . esc_html( $title ) . '</h3>';
108 echo 'original : <code>' . esc_url( $url ) . '</code><br />';
109 echo 'result : <code>' . esc_url( $resize_url ) . '</code><br /><br />';
110
111 if ( isset( $engine->last_error ) ) {
112 print_r( $engine->last_error );
113
114 if ( !$expect_error ) {
115 if ( !isset( $foogallery_thumbnail_generation_test_errors ) ) {
116 $foogallery_thumbnail_generation_test_errors = array();
117 }
118 $foogallery_thumbnail_generation_test_errors[] = array(
119 'title' => $title,
120 'url' => $url,
121 'error' => $engine->last_error
122 );
123 }
124
125 } else {
126 echo '<img src="' . esc_url( $url ) . '" alt="' . esc_attr__( 'Original image', 'foogallery' ) . '" />';
127 echo '&nbsp;&nbsp;&nbsp;→→→&nbsp;&nbsp;&nbsp;';
128 echo '<img src="' . esc_url( $resize_url ) . '" alt="' . esc_attr__( 'Generated thumbnail', 'foogallery' ) . '" />';
129 }
130
131
132
133 echo '<br />';
134 }
135
136 /**
137 * The default engine uses a local cache to store thumbnails
138 *
139 * @return bool
140 */
141 public function has_local_cache() {
142 return true;
143 }
144
145 /**
146 * Generates the thumbnail and returns the thumb URL
147 *
148 * @param $url
149 * @param array $args
150 *
151 * @return string|void
152 */
153 function generate( $url, $args = array() ) {
154 $generator = new FooGallery_Thumb_Generator( $url, $args );
155 $result = $generator->generate();
156 $this->last_error = $generator->error();
157 return $result;
158 }
159
160 /**
161 * Returns the last error that was encountered
162 * @return mixed
163 */
164 function get_last_error() {
165 return $this->last_error;
166 }
167
168 /**
169 * Delete the cache directory for a file
170 *
171 * @param $file
172 */
173 public function clear_local_cache_for_file( $file ) {
174 if ( empty( $file ) ) {
175 return;
176 }
177
178 $thumb = new FooGallery_Thumb_Generator( $file );
179 $directory = $thumb->get_cache_file_directory();
180
181 if ( false === $directory ) {
182 return;
183 }
184
185 //use the WP FileSystem to remove the folder recursively
186 $fs = foogallery_wp_filesystem();
187 if ( $fs !== false ) {
188 $fs->rmdir( $directory, true );
189 }
190 }
191
192 /**
193 * Hook into deleted_post and delete the associated cache file folder for an attachment
194 *
195 * @param string $post_id
196 *
197 * @return string
198 */
199 function delete_cache_folder_for_attachment( $post_id ) {
200 $url = wp_get_attachment_url( $post_id );
201
202 if ( $url !== false ) {
203 $this->clear_local_cache_for_file( $url );
204 }
205 }
206 }
207 }
208