Media File Renamer - Version 4.4.2

Version Description

Download this release

Release Info

Developer TigrouMeow
Plugin Icon 128x128 Media File Renamer
Version 4.4.2
Comparing to
See all releases

Code changes from version 4.4.0 to 4.4.2

Files changed (2) hide show
  1. core.php +38 -17
  2. mfrh_admin.php +114 -21
core.php CHANGED
@@ -156,23 +156,32 @@ SQL;
156
 
157
  // If everything's fine, renames in based on the Title in the EXIF
158
  $method = apply_filters( 'mfrh_method', 'media_title' );
159
- if ( $method == 'media_title' ) {
 
160
  $exif = wp_read_image_metadata( $file['tmp_name'] );
161
  if ( !empty( $exif ) && isset( $exif[ 'title' ] ) && !empty( $exif[ 'title' ] ) ) {
162
- $file['name'] = $this->new_filename( null, $exif[ 'title' ] ) . '.' . $pp['extension'];
163
- $this->log( "New file should be: " . $file['name'] );
 
 
 
164
  return $file;
165
  }
166
- }
167
- else if ( $method == 'post_title' && isset( $_POST['post_id'] ) && $_POST['post_id'] > 0 ) {
 
 
168
  $post = get_post( $_POST['post_id'] );
169
  if ( !empty( $post ) && !empty( $post->post_title ) ) {
170
- $file['name'] = $this->new_filename( null, $post->post_title ) . '.' . $pp['extension'];
171
- $this->log( "New file should be: " . $file['name'] );
 
 
 
172
  return $file;
173
  }
 
174
  }
175
-
176
  // Otherwise, let's do the basics based on the filename
177
 
178
  // The name will be modified at this point so let's keep it in a global
@@ -183,7 +192,8 @@ SQL;
183
 
184
  // Modify the filename
185
  $pp = mfrh_pathinfo( $file['name'] );
186
- $file['name'] = $this->new_filename( null, $pp['basename'] );
 
187
  return $file;
188
  }
189
 
@@ -238,25 +248,30 @@ SQL;
238
  delete_post_meta( $id, '_require_file_renaming' );
239
  return false;
240
  }
241
-
242
  // Get information
243
  $base_title = $post['post_title'];
244
- if ( $method == 'post_title' ) {
 
 
245
  $attachedpost = $this->get_post_from_media( $id );
246
  if ( is_null( $attachedpost ) )
247
  return false;
248
  $base_title = $attachedpost->post_title;
249
- }
250
- else if ( $method == 'alt_text' ) {
251
  $image_alt = get_post_meta( $id, '_wp_attachment_image_alt', true );
252
  if ( is_null( $image_alt ) )
253
  return false;
254
  $base_title = $image_alt;
 
255
  }
256
  $new_filename = $this->new_filename( $post, $base_title );
 
 
257
  //$this->log( "New title: $base_title, New filename: $new_filename" );
258
  }
259
-
260
  // If a filename has a counter, and the ideal is without the counter, let's ignore it
261
  $ideal = preg_replace( '/-[1-9]{1,10}\./', '$1.', $old_filename );
262
  if ( !$manual_filename ) {
@@ -510,7 +525,13 @@ SQL;
510
  return $str;
511
  }
512
 
513
- // NEW MEDIA FILE INFO (depending on the text/filename of the media)
 
 
 
 
 
 
514
  function new_filename( $media, $text, $manual_filename = null ) {
515
 
516
  $old_filename = null;
@@ -557,8 +578,8 @@ SQL;
557
  $new_filename = strtolower( $new_filename );
558
  }
559
 
560
- if ( empty( $new_filename ) )
561
- $new_filename = "empty";
562
 
563
  if ( !$manual_filename )
564
  $new_filename = apply_filters( 'mfrh_new_filename', $new_filename, $old_filename_no_ext, $media );
156
 
157
  // If everything's fine, renames in based on the Title in the EXIF
158
  $method = apply_filters( 'mfrh_method', 'media_title' );
159
+ switch ( $method ) {
160
+ case 'media_title':
161
  $exif = wp_read_image_metadata( $file['tmp_name'] );
162
  if ( !empty( $exif ) && isset( $exif[ 'title' ] ) && !empty( $exif[ 'title' ] ) ) {
163
+ $new_filename = $this->new_filename( null, $exif[ 'title' ] );
164
+ if ( !is_null( $new_filename ) ) {
165
+ $file['name'] = "{$new_filename}.{$pp['extension']}";
166
+ $this->log( "New file should be: " . $file['name'] );
167
+ }
168
  return $file;
169
  }
170
+ break;
171
+ case 'post_title':
172
+ case 'product_title':
173
+ if ( !isset( $_POST['post_id'] ) || $_POST['post_id'] < 1 ) break;
174
  $post = get_post( $_POST['post_id'] );
175
  if ( !empty( $post ) && !empty( $post->post_title ) ) {
176
+ $new_filename = $this->new_filename( null, $post->post_title );
177
+ if ( !is_null( $new_filename ) ) {
178
+ $file['name'] = "{$new_filename}.{$pp['extension']}";
179
+ $this->log( "New file should be: " . $file['name'] );
180
+ }
181
  return $file;
182
  }
183
+ break;
184
  }
 
185
  // Otherwise, let's do the basics based on the filename
186
 
187
  // The name will be modified at this point so let's keep it in a global
192
 
193
  // Modify the filename
194
  $pp = mfrh_pathinfo( $file['name'] );
195
+ $new_filename = $this->new_filename( null, $pp['basename'] );
196
+ if ( !is_null( $new_filename ) ) $file['name'] = $new_filename;
197
  return $file;
198
  }
199
 
248
  delete_post_meta( $id, '_require_file_renaming' );
249
  return false;
250
  }
251
+
252
  // Get information
253
  $base_title = $post['post_title'];
254
+ switch ( $method ) {
255
+ case 'post_title':
256
+ case 'product_title':
257
  $attachedpost = $this->get_post_from_media( $id );
258
  if ( is_null( $attachedpost ) )
259
  return false;
260
  $base_title = $attachedpost->post_title;
261
+ break;
262
+ case 'alt_text':
263
  $image_alt = get_post_meta( $id, '_wp_attachment_image_alt', true );
264
  if ( is_null( $image_alt ) )
265
  return false;
266
  $base_title = $image_alt;
267
+ break;
268
  }
269
  $new_filename = $this->new_filename( $post, $base_title );
270
+ if ( is_null( $new_filename ) ) return false; // Leave it as it is
271
+
272
  //$this->log( "New title: $base_title, New filename: $new_filename" );
273
  }
274
+
275
  // If a filename has a counter, and the ideal is without the counter, let's ignore it
276
  $ideal = preg_replace( '/-[1-9]{1,10}\./', '$1.', $old_filename );
277
  if ( !$manual_filename ) {
525
  return $str;
526
  }
527
 
528
+ /**
529
+ * Computes the ideal filename based on a text
530
+ * @param array $media
531
+ * @param string $text
532
+ * @param string $manual_filename
533
+ * @return string|NULL If the resulting filename had no any valid characters, NULL is returned
534
+ */
535
  function new_filename( $media, $text, $manual_filename = null ) {
536
 
537
  $old_filename = null;
578
  $new_filename = strtolower( $new_filename );
579
  }
580
 
581
+ // If the resulting filename had no any valid character, return NULL
582
+ if ( empty( $new_filename ) ) return null;
583
 
584
  if ( !$manual_filename )
585
  $new_filename = apply_filters( 'mfrh_new_filename', $new_filename, $old_filename_no_ext, $media );
mfrh_admin.php CHANGED
@@ -114,12 +114,12 @@ class Meow_MFRH_Admin extends MeowApps_Admin {
114
  array( $this, 'admin_numbered_files_callback' ),
115
  'mfrh_advanced_settings-menu', 'mfrh_advanced_settings' );
116
 
117
- if ( $method == 'media_title' || $method == 'post_title' ) {
118
  add_settings_field( 'mfrh_sync_alt', "Sync ALT<br />(Pro)",
119
  array( $this, 'admin_sync_alt_callback' ),
120
  'mfrh_advanced_settings-menu', 'mfrh_advanced_settings' );
121
  }
122
- if ( $method == 'post_title' || $method == 'alt_text' ) {
123
  add_settings_field( 'mfrh_sync_media_title', "Sync Media Title<br />(Pro)",
124
  array( $this, 'admin_sync_media_title_callback' ),
125
  'mfrh_advanced_settings-menu', 'mfrh_advanced_settings' );
@@ -294,12 +294,22 @@ class Meow_MFRH_Admin extends MeowApps_Admin {
294
  function admin_sync_alt_callback( $args ) {
295
  $html = '<input ' . disabled( $this->is_registered(), false, false ) . ' type="checkbox" id="mfrh_sync_alt" name="mfrh_sync_alt" value="1" ' .
296
  checked( 1, get_option( 'mfrh_sync_alt' ), false ) . '/>';
 
 
297
  $method = apply_filters( 'mfrh_method', 'media_title' );
298
- $what = __( "Error!", 'media-file-renamer' );
299
- if ( $method == "media_title" )
300
  $what = __( "Title of Media", 'media-file-renamer' );
301
- else if ( $method == "post_title" )
 
302
  $what = __( "Attached Post Title", 'media-file-renamer' );
 
 
 
 
 
 
 
303
  $label = sprintf(
304
  /* Translators: %1$s: update target name, %2$s: update resourse name */
305
  esc_html__( 'Update %1$s with %2$s', 'media-file-renamer' ),
@@ -314,12 +324,19 @@ class Meow_MFRH_Admin extends MeowApps_Admin {
314
  function admin_sync_media_title_callback( $args ) {
315
  $html = '<input ' . disabled( $this->is_registered(), false, false ) . ' type="checkbox" id="mfrh_sync_media_title" name="mfrh_sync_media_title" value="1" ' .
316
  checked( 1, get_option( 'mfrh_sync_media_title' ), false ) . '/>';
317
- $method = apply_filters( 'mfrh_method', 'media_title' );
318
  $what = __( "Error!", 'media-file-renamer' );
319
- if ( $method == "alt_text" )
 
 
320
  $what = __( "Media ALT", 'media-file-renamer' );
321
- else if ( $method == "post_title" )
 
322
  $what = __( "Attached Post Title", 'media-file-renamer' );
 
 
 
 
 
323
  $label = sprintf(
324
  /* Translators: %1$s: update target name, %2$s: update resourse name */
325
  esc_html__( 'Update %1$s with %2$s', 'media-file-renamer' ),
@@ -340,19 +357,54 @@ class Meow_MFRH_Admin extends MeowApps_Admin {
340
  }
341
 
342
  function admin_auto_rename_callback( $args ) {
343
- $value = apply_filters( 'mfrh_method', 'media_title' );
344
- $html = '<label><select id="mfrh_auto_rename" name="mfrh_auto_rename">
345
- <option ' . selected( 'media_title', $value, false ) . 'value="media_title">Title of Media</option>
346
- <option ' .
347
- disabled( $this->is_registered(), false, false ) . ' ' .
348
- selected( 'post_title', $value, false ) . 'value="post_title">Attached Post Title (Pro)</option>
349
- <option ' .
350
- disabled( $this->is_registered(), false, false ) . ' ' .
351
- selected( 'alt_text', $value, false ) . 'value="alt_text">Alternative Text (Pro)</option>
352
- <option ' . selected( 'none', $value, false ) . 'value="none">None</option>
353
- </select></label><small><br />' . __( 'If the plugin considers that it is too dangerous to rename the file directly at some point, it will be flagged internally <b>as to be renamed</b>. The list of those flagged files can be found in Media > File Renamer and they can be renamed from there.', 'media-file-renamer' ) . '</small>';
354
- echo $html;
355
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
 
357
  function admin_on_upload_callback( $args ) {
358
  $html = '<input type="checkbox" id="mfrh_on_upload" name="mfrh_on_upload" value="1" ' .
@@ -458,6 +510,47 @@ class Meow_MFRH_Admin extends MeowApps_Admin {
458
  return $default;
459
  }
460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
461
  }
462
 
463
  ?>
114
  array( $this, 'admin_numbered_files_callback' ),
115
  'mfrh_advanced_settings-menu', 'mfrh_advanced_settings' );
116
 
117
+ if ( $method == 'media_title' || $method == 'post_title' || $method == 'product_title' ) {
118
  add_settings_field( 'mfrh_sync_alt', "Sync ALT<br />(Pro)",
119
  array( $this, 'admin_sync_alt_callback' ),
120
  'mfrh_advanced_settings-menu', 'mfrh_advanced_settings' );
121
  }
122
+ if ( $method == 'post_title' || $method == 'product_title' || $method == 'alt_text' ) {
123
  add_settings_field( 'mfrh_sync_media_title', "Sync Media Title<br />(Pro)",
124
  array( $this, 'admin_sync_media_title_callback' ),
125
  'mfrh_advanced_settings-menu', 'mfrh_advanced_settings' );
294
  function admin_sync_alt_callback( $args ) {
295
  $html = '<input ' . disabled( $this->is_registered(), false, false ) . ' type="checkbox" id="mfrh_sync_alt" name="mfrh_sync_alt" value="1" ' .
296
  checked( 1, get_option( 'mfrh_sync_alt' ), false ) . '/>';
297
+
298
+ $what = '';
299
  $method = apply_filters( 'mfrh_method', 'media_title' );
300
+ switch ( $method ) {
301
+ case 'media_title':
302
  $what = __( "Title of Media", 'media-file-renamer' );
303
+ break;
304
+ case 'post_title':
305
  $what = __( "Attached Post Title", 'media-file-renamer' );
306
+ break;
307
+ case 'product_title':
308
+ $what = __( "Title of Product", 'media-file-renamer' );
309
+ break;
310
+ default:
311
+ $what = __( "Error!", 'media-file-renamer' );
312
+ }
313
  $label = sprintf(
314
  /* Translators: %1$s: update target name, %2$s: update resourse name */
315
  esc_html__( 'Update %1$s with %2$s', 'media-file-renamer' ),
324
  function admin_sync_media_title_callback( $args ) {
325
  $html = '<input ' . disabled( $this->is_registered(), false, false ) . ' type="checkbox" id="mfrh_sync_media_title" name="mfrh_sync_media_title" value="1" ' .
326
  checked( 1, get_option( 'mfrh_sync_media_title' ), false ) . '/>';
 
327
  $what = __( "Error!", 'media-file-renamer' );
328
+ $method = apply_filters( 'mfrh_method', 'media_title' );
329
+ switch ( $method ) {
330
+ case 'alt_text':
331
  $what = __( "Media ALT", 'media-file-renamer' );
332
+ break;
333
+ case 'post_title':
334
  $what = __( "Attached Post Title", 'media-file-renamer' );
335
+ break;
336
+ case 'product_title':
337
+ $what = __( "Title of Product", 'media-file-renamer' );
338
+ break;
339
+ }
340
  $label = sprintf(
341
  /* Translators: %1$s: update target name, %2$s: update resourse name */
342
  esc_html__( 'Update %1$s with %2$s', 'media-file-renamer' ),
357
  }
358
 
359
  function admin_auto_rename_callback( $args ) {
360
+ $r = '';
361
+ $value = apply_filters( 'mfrh_method', 'media_title' );
362
+
363
+ // Available options
364
+ $options = array (
365
+ 0 => array (
366
+ 'value' => 'media_title',
367
+ 'label' => 'Title of Media'
368
+ ),
369
+ 10 => array (
370
+ 'value' => 'post_title',
371
+ 'label' => 'Attached Post Title (Pro)',
372
+ 'disabled' => !$this->is_registered()
373
+ ),
374
+ 20 => array (
375
+ 'value' => 'alt_text',
376
+ 'label' => 'Alternative Text (Pro)',
377
+ 'disabled' => !$this->is_registered()
378
+ ),
379
+ 100 => array (
380
+ 'value' => 'none',
381
+ 'label' => 'None'
382
+ )
383
+ );
384
+ //// WooCommerce suppport
385
+ $x = is_plugin_active( 'woocommerce/woocommerce.php' );
386
+ $options[1] = array (
387
+ 'value' => 'product_title',
388
+ 'label' => 'Title of Product (Pro)',
389
+ 'disabled' => !$x || !$this->is_registered(),
390
+ 'hidden' => !$x
391
+ );
392
+ // Convert the options to HTML
393
+ ksort( $options );
394
+ foreach ( $options as $option ) {
395
+ $option['selected'] = $value == $option['value'];
396
+ $r .= $this->elm( 'option', $option, $option['label'] );
397
+ }
398
+ // Wrap with <select>
399
+ $r = $this->elm( 'select', array (
400
+ 'id' => 'mfrh_auto_rename',
401
+ 'name' => 'mfrh_auto_rename'
402
+ ), $r );
403
+ // Add a note
404
+ $r = "<label>{$r}</label>" . '<small><br />' . __( 'If the plugin considers that it is too dangerous to rename the file directly at some point, it will be flagged internally <b>as to be renamed</b>. The list of those flagged files can be found in Media > File Renamer and they can be renamed from there.', 'media-file-renamer' ) . '</small>';
405
+
406
+ echo $r;
407
+ }
408
 
409
  function admin_on_upload_callback( $args ) {
410
  $html = '<input type="checkbox" id="mfrh_on_upload" name="mfrh_on_upload" value="1" ' .
510
  return $default;
511
  }
512
 
513
+ /**
514
+ * TODO: Move to the common library
515
+ * Composes HTML expression for a single element
516
+ * @param string $tag Tag name
517
+ * @param array|string $attrs Attributes
518
+ * @param string $content='' Content. Null omits the closing tag
519
+ * @return string HTML expression for an element
520
+ */
521
+ public function elm( $tag, $attrs = null, $content = '' ) {
522
+ $r = "<{$tag}";
523
+ if ( $attrs ) $r .= is_string( $attrs ) ? " {$attrs}" : $this->attrs( $attrs );
524
+ $r .= '>';
525
+ if ( is_null( $content ) ) return $r;
526
+ return "{$r}{$content}</{$tag}>";
527
+ }
528
+
529
+ /**
530
+ * TODO: Move to the common library
531
+ * Converts an associative array to HTML attributes
532
+ * @param array $map An associative array
533
+ * @return string HTML expression for attributes
534
+ */
535
+ public function attrs( $map ) {
536
+ $r = '';
537
+ foreach ( $map as $attr => $value ) $r .= $this->attr( $attr, $value );
538
+ return $r;
539
+ }
540
+
541
+ /**
542
+ * TODO: Move to the common library
543
+ * Composes HTML expression for a single attribute.
544
+ * If the value was exact FALSE, returns empty string
545
+ * @param string $name
546
+ * @param mixed $value
547
+ * @return string HTML expression for an attribute
548
+ */
549
+ public function attr( $name, $value ) {
550
+ if ( is_null( $value ) ) return '';
551
+ if ( is_bool( $value ) ) return $value ? " {$name}" : '';
552
+ return " {$name}=\"" . ( esc_attr( (string) $value ) ) . '"';
553
+ }
554
  }
555
 
556
  ?>