Attachments - Version 3.5.5

Version Description

  • Fixed an issue where field values were improperly overwritten when the instance was set to prepend in some cases
Download this release

Release Info

Developer jchristopher
Plugin Icon wp plugin Attachments
Version 3.5.5
Comparing to
See all releases

Code changes from version 3.5.4 to 3.5.5

classes/class.attachments.legacy.php CHANGED
@@ -1,27 +1,26 @@
1
  <?php
2
 
3
  // exit if accessed directly
4
- if( !defined( 'ABSPATH' ) ) exit;
 
 
5
 
6
  // exit if we can't extend Attachments
7
- if ( !class_exists( 'Attachments' ) ) return;
8
-
9
-
10
-
11
 
12
  /**
13
  * Check for and handle legacy data appropriately
14
  *
15
  * @since 3.4.1
16
  */
17
- class AttachmentsLegacyHandler
18
- {
19
 
20
  public $legacy = false; // whether or not there is legacy Attachments data
21
  public $legacy_pro = false; // whether or not there is legacy Attachment Pro data
22
 
23
- function __construct()
24
- {
25
  // deal with our legacy issues if the user hasn't dismissed or migrated already
26
  $this->check_for_legacy_data();
27
 
@@ -38,30 +37,29 @@ class AttachmentsLegacyHandler
38
  *
39
  * @since 3.1.3
40
  */
41
- function check_for_legacy_data()
42
- {
43
  // we'll get a warning issued if fired when Network Activated
44
  // since it's supremely unlikely we'd have legacy data at this point, we're going to short circuit
45
- if( is_multisite() )
46
- {
47
  $plugins = get_site_option( 'active_sitewide_plugins' );
48
- if( isset( $plugins['attachments/index.php'] ) )
49
  return;
 
50
  }
51
 
52
  // deal with our legacy issues if the user hasn't dismissed or migrated already
53
- if( false == get_option( 'attachments_migrated' ) && false == get_option( 'attachments_ignore_migration' ) )
54
- {
55
  $legacy_attachments_settings = get_option( 'attachments_settings' );
56
 
57
- if( $legacy_attachments_settings && is_array( $legacy_attachments_settings['post_types'] ) && count( $legacy_attachments_settings['post_types'] ) )
58
- {
59
  // we have legacy settings, so we're going to use the post types that Attachments is currently utilizing
60
 
61
  // the keys are the actual CPT names, so we need those
62
- foreach( $legacy_attachments_settings['post_types'] as $post_type => $value )
63
- if( $value )
64
  $post_types[] = $post_type;
 
 
65
 
66
  // set up our WP_Query args to grab anything with legacy data
67
  $args = array(
@@ -78,13 +76,12 @@ class AttachmentsLegacyHandler
78
  }
79
 
80
  // deal with our legacy Pro issues if the user hasn't dismissed or migrated already
81
- if( false == get_option( 'attachments_pro_migrated' ) && false == get_option( 'attachments_pro_ignore_migration' ) )
82
- {
83
  $post_types = get_post_types();
84
 
85
  // set up our WP_Query args to grab anything (really anything) with legacy data
86
  $args = array(
87
- 'post_type' => !empty( $post_types ) ? $post_types : array( 'post', 'page' ),
88
  'post_status' => 'any',
89
  'posts_per_page' => 1,
90
  'meta_key' => '_attachments_pro',
@@ -103,10 +100,8 @@ class AttachmentsLegacyHandler
103
  *
104
  * @since 3.0
105
  */
106
- function admin_notice()
107
- {
108
-
109
- if( $this->has_outstanding_legacy_data() && ( isset( $_GET['page'] ) && $_GET['page'] !== 'attachments' || !isset( $_GET['page'] ) ) ) : ?>
110
  <div class="message updated" id="message">
111
  <p><?php _e( '<strong>Attachments has detected legacy Attachments data.</strong> A lot has changed since Attachments 1.x.' ,'attachments' ); ?> <a href="options-general.php?page=attachments&amp;overview=1"><?php _e( 'Find out more', 'attachments' ); ?>.</a></p>
112
  </div>
@@ -120,22 +115,18 @@ class AttachmentsLegacyHandler
120
  *
121
  * @since 3.0
122
  */
123
- function has_outstanding_legacy_data()
124
- {
125
  if(
126
  // migration has not taken place and we have legacy data
127
- ( false == get_option( 'attachments_migrated' ) && !empty( $this->legacy ) )
128
 
129
  &&
130
 
131
  // we're not intentionally ignoring the message
132
  ( false == get_option( 'attachments_ignore_migration' ) )
133
- )
134
- {
135
  return true;
136
- }
137
- else
138
- {
139
  return false;
140
  }
141
  }
@@ -147,8 +138,7 @@ class AttachmentsLegacyHandler
147
  *
148
  * @since 3.0
149
  */
150
- function admin_pointer( $hook_suffix )
151
- {
152
 
153
  // Assume pointer shouldn't be shown
154
  $enqueue_pointer_script_style = false;
@@ -178,8 +168,7 @@ class AttachmentsLegacyHandler
178
  *
179
  * @since 3.0
180
  */
181
- function pointer_legacy()
182
- {
183
  $pointer_content = "<h3>". __( esc_html( 'Attachments 3.0 brings big changes!' ), 'attachments' ) ."</h3>";
184
  $pointer_content .= "<p>". __( esc_html( 'It is very important that you take a few minutes to see what has been updated. The changes will affect your themes/plugins.' ), 'attachments' ) ."</p>";
185
  ?>
1
  <?php
2
 
3
  // exit if accessed directly
4
+ if ( ! defined( 'ABSPATH' ) ) {
5
+ exit;
6
+ }
7
 
8
  // exit if we can't extend Attachments
9
+ if ( ! class_exists( 'Attachments' ) ) {
10
+ return;
11
+ }
 
12
 
13
  /**
14
  * Check for and handle legacy data appropriately
15
  *
16
  * @since 3.4.1
17
  */
18
+ class AttachmentsLegacyHandler {
 
19
 
20
  public $legacy = false; // whether or not there is legacy Attachments data
21
  public $legacy_pro = false; // whether or not there is legacy Attachment Pro data
22
 
23
+ function __construct() {
 
24
  // deal with our legacy issues if the user hasn't dismissed or migrated already
25
  $this->check_for_legacy_data();
26
 
37
  *
38
  * @since 3.1.3
39
  */
40
+ function check_for_legacy_data() {
 
41
  // we'll get a warning issued if fired when Network Activated
42
  // since it's supremely unlikely we'd have legacy data at this point, we're going to short circuit
43
+ if( is_multisite() ) {
 
44
  $plugins = get_site_option( 'active_sitewide_plugins' );
45
+ if ( isset( $plugins['attachments/index.php'] ) ) {
46
  return;
47
+ }
48
  }
49
 
50
  // deal with our legacy issues if the user hasn't dismissed or migrated already
51
+ if ( false == get_option( 'attachments_migrated' ) && false == get_option( 'attachments_ignore_migration' ) ) {
 
52
  $legacy_attachments_settings = get_option( 'attachments_settings' );
53
 
54
+ if ( $legacy_attachments_settings && is_array( $legacy_attachments_settings['post_types'] ) && count( $legacy_attachments_settings['post_types'] ) ) {
 
55
  // we have legacy settings, so we're going to use the post types that Attachments is currently utilizing
56
 
57
  // the keys are the actual CPT names, so we need those
58
+ foreach ( $legacy_attachments_settings['post_types'] as $post_type => $value ) {
59
+ if ( $value ) {
60
  $post_types[] = $post_type;
61
+ }
62
+ }
63
 
64
  // set up our WP_Query args to grab anything with legacy data
65
  $args = array(
76
  }
77
 
78
  // deal with our legacy Pro issues if the user hasn't dismissed or migrated already
79
+ if ( false == get_option( 'attachments_pro_migrated' ) && false == get_option( 'attachments_pro_ignore_migration' ) ) {
 
80
  $post_types = get_post_types();
81
 
82
  // set up our WP_Query args to grab anything (really anything) with legacy data
83
  $args = array(
84
+ 'post_type' => ! empty( $post_types ) ? $post_types : array( 'post', 'page' ),
85
  'post_status' => 'any',
86
  'posts_per_page' => 1,
87
  'meta_key' => '_attachments_pro',
100
  *
101
  * @since 3.0
102
  */
103
+ function admin_notice() {
104
+ if ( $this->has_outstanding_legacy_data() && ( isset( $_GET['page'] ) && $_GET['page'] !== 'attachments' || ! isset( $_GET['page'] ) ) ) : ?>
 
 
105
  <div class="message updated" id="message">
106
  <p><?php _e( '<strong>Attachments has detected legacy Attachments data.</strong> A lot has changed since Attachments 1.x.' ,'attachments' ); ?> <a href="options-general.php?page=attachments&amp;overview=1"><?php _e( 'Find out more', 'attachments' ); ?>.</a></p>
107
  </div>
115
  *
116
  * @since 3.0
117
  */
118
+ function has_outstanding_legacy_data() {
 
119
  if(
120
  // migration has not taken place and we have legacy data
121
+ ( false == get_option( 'attachments_migrated' ) && ! empty( $this->legacy ) )
122
 
123
  &&
124
 
125
  // we're not intentionally ignoring the message
126
  ( false == get_option( 'attachments_ignore_migration' ) )
127
+ ) {
 
128
  return true;
129
+ } else {
 
 
130
  return false;
131
  }
132
  }
138
  *
139
  * @since 3.0
140
  */
141
+ function admin_pointer( $hook_suffix ) {
 
142
 
143
  // Assume pointer shouldn't be shown
144
  $enqueue_pointer_script_style = false;
168
  *
169
  * @since 3.0
170
  */
171
+ function pointer_legacy() {
 
172
  $pointer_content = "<h3>". __( esc_html( 'Attachments 3.0 brings big changes!' ), 'attachments' ) ."</h3>";
173
  $pointer_content .= "<p>". __( esc_html( 'It is very important that you take a few minutes to see what has been updated. The changes will affect your themes/plugins.' ), 'attachments' ) ."</p>";
174
  ?>
classes/class.attachments.migrate.php CHANGED
@@ -1,18 +1,18 @@
1
  <?php
2
 
3
  // Exit if accessed directly
4
- if( !defined( 'ABSPATH' ) ) exit;
 
 
5
 
6
  /**
7
  * Migration class for legacy Attachments data
8
  *
9
  * @since 3.1.3
10
  */
11
- class AttachmentsMigrate extends Attachments
12
- {
13
 
14
- function __construct()
15
- {
16
  parent::__construct();
17
  }
18
 
@@ -21,11 +21,11 @@ class AttachmentsMigrate extends Attachments
21
  *
22
  * @since 3.0
23
  */
24
- function migrate( $instance = null, $title = null, $caption = null )
25
- {
26
  // sanitize
27
- if( is_null( $instance ) || empty( $instance ) || is_null( $title ) || is_null( $caption ) )
28
  return false;
 
29
 
30
  $instance = str_replace( '-', '_', sanitize_title( $instance ) );
31
  $title = empty( $title ) ? false : str_replace( '-', '_', sanitize_title( $title ) );
@@ -38,15 +38,16 @@ class AttachmentsMigrate extends Attachments
38
 
39
  $query = false;
40
 
41
- if( $legacy_attachments_settings && is_array( $legacy_attachments_settings['post_types'] ) && count( $legacy_attachments_settings['post_types'] ) )
42
- {
43
  // we have legacy settings, so we're going to use the post types
44
  // that Attachments is currently utilizing
45
 
46
  // the keys are the actual CPT names, so we need those
47
- foreach( $legacy_attachments_settings['post_types'] as $post_type => $value )
48
- if( $value )
49
  $post_types[] = $post_type;
 
 
50
 
51
  // set up our WP_Query args to grab anything with legacy data
52
  $args = array(
@@ -63,94 +64,96 @@ class AttachmentsMigrate extends Attachments
63
  $count = 0;
64
 
65
  // loop through each post
66
- if( $query ) { while( $query->have_posts() )
67
- {
68
- // set up postdata
69
- $query->the_post();
70
 
71
- // let's first decode our Attachments data
72
- $existing_attachments = get_post_meta( $query->post->ID, '_attachments', false );
73
 
74
- $post_attachments = array();
 
75
 
76
- // check to make sure we've got data
77
- if( is_array( $existing_attachments ) && count( $existing_attachments ) > 0 )
78
- {
79
- // loop through each existing attachment
80
- foreach( $existing_attachments as $attachment )
81
- {
82
- // decode and unserialize the data
83
- $data = unserialize( base64_decode( $attachment ) );
84
-
85
- array_push( $post_attachments, array(
86
- 'id' => stripslashes( $data['id'] ),
87
- 'title' => stripslashes( $data['title'] ),
88
- 'caption' => stripslashes( $data['caption'] ),
89
- 'order' => stripslashes( $data['order'] )
90
- ));
91
- }
92
 
93
- // sort attachments
94
- if( count( $post_attachments ) > 1 )
95
- {
96
- usort( $post_attachments, 'attachments_cmp' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  }
98
- }
99
 
100
- // we have our Attachments entries
101
 
102
- // let's check to see if we're migrating after population has taken place
103
- $existing_attachments = get_post_meta( $query->post->ID, $this->get_meta_key(), false );
104
 
105
- if( !isset( $existing_attachments[0] ) )
106
- $existing_attachments[0] = '';
 
107
 
108
- $existing_attachments = json_decode( $existing_attachments[0] );
109
 
110
- if( !is_object( $existing_attachments ) )
111
- $existing_attachments = new stdClass();
 
112
 
113
- // we'll loop through the legacy Attachments and save them in the new format
114
- foreach( $post_attachments as $legacy_attachment )
115
- {
116
- // convert to the new format
117
- $converted_attachment = array( 'id' => $legacy_attachment['id'] );
118
 
119
- // fields are technically optional so we'll add those separately
120
- // we're also going to encode them in the same way the main class does
121
- if( $title )
122
- $converted_attachment['fields'][$title] = htmlentities( stripslashes( $legacy_attachment['title'] ), ENT_QUOTES, 'UTF-8' );
 
123
 
124
- if( $caption )
125
- $converted_attachment['fields'][$caption] = htmlentities( stripslashes( $legacy_attachment['caption'] ), ENT_QUOTES, 'UTF-8' );
 
126
 
127
- // check to see if the existing Attachments have our target instance
128
- if( !isset( $existing_attachments->$instance ) )
129
- {
130
- // the instance doesn't exist so we need to create it
131
- $existing_attachments->$instance = array();
132
- }
133
 
134
- // we need to convert our array to an object
135
- $converted_attachment['fields'] = (object) $converted_attachment['fields'];
136
- $converted_attachment = (object) $converted_attachment;
137
 
138
- // append this legacy attachment to the existing instance
139
- array_push( $existing_attachments->$instance, $converted_attachment );
140
- }
141
 
142
- // we're done! let's save everything in our new format
143
- $existing_attachments = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $existing_attachments, JSON_UNESCAPED_UNICODE ) : json_encode( $existing_attachments );
144
 
145
- // fix potentially encoded Unicode
146
- $existing_attachments = str_replace( '\\', '\\\\', $existing_attachments );
147
 
148
- // save it to the database
149
- update_post_meta( $query->post->ID, 'attachments', $existing_attachments );
150
 
151
- // increment our counter
152
- $count++;
153
- } }
 
154
 
155
  return $count;
156
  }
@@ -162,10 +165,10 @@ class AttachmentsMigrate extends Attachments
162
  *
163
  * @since 3.2
164
  */
165
- function prepare_migration()
166
- {
167
- if( !wp_verify_nonce( $_GET['nonce'], 'attachments-migrate-1') ) wp_die( __( 'Invalid request', 'attachments' ) );
168
- ?>
169
  <h3><?php _e( 'Migration Step 1', 'attachments' ); ?></h3>
170
  <p><?php _e( "In order to migrate Attachments 1.x data, you need to set which instance and fields in version 3.0+ you'd like to use:", 'attachments' ); ?></p>
171
  <form action="options-general.php" method="get">
@@ -217,21 +220,20 @@ class AttachmentsMigrate extends Attachments
217
  *
218
  * @since 3.2
219
  */
220
- function init_migration()
221
- {
222
- if( !wp_verify_nonce( $_GET['nonce'], 'attachments-migrate-2') )
223
  wp_die( __( 'Invalid request', 'attachments' ) );
 
224
 
225
  $total = $this->migrate( $_GET['attachments-instance'], $_GET['attachments-title'], $_GET['attachments-caption'] );
226
 
227
- if( false == get_option( 'attachments_migrated' ) ) :
228
- ?>
229
  <h3><?php _e( 'Migration Complete!', 'attachments' ); ?></h3>
230
  <p><?php _e( 'The migration has completed.', 'attachments' ); ?> <strong><?php _e( 'Migrated', 'attachments'); ?>: <?php echo $total; ?></strong>.</p>
231
- <?php else : ?>
232
  <h3><?php _e( 'Migration has already Run!', 'attachments' ); ?></h3>
233
  <p><?php _e( 'The migration has already been run. The migration process has not been repeated.', 'attachments' ); ?></p>
234
- <?php endif;
235
 
236
  // make sure the database knows the migration has run
237
  add_option( 'attachments_migrated', true, '', 'no' );
@@ -244,10 +246,10 @@ class AttachmentsMigrate extends Attachments
244
  *
245
  * @since 3.5
246
  */
247
- function prepare_pro_migration()
248
- {
249
- if( !wp_verify_nonce( $_GET['nonce'], 'attachments-pro-migrate-1') ) wp_die( __( 'Invalid request', 'attachments' ) );
250
- ?>
251
  <h3><?php _e( 'Migration Step 1', 'attachments' ); ?></h3>
252
  <form action="options-general.php" method="get">
253
  <input type="hidden" name="page" value="attachments" />
@@ -262,11 +264,11 @@ class AttachmentsMigrate extends Attachments
262
  $attachments_pro_settings = get_option( '_iti_apro_settings' );
263
  ?>
264
 
265
- <?php if( is_array( $attachments_pro_settings['positions'] ) ) : ?>
266
  <p><?php _e( 'The following Attachments Pro Instances will be migrated:', 'attachments' ); ?></p>
267
  <ul style="padding-left:32px;list-style:disc;">
268
  <?php foreach( $attachments_pro_settings['positions'] as $attachments_pro_instance ) : ?>
269
- <li><?php echo $attachments_pro_instance['label']; ?></li>
270
  <?php endforeach; ?>
271
  </ul>
272
  <h2><?php _e( 'Note: this is a multi-step process', 'attachments' ); ?></h2>
@@ -288,30 +290,32 @@ class AttachmentsMigrate extends Attachments
288
  *
289
  * @since 3.5
290
  */
291
- function init_pro_migration()
292
- {
293
  global $current_user;
294
 
295
  get_currentuserinfo();
296
 
297
- if( !wp_verify_nonce( $_GET['nonce'], 'attachments-pro-migrate-2') )
298
  wp_die( __( 'Invalid request', 'attachments' ) );
 
299
 
300
  $attachments_pro_settings = get_option( '_iti_apro_settings' );
301
- if( is_array( $attachments_pro_settings['positions'] ) )
302
- {
303
  $totals = array();
304
 
305
- foreach( $attachments_pro_settings['positions'] as $attachments_pro_instance )
306
  $totals[] = $this->migrate_pro( $attachments_pro_instance );
 
307
 
308
  $total_attachments = 0;
309
 
310
- if( !empty( $totals ) )
311
- foreach( $totals as $instance_total )
312
  $total_attachments += $instance_total['total'];
 
 
313
 
314
- if( false == get_option( 'attachments_pro_migrated' ) ) :
315
  ?>
316
  <h3><?php _e( 'Data conversion complete!', 'attachments' ); ?></h3>
317
  <p><?php _e( 'The data conversion has been completed successfully.', 'attachments' ); ?> <strong><?php _e( 'Converted', 'attachments'); ?>: <?php echo $total_attachments; ?> <?php echo ( $total_attachments == 1 ) ? __( 'Attachment', 'attachments' ) : __( 'Attachments', 'attachments' ); ?></strong></p>
@@ -326,15 +330,15 @@ class AttachmentsMigrate extends Attachments
326
 
327
  array(
328
  'name' => '<?php echo str_replace( '-', '_', sanitize_title( $field['label'] ) ); ?>',
329
- 'type' => '<?php echo $field['type']; ?>',
330
- 'label' => '<?php echo $field['label']; ?>',
331
- 'default' => '<?php echo isset( $field['mapped_to'] ) ? $field['mapped_to'] : ''; ?>',
332
  ),<?php endforeach; echo "\n"; endif; ?>
333
  );
334
  <?php
335
  $post_types = array();
336
- if( isset( $attachments_pro_instance['conditions'] ) && is_array( $attachments_pro_instance['conditions'] ) && !empty( $attachments_pro_instance['conditions'] ) )
337
- foreach( $attachments_pro_instance['conditions'] as $condition )
338
  if( $condition['param'] == 'post_type' && $condition['operator'] == 'is' )
339
  $post_types[] = $condition['limiter'];
340
  ?>
1
  <?php
2
 
3
  // Exit if accessed directly
4
+ if ( ! defined( 'ABSPATH' ) ) {
5
+ exit;
6
+ }
7
 
8
  /**
9
  * Migration class for legacy Attachments data
10
  *
11
  * @since 3.1.3
12
  */
13
+ class AttachmentsMigrate extends Attachments {
 
14
 
15
+ function __construct() {
 
16
  parent::__construct();
17
  }
18
 
21
  *
22
  * @since 3.0
23
  */
24
+ function migrate( $instance = null, $title = null, $caption = null ) {
 
25
  // sanitize
26
+ if( is_null( $instance ) || empty( $instance ) || is_null( $title ) || is_null( $caption ) ) {
27
  return false;
28
+ }
29
 
30
  $instance = str_replace( '-', '_', sanitize_title( $instance ) );
31
  $title = empty( $title ) ? false : str_replace( '-', '_', sanitize_title( $title ) );
38
 
39
  $query = false;
40
 
41
+ if ( $legacy_attachments_settings && is_array( $legacy_attachments_settings['post_types'] ) && count( $legacy_attachments_settings['post_types'] ) ) {
 
42
  // we have legacy settings, so we're going to use the post types
43
  // that Attachments is currently utilizing
44
 
45
  // the keys are the actual CPT names, so we need those
46
+ foreach ( $legacy_attachments_settings['post_types'] as $post_type => $value ) {
47
+ if ( $value ) {
48
  $post_types[] = $post_type;
49
+ }
50
+ }
51
 
52
  // set up our WP_Query args to grab anything with legacy data
53
  $args = array(
64
  $count = 0;
65
 
66
  // loop through each post
67
+ if ( $query ) {
68
+ while( $query->have_posts() ) {
 
 
69
 
70
+ // set up postdata
71
+ $query->the_post();
72
 
73
+ // let's first decode our Attachments data
74
+ $existing_attachments = get_post_meta( $query->post->ID, '_attachments', false );
75
 
76
+ $post_attachments = array();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
+ // check to make sure we've got data
79
+ if ( is_array( $existing_attachments ) && count( $existing_attachments ) > 0 ) {
80
+ // loop through each existing attachment
81
+ foreach( $existing_attachments as $attachment ) {
82
+ // decode and unserialize the data
83
+ $data = unserialize( base64_decode( $attachment ) );
84
+
85
+ array_push( $post_attachments, array(
86
+ 'id' => stripslashes( $data['id'] ),
87
+ 'title' => stripslashes( $data['title'] ),
88
+ 'caption' => stripslashes( $data['caption'] ),
89
+ 'order' => stripslashes( $data['order'] )
90
+ )
91
+ );
92
+ }
93
+
94
+ // sort attachments
95
+ if ( count( $post_attachments ) > 1 ) {
96
+ usort( $post_attachments, 'attachments_cmp' );
97
+ }
98
  }
 
99
 
100
+ // we have our Attachments entries
101
 
102
+ // let's check to see if we're migrating after population has taken place
103
+ $existing_attachments = get_post_meta( $query->post->ID, $this->get_meta_key(), false );
104
 
105
+ if ( ! isset( $existing_attachments[0] ) ) {
106
+ $existing_attachments[0] = '';
107
+ }
108
 
109
+ $existing_attachments = json_decode( $existing_attachments[0] );
110
 
111
+ if ( ! is_object( $existing_attachments ) ) {
112
+ $existing_attachments = new stdClass();
113
+ }
114
 
115
+ // we'll loop through the legacy Attachments and save them in the new format
116
+ foreach ( $post_attachments as $legacy_attachment ) {
117
+ // convert to the new format
118
+ $converted_attachment = array( 'id' => $legacy_attachment['id'] );
 
119
 
120
+ // fields are technically optional so we'll add those separately
121
+ // we're also going to encode them in the same way the main class does
122
+ if ( $title ) {
123
+ $converted_attachment['fields'][$title] = htmlentities( stripslashes( $legacy_attachment['title'] ), ENT_QUOTES, 'UTF-8' );
124
+ }
125
 
126
+ if ( $caption ) {
127
+ $converted_attachment['fields'][$caption] = htmlentities( stripslashes( $legacy_attachment['caption'] ), ENT_QUOTES, 'UTF-8' );
128
+ }
129
 
130
+ // check to see if the existing Attachments have our target instance
131
+ if ( ! isset( $existing_attachments->$instance ) ) {
132
+ // the instance doesn't exist so we need to create it
133
+ $existing_attachments->$instance = array();
134
+ }
 
135
 
136
+ // we need to convert our array to an object
137
+ $converted_attachment['fields'] = (object) $converted_attachment['fields'];
138
+ $converted_attachment = (object) $converted_attachment;
139
 
140
+ // append this legacy attachment to the existing instance
141
+ array_push( $existing_attachments->$instance, $converted_attachment );
142
+ }
143
 
144
+ // we're done! let's save everything in our new format
145
+ $existing_attachments = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $existing_attachments, JSON_UNESCAPED_UNICODE ) : json_encode( $existing_attachments );
146
 
147
+ // fix potentially encoded Unicode
148
+ $existing_attachments = str_replace( '\\', '\\\\', $existing_attachments );
149
 
150
+ // save it to the database
151
+ update_post_meta( $query->post->ID, 'attachments', $existing_attachments );
152
 
153
+ // increment our counter
154
+ $count++;
155
+ }
156
+ }
157
 
158
  return $count;
159
  }
165
  *
166
  * @since 3.2
167
  */
168
+ function prepare_migration() {
169
+ if ( ! wp_verify_nonce( $_GET['nonce'], 'attachments-migrate-1') ) {
170
+ wp_die( __( 'Invalid request', 'attachments' ) );
171
+ } ?>
172
  <h3><?php _e( 'Migration Step 1', 'attachments' ); ?></h3>
173
  <p><?php _e( "In order to migrate Attachments 1.x data, you need to set which instance and fields in version 3.0+ you'd like to use:", 'attachments' ); ?></p>
174
  <form action="options-general.php" method="get">
220
  *
221
  * @since 3.2
222
  */
223
+ function init_migration() {
224
+ if ( ! wp_verify_nonce( $_GET['nonce'], 'attachments-migrate-2') ) {
 
225
  wp_die( __( 'Invalid request', 'attachments' ) );
226
+ }
227
 
228
  $total = $this->migrate( $_GET['attachments-instance'], $_GET['attachments-title'], $_GET['attachments-caption'] );
229
 
230
+ if( false == get_option( 'attachments_migrated' ) ) { ?>
 
231
  <h3><?php _e( 'Migration Complete!', 'attachments' ); ?></h3>
232
  <p><?php _e( 'The migration has completed.', 'attachments' ); ?> <strong><?php _e( 'Migrated', 'attachments'); ?>: <?php echo $total; ?></strong>.</p>
233
+ <?php } else { ?>
234
  <h3><?php _e( 'Migration has already Run!', 'attachments' ); ?></h3>
235
  <p><?php _e( 'The migration has already been run. The migration process has not been repeated.', 'attachments' ); ?></p>
236
+ <?php }
237
 
238
  // make sure the database knows the migration has run
239
  add_option( 'attachments_migrated', true, '', 'no' );
246
  *
247
  * @since 3.5
248
  */
249
+ function prepare_pro_migration() {
250
+ if ( ! wp_verify_nonce( $_GET['nonce'], 'attachments-pro-migrate-1') ) {
251
+ wp_die( __( 'Invalid request', 'attachments' ) );
252
+ } ?>
253
  <h3><?php _e( 'Migration Step 1', 'attachments' ); ?></h3>
254
  <form action="options-general.php" method="get">
255
  <input type="hidden" name="page" value="attachments" />
264
  $attachments_pro_settings = get_option( '_iti_apro_settings' );
265
  ?>
266
 
267
+ <?php if ( is_array( $attachments_pro_settings['positions'] ) ) : ?>
268
  <p><?php _e( 'The following Attachments Pro Instances will be migrated:', 'attachments' ); ?></p>
269
  <ul style="padding-left:32px;list-style:disc;">
270
  <?php foreach( $attachments_pro_settings['positions'] as $attachments_pro_instance ) : ?>
271
+ <li><?php echo esc_html( $attachments_pro_instance['label'] ); ?></li>
272
  <?php endforeach; ?>
273
  </ul>
274
  <h2><?php _e( 'Note: this is a multi-step process', 'attachments' ); ?></h2>
290
  *
291
  * @since 3.5
292
  */
293
+ function init_pro_migration() {
 
294
  global $current_user;
295
 
296
  get_currentuserinfo();
297
 
298
+ if ( ! wp_verify_nonce( $_GET['nonce'], 'attachments-pro-migrate-2') ) {
299
  wp_die( __( 'Invalid request', 'attachments' ) );
300
+ }
301
 
302
  $attachments_pro_settings = get_option( '_iti_apro_settings' );
303
+ if ( is_array( $attachments_pro_settings['positions'] ) ) {
 
304
  $totals = array();
305
 
306
+ foreach ( $attachments_pro_settings['positions'] as $attachments_pro_instance ) {
307
  $totals[] = $this->migrate_pro( $attachments_pro_instance );
308
+ }
309
 
310
  $total_attachments = 0;
311
 
312
+ if ( ! empty( $totals ) ) {
313
+ foreach ( $totals as $instance_total ) {
314
  $total_attachments += $instance_total['total'];
315
+ }
316
+ }
317
 
318
+ if ( false == get_option( 'attachments_pro_migrated' ) ) :
319
  ?>
320
  <h3><?php _e( 'Data conversion complete!', 'attachments' ); ?></h3>
321
  <p><?php _e( 'The data conversion has been completed successfully.', 'attachments' ); ?> <strong><?php _e( 'Converted', 'attachments'); ?>: <?php echo $total_attachments; ?> <?php echo ( $total_attachments == 1 ) ? __( 'Attachment', 'attachments' ) : __( 'Attachments', 'attachments' ); ?></strong></p>
330
 
331
  array(
332
  'name' => '<?php echo str_replace( '-', '_', sanitize_title( $field['label'] ) ); ?>',
333
+ 'type' => '<?php echo esc_html( $field['type'] ); ?>',
334
+ 'label' => '<?php echo esc_html( $field['label'] ); ?>',
335
+ 'default' => '<?php echo isset( $field['mapped_to'] ) ? esc_html( $field['mapped_to'] ) : ''; ?>',
336
  ),<?php endforeach; echo "\n"; endif; ?>
337
  );
338
  <?php
339
  $post_types = array();
340
+ if ( isset( $attachments_pro_instance['conditions'] ) && is_array( $attachments_pro_instance['conditions'] ) && ! empty( $attachments_pro_instance['conditions'] ) )
341
+ foreach ( $attachments_pro_instance['conditions'] as $condition )
342
  if( $condition['param'] == 'post_type' && $condition['operator'] == 'is' )
343
  $post_types[] = $condition['limiter'];
344
  ?>
classes/class.attachments.php CHANGED
@@ -11,10 +11,12 @@
11
  */
12
 
13
  // Exit if accessed directly
14
- if( !defined( 'ABSPATH' ) ) exit;
 
 
15
 
16
  // Declare our class
17
- if( !class_exists( 'Attachments' ) ) :
18
 
19
  /**
20
  * Main Attachments Class
@@ -22,8 +24,8 @@ if( !class_exists( 'Attachments' ) ) :
22
  * @since 3.0
23
  */
24
 
25
- class Attachments
26
- {
27
  private $version; // stores Attachments' version number
28
  private $url; // stores Attachments' URL
29
  private $dir; // stores Attachments' directory
@@ -50,12 +52,11 @@ if( !class_exists( 'Attachments' ) ) :
50
  *
51
  * @since 3.0
52
  */
53
- function __construct( $instance = null, $post_id = null )
54
- {
55
  global $_wp_additional_image_sizes;
56
 
57
  // establish our environment variables
58
- $this->version = '3.5.4';
59
  $this->url = ATTACHMENTS_URL;
60
  $this->dir = ATTACHMENTS_DIR;
61
  $plugin = 'attachments/index.php';
@@ -76,7 +77,7 @@ if( !class_exists( 'Attachments' ) ) :
76
  add_filter( "plugin_action_links_$plugin", array( $this, 'plugin_settings_link' ) );
77
 
78
  // add update message(s)
79
- add_action( 'in_plugin_update_message-attachments/index.php', array( $this, 'update_message' ) );
80
 
81
  // set up l10n
82
  add_action( 'plugins_loaded', array( $this, 'l10n' ) );
@@ -106,13 +107,10 @@ if( !class_exists( 'Attachments' ) ) :
106
  add_action( 'admin_init', array( $this, 'admin_init' ) );
107
 
108
  // execution of actions varies depending on whether we're in the admin or not and an instance was passed
109
- if( is_admin() )
110
- {
111
  add_action( 'after_setup_theme', array( $this, 'apply_init_filters' ), 999 );
112
  $this->attachments = $this->get_attachments( $instance, $post_id );
113
- }
114
- elseif( !is_null( $instance ) )
115
- {
116
  $this->apply_init_filters();
117
  $this->attachments = $this->get_attachments( $instance, $post_id );
118
  }
@@ -124,8 +122,7 @@ if( !class_exists( 'Attachments' ) ) :
124
  /**
125
  * Add notification about available Attachments extensions
126
  */
127
- function update_message()
128
- { ?>
129
  <div style="margin-top:10px;padding-top:8px;border-top:1px solid #eaeaea;"><span style="color:#f00;"><?php _e( 'Attachments Extensions Available!', 'attachments' ); ?></span> <span style="font-weight:normal;"><?php _e( 'These utilities make working with Attachments even easier!', 'attachments' ); ?></span></div>
130
  <div style="font-weight:normal;padding-top:8px;">
131
  <p><strong><a href="https://mondaybynoon.com/members/plugins/attachments-ui/?utm_campaign=Attachments&utm_term=Upgrade%2bNotice">Attachments UI</a></strong> - <?php _e( 'Create Attachments Instances with an easy-to-use UI, easily limit meta box locations with fine grained control (e.g. Home page only), and more.', 'attachments' ); ?></p>
@@ -140,12 +137,12 @@ if( !class_exists( 'Attachments' ) ) :
140
  * @param $links
141
  * @return mixed
142
  */
143
- function plugin_settings_link( $links )
144
- {
145
  $settings_link = '<a href="options-general.php?page=attachments">'. __( 'Settings', 'attachments' ) . '</a>';
146
  array_unshift( $links, $settings_link );
147
  $extend_link = '<a href="https://mondaybynoon.com/members/plugins/?utm_campaign=Attachments&utm_term=Plugins%2bExtend#attachments">'. __( 'Extend', 'attachments' ) . '</a>';
148
  array_unshift( $links, $extend_link );
 
149
  return $links;
150
  }
151
 
@@ -156,10 +153,10 @@ if( !class_exists( 'Attachments' ) ) :
156
  *
157
  * @since 3.4.3
158
  */
159
- function admin_init()
160
- {
161
- if( current_user_can( 'delete_posts' ) )
162
  add_action( 'delete_post', array( $this, 'handle_wp_post_delete' ), 10 );
 
163
  }
164
 
165
 
@@ -170,8 +167,7 @@ if( !class_exists( 'Attachments' ) ) :
170
  * @return array
171
  * @since 3.5
172
  */
173
- function get_fields()
174
- {
175
  return $this->fields;
176
  }
177
 
@@ -184,11 +180,16 @@ if( !class_exists( 'Attachments' ) ) :
184
  * @param int $pid Post ID
185
  * @since 3.4.3
186
  */
187
- function handle_wp_post_delete( $pid )
188
- {
 
 
 
 
189
  // check to make sure it was an attachment
190
- if( 'attachment' != get_post_type( $pid ) )
191
  return;
 
192
 
193
  // if a user deletes an attachment from the Media library (but it's been used
194
  // in Attachments somewhere else) we need to clean that up...
@@ -200,10 +201,8 @@ if( !class_exists( 'Attachments' ) ) :
200
  // so we're going to use the search class to find all instances
201
  // for any occurrence of the deleted attachment (which has an ID of $pid)
202
 
203
- if( is_array( $this->instances ) )
204
- {
205
- foreach( $this->instances as $instance => $details )
206
- {
207
  $search_args = array(
208
  'instance' => $instance,
209
  'attachment_id' => intval( $pid ),
@@ -211,22 +210,22 @@ if( !class_exists( 'Attachments' ) ) :
211
 
212
  $this->search( null, $search_args );
213
 
214
- if( $this->exist() )
215
- {
216
  // we've got a hit (e.g. an existing post uses the deleted attachment)
217
- while( $attachment = $this->get() )
218
- {
219
  $post_id = $attachment->post_id;
220
 
221
  // we'll use the post ID to snag the details
222
  $post_attachments = $this->get_attachments_metadata( $post_id );
223
 
224
- if( is_object( $post_attachments ) )
225
- {
226
- foreach( $post_attachments as $existing_instance => $existing_instance_attachments )
227
- foreach( $existing_instance_attachments as $existing_instance_attachment_key => $existing_instance_attachment )
228
- if( $pid == intval( $existing_instance_attachment->id ) )
229
  unset( $post_attachments->{$existing_instance}[$existing_instance_attachment_key] );
 
 
 
230
 
231
  // saving routine assumes array from POST so we'll typecast it
232
  $post_attachments = (array) $post_attachments;
@@ -247,8 +246,7 @@ if( !class_exists( 'Attachments' ) ) :
247
  *
248
  * @since 3.4.2
249
  */
250
- function get_meta_key()
251
- {
252
  return $this->meta_key;
253
  }
254
 
@@ -259,8 +257,7 @@ if( !class_exists( 'Attachments' ) ) :
259
  *
260
  * @since 3.5
261
  */
262
- function get_url()
263
- {
264
  return $this->url;
265
  }
266
 
@@ -271,15 +268,13 @@ if( !class_exists( 'Attachments' ) ) :
271
  *
272
  * @since 3.4.2
273
  */
274
- function l10n()
275
- {
276
- load_plugin_textdomain( 'attachments', false, 'attachments/languages/' );
277
  }
278
 
279
 
280
 
281
- function load_extensions()
282
- {
283
  do_action( 'attachments_extension', $this );
284
  }
285
 
@@ -290,8 +285,7 @@ if( !class_exists( 'Attachments' ) ) :
290
  *
291
  * @since 3.4
292
  */
293
- function apply_init_filters()
294
- {
295
  // allows a different meta_key to be used
296
  $this->meta_key = apply_filters( 'attachments_meta_key', $this->meta_key );
297
  }
@@ -303,8 +297,7 @@ if( !class_exists( 'Attachments' ) ) :
303
  *
304
  * @since 3.3
305
  */
306
- function search( $query = null, $params = array() )
307
- {
308
  $results = new AttachmentsSearch( $query, $params );
309
  $this->attachments = $results->results;
310
  }
@@ -316,9 +309,8 @@ if( !class_exists( 'Attachments' ) ) :
316
  *
317
  * @since 3.0
318
  */
319
- function exist()
320
- {
321
- return !empty( $this->attachments );
322
  }
323
 
324
 
@@ -328,8 +320,7 @@ if( !class_exists( 'Attachments' ) ) :
328
  *
329
  * @since 3.0.6
330
  */
331
- function total()
332
- {
333
  return count( $this->attachments );
334
  }
335
 
@@ -340,8 +331,7 @@ if( !class_exists( 'Attachments' ) ) :
340
  *
341
  * @since 3.6
342
  */
343
- function rewind()
344
- {
345
  $this->attachments_ref = -1;
346
  }
347
 
@@ -352,14 +342,14 @@ if( !class_exists( 'Attachments' ) ) :
352
  *
353
  * @since 3.0
354
  */
355
- function get()
356
- {
357
  $this->attachments_ref++;
358
 
359
- if( !count( $this->attachments ) || $this->attachments_ref >= count( $this->attachments ) )
360
  return false;
 
361
 
362
- return $this->attachments[$this->attachments_ref];
363
  }
364
 
365
 
@@ -369,9 +359,8 @@ if( !class_exists( 'Attachments' ) ) :
369
  *
370
  * @since 3.2
371
  */
372
- function get_single( $index )
373
- {
374
- return isset( $this->attachments[$index] ) ? $this->attachments[$index] : false;
375
  }
376
 
377
 
@@ -381,21 +370,18 @@ if( !class_exists( 'Attachments' ) ) :
381
  *
382
  * @since 3.0.6
383
  */
384
- function asset( $size = 'thumbnail', $index = null )
385
- {
386
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
387
 
388
  // do we have our meta yet?
389
- if( !isset( $this->attachments[$index]->meta ) )
390
- $this->attachments[$index]->meta = wp_get_attachment_metadata( $this->attachments[$index]->id );
 
391
 
392
  // is it an image?
393
- if( isset( $this->attachments[$index]->meta['sizes'] ) )
394
- {
395
- $asset = wp_get_attachment_image_src( $this->attachments[$index]->id, $size );
396
- }
397
- else
398
- {
399
  // either it's not an image or we don't have the proper size, so we'll use the icon
400
  $asset = $this->icon( $index );
401
  }
@@ -410,10 +396,9 @@ if( !class_exists( 'Attachments' ) ) :
410
  *
411
  * @since 3.0.6
412
  */
413
- function icon( $index = null )
414
- {
415
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
416
- $asset = wp_get_attachment_image_src( $this->attachments[$index]->id, null, true );
417
 
418
  return $asset;
419
  }
@@ -426,10 +411,9 @@ if( !class_exists( 'Attachments' ) ) :
426
  *
427
  * @since 3.4.1
428
  */
429
- function date( $d = "d/m/Y", $index = null )
430
- {
431
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
432
- $date = get_the_time( $d, $this->attachments[$index]->id );
433
 
434
  return $date;
435
  }
@@ -441,17 +425,16 @@ if( !class_exists( 'Attachments' ) ) :
441
  *
442
  * @since 3.0
443
  */
444
- function image( $size = 'thumbnail', $index = null )
445
- {
446
  $asset = $this->asset( $size, $index );
447
 
448
  $image_src = $asset[0];
449
  $image_width = $asset[1];
450
  $image_height = $asset[2];
451
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
452
- $image_alt = get_post_meta( $this->attachments[$index]->id, '_wp_attachment_image_alt', true );
453
 
454
- $image = '<img src="' . $image_src . '" width="' . $image_width . '" height="' . $image_height . '" alt="' . $image_alt . '" />';
455
 
456
  return $image;
457
  }
@@ -463,10 +446,10 @@ if( !class_exists( 'Attachments' ) ) :
463
  *
464
  * @since 3.0
465
  */
466
- function src( $size = 'thumbnail', $index = null )
467
- {
468
  $asset = $this->asset( $size, $index );
469
- return $asset[0];
 
470
  }
471
 
472
 
@@ -476,10 +459,10 @@ if( !class_exists( 'Attachments' ) ) :
476
  *
477
  * @since 3.5
478
  */
479
- function width( $size = 'thumbnail', $index = null )
480
- {
481
  $asset = $this->asset( $size, $index );
482
- return $asset[1];
 
483
  }
484
 
485
 
@@ -489,10 +472,10 @@ if( !class_exists( 'Attachments' ) ) :
489
  *
490
  * @since 3.5
491
  */
492
- function height( $size = 'thumbnail', $index = null )
493
- {
494
  $asset = $this->asset( $size, $index );
495
- return $asset[2];
 
496
  }
497
 
498
 
@@ -502,21 +485,22 @@ if( !class_exists( 'Attachments' ) ) :
502
  *
503
  * @since 3.0
504
  */
505
- function filesize( $index = null, $size = null )
506
- {
507
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
508
 
509
- if( !isset( $this->attachments[$index]->id ) )
510
  return false;
 
511
 
512
  // if an image size is passed along, use that
513
- $url = is_string( $size ) ? $this->src( $size, $index ) : wp_get_attachment_url( $this->attachments[$index]->id );
514
  $uploads = wp_upload_dir();
515
  $file_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $url );
516
 
517
  $formatted = '0 bytes';
518
- if( file_exists( $file_path ) )
519
  $formatted = size_format( @filesize( $file_path ) );
 
520
 
521
  return $formatted;
522
  }
@@ -528,14 +512,15 @@ if( !class_exists( 'Attachments' ) ) :
528
  *
529
  * @since 3.0
530
  */
531
- function type( $index = null )
532
- {
533
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
534
 
535
- if( !isset( $this->attachments[$index]->id ) )
536
  return false;
 
537
 
538
  $attachment_mime = $this->get_mime_type( $this->attachments[$index]->id );
 
539
  return $attachment_mime;
540
  }
541
 
@@ -546,9 +531,9 @@ if( !class_exists( 'Attachments' ) ) :
546
  *
547
  * @since 3.4.2
548
  */
549
- function get_mime_type( $id = null )
550
- {
551
  $attachment_mime = explode( '/', get_post_mime_type( intval( $id ) ) );
 
552
  return isset( $attachment_mime[0] ) ? $attachment_mime[0] : false;
553
  }
554
 
@@ -559,14 +544,15 @@ if( !class_exists( 'Attachments' ) ) :
559
  *
560
  * @since 3.0
561
  */
562
- function subtype( $index = null )
563
- {
564
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
565
 
566
- if( !isset( $this->attachments[$index]->id ) )
567
  return false;
 
 
 
568
 
569
- $attachment_mime = explode( '/', get_post_mime_type( $this->attachments[$index]->id ) );
570
  return isset( $attachment_mime[1] ) ? $attachment_mime[1] : false;
571
  }
572
 
@@ -577,11 +563,10 @@ if( !class_exists( 'Attachments' ) ) :
577
  *
578
  * @since 3.0
579
  */
580
- function id( $index = null )
581
- {
582
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
583
 
584
- return isset( $this->attachments[$index]->id ) ? $this->attachments[$index]->id : false;
585
  }
586
 
587
 
@@ -591,11 +576,10 @@ if( !class_exists( 'Attachments' ) ) :
591
  *
592
  * @since 3.3
593
  */
594
- function post_id( $index = null )
595
- {
596
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
597
 
598
- return isset( $this->attachments[$index]->post_id ) ? $this->attachments[$index]->post_id : false;
599
  }
600
 
601
 
@@ -605,11 +589,10 @@ if( !class_exists( 'Attachments' ) ) :
605
  *
606
  * @since 3.0
607
  */
608
- function url( $index = null )
609
- {
610
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
611
 
612
- return isset( $this->attachments[$index]->id ) ? wp_get_attachment_url( $this->attachments[$index]->id ) : false;
613
  }
614
 
615
 
@@ -619,11 +602,10 @@ if( !class_exists( 'Attachments' ) ) :
619
  *
620
  * @since 3.0
621
  */
622
- function field( $name = 'title', $index = null )
623
- {
624
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
625
 
626
- return isset( $this->attachments[$index]->fields->$name ) ? $this->attachments[$index]->fields->$name : false;
627
  }
628
 
629
 
@@ -633,12 +615,11 @@ if( !class_exists( 'Attachments' ) ) :
633
  *
634
  * @since 3.0
635
  */
636
- function setup_instances()
637
- {
638
  // implement our default instance if appropriate
639
- $filtered = apply_filters( 'attachments_default_instance', true );
640
- if( $filtered && ( ! defined( 'ATTACHMENTS_DEFAULT_INSTANCE' ) || true == ATTACHMENTS_DEFAULT_INSTANCE ) ) {
641
- $this->register();
642
  }
643
 
644
  // facilitate user-defined instance registration
@@ -652,17 +633,18 @@ if( !class_exists( 'Attachments' ) ) :
652
  *
653
  * @since 3.0
654
  */
655
- function assets( $hook )
656
- {
657
  global $post;
658
 
659
  // we only want our assets on edit screens
660
- if( !empty( $this->instances_for_post_type ) && 'edit.php' != $hook && 'post.php' != $hook && 'post-new.php' != $hook )
661
  return;
 
662
 
663
  // we only want to enqueue if appropriate
664
- if( empty( $this->instances_for_post_type ) )
665
  return;
 
666
 
667
  $post_id = isset( $post->ID ) ? $post->ID : null;
668
  wp_enqueue_media( array( 'post' => $post_id ) );
@@ -679,14 +661,11 @@ if( !class_exists( 'Attachments' ) ) :
679
  *
680
  * @since 3.0
681
  */
682
- function meta_box_init()
683
- {
684
  $nonce_sent = false;
685
 
686
- if( !empty( $this->instances_for_post_type ) )
687
- {
688
- foreach( $this->instances_for_post_type as $instance )
689
- {
690
  // facilitate more fine-grained meta box positioning than post type
691
  $applicable = apply_filters( "attachments_location_{$instance}", true, $instance );
692
 
@@ -698,19 +677,19 @@ if( !class_exists( 'Attachments' ) ) :
698
  $priority = isset($instance->priority) ? $instance->priority : 'high';
699
 
700
  if( $applicable ) {
701
- add_meta_box(
702
- 'attachments-' . $instance_name,
703
- __( esc_attr( $instance->label ) ),
704
- array( $this, 'meta_box_markup' ),
705
- $this->get_post_type(),
706
- $position,
707
- $priority,
708
- array(
709
- 'instance' => $instance,
710
- 'setup_nonce' => !$nonce_sent
711
- )
712
- );
713
- $nonce_sent = true;
714
  }
715
 
716
  }
@@ -724,17 +703,17 @@ if( !class_exists( 'Attachments' ) ) :
724
  *
725
  * @since 3.0
726
  */
727
- function meta_box_markup( $post, $metabox )
728
- {
729
  // single out our $instance
730
  $instance = (object) $metabox['args']['instance'];
731
 
732
- if( $metabox['args']['setup_nonce'] )
733
  wp_nonce_field( 'attachments_save', 'attachments_nonce' );
 
734
 
735
  ?>
736
 
737
- <div id="attachments-<?php echo $instance->name; ?>" class="attachments-parent-container<?php if( $instance->append == false ) : ?> attachments-prepend<?php endif; ?>">
738
  <?php if( ! empty( $instance->note ) ) : ?>
739
  <div class="attachments-note"><?php echo wpautop( $instance->note ); ?></div>
740
  <?php endif; ?>
@@ -743,11 +722,9 @@ if( !class_exists( 'Attachments' ) ) :
743
  <a class="button attachments-invoke"><?php _e( esc_attr( $instance->button_text ), 'attachments' ); ?></a>
744
  </div>
745
  <?php endif; ?>
746
- <div class="attachments-container attachments-<?php echo $instance->name; ?>"><?php
747
- if( isset( $instance->attachments ) && !empty( $instance->attachments ) )
748
- {
749
- foreach( $instance->attachments as $attachment )
750
- {
751
  // we need to give our Attachment a uid to carry through to all the fields
752
  $attachment->uid = uniqid();
753
 
@@ -769,7 +746,7 @@ if( !class_exists( 'Attachments' ) ) :
769
  button = '<?php echo __( esc_attr( $instance->modal_text ) ); ?>',
770
  router = '<?php echo __( esc_attr( $instance->router ) ); ?>',
771
  limit = <?php echo intval( $instance->limit ); ?>,
772
- existing = <?php echo ( isset( $instance->attachments ) && !empty( $instance->attachments ) ) ? count( $instance->attachments ): 0; ?>,
773
  attachmentsframe,
774
  editframe;
775
 
@@ -864,12 +841,12 @@ if( !class_exists( 'Attachments' ) ) :
864
 
865
  // if we're in a sidebar we DO want to show the fields which are normally hidden on load via CSS
866
  if($element.parents('#side-sortables')){
867
- $element.find('.attachments-attachment:last .attachments-fields').show();
868
  }
869
 
870
  // see if we need to set a default
871
  // TODO: can we tie this into other field types (select, radio, checkbox)?
872
- $element.find('.attachments-attachment:last .attachments-fields input, .attachments-attachment:last .attachments-fields textarea').each(function(){
873
  if($(this).data('default')){
874
  var meta_for_default = $(this).data('default');
875
  if(attachments_isset(attachment.attributes)){
@@ -884,7 +861,7 @@ if( !class_exists( 'Attachments' ) ) :
884
 
885
  // if it wasn't an image we need to ditch the dimensions
886
  if(!attachments_isset(attachment.attributes.width)||!attachments_isset(attachment.attributes.height)){
887
- $element.find('.attachments-attachment:last .dimensions').hide();
888
  }
889
  });
890
  });
@@ -996,8 +973,7 @@ if( !class_exists( 'Attachments' ) ) :
996
  *
997
  * @since 3.0
998
  **/
999
- function get_field_types()
1000
- {
1001
  $field_types = array(
1002
  'text' => ATTACHMENTS_DIR . 'classes/fields/class.field.text.php',
1003
  'textarea' => ATTACHMENTS_DIR . 'classes/fields/class.field.textarea.php',
@@ -1009,11 +985,9 @@ if( !class_exists( 'Attachments' ) ) :
1009
  // $field_types = apply_filters( 'attachments_fields', $field_types );
1010
 
1011
  $field_index = 0;
1012
- foreach( $field_types as $type => $path )
1013
- {
1014
  // proceed with inclusion
1015
- if( file_exists( $path ) )
1016
- {
1017
  // include the file
1018
  include_once( $path );
1019
 
@@ -1032,7 +1006,7 @@ if( !class_exists( 'Attachments' ) ) :
1032
  $field_class = $existing_classes[$flag + $field_index + 1];
1033
 
1034
  // create our link using our new field class
1035
- $field_types[$type] = $field_class;
1036
 
1037
  $field_index++;
1038
  }
@@ -1049,8 +1023,7 @@ if( !class_exists( 'Attachments' ) ) :
1049
  *
1050
  * @since 3.0
1051
  */
1052
- function register_field( $params = array() )
1053
- {
1054
  $defaults = array(
1055
  'name' => 'title',
1056
  'type' => 'text',
@@ -1061,18 +1034,22 @@ if( !class_exists( 'Attachments' ) ) :
1061
  $params = array_merge( $defaults, $params );
1062
 
1063
  // ensure it's a valid type
1064
- if( !isset( $this->fields[$params['type']] ) )
1065
  return false;
 
1066
 
1067
- // sanitize
1068
- if( isset( $params['name'] ) )
1069
  $params['name'] = str_replace( '-', '_', sanitize_title( $params['name'] ) );
 
1070
 
1071
- if( isset( $params['label'] ) )
1072
  $params['label'] = __( esc_html( $params['label'] ) );
 
1073
 
1074
- if( !isset( $params['meta'] ) || !is_array( $params['meta'] ) )
1075
  $params['meta'] = array();
 
1076
 
1077
  // instantiate the class for this field and send it back
1078
  return new $this->fields[ $params['type'] ]( $params['name'], $params['label'], $params['meta'] );
@@ -1085,8 +1062,7 @@ if( !class_exists( 'Attachments' ) ) :
1085
  *
1086
  * @since 3.0
1087
  */
1088
- function register( $name = 'attachments', $params = array() )
1089
- {
1090
  $defaults = array(
1091
 
1092
  // title of the meta box (string)
@@ -1144,53 +1120,50 @@ if( !class_exists( 'Attachments' ) ) :
1144
  $params = array_merge( $defaults, $params );
1145
 
1146
  // sanitize
1147
- if( !is_array( $params['post_type'] ) )
1148
  $params['post_type'] = array( $params['post_type'] ); // we always want an array
 
1149
 
1150
- if( !is_array( $params['filetype'] ) )
1151
  $params['filetype'] = array( $params['filetype'] ); // we always want an array
 
1152
 
1153
  $params['label'] = esc_html( $params['label'] );
1154
  $params['limit'] = intval( $params['limit'] );
1155
- $params['note'] = esc_sql( $params['note'] );
1156
  $params['button_text'] = esc_attr( $params['button_text'] );
1157
  $params['modal_text'] = esc_attr( $params['modal_text'] );
1158
 
1159
  // make sure we've got valid filetypes
1160
- if( is_array( $params['filetype'] ) )
1161
- {
1162
- foreach( $params['filetype'] as $key => $filetype )
1163
- {
1164
- if( !in_array( $filetype, $this->valid_filetypes ) )
1165
- {
1166
- unset( $params['filetype'][$key] );
1167
  }
1168
  }
1169
  }
1170
 
1171
  // WordPress sanitizes post type names when registering, so we will too
1172
- foreach( $params['post_type'] as $key => $post_type )
1173
- $params['post_type'][$key] = sanitize_key( $post_type );
1174
-
1175
 
1176
  // make sure the instance name is proper
1177
  $instance = str_replace( '-', '_', sanitize_title( $name ) );
1178
 
1179
  // register the fields
1180
- if( isset( $params['fields'] ) && is_array( $params['fields'] ) && count( $params['fields'] ) )
1181
- {
1182
- foreach( $params['fields'] as $field )
1183
- {
1184
  // register the field
1185
  $this->register_field( $field );
1186
  }
1187
  }
1188
 
1189
  // set the instance
1190
- $this->instances[$instance] = $params;
1191
 
1192
  // set the Attachments for this instance
1193
- $this->instances[$instance]['attachments'] = $this->get_attachments( $instance );
1194
 
1195
  }
1196
 
@@ -1201,18 +1174,14 @@ if( !class_exists( 'Attachments' ) ) :
1201
  *
1202
  * @since 3.0
1203
  */
1204
- function get_instances_for_post_type( $post_type = null )
1205
- {
1206
- $post_type = ( !is_null( $post_type ) && post_type_exists( $post_type ) ) ? $post_type : $this->get_post_type();
1207
 
1208
  $instances = array();
1209
 
1210
- if( !empty( $this->instances ) )
1211
- {
1212
- foreach( $this->instances as $name => $params )
1213
- {
1214
- if( in_array( $post_type, $params['post_type'] ) )
1215
- {
1216
  $instances[] = $name;
1217
  }
1218
  }
@@ -1229,25 +1198,17 @@ if( !class_exists( 'Attachments' ) ) :
1229
  *
1230
  * @since 3.0
1231
  */
1232
- function get_post_type()
1233
- {
1234
  global $post;
1235
 
1236
  // TODO: Retrieving the post_type at this point is ugly to say the least. This needs major cleanup.
1237
- if( empty( $post->ID ) && isset( $_GET['post_type'] ) )
1238
- {
1239
- $post_type = esc_attr( $_GET['post_type'] );
1240
- }
1241
- elseif( !empty( $post->ID ) )
1242
- {
1243
  $post_type = get_post_type( $post->ID );
1244
- }
1245
- elseif( isset( $_GET['post'] ) )
1246
- {
1247
  $post_type = get_post_type( intval( $_GET['post'] ) );
1248
- }
1249
- else
1250
- {
1251
  $post_type = 'post';
1252
  }
1253
 
@@ -1261,8 +1222,7 @@ if( !class_exists( 'Attachments' ) ) :
1261
  *
1262
  * @since 3.0
1263
  */
1264
- function set_instances_for_current_post_type()
1265
- {
1266
  // store the applicable instances for this post type
1267
  $this->instances_for_post_type = $this->get_instances_for_post_type( $this->get_post_type() );
1268
  }
@@ -1274,22 +1234,20 @@ if( !class_exists( 'Attachments' ) ) :
1274
  *
1275
  * @since 3.0
1276
  */
1277
- function create_attachment_field( $instance, $field, $attachment = null )
1278
- {
1279
 
1280
  // the $field at this point is just the user-declared array
1281
  // we need to make it a field object
1282
  $type = $field['type'];
1283
 
1284
- if( isset( $this->fields[$type] ) )
1285
- {
1286
  $name = sanitize_title( $field['name'] );
1287
  $label = esc_html( $field['label'] );
1288
  $default = isset( $field['default'] ) ? $field['default'] : false; // validated in the class
1289
  $meta = isset( $field['meta'] ) ? $field['meta'] : array();
1290
  $value = isset( $attachment->fields->$name ) ? $attachment->fields->$name : null;
1291
 
1292
- $field = new $this->fields[$type]( $name, $label, $value, $meta );
1293
  $field->value = $field->format_value_for_input( $field->value );
1294
 
1295
  // does this field already have a unique ID?
@@ -1302,18 +1260,15 @@ if( !class_exists( 'Attachments' ) ) :
1302
  $field->set_field_default( $default );
1303
 
1304
  ?>
1305
- <div class="attachments-attachment-field attachments-attachment-field-<?php echo $instance; ?> attachments-attachment-field-<?php echo $field->type; ?> attachment-field-<?php echo $field->name; ?>">
1306
- <div class="attachment-label attachment-label-<?php echo $instance; ?>">
1307
- <label for="<?php echo $field->field_id; ?>"><?php echo $field->label; ?></label>
1308
  </div>
1309
- <div class="attachment-field attachment-field-<?php echo $instance; ?>">
1310
  <?php echo $this->create_field( $instance, $field ); ?>
1311
  </div>
1312
  </div>
1313
- <?php
1314
- }
1315
- else
1316
- {
1317
  $field = false;
1318
  }
1319
 
@@ -1327,8 +1282,7 @@ if( !class_exists( 'Attachments' ) ) :
1327
  *
1328
  * @since 3.0
1329
  */
1330
- function create_field( $instance, $field )
1331
- {
1332
  $field = (object) $field;
1333
 
1334
  // with all of our attributes properly set, we can output
@@ -1342,26 +1296,24 @@ if( !class_exists( 'Attachments' ) ) :
1342
  *
1343
  * @since 3.0
1344
  */
1345
- function create_attachment( $instance, $attachment = null )
1346
- {
1347
  ?>
1348
- <div class="attachments-attachment attachments-attachment-<?php echo $instance; ?>">
1349
  <?php $array_flag = ( isset( $attachment->uid ) ) ? $attachment->uid : '{{ attachments.attachment_uid }}'; ?>
1350
 
1351
- <input type="hidden" class="attachments-track-id" name="attachments[<?php echo $instance; ?>][<?php echo $array_flag; ?>][id]" value="<?php echo isset( $attachment->id ) ? $attachment->id : '{{ attachments.id }}' ; ?>" />
1352
 
1353
  <?php
1354
  // since attributes can change over time (image gets replaced, cropped, etc.) we'll pull that info
1355
- if( isset( $attachment->id ) )
1356
- {
1357
  // we'll just use the full size since that's what Media in 3.5 uses
1358
  $attachment_meta = wp_get_attachment_metadata( $attachment->id );
1359
 
1360
  // only images return the 'file' key
1361
- if( !isset( $attachment_meta['file'] ))
1362
  $attachment_meta['file'] = get_attached_file( $attachment->id );
1363
-
1364
- $filename = explode( "/", $attachment_meta['file'] );
1365
 
1366
  $attachment->width = isset( $attachment_meta['width'] ) ? $attachment_meta['width'] : null;
1367
  $attachment->height = isset( $attachment_meta['height'] ) ? $attachment_meta['height'] : null;
@@ -1383,9 +1335,9 @@ if( !class_exists( 'Attachments' ) ) :
1383
  <img src="<?php echo $image; ?>" alt="Thumbnail" />
1384
  </div>
1385
  <div class="attachment-details attachment-info details">
1386
- <div class="filename"><?php echo isset( $attachment->filename ) ? $attachment->filename : '{{ attachments.filename }}' ; ?></div>
1387
- <?php if( ( isset( $attachment->id ) && isset( $attachment->width ) ) || !isset( $attachment->id ) ) : ?>
1388
- <div class="dimensions"><?php echo isset( $attachment->width ) ? $attachment->width : '{{ attachments.width }}' ; ?> &times; <?php echo isset( $attachment->height ) ? $attachment->height : '{{ attachments.height }}' ; ?></div>
1389
  <?php endif; ?>
1390
  <div class="edit-attachment-asset"><a href="#"><?php _e( 'Change', 'attachments' ); ?></a></div>
1391
  <div class="delete-attachment"><a href="#"><?php _e( 'Remove', 'attachments' ); ?></a></div>
@@ -1397,8 +1349,9 @@ if( !class_exists( 'Attachments' ) ) :
1397
 
1398
  <div class="attachments-fields">
1399
  <?php
1400
- foreach( $this->instances[$instance]['fields'] as $field )
1401
  $field_ref = $this->create_attachment_field( $instance, $field, $attachment );
 
1402
  ?>
1403
  </div>
1404
 
@@ -1413,29 +1366,30 @@ if( !class_exists( 'Attachments' ) ) :
1413
  *
1414
  * @since 3.1
1415
  */
1416
- function field_assets()
1417
- {
1418
  global $post;
1419
 
1420
  // we only want to enqueue if we're on an edit screen and it's applicable
1421
- if( empty( $this->instances_for_post_type ) || empty( $post ) )
1422
  return;
 
1423
 
1424
  // all metaboxes have been put in place, we can now determine which field assets need to be included
1425
 
1426
  // first we'll get a list of the field types on screen
1427
  $fieldtypes = array();
1428
- foreach( $this->instances_for_post_type as $instance )
1429
- foreach( $this->instances[$instance]['fields'] as $field )
1430
  $fieldtypes[] = $field['type'];
 
 
1431
 
1432
  // we only want to dump out assets once for each field type
1433
  $fieldtypes = array_unique( $fieldtypes );
1434
 
1435
  // loop through and dump out all the assets
1436
- foreach( $fieldtypes as $fieldtype )
1437
- {
1438
- $field = new $this->fields[$fieldtype];
1439
  $field->assets();
1440
  }
1441
  }
@@ -1447,29 +1401,30 @@ if( !class_exists( 'Attachments' ) ) :
1447
  *
1448
  * @since 3.1
1449
  */
1450
- function field_inits()
1451
- {
1452
  global $post;
1453
 
1454
  // we only want to enqueue if we're on an edit screen and it's applicable
1455
- if( empty( $this->instances_for_post_type ) || empty( $post ) )
1456
  return;
 
1457
 
1458
  // all metaboxes have been put in place, we can now determine which field assets need to be included
1459
 
1460
  // first we'll get a list of the field types on screen
1461
  $fieldtypes = array();
1462
- foreach( $this->instances_for_post_type as $instance )
1463
- foreach( $this->instances[$instance]['fields'] as $field )
1464
  $fieldtypes[] = $field['type'];
 
 
1465
 
1466
  // we only want to dump out assets once for each field type
1467
  $fieldtypes = array_unique( $fieldtypes );
1468
 
1469
  // loop through and dump out all the assets
1470
- foreach( $fieldtypes as $fieldtype )
1471
- {
1472
- $field = new $this->fields[$fieldtype];
1473
  $field->init();
1474
  }
1475
  }
@@ -1482,16 +1437,13 @@ if( !class_exists( 'Attachments' ) ) :
1482
  *
1483
  * @since 3.0
1484
  */
1485
- function admin_footer()
1486
- {
1487
- if( !empty( $this->instances_for_post_type ) )
1488
- { ?>
1489
  <script type="text/javascript">
1490
  var ATTACHMENTS_VIEWS = {};
1491
  </script>
1492
- <?php
1493
- foreach( $this->instances_for_post_type as $instance ) : ?>
1494
- <script type="text/template" id="tmpl-attachments-<?php echo $instance; ?>">
1495
  <?php $this->create_attachment( $instance ); ?>
1496
  </script>
1497
  <?php endforeach;
@@ -1505,23 +1457,26 @@ if( !class_exists( 'Attachments' ) ) :
1505
  *
1506
  * @since 3.0
1507
  */
1508
- function save( $post_id )
1509
- {
1510
  // is the user logged in?
1511
- if( !is_user_logged_in() )
1512
  return $post_id;
 
1513
 
1514
  // is the nonce set?
1515
- if( !isset( $_POST['attachments_nonce'] ) )
1516
  return $post_id;
 
1517
 
1518
  // is the nonce valid?
1519
- if( !wp_verify_nonce( $_POST['attachments_nonce'], 'attachments_save' ) )
1520
  return $post_id;
 
1521
 
1522
  // can this user edit this post?
1523
- if( !current_user_can( 'edit_post', $post_id ) )
1524
  return $post_id;
 
1525
 
1526
  // passed authentication, proceed with save
1527
 
@@ -1543,45 +1498,38 @@ if( !class_exists( 'Attachments' ) ) :
1543
  * @return bool
1544
  * @since 3.4.3
1545
  */
1546
- function save_metadata( $post_id = 0, $attachments_meta = null )
1547
- {
1548
- if( !is_array( $attachments_meta ) || !is_int( $post_id ) || intval( $post_id ) < 1 )
1549
  return false;
 
1550
 
1551
  // final data store
1552
  $attachments = array();
1553
 
1554
  // loop through each submitted instance
1555
- foreach( $attachments_meta as $instance => $instance_attachments )
1556
- {
1557
  // loop through each Attachment of this instance
1558
- foreach( $instance_attachments as $key => $attachment )
1559
- {
1560
- // see if it was pulled as JSON from a delete cleanup
1561
- if( is_object( $attachment ) )
1562
- {
1563
- $attachment = get_object_vars( $attachment );
1564
- if( is_array( $attachment ) && !empty( $attachment ) )
1565
- {
1566
- if( isset( $attachment['fields'] ) && is_object( $attachment['fields'] ) ) {
1567
- $attachment['fields'] = get_object_vars( $attachment['fields'] );
1568
- }
1569
- }
1570
- }
1571
-
1572
- $attachment_exists = isset( $attachment['id'] ) ? get_post( absint( $attachment['id'] ) ) : false;
1573
 
1574
  // make sure the attachment exists
1575
- if( $attachment_exists )
1576
- {
1577
  // since we're using JSON for storage in the database, we need
1578
  // to make sure that characters are encoded that would otherwise
1579
  // break the JSON
1580
- if( isset( $attachment['fields'] ) && is_array( $attachment['fields'] ) )
1581
- {
1582
 
1583
- foreach( $attachment['fields'] as $key => $field_value )
1584
- {
1585
  // take care of our returns
1586
  $field_value = str_replace( "\r\n", "\n", $field_value );
1587
  $field_value = str_replace( "\r", "\n", $field_value );
@@ -1599,30 +1547,29 @@ if( !class_exists( 'Attachments' ) ) :
1599
  $field_value = $this->encode_field_value( $field_value );
1600
 
1601
  // encode things properly
1602
- $attachment['fields'][$key] = $field_value;
1603
  }
1604
  }
1605
 
1606
- // set the post parent if applicable
1607
- // need to first check to make sure we're not overwriting a native Attach
1608
- $attach_post_ref = $attachment_exists;
1609
- if( $attach_post_ref->post_parent == 0 && !empty( $this->instances[$instance]['post_parent'] ) ) {
1610
- // no current Attach, we can add ours
1611
- $attach_post = array(
1612
- 'ID' => absint( $attachment['id'] ),
1613
- 'post_parent' => $post_id,
1614
- );
1615
-
1616
- wp_update_post( $attach_post );
1617
- }
1618
 
1619
- $attachments[$instance][] = $attachment;
1620
  }
1621
  }
1622
  }
1623
 
1624
- if( !empty( $attachments ) )
1625
- {
1626
  // we're going to store JSON (JSON_UNESCAPED_UNICODE is PHP 5.4+)
1627
  $attachments = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $attachments, JSON_UNESCAPED_UNICODE ) : json_encode( $attachments );
1628
 
@@ -1631,9 +1578,7 @@ if( !class_exists( 'Attachments' ) ) :
1631
 
1632
  // we're going to wipe out any existing Attachments meta (because we'll put it back)
1633
  return update_post_meta( $post_id, $this->meta_key, $attachments );
1634
- }
1635
- else
1636
- {
1637
  // there are no attachments so we'll clean up the record
1638
  return delete_post_meta( $post_id, $this->meta_key );
1639
  }
@@ -1648,27 +1593,26 @@ if( !class_exists( 'Attachments' ) ) :
1648
  *
1649
  * @since 3.3
1650
  */
1651
- function encode_field_value( $field_value = null )
1652
- {
1653
- if( is_null( $field_value ) )
1654
  return false;
 
1655
 
1656
- if( is_object( $field_value ) )
1657
- {
1658
  $input = get_object_vars( $field_value );
1659
 
1660
- foreach( $input as $key => $val )
1661
- $field_value[$key] = $this->encode_field_value( $val );
 
1662
 
1663
  $field_value = (object) $field_value;
1664
- }
1665
- elseif( is_array( $field_value ) )
1666
- {
1667
- foreach( $field_value as $key => $val )
1668
  $field_value[$key] = $this->encode_field_value( $val );
1669
- }
1670
- else
1671
  $field_value = htmlentities( $field_value, ENT_QUOTES, 'UTF-8' );
 
1672
 
1673
  return $field_value;
1674
  }
@@ -1680,44 +1624,45 @@ if( !class_exists( 'Attachments' ) ) :
1680
  *
1681
  * @since 3.0
1682
  */
1683
- function get_attachments( $instance = null, $post_id = null )
1684
- {
1685
  $post_id = $this->determine_post_id( $post_id );
1686
 
1687
- if( !$post_id )
1688
  return false;
 
1689
 
1690
  $attachments = array();
1691
  $attachments_raw = $this->get_attachments_metadata( $post_id );
1692
 
1693
  // we need to decode the fields (that were encoded during save) and run them through
1694
  // their format_value_for_input as defined in it's class
1695
- if( !is_null( $instance ) && is_string( $instance ) && isset( $attachments_raw->$instance ) )
1696
- {
1697
- foreach( $attachments_raw->$instance as $attachment )
1698
  $attachments[] = $this->process_attachment( $attachment, $instance );
1699
- }
1700
- elseif( is_null( $instance ) )
1701
- {
1702
  // return them all, regardless of instance
1703
- if( ( is_array( $attachments_raw ) && count( $attachments_raw ) ) || is_object( $attachments_raw ) )
1704
- {
1705
  // cast an object if necessary
1706
- if( is_object( $attachments_raw ) ) $attachments_raw = (array) $attachments_raw;
1707
 
1708
- foreach( $attachments_raw as $instance => $attachments_unprocessed )
1709
- foreach( $attachments_unprocessed as $unprocessed_attachment )
1710
  $attachments[] = $this->process_attachment( $unprocessed_attachment, $instance );
 
 
1711
  }
1712
  }
1713
 
1714
  // tack on the post ID for each attachment
1715
- for( $i = 0; $i < count( $attachments ); $i++ )
1716
- $attachments[$i]->post_id = $post_id;
 
1717
 
1718
  // we don't want the filter to run on the admin side of things
1719
- if( !is_admin() )
1720
  $attachments = apply_filters( "attachments_get_{$instance}", $attachments );
 
1721
 
1722
  return $attachments;
1723
  }
@@ -1731,21 +1676,25 @@ if( !class_exists( 'Attachments' ) ) :
1731
  *
1732
  * @since 3.3
1733
  */
1734
- function get_attachments_metadata( $post_id )
1735
- {
1736
  $post_id = intval( $post_id );
1737
 
1738
  // grab our JSON and decode it
1739
  $attachments_json = get_post_meta( $post_id, $this->meta_key, true );
1740
  $attachments_raw = is_string( $attachments_json ) ? json_decode( $attachments_json ) : false;
1741
 
1742
- // convert field newline characters properly
1743
- if( !empty( $attachments_raw ) )
1744
- foreach( $attachments_raw as $instanceKey => $instance )
1745
- foreach( $instance as $attachmentKey => $attachment )
1746
- if( isset( $attachment->fields ) )
1747
- foreach( $attachment->fields as $fieldKey => $fieldValue )
1748
- $attachment->fields->$fieldKey = str_replace( '\\n', "\n", $fieldValue );
 
 
 
 
 
1749
 
1750
  return $attachments_raw;
1751
  }
@@ -1757,25 +1706,17 @@ if( !class_exists( 'Attachments' ) ) :
1757
  * @param int $post_id Desired post ID
1758
  * @return mixed The understood post ID
1759
  */
1760
- function determine_post_id( $post_id = null )
1761
- {
1762
  global $post;
1763
 
1764
  // if a post id was passed, we'll use it
1765
- if( !is_null( $post_id ) )
1766
- {
1767
  $post_id = intval( $post_id );
1768
- }
1769
- elseif( is_null( $post_id ) && is_object( $post ) && isset( $post->ID ) )
1770
- {
1771
  $post_id = $post->ID;
1772
- }
1773
- elseif( isset( $_GET['post'] ) )
1774
- {
1775
  $post_id = intval( $_GET['post'] );
1776
- }
1777
- else
1778
- {
1779
  // no post ID, nothing to do...
1780
  $post_id = false;
1781
  }
@@ -1793,40 +1734,30 @@ if( !class_exists( 'Attachments' ) ) :
1793
  *
1794
  * @since 3.3
1795
  */
1796
- function process_attachment( $attachment, $instance )
1797
- {
1798
- if( !is_object( $attachment ) || !is_string( $instance ) )
1799
  return $attachment;
 
1800
 
1801
- if( isset( $attachment->fields ) && is_object( $attachment->fields ) )
1802
- {
1803
- foreach( $attachment->fields as $key => $value )
1804
- {
1805
  // loop through the instance fields to get the type
1806
- if( isset( $this->instances[$instance]['fields'] ) )
1807
- {
1808
  $type = '';
1809
- foreach( $this->instances[$instance]['fields'] as $field )
1810
- {
1811
- if( isset( $field['name'] ) && $field['name'] == $key )
1812
- {
1813
  $type = isset( $field['type'] ) ? $field['type'] : false;
1814
  break;
1815
  }
1816
  }
1817
- if( isset( $this->fields[$type] ) )
1818
- {
1819
  // we need to decode the html entities that were encoded for the save
1820
  $attachment->fields->$key = $this->decode_field_value( $attachment->fields->$key );
1821
- }
1822
- else
1823
- {
1824
  // the type doesn't exist
1825
  $attachment->fields->$key = false;
1826
  }
1827
- }
1828
- else
1829
- {
1830
  // this was a theme file request, just grab it
1831
  $attachment->fields->$key = $this->decode_field_value( $attachment->fields->$key );
1832
  }
@@ -1844,27 +1775,26 @@ if( !class_exists( 'Attachments' ) ) :
1844
  *
1845
  * @since 3.3
1846
  */
1847
- function decode_field_value( $field_value = null )
1848
- {
1849
- if( is_null( $field_value ) )
1850
  return false;
 
1851
 
1852
- if( is_object( $field_value ) )
1853
- {
1854
  $input = get_object_vars( $field_value );
1855
 
1856
- foreach( $input as $key => $val )
1857
  $field_value[$key] = $this->decode_field_value( $val );
 
1858
 
1859
  $field_value = (object) $field_value;
1860
- }
1861
- elseif( is_array( $field_value ) )
1862
- {
1863
- foreach( $field_value as $key => $val )
1864
  $field_value[$key] = $this->decode_field_value( $val );
1865
- }
1866
- else
1867
  $field_value = html_entity_decode( $field_value, ENT_QUOTES, 'UTF-8' );
 
1868
 
1869
  return $field_value;
1870
  }
@@ -1877,11 +1807,11 @@ if( !class_exists( 'Attachments' ) ) :
1877
  * @since 3.0
1878
  */
1879
  function admin_page() {
1880
- if( !( defined( 'ATTACHMENTS_SETTINGS_SCREEN' ) && ATTACHMENTS_SETTINGS_SCREEN === false ) ) {
1881
- if( apply_filters( 'attachments_settings_screen', true ) ) {
1882
- add_options_page( 'Settings', __( 'Attachments', 'attachments' ), 'manage_options', 'attachments', array( $this, 'options_page' ) );
1883
- }
1884
- }
1885
  }
1886
 
1887
 
11
  */
12
 
13
  // Exit if accessed directly
14
+ if ( ! defined( 'ABSPATH' ) ) {
15
+ exit;
16
+ }
17
 
18
  // Declare our class
19
+ if ( ! class_exists( 'Attachments' ) ) :
20
 
21
  /**
22
  * Main Attachments Class
24
  * @since 3.0
25
  */
26
 
27
+ class Attachments {
28
+
29
  private $version; // stores Attachments' version number
30
  private $url; // stores Attachments' URL
31
  private $dir; // stores Attachments' directory
52
  *
53
  * @since 3.0
54
  */
55
+ function __construct( $instance = null, $post_id = null ) {
 
56
  global $_wp_additional_image_sizes;
57
 
58
  // establish our environment variables
59
+ $this->version = '3.5.5';
60
  $this->url = ATTACHMENTS_URL;
61
  $this->dir = ATTACHMENTS_DIR;
62
  $plugin = 'attachments/index.php';
77
  add_filter( "plugin_action_links_$plugin", array( $this, 'plugin_settings_link' ) );
78
 
79
  // add update message(s)
80
+ // add_action( 'in_plugin_update_message-attachments/index.php', array( $this, 'update_message' ) );
81
 
82
  // set up l10n
83
  add_action( 'plugins_loaded', array( $this, 'l10n' ) );
107
  add_action( 'admin_init', array( $this, 'admin_init' ) );
108
 
109
  // execution of actions varies depending on whether we're in the admin or not and an instance was passed
110
+ if( is_admin() ) {
 
111
  add_action( 'after_setup_theme', array( $this, 'apply_init_filters' ), 999 );
112
  $this->attachments = $this->get_attachments( $instance, $post_id );
113
+ } elseif ( ! is_null( $instance ) ) {
 
 
114
  $this->apply_init_filters();
115
  $this->attachments = $this->get_attachments( $instance, $post_id );
116
  }
122
  /**
123
  * Add notification about available Attachments extensions
124
  */
125
+ function update_message() { ?>
 
126
  <div style="margin-top:10px;padding-top:8px;border-top:1px solid #eaeaea;"><span style="color:#f00;"><?php _e( 'Attachments Extensions Available!', 'attachments' ); ?></span> <span style="font-weight:normal;"><?php _e( 'These utilities make working with Attachments even easier!', 'attachments' ); ?></span></div>
127
  <div style="font-weight:normal;padding-top:8px;">
128
  <p><strong><a href="https://mondaybynoon.com/members/plugins/attachments-ui/?utm_campaign=Attachments&utm_term=Upgrade%2bNotice">Attachments UI</a></strong> - <?php _e( 'Create Attachments Instances with an easy-to-use UI, easily limit meta box locations with fine grained control (e.g. Home page only), and more.', 'attachments' ); ?></p>
137
  * @param $links
138
  * @return mixed
139
  */
140
+ function plugin_settings_link( $links ) {
 
141
  $settings_link = '<a href="options-general.php?page=attachments">'. __( 'Settings', 'attachments' ) . '</a>';
142
  array_unshift( $links, $settings_link );
143
  $extend_link = '<a href="https://mondaybynoon.com/members/plugins/?utm_campaign=Attachments&utm_term=Plugins%2bExtend#attachments">'. __( 'Extend', 'attachments' ) . '</a>';
144
  array_unshift( $links, $extend_link );
145
+
146
  return $links;
147
  }
148
 
153
  *
154
  * @since 3.4.3
155
  */
156
+ function admin_init() {
157
+ if ( current_user_can( 'delete_posts' ) ) {
 
158
  add_action( 'delete_post', array( $this, 'handle_wp_post_delete' ), 10 );
159
+ }
160
  }
161
 
162
 
167
  * @return array
168
  * @since 3.5
169
  */
170
+ function get_fields() {
 
171
  return $this->fields;
172
  }
173
 
180
  * @param int $pid Post ID
181
  * @since 3.4.3
182
  */
183
+ function handle_wp_post_delete( $pid ) {
184
+
185
+ if ( ! current_user_can( 'delete_posts' ) ) {
186
+ return;
187
+ }
188
+
189
  // check to make sure it was an attachment
190
+ if( 'attachment' != get_post_type( $pid ) ) {
191
  return;
192
+ }
193
 
194
  // if a user deletes an attachment from the Media library (but it's been used
195
  // in Attachments somewhere else) we need to clean that up...
201
  // so we're going to use the search class to find all instances
202
  // for any occurrence of the deleted attachment (which has an ID of $pid)
203
 
204
+ if ( is_array( $this->instances ) ) {
205
+ foreach( $this->instances as $instance => $details ) {
 
 
206
  $search_args = array(
207
  'instance' => $instance,
208
  'attachment_id' => intval( $pid ),
210
 
211
  $this->search( null, $search_args );
212
 
213
+ if( $this->exist() ) {
 
214
  // we've got a hit (e.g. an existing post uses the deleted attachment)
215
+ while( $attachment = $this->get() ) {
 
216
  $post_id = $attachment->post_id;
217
 
218
  // we'll use the post ID to snag the details
219
  $post_attachments = $this->get_attachments_metadata( $post_id );
220
 
221
+ if( is_object( $post_attachments ) ) {
222
+ foreach( $post_attachments as $existing_instance => $existing_instance_attachments ) {
223
+ foreach( $existing_instance_attachments as $existing_instance_attachment_key => $existing_instance_attachment ) {
224
+ if( $pid == intval( $existing_instance_attachment->id ) ) {
 
225
  unset( $post_attachments->{$existing_instance}[$existing_instance_attachment_key] );
226
+ }
227
+ }
228
+ }
229
 
230
  // saving routine assumes array from POST so we'll typecast it
231
  $post_attachments = (array) $post_attachments;
246
  *
247
  * @since 3.4.2
248
  */
249
+ function get_meta_key() {
 
250
  return $this->meta_key;
251
  }
252
 
257
  *
258
  * @since 3.5
259
  */
260
+ function get_url() {
 
261
  return $this->url;
262
  }
263
 
268
  *
269
  * @since 3.4.2
270
  */
271
+ function l10n() {
272
+ load_plugin_textdomain( 'attachments', false, 'attachments/languages/' );
 
273
  }
274
 
275
 
276
 
277
+ function load_extensions() {
 
278
  do_action( 'attachments_extension', $this );
279
  }
280
 
285
  *
286
  * @since 3.4
287
  */
288
+ function apply_init_filters() {
 
289
  // allows a different meta_key to be used
290
  $this->meta_key = apply_filters( 'attachments_meta_key', $this->meta_key );
291
  }
297
  *
298
  * @since 3.3
299
  */
300
+ function search( $query = null, $params = array() ) {
 
301
  $results = new AttachmentsSearch( $query, $params );
302
  $this->attachments = $results->results;
303
  }
309
  *
310
  * @since 3.0
311
  */
312
+ function exist() {
313
+ return ! empty( $this->attachments );
 
314
  }
315
 
316
 
320
  *
321
  * @since 3.0.6
322
  */
323
+ function total() {
 
324
  return count( $this->attachments );
325
  }
326
 
331
  *
332
  * @since 3.6
333
  */
334
+ function rewind() {
 
335
  $this->attachments_ref = -1;
336
  }
337
 
342
  *
343
  * @since 3.0
344
  */
345
+ function get() {
 
346
  $this->attachments_ref++;
347
 
348
+ if ( ! count( $this->attachments ) || $this->attachments_ref >= count( $this->attachments ) ) {
349
  return false;
350
+ }
351
 
352
+ return $this->attachments[ $this->attachments_ref ];
353
  }
354
 
355
 
359
  *
360
  * @since 3.2
361
  */
362
+ function get_single( $index ) {
363
+ return isset( $this->attachments[ $index ] ) ? $this->attachments[ $index ] : false;
 
364
  }
365
 
366
 
370
  *
371
  * @since 3.0.6
372
  */
373
+ function asset( $size = 'thumbnail', $index = null ) {
 
374
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
375
 
376
  // do we have our meta yet?
377
+ if ( ! isset( $this->attachments[ $index ]->meta ) ) {
378
+ $this->attachments[ $index ]->meta = wp_get_attachment_metadata( $this->attachments[ $index ]->id );
379
+ }
380
 
381
  // is it an image?
382
+ if ( isset( $this->attachments[ $index ]->meta['sizes'] ) ) {
383
+ $asset = wp_get_attachment_image_src( $this->attachments[ $index ]->id, $size );
384
+ } else {
 
 
 
385
  // either it's not an image or we don't have the proper size, so we'll use the icon
386
  $asset = $this->icon( $index );
387
  }
396
  *
397
  * @since 3.0.6
398
  */
399
+ function icon( $index = null ) {
 
400
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
401
+ $asset = wp_get_attachment_image_src( $this->attachments[ $index ]->id, null, true );
402
 
403
  return $asset;
404
  }
411
  *
412
  * @since 3.4.1
413
  */
414
+ function date( $d = "d/m/Y", $index = null ) {
 
415
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
416
+ $date = get_the_time( $d, $this->attachments[ $index ]->id );
417
 
418
  return $date;
419
  }
425
  *
426
  * @since 3.0
427
  */
428
+ function image( $size = 'thumbnail', $index = null ) {
 
429
  $asset = $this->asset( $size, $index );
430
 
431
  $image_src = $asset[0];
432
  $image_width = $asset[1];
433
  $image_height = $asset[2];
434
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
435
+ $image_alt = get_post_meta( $this->attachments[ $index ]->id, '_wp_attachment_image_alt', true );
436
 
437
+ $image = '<img src="' . esc_url( $image_src ) . '" width="' . absint( $image_width ) . '" height="' . absint( $image_height ) . '" alt="' . esc_attr( $image_alt ) . '" />';
438
 
439
  return $image;
440
  }
446
  *
447
  * @since 3.0
448
  */
449
+ function src( $size = 'thumbnail', $index = null ) {
 
450
  $asset = $this->asset( $size, $index );
451
+
452
+ return esc_url( $asset[0] );
453
  }
454
 
455
 
459
  *
460
  * @since 3.5
461
  */
462
+ function width( $size = 'thumbnail', $index = null ) {
 
463
  $asset = $this->asset( $size, $index );
464
+
465
+ return absint( $asset[1] );
466
  }
467
 
468
 
472
  *
473
  * @since 3.5
474
  */
475
+ function height( $size = 'thumbnail', $index = null ) {
 
476
  $asset = $this->asset( $size, $index );
477
+
478
+ return absint( $asset[2] );
479
  }
480
 
481
 
485
  *
486
  * @since 3.0
487
  */
488
+ function filesize( $index = null, $size = null ) {
 
489
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
490
 
491
+ if ( ! isset( $this->attachments[ $index ]->id ) ) {
492
  return false;
493
+ }
494
 
495
  // if an image size is passed along, use that
496
+ $url = is_string( $size ) ? $this->src( $size, $index ) : wp_get_attachment_url( $this->attachments[ $index ]->id );
497
  $uploads = wp_upload_dir();
498
  $file_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $url );
499
 
500
  $formatted = '0 bytes';
501
+ if ( file_exists( $file_path ) ) {
502
  $formatted = size_format( @filesize( $file_path ) );
503
+ }
504
 
505
  return $formatted;
506
  }
512
  *
513
  * @since 3.0
514
  */
515
+ function type( $index = null ) {
 
516
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
517
 
518
+ if ( ! isset( $this->attachments[$index]->id ) ) {
519
  return false;
520
+ }
521
 
522
  $attachment_mime = $this->get_mime_type( $this->attachments[$index]->id );
523
+
524
  return $attachment_mime;
525
  }
526
 
531
  *
532
  * @since 3.4.2
533
  */
534
+ function get_mime_type( $id = null ) {
 
535
  $attachment_mime = explode( '/', get_post_mime_type( intval( $id ) ) );
536
+
537
  return isset( $attachment_mime[0] ) ? $attachment_mime[0] : false;
538
  }
539
 
544
  *
545
  * @since 3.0
546
  */
547
+ function subtype( $index = null ) {
 
548
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
549
 
550
+ if ( ! isset( $this->attachments[$index]->id ) ) {
551
  return false;
552
+ }
553
+
554
+ $attachment_mime = explode( '/', get_post_mime_type( $this->attachments[ $index ]->id ) );
555
 
 
556
  return isset( $attachment_mime[1] ) ? $attachment_mime[1] : false;
557
  }
558
 
563
  *
564
  * @since 3.0
565
  */
566
+ function id( $index = null ) {
 
567
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
568
 
569
+ return isset( $this->attachments[ $index ]->id ) ? $this->attachments[ $index ]->id : false;
570
  }
571
 
572
 
576
  *
577
  * @since 3.3
578
  */
579
+ function post_id( $index = null ) {
 
580
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
581
 
582
+ return isset( $this->attachments[ $index ]->post_id ) ? $this->attachments[ $index ]->post_id : false;
583
  }
584
 
585
 
589
  *
590
  * @since 3.0
591
  */
592
+ function url( $index = null ) {
 
593
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
594
 
595
+ return isset( $this->attachments[ $index ]->id ) ? wp_get_attachment_url( $this->attachments[ $index ]->id ) : false;
596
  }
597
 
598
 
602
  *
603
  * @since 3.0
604
  */
605
+ function field( $name = 'title', $index = null ) {
 
606
  $index = is_null( $index ) ? $this->attachments_ref : intval( $index );
607
 
608
+ return isset( $this->attachments[ $index ]->fields->$name ) ? $this->attachments[ $index ]->fields->$name : false;
609
  }
610
 
611
 
615
  *
616
  * @since 3.0
617
  */
618
+ function setup_instances() {
 
619
  // implement our default instance if appropriate
620
+ $filtered = apply_filters( 'attachments_default_instance', true );
621
+ if ( $filtered && ( ! defined( 'ATTACHMENTS_DEFAULT_INSTANCE' ) || true == ATTACHMENTS_DEFAULT_INSTANCE ) ) {
622
+ $this->register();
623
  }
624
 
625
  // facilitate user-defined instance registration
633
  *
634
  * @since 3.0
635
  */
636
+ function assets( $hook ) {
 
637
  global $post;
638
 
639
  // we only want our assets on edit screens
640
+ if ( ! empty( $this->instances_for_post_type ) && 'edit.php' != $hook && 'post.php' != $hook && 'post-new.php' != $hook ) {
641
  return;
642
+ }
643
 
644
  // we only want to enqueue if appropriate
645
+ if ( empty( $this->instances_for_post_type ) ) {
646
  return;
647
+ }
648
 
649
  $post_id = isset( $post->ID ) ? $post->ID : null;
650
  wp_enqueue_media( array( 'post' => $post_id ) );
661
  *
662
  * @since 3.0
663
  */
664
+ function meta_box_init() {
 
665
  $nonce_sent = false;
666
 
667
+ if ( !empty ( $this->instances_for_post_type ) ) {
668
+ foreach( $this->instances_for_post_type as $instance ) {
 
 
669
  // facilitate more fine-grained meta box positioning than post type
670
  $applicable = apply_filters( "attachments_location_{$instance}", true, $instance );
671
 
677
  $priority = isset($instance->priority) ? $instance->priority : 'high';
678
 
679
  if( $applicable ) {
680
+ add_meta_box(
681
+ 'attachments-' . $instance_name,
682
+ __( esc_attr( $instance->label ) ),
683
+ array( $this, 'meta_box_markup' ),
684
+ $this->get_post_type(),
685
+ $position,
686
+ $priority,
687
+ array(
688
+ 'instance' => $instance,
689
+ 'setup_nonce' => !$nonce_sent
690
+ )
691
+ );
692
+ $nonce_sent = true;
693
  }
694
 
695
  }
703
  *
704
  * @since 3.0
705
  */
706
+ function meta_box_markup( $post, $metabox ) {
 
707
  // single out our $instance
708
  $instance = (object) $metabox['args']['instance'];
709
 
710
+ if( $metabox['args']['setup_nonce'] ) {
711
  wp_nonce_field( 'attachments_save', 'attachments_nonce' );
712
+ }
713
 
714
  ?>
715
 
716
+ <div id="attachments-<?php echo esc_attr( $instance->name ); ?>" class="attachments-parent-container<?php if( $instance->append == false ) : ?> attachments-prepend<?php endif; ?>">
717
  <?php if( ! empty( $instance->note ) ) : ?>
718
  <div class="attachments-note"><?php echo wpautop( $instance->note ); ?></div>
719
  <?php endif; ?>
722
  <a class="button attachments-invoke"><?php _e( esc_attr( $instance->button_text ), 'attachments' ); ?></a>
723
  </div>
724
  <?php endif; ?>
725
+ <div class="attachments-container attachments-<?php echo esc_attr( $instance->name ); ?>"><?php
726
+ if ( isset( $instance->attachments ) && !empty( $instance->attachments ) ) {
727
+ foreach( $instance->attachments as $attachment ) {
 
 
728
  // we need to give our Attachment a uid to carry through to all the fields
729
  $attachment->uid = uniqid();
730
 
746
  button = '<?php echo __( esc_attr( $instance->modal_text ) ); ?>',
747
  router = '<?php echo __( esc_attr( $instance->router ) ); ?>',
748
  limit = <?php echo intval( $instance->limit ); ?>,
749
+ existing = <?php echo ( isset( $instance->attachments ) && ! empty( $instance->attachments ) ) ? count( $instance->attachments ): 0; ?>,
750
  attachmentsframe,
751
  editframe;
752
 
841
 
842
  // if we're in a sidebar we DO want to show the fields which are normally hidden on load via CSS
843
  if($element.parents('#side-sortables')){
844
+ $element.find('.attachments-attachment:<?php if( $instance->append ) : ?>last<?php else : ?>first<?php endif; ?> .attachments-fields').show();
845
  }
846
 
847
  // see if we need to set a default
848
  // TODO: can we tie this into other field types (select, radio, checkbox)?
849
+ $element.find('.attachments-attachment:<?php if( $instance->append ) : ?>last<?php else : ?>first<?php endif; ?> .attachments-fields input, .attachments-attachment:<?php if( $instance->append ) : ?>last<?php else : ?>first<?php endif; ?> .attachments-fields textarea').each(function(){
850
  if($(this).data('default')){
851
  var meta_for_default = $(this).data('default');
852
  if(attachments_isset(attachment.attributes)){
861
 
862
  // if it wasn't an image we need to ditch the dimensions
863
  if(!attachments_isset(attachment.attributes.width)||!attachments_isset(attachment.attributes.height)){
864
+ $element.find('.attachments-attachment:<?php if( $instance->append ) : ?>last<?php else : ?>first<?php endif; ?> .dimensions').hide();
865
  }
866
  });
867
  });
973
  *
974
  * @since 3.0
975
  **/
976
+ function get_field_types() {
 
977
  $field_types = array(
978
  'text' => ATTACHMENTS_DIR . 'classes/fields/class.field.text.php',
979
  'textarea' => ATTACHMENTS_DIR . 'classes/fields/class.field.textarea.php',
985
  // $field_types = apply_filters( 'attachments_fields', $field_types );
986
 
987
  $field_index = 0;
988
+ foreach ( $field_types as $type => $path ) {
 
989
  // proceed with inclusion
990
+ if ( file_exists( $path ) ) {
 
991
  // include the file
992
  include_once( $path );
993
 
1006
  $field_class = $existing_classes[$flag + $field_index + 1];
1007
 
1008
  // create our link using our new field class
1009
+ $field_types[ $type ] = $field_class;
1010
 
1011
  $field_index++;
1012
  }
1023
  *
1024
  * @since 3.0
1025
  */
1026
+ function register_field( $params = array() ) {
 
1027
  $defaults = array(
1028
  'name' => 'title',
1029
  'type' => 'text',
1034
  $params = array_merge( $defaults, $params );
1035
 
1036
  // ensure it's a valid type
1037
+ if ( ! isset( $this->fields[$params['type']] ) ) {
1038
  return false;
1039
+ }
1040
 
1041
+ // sanitize
1042
+ if ( isset( $params['name'] ) ) {
1043
  $params['name'] = str_replace( '-', '_', sanitize_title( $params['name'] ) );
1044
+ }
1045
 
1046
+ if ( isset( $params['label'] ) ) {
1047
  $params['label'] = __( esc_html( $params['label'] ) );
1048
+ }
1049
 
1050
+ if ( !isset( $params['meta'] ) || !is_array( $params['meta'] ) ) {
1051
  $params['meta'] = array();
1052
+ }
1053
 
1054
  // instantiate the class for this field and send it back
1055
  return new $this->fields[ $params['type'] ]( $params['name'], $params['label'], $params['meta'] );
1062
  *
1063
  * @since 3.0
1064
  */
1065
+ function register( $name = 'attachments', $params = array() ) {
 
1066
  $defaults = array(
1067
 
1068
  // title of the meta box (string)
1120
  $params = array_merge( $defaults, $params );
1121
 
1122
  // sanitize
1123
+ if ( ! is_array( $params['post_type'] ) ) {
1124
  $params['post_type'] = array( $params['post_type'] ); // we always want an array
1125
+ }
1126
 
1127
+ if ( ! is_array( $params['filetype'] ) ) {
1128
  $params['filetype'] = array( $params['filetype'] ); // we always want an array
1129
+ }
1130
 
1131
  $params['label'] = esc_html( $params['label'] );
1132
  $params['limit'] = intval( $params['limit'] );
1133
+ $params['note'] = esc_html( $params['note'] );
1134
  $params['button_text'] = esc_attr( $params['button_text'] );
1135
  $params['modal_text'] = esc_attr( $params['modal_text'] );
1136
 
1137
  // make sure we've got valid filetypes
1138
+ if ( is_array( $params['filetype'] ) ) {
1139
+ foreach ( $params['filetype'] as $key => $filetype ) {
1140
+ if ( ! in_array( $filetype, $this->valid_filetypes ) ) {
1141
+ unset( $params['filetype'][ $key ] );
 
 
 
1142
  }
1143
  }
1144
  }
1145
 
1146
  // WordPress sanitizes post type names when registering, so we will too
1147
+ foreach ( $params['post_type'] as $key => $post_type ) {
1148
+ $params['post_type'][ $key ] = sanitize_key( $post_type );
1149
+ }
1150
 
1151
  // make sure the instance name is proper
1152
  $instance = str_replace( '-', '_', sanitize_title( $name ) );
1153
 
1154
  // register the fields
1155
+ if ( isset( $params['fields'] ) && is_array( $params['fields'] ) && count( $params['fields'] ) ) {
1156
+ foreach( $params['fields'] as $field ) {
 
 
1157
  // register the field
1158
  $this->register_field( $field );
1159
  }
1160
  }
1161
 
1162
  // set the instance
1163
+ $this->instances[ $instance ] = $params;
1164
 
1165
  // set the Attachments for this instance
1166
+ $this->instances[ $instance ]['attachments'] = $this->get_attachments( $instance );
1167
 
1168
  }
1169
 
1174
  *
1175
  * @since 3.0
1176
  */
1177
+ function get_instances_for_post_type( $post_type = null ) {
1178
+ $post_type = ( ! is_null( $post_type ) && post_type_exists( $post_type ) ) ? $post_type : $this->get_post_type();
 
1179
 
1180
  $instances = array();
1181
 
1182
+ if ( ! empty( $this->instances ) ) {
1183
+ foreach ( $this->instances as $name => $params ) {
1184
+ if ( in_array( $post_type, $params['post_type'] ) ) {
 
 
 
1185
  $instances[] = $name;
1186
  }
1187
  }
1198
  *
1199
  * @since 3.0
1200
  */
1201
+ function get_post_type() {
 
1202
  global $post;
1203
 
1204
  // TODO: Retrieving the post_type at this point is ugly to say the least. This needs major cleanup.
1205
+ if ( empty( $post->ID ) && isset( $_GET['post_type'] ) ) {
1206
+ $post_type = sanitize_text_field( $_GET['post_type'] );
1207
+ } elseif( !empty( $post->ID ) ) {
 
 
 
1208
  $post_type = get_post_type( $post->ID );
1209
+ } elseif( isset( $_GET['post'] ) ) {
 
 
1210
  $post_type = get_post_type( intval( $_GET['post'] ) );
1211
+ } else {
 
 
1212
  $post_type = 'post';
1213
  }
1214
 
1222
  *
1223
  * @since 3.0
1224
  */
1225
+ function set_instances_for_current_post_type() {
 
1226
  // store the applicable instances for this post type
1227
  $this->instances_for_post_type = $this->get_instances_for_post_type( $this->get_post_type() );
1228
  }
1234
  *
1235
  * @since 3.0
1236
  */
1237
+ function create_attachment_field( $instance, $field, $attachment = null ) {
 
1238
 
1239
  // the $field at this point is just the user-declared array
1240
  // we need to make it a field object
1241
  $type = $field['type'];
1242
 
1243
+ if( isset( $this->fields[ $type ] ) ) {
 
1244
  $name = sanitize_title( $field['name'] );
1245
  $label = esc_html( $field['label'] );
1246
  $default = isset( $field['default'] ) ? $field['default'] : false; // validated in the class
1247
  $meta = isset( $field['meta'] ) ? $field['meta'] : array();
1248
  $value = isset( $attachment->fields->$name ) ? $attachment->fields->$name : null;
1249
 
1250
+ $field = new $this->fields[ $type ]( $name, $label, $value, $meta );
1251
  $field->value = $field->format_value_for_input( $field->value );
1252
 
1253
  // does this field already have a unique ID?
1260
  $field->set_field_default( $default );
1261
 
1262
  ?>
1263
+ <div class="attachments-attachment-field attachments-attachment-field-<?php echo esc_attr( $instance ); ?> attachments-attachment-field-<?php echo esc_attr( $field->type ); ?> attachment-field-<?php echo esc_attr( $field->name ); ?>">
1264
+ <div class="attachment-label attachment-label-<?php echo esc_attr( $instance ); ?>">
1265
+ <label for="<?php echo esc_attr( $field->field_id ); ?>"><?php echo esc_html( $field->label ); ?></label>
1266
  </div>
1267
+ <div class="attachment-field attachment-field-<?php echo esc_attr( $instance ); ?>">
1268
  <?php echo $this->create_field( $instance, $field ); ?>
1269
  </div>
1270
  </div>
1271
+ <?php } else {
 
 
 
1272
  $field = false;
1273
  }
1274
 
1282
  *
1283
  * @since 3.0
1284
  */
1285
+ function create_field( $instance, $field ) {
 
1286
  $field = (object) $field;
1287
 
1288
  // with all of our attributes properly set, we can output
1296
  *
1297
  * @since 3.0
1298
  */
1299
+ function create_attachment( $instance, $attachment = null ) {
 
1300
  ?>
1301
+ <div class="attachments-attachment attachments-attachment-<?php echo esc_attr( $instance ); ?>">
1302
  <?php $array_flag = ( isset( $attachment->uid ) ) ? $attachment->uid : '{{ attachments.attachment_uid }}'; ?>
1303
 
1304
+ <input type="hidden" class="attachments-track-id" name="attachments[<?php echo esc_attr( $instance ); ?>][<?php echo esc_attr( $array_flag ); ?>][id]" value="<?php echo isset( $attachment->id ) ? esc_attr( $attachment->id ) : '{{ attachments.id }}' ; ?>" />
1305
 
1306
  <?php
1307
  // since attributes can change over time (image gets replaced, cropped, etc.) we'll pull that info
1308
+ if ( isset( $attachment->id ) ) {
 
1309
  // we'll just use the full size since that's what Media in 3.5 uses
1310
  $attachment_meta = wp_get_attachment_metadata( $attachment->id );
1311
 
1312
  // only images return the 'file' key
1313
+ if ( ! isset( $attachment_meta['file'] ) ) {
1314
  $attachment_meta['file'] = get_attached_file( $attachment->id );
1315
+ $filename = explode( "/", $attachment_meta['file'] );
1316
+ }
1317
 
1318
  $attachment->width = isset( $attachment_meta['width'] ) ? $attachment_meta['width'] : null;
1319
  $attachment->height = isset( $attachment_meta['height'] ) ? $attachment_meta['height'] : null;
1335
  <img src="<?php echo $image; ?>" alt="Thumbnail" />
1336
  </div>
1337
  <div class="attachment-details attachment-info details">
1338
+ <div class="filename"><?php echo isset( $attachment->filename ) ? esc_html( $attachment->filename ) : '{{ attachments.filename }}' ; ?></div>
1339
+ <?php if( ( isset( $attachment->id ) && isset( $attachment->width ) ) || ! isset( $attachment->id ) ) : ?>
1340
+ <div class="dimensions"><?php echo isset( $attachment->width ) ? esc_html( $attachment->width ) : '{{ attachments.width }}' ; ?> &times; <?php echo isset( $attachment->height ) ? esc_html( $attachment->height ) : '{{ attachments.height }}' ; ?></div>
1341
  <?php endif; ?>
1342
  <div class="edit-attachment-asset"><a href="#"><?php _e( 'Change', 'attachments' ); ?></a></div>
1343
  <div class="delete-attachment"><a href="#"><?php _e( 'Remove', 'attachments' ); ?></a></div>
1349
 
1350
  <div class="attachments-fields">
1351
  <?php
1352
+ foreach( $this->instances[$instance]['fields'] as $field ) {
1353
  $field_ref = $this->create_attachment_field( $instance, $field, $attachment );
1354
+ }
1355
  ?>
1356
  </div>
1357
 
1366
  *
1367
  * @since 3.1
1368
  */
1369
+ function field_assets() {
 
1370
  global $post;
1371
 
1372
  // we only want to enqueue if we're on an edit screen and it's applicable
1373
+ if ( empty( $this->instances_for_post_type ) || empty( $post ) ) {
1374
  return;
1375
+ }
1376
 
1377
  // all metaboxes have been put in place, we can now determine which field assets need to be included
1378
 
1379
  // first we'll get a list of the field types on screen
1380
  $fieldtypes = array();
1381
+ foreach ( $this->instances_for_post_type as $instance ) {
1382
+ foreach ( $this->instances[$instance]['fields'] as $field ) {
1383
  $fieldtypes[] = $field['type'];
1384
+ }
1385
+ }
1386
 
1387
  // we only want to dump out assets once for each field type
1388
  $fieldtypes = array_unique( $fieldtypes );
1389
 
1390
  // loop through and dump out all the assets
1391
+ foreach ( $fieldtypes as $fieldtype ) {
1392
+ $field = new $this->fields[ $fieldtype ];
 
1393
  $field->assets();
1394
  }
1395
  }
1401
  *
1402
  * @since 3.1
1403
  */
1404
+ function field_inits() {
 
1405
  global $post;
1406
 
1407
  // we only want to enqueue if we're on an edit screen and it's applicable
1408
+ if ( empty( $this->instances_for_post_type ) || empty( $post ) ) {
1409
  return;
1410
+ }
1411
 
1412
  // all metaboxes have been put in place, we can now determine which field assets need to be included
1413
 
1414
  // first we'll get a list of the field types on screen
1415
  $fieldtypes = array();
1416
+ foreach ( $this->instances_for_post_type as $instance ) {
1417
+ foreach( $this->instances[ $instance ]['fields'] as $field ) {
1418
  $fieldtypes[] = $field['type'];
1419
+ }
1420
+ }
1421
 
1422
  // we only want to dump out assets once for each field type
1423
  $fieldtypes = array_unique( $fieldtypes );
1424
 
1425
  // loop through and dump out all the assets
1426
+ foreach( $fieldtypes as $fieldtype ) {
1427
+ $field = new $this->fields[ $fieldtype ];
 
1428
  $field->init();
1429
  }
1430
  }
1437
  *
1438
  * @since 3.0
1439
  */
1440
+ function admin_footer() {
1441
+ if ( ! empty( $this->instances_for_post_type ) ) { ?>
 
 
1442
  <script type="text/javascript">
1443
  var ATTACHMENTS_VIEWS = {};
1444
  </script>
1445
+ <?php foreach ( $this->instances_for_post_type as $instance ) : ?>
1446
+ <script type="text/template" id="tmpl-attachments-<?php echo esc_attr( $instance ); ?>">
 
1447
  <?php $this->create_attachment( $instance ); ?>
1448
  </script>
1449
  <?php endforeach;
1457
  *
1458
  * @since 3.0
1459
  */
1460
+ function save( $post_id ) {
 
1461
  // is the user logged in?
1462
+ if ( ! is_user_logged_in() ) {
1463
  return $post_id;
1464
+ }
1465
 
1466
  // is the nonce set?
1467
+ if ( ! isset( $_POST['attachments_nonce'] ) ) {
1468
  return $post_id;
1469
+ }
1470
 
1471
  // is the nonce valid?
1472
+ if ( ! wp_verify_nonce( $_POST['attachments_nonce'], 'attachments_save' ) ) {
1473
  return $post_id;
1474
+ }
1475
 
1476
  // can this user edit this post?
1477
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
1478
  return $post_id;
1479
+ }
1480
 
1481
  // passed authentication, proceed with save
1482
 
1498
  * @return bool
1499
  * @since 3.4.3
1500
  */
1501
+ function save_metadata( $post_id = 0, $attachments_meta = null ) {
1502
+ if ( ! is_array( $attachments_meta ) || ! is_int( $post_id ) || intval( $post_id ) < 1 ) {
 
1503
  return false;
1504
+ }
1505
 
1506
  // final data store
1507
  $attachments = array();
1508
 
1509
  // loop through each submitted instance
1510
+ foreach ( $attachments_meta as $instance => $instance_attachments ) {
 
1511
  // loop through each Attachment of this instance
1512
+ foreach( $instance_attachments as $key => $attachment ) {
1513
+ // see if it was pulled as JSON from a delete cleanup
1514
+ if ( is_object( $attachment ) ) {
1515
+ $attachment = get_object_vars( $attachment );
1516
+ if ( is_array( $attachment ) && !empty( $attachment ) ) {
1517
+ if ( isset( $attachment['fields'] ) && is_object( $attachment['fields'] ) ) {
1518
+ $attachment['fields'] = get_object_vars( $attachment['fields'] );
1519
+ }
1520
+ }
1521
+ }
1522
+
1523
+ $attachment_exists = isset( $attachment['id'] ) ? get_post( absint( $attachment['id'] ) ) : false;
 
 
 
1524
 
1525
  // make sure the attachment exists
1526
+ if ( $attachment_exists ) {
 
1527
  // since we're using JSON for storage in the database, we need
1528
  // to make sure that characters are encoded that would otherwise
1529
  // break the JSON
1530
+ if ( isset( $attachment['fields'] ) && is_array( $attachment['fields'] ) ) {
 
1531
 
1532
+ foreach( $attachment['fields'] as $key => $field_value ) {
 
1533
  // take care of our returns
1534
  $field_value = str_replace( "\r\n", "\n", $field_value );
1535
  $field_value = str_replace( "\r", "\n", $field_value );
1547
  $field_value = $this->encode_field_value( $field_value );
1548
 
1549
  // encode things properly
1550
+ $attachment['fields'][ $key ] = $field_value;
1551
  }
1552
  }
1553
 
1554
+ // set the post parent if applicable
1555
+ // need to first check to make sure we're not overwriting a native Attach
1556
+ $attach_post_ref = $attachment_exists;
1557
+ if ( $attach_post_ref->post_parent == 0 && ! empty( $this->instances[ $instance ]['post_parent'] ) ) {
1558
+ // no current Attach, we can add ours
1559
+ $attach_post = array(
1560
+ 'ID' => absint( $attachment['id'] ),
1561
+ 'post_parent' => $post_id,
1562
+ );
1563
+
1564
+ wp_update_post( $attach_post );
1565
+ }
1566
 
1567
+ $attachments[ $instance ][] = $attachment;
1568
  }
1569
  }
1570
  }
1571
 
1572
+ if ( ! empty( $attachments ) ) {
 
1573
  // we're going to store JSON (JSON_UNESCAPED_UNICODE is PHP 5.4+)
1574
  $attachments = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $attachments, JSON_UNESCAPED_UNICODE ) : json_encode( $attachments );
1575
 
1578
 
1579
  // we're going to wipe out any existing Attachments meta (because we'll put it back)
1580
  return update_post_meta( $post_id, $this->meta_key, $attachments );
1581
+ } else {
 
 
1582
  // there are no attachments so we'll clean up the record
1583
  return delete_post_meta( $post_id, $this->meta_key );
1584
  }
1593
  *
1594
  * @since 3.3
1595
  */
1596
+ function encode_field_value( $field_value = null ) {
1597
+ if ( is_null( $field_value ) ) {
 
1598
  return false;
1599
+ }
1600
 
1601
+ if ( is_object( $field_value ) ) {
 
1602
  $input = get_object_vars( $field_value );
1603
 
1604
+ foreach ( $input as $key => $val ) {
1605
+ $field_value[ $key ] = $this->encode_field_value( $val );
1606
+ }
1607
 
1608
  $field_value = (object) $field_value;
1609
+ } elseif ( is_array( $field_value ) ) {
1610
+ foreach ( $field_value as $key => $val ) {
 
 
1611
  $field_value[$key] = $this->encode_field_value( $val );
1612
+ }
1613
+ } else {
1614
  $field_value = htmlentities( $field_value, ENT_QUOTES, 'UTF-8' );
1615
+ }
1616
 
1617
  return $field_value;
1618
  }
1624
  *
1625
  * @since 3.0
1626
  */
1627
+ function get_attachments( $instance = null, $post_id = null ) {
 
1628
  $post_id = $this->determine_post_id( $post_id );
1629
 
1630
+ if ( ! $post_id ) {
1631
  return false;
1632
+ }
1633
 
1634
  $attachments = array();
1635
  $attachments_raw = $this->get_attachments_metadata( $post_id );
1636
 
1637
  // we need to decode the fields (that were encoded during save) and run them through
1638
  // their format_value_for_input as defined in it's class
1639
+ if ( ! is_null( $instance ) && is_string( $instance ) && isset( $attachments_raw->$instance ) ) {
1640
+ foreach( $attachments_raw->$instance as $attachment ) {
 
1641
  $attachments[] = $this->process_attachment( $attachment, $instance );
1642
+ }
1643
+ } elseif ( is_null( $instance ) ) {
 
1644
  // return them all, regardless of instance
1645
+ if ( ( is_array( $attachments_raw ) && count( $attachments_raw ) ) || is_object( $attachments_raw ) ) {
 
1646
  // cast an object if necessary
1647
+ if ( is_object( $attachments_raw ) ) $attachments_raw = (array) $attachments_raw;
1648
 
1649
+ foreach( $attachments_raw as $instance => $attachments_unprocessed ) {
1650
+ foreach( $attachments_unprocessed as $unprocessed_attachment ) {
1651
  $attachments[] = $this->process_attachment( $unprocessed_attachment, $instance );
1652
+ }
1653
+ }
1654
  }
1655
  }
1656
 
1657
  // tack on the post ID for each attachment
1658
+ for ( $i = 0; $i < count( $attachments ); $i++ ) {
1659
+ $attachments[ $i ]->post_id = $post_id;
1660
+ }
1661
 
1662
  // we don't want the filter to run on the admin side of things
1663
+ if ( ! is_admin() ) {
1664
  $attachments = apply_filters( "attachments_get_{$instance}", $attachments );
1665
+ }
1666
 
1667
  return $attachments;
1668
  }
1676
  *
1677
  * @since 3.3
1678
  */
1679
+ function get_attachments_metadata( $post_id ) {
 
1680
  $post_id = intval( $post_id );
1681
 
1682
  // grab our JSON and decode it
1683
  $attachments_json = get_post_meta( $post_id, $this->meta_key, true );
1684
  $attachments_raw = is_string( $attachments_json ) ? json_decode( $attachments_json ) : false;
1685
 
1686
+ // convert field newline characters properly
1687
+ if ( ! empty( $attachments_raw ) ) {
1688
+ foreach ( $attachments_raw as $instanceKey => $instance ) {
1689
+ foreach( $instance as $attachmentKey => $attachment ) {
1690
+ if( isset( $attachment->fields ) ) {
1691
+ foreach( $attachment->fields as $fieldKey => $fieldValue ) {
1692
+ $attachment->fields->$fieldKey = str_replace( '\\n', "\n", $fieldValue );
1693
+ }
1694
+ }
1695
+ }
1696
+ }
1697
+ }
1698
 
1699
  return $attachments_raw;
1700
  }
1706
  * @param int $post_id Desired post ID
1707
  * @return mixed The understood post ID
1708
  */
1709
+ function determine_post_id( $post_id = null ) {
 
1710
  global $post;
1711
 
1712
  // if a post id was passed, we'll use it
1713
+ if ( !is_null( $post_id ) ) {
 
1714
  $post_id = intval( $post_id );
1715
+ } elseif( is_null( $post_id ) && is_object( $post ) && isset( $post->ID ) ) {
 
 
1716
  $post_id = $post->ID;
1717
+ } elseif( isset( $_GET['post'] ) ) {
 
 
1718
  $post_id = intval( $_GET['post'] );
1719
+ } else {
 
 
1720
  // no post ID, nothing to do...
1721
  $post_id = false;
1722
  }
1734
  *
1735
  * @since 3.3
1736
  */
1737
+ function process_attachment( $attachment, $instance ) {
1738
+ if ( ! is_object( $attachment ) || !is_string( $instance ) ) {
 
1739
  return $attachment;
1740
+ }
1741
 
1742
+ if ( isset( $attachment->fields ) && is_object( $attachment->fields ) ) {
1743
+ foreach ( $attachment->fields as $key => $value ) {
 
 
1744
  // loop through the instance fields to get the type
1745
+ if ( isset( $this->instances[ $instance ]['fields'] ) ) {
 
1746
  $type = '';
1747
+ foreach ( $this->instances[$instance]['fields'] as $field ) {
1748
+ if ( isset( $field['name'] ) && $field['name'] == $key ) {
 
 
1749
  $type = isset( $field['type'] ) ? $field['type'] : false;
1750
  break;
1751
  }
1752
  }
1753
+ if ( isset( $this->fields[$type] ) ) {
 
1754
  // we need to decode the html entities that were encoded for the save
1755
  $attachment->fields->$key = $this->decode_field_value( $attachment->fields->$key );
1756
+ } else {
 
 
1757
  // the type doesn't exist
1758
  $attachment->fields->$key = false;
1759
  }
1760
+ } else {
 
 
1761
  // this was a theme file request, just grab it
1762
  $attachment->fields->$key = $this->decode_field_value( $attachment->fields->$key );
1763
  }
1775
  *
1776
  * @since 3.3
1777
  */
1778
+ function decode_field_value( $field_value = null ) {
1779
+ if ( is_null( $field_value ) ) {
 
1780
  return false;
1781
+ }
1782
 
1783
+ if ( is_object( $field_value ) ) {
 
1784
  $input = get_object_vars( $field_value );
1785
 
1786
+ foreach ( $input as $key => $val ) {
1787
  $field_value[$key] = $this->decode_field_value( $val );
1788
+ }
1789
 
1790
  $field_value = (object) $field_value;
1791
+ } elseif( is_array( $field_value ) ) {
1792
+ foreach ( $field_value as $key => $val ) {
 
 
1793
  $field_value[$key] = $this->decode_field_value( $val );
1794
+ }
1795
+ } else {
1796
  $field_value = html_entity_decode( $field_value, ENT_QUOTES, 'UTF-8' );
1797
+ }
1798
 
1799
  return $field_value;
1800
  }
1807
  * @since 3.0
1808
  */
1809
  function admin_page() {
1810
+ if ( ! ( defined( 'ATTACHMENTS_SETTINGS_SCREEN' ) && ATTACHMENTS_SETTINGS_SCREEN === false ) ) {
1811
+ if ( apply_filters( 'attachments_settings_screen', true ) ) {
1812
+ add_options_page( 'Settings', __( 'Attachments', 'attachments' ), 'manage_options', 'attachments', array( $this, 'options_page' ) );
1813
+ }
1814
+ }
1815
  }
1816
 
1817
 
classes/class.attachments.search.php CHANGED
@@ -1,10 +1,14 @@
1
  <?php
2
 
3
  // exit if accessed directly
4
- if( !defined( 'ABSPATH' ) ) exit;
 
 
5
 
6
  // exit if we can't extend Attachments
7
- if ( !class_exists( 'Attachments' ) ) return;
 
 
8
 
9
 
10
 
@@ -14,8 +18,7 @@ if ( !class_exists( 'Attachments' ) ) return;
14
  *
15
  * @since 3.4.1
16
  */
17
- class AttachmentsSearch extends Attachments
18
- {
19
 
20
  public $results;
21
 
@@ -24,8 +27,7 @@ class AttachmentsSearch extends Attachments
24
  *
25
  * @since 3.3
26
  */
27
- function __construct( $query = null, $params = array() )
28
- {
29
  parent::__construct();
30
  $this->apply_init_filters();
31
 
@@ -50,15 +52,18 @@ class AttachmentsSearch extends Attachments
50
 
51
  $params['post_status'] = sanitize_text_field( $params['post_status'] );
52
 
53
- if( is_string( $params['fields'] ) )
54
  $params['fields'] = array( $params['fields'] ); // we always want an array
 
55
 
56
- if( is_string( $params['filetype'] ) )
57
  $params['filetype'] = array( $params['filetype'] ); // we always want an array
 
58
 
59
  // since we have an array for our fields, we need to loop through and sanitize
60
- for( $i = 0; $i < count( $params['fields'] ); $i++ )
61
- $params['fields'][$i] = sanitize_text_field( $params['fields'][$i] );
 
62
 
63
  // prepare our search args
64
  $args = array(
@@ -74,11 +79,13 @@ class AttachmentsSearch extends Attachments
74
  );
75
 
76
  // append any applicable parameters that got passed to the original method call
77
- if( $params['post_type'] )
78
  $args['post_type'] = $params['post_type'];
 
79
 
80
- if( $params['post_id'] )
81
  $args['post__in'] = array( $params['post_id'] ); // avoid using 'p' or 'page_id'
 
82
 
83
  // we haven't utilized all parameters yet because they're meta-value based so we need to
84
  // do some parsing on our end to validate the returned results
@@ -86,71 +93,76 @@ class AttachmentsSearch extends Attachments
86
  $possible_posts = new WP_Query( $args );
87
  $potential_attachments = false; // stores valid attachments as restrictions are added
88
 
89
- if( $possible_posts->found_posts )
90
- {
91
  // we have results from the reliminary search, we need to quantify them
92
- while( $possible_posts->have_posts() )
93
- {
94
  $possible_posts->next_post();
95
  $possible_post_ids[] = $possible_posts->post->ID;
96
  }
97
 
98
  // now that we have our possible post IDs we can grab all Attachments for all of those posts
99
- foreach( $possible_post_ids as $possible_post_id )
100
- {
101
  $possible_attachments = parent::get_attachments( $params['instance'], $possible_post_id );
102
 
103
- foreach( $possible_attachments as $possible_attachment )
104
  $potential_attachments[] = $possible_attachment;
 
105
  }
106
 
107
  }
108
 
109
  // if there aren't even any potential attachments, we'll just short circuit
110
- if( !$potential_attachments )
111
  return;
 
112
 
113
  // first we need to make sure that our query matches each attachment
114
  // we need to do this because the LIKE query returned the entire meta record,
115
  // not necessarily tied to any specific Attachment
116
  $total_potentials = count( $potential_attachments );
117
- for( $i = 0; $i < $total_potentials; $i++ )
118
- {
119
  $valid = false;
120
 
121
  // if we need to limit our search to specific fields, we'll do that here
122
- if( $params['fields'] )
123
- {
124
  // we only want to check certain fields
125
- foreach( $params['fields'] as $field )
126
- if( isset( $potential_attachments[$i]->fields->$field ) ) // does the field exist?
127
  if( empty( $query ) || strpos( strtolower( $potential_attachments[$i]->fields->$field ),
128
- strtolower( $query ) ) !== false ) // does the value match?
129
- if( is_null( $params['filetype'] ) || ( !is_null( $params['filetype'] ) && in_array( parent::get_mime_type( $potential_attachments[$i]->id ), $params['filetype'] ) ) )
130
  $valid = true;
131
- }
132
- else
133
- {
 
 
134
  // we want to check all fields
135
- if( isset( $potential_attachments[$i]->fields ) )
136
- foreach( $potential_attachments[$i]->fields as $field_name => $field_value )
137
- if( empty( $query ) || strpos( strtolower( $field_value) , strtolower( $query ) ) !== false )
138
- if( is_null( $params['filetype'] ) || ( !is_null( $params['filetype'] ) && in_array( parent::get_mime_type( $potential_attachments[$i]->id ), $params['filetype'] ) ) )
139
  $valid = true;
 
 
 
 
140
  }
141
 
142
- if( !$valid )
143
- unset( $potential_attachments[$i] );
 
144
 
145
  // now our potentials have been limited to each match the query based on any field
146
  }
147
 
148
  // limit to attachment ID if applicable
149
- if( $params['attachment_id'] )
150
- {
151
- foreach( $potential_attachments as $key => $value )
152
- if( $potential_attachments[$key]->id != $params['attachment_id'] )
153
  unset( $potential_attachments[$key] );
 
 
154
  }
155
 
156
  $this->results = array_values( $potential_attachments );
1
  <?php
2
 
3
  // exit if accessed directly
4
+ if ( ! defined( 'ABSPATH' ) ) {
5
+ exit;
6
+ }
7
 
8
  // exit if we can't extend Attachments
9
+ if ( ! class_exists( 'Attachments' ) ) {
10
+ exit;
11
+ }
12
 
13
 
14
 
18
  *
19
  * @since 3.4.1
20
  */
21
+ class AttachmentsSearch extends Attachments {
 
22
 
23
  public $results;
24
 
27
  *
28
  * @since 3.3
29
  */
30
+ function __construct( $query = null, $params = array() ) {
 
31
  parent::__construct();
32
  $this->apply_init_filters();
33
 
52
 
53
  $params['post_status'] = sanitize_text_field( $params['post_status'] );
54
 
55
+ if( is_string( $params['fields'] ) ) {
56
  $params['fields'] = array( $params['fields'] ); // we always want an array
57
+ }
58
 
59
+ if( is_string( $params['filetype'] ) ) {
60
  $params['filetype'] = array( $params['filetype'] ); // we always want an array
61
+ }
62
 
63
  // since we have an array for our fields, we need to loop through and sanitize
64
+ for ( $i = 0; $i < count( $params['fields'] ); $i++ ) {
65
+ $params['fields'][ $i ] = sanitize_text_field( $params['fields'][ $i ] );
66
+ }
67
 
68
  // prepare our search args
69
  $args = array(
79
  );
80
 
81
  // append any applicable parameters that got passed to the original method call
82
+ if( $params['post_type'] ) {
83
  $args['post_type'] = $params['post_type'];
84
+ }
85
 
86
+ if( $params['post_id'] ) {
87
  $args['post__in'] = array( $params['post_id'] ); // avoid using 'p' or 'page_id'
88
+ }
89
 
90
  // we haven't utilized all parameters yet because they're meta-value based so we need to
91
  // do some parsing on our end to validate the returned results
93
  $possible_posts = new WP_Query( $args );
94
  $potential_attachments = false; // stores valid attachments as restrictions are added
95
 
96
+ if ( $possible_posts->found_posts ) {
 
97
  // we have results from the reliminary search, we need to quantify them
98
+ while ( $possible_posts->have_posts() ) {
 
99
  $possible_posts->next_post();
100
  $possible_post_ids[] = $possible_posts->post->ID;
101
  }
102
 
103
  // now that we have our possible post IDs we can grab all Attachments for all of those posts
104
+ foreach ( $possible_post_ids as $possible_post_id ) {
 
105
  $possible_attachments = parent::get_attachments( $params['instance'], $possible_post_id );
106
 
107
+ foreach ( $possible_attachments as $possible_attachment ) {
108
  $potential_attachments[] = $possible_attachment;
109
+ }
110
  }
111
 
112
  }
113
 
114
  // if there aren't even any potential attachments, we'll just short circuit
115
+ if ( ! $potential_attachments ) {
116
  return;
117
+ }
118
 
119
  // first we need to make sure that our query matches each attachment
120
  // we need to do this because the LIKE query returned the entire meta record,
121
  // not necessarily tied to any specific Attachment
122
  $total_potentials = count( $potential_attachments );
123
+ for( $i = 0; $i < $total_potentials; $i++ ) {
 
124
  $valid = false;
125
 
126
  // if we need to limit our search to specific fields, we'll do that here
127
+ if ( $params['fields'] ) {
 
128
  // we only want to check certain fields
129
+ foreach ( $params['fields'] as $field ) {
130
+ if( isset( $potential_attachments[$i]->fields->$field ) ) { // does the field exist?
131
  if( empty( $query ) || strpos( strtolower( $potential_attachments[$i]->fields->$field ),
132
+ strtolower( $query ) ) !== false ) { // does the value match?
133
+ if( is_null( $params['filetype'] ) || ( !is_null( $params['filetype'] ) && in_array( parent::get_mime_type( $potential_attachments[ $i ]->id ), $params['filetype'] ) ) ) {
134
  $valid = true;
135
+ }
136
+ }
137
+ }
138
+ }
139
+ } else {
140
  // we want to check all fields
141
+ if ( isset( $potential_attachments[$i]->fields ) ) {
142
+ foreach ( $potential_attachments[$i]->fields as $field_name => $field_value ) {
143
+ if ( empty( $query ) || strpos( strtolower( $field_value) , strtolower( $query ) ) !== false ) {
144
+ if ( is_null( $params['filetype'] ) || ( !is_null( $params['filetype'] ) && in_array( parent::get_mime_type( $potential_attachments[ $i ]->id ), $params['filetype'] ) ) ) {
145
  $valid = true;
146
+ }
147
+ }
148
+ }
149
+ }
150
  }
151
 
152
+ if ( ! $valid ) {
153
+ unset( $potential_attachments[ $i ] );
154
+ }
155
 
156
  // now our potentials have been limited to each match the query based on any field
157
  }
158
 
159
  // limit to attachment ID if applicable
160
+ if ( $params['attachment_id'] ) {
161
+ foreach ( $potential_attachments as $key => $value ) {
162
+ if ( $potential_attachments[ $key ]->id != $params['attachment_id'] ) {
 
163
  unset( $potential_attachments[$key] );
164
+ }
165
+ }
166
  }
167
 
168
  $this->results = array_values( $potential_attachments );
classes/class.field.php CHANGED
@@ -7,14 +7,19 @@
7
  * @subpackage Main
8
  */
9
 
 
 
 
 
 
10
  // Declare our class
11
- if ( !class_exists( 'Attachments_Field' ) ) :
12
 
13
  /**
14
  * Attachments_Field
15
  */
16
- abstract class Attachments_Field
17
- {
18
  public $instance; // the instance this field is used within
19
  public $name; // the user-defined field name
20
  public $field_name; // the name attribute to be used
@@ -35,8 +40,7 @@ if ( !class_exists( 'Attachments_Field' ) ) :
35
  * @param string $label Field label
36
  * @param mixed $value Field value
37
  */
38
- function __construct( $name = 'name', $label = 'Name', $value = null, $meta = array() )
39
- {
40
  $this->name = sanitize_title( $name );
41
  $this->label = __( esc_attr( $label) );
42
  $this->value = $value;
@@ -53,8 +57,7 @@ if ( !class_exists( 'Attachments_Field' ) ) :
53
  * @param string $instance The instance name
54
  * @param Attachments_Field $field The field object
55
  */
56
- function set_field_instance( $instance, $field )
57
- {
58
  $field->instance = $instance;
59
  }
60
 
@@ -65,16 +68,16 @@ if ( !class_exists( 'Attachments_Field' ) ) :
65
  * @param Attachments_Field $field The field object
66
  * @param string $uid Existing UID if applicable
67
  */
68
- function set_field_identifiers( $field, $uid = null )
69
- {
70
  // we MUST have an instance
71
- if( empty( $field->instance ) )
72
  return false;
 
73
 
74
  // if we're pulling an existing Attachment (field has a value) we're going to use
75
  // a PHP uniqid to set up our array flags but if we're setting up our Underscore
76
  // template we need to use a variable flag to be processed later
77
- $this->uid = !is_null( $uid ) ? $uid : '{{ attachments.attachment_uid }}';
78
 
79
  // set the name
80
  $field->field_name = "attachments[$field->instance][$this->uid][fields][$field->name]";
@@ -89,8 +92,7 @@ if ( !class_exists( 'Attachments_Field' ) ) :
89
  * Sets the field type of the field
90
  * @param string $field_type Registered field type name
91
  */
92
- function set_field_type( $field_type )
93
- {
94
  $this->type = $field_type;
95
  }
96
 
@@ -100,10 +102,10 @@ if ( !class_exists( 'Attachments_Field' ) ) :
100
  * Sets the WordPress meta attribute to be used as the default
101
  * @param string $default One of the approved defauls (title, caption, alt, description)
102
  */
103
- function set_field_default( $default = '' )
104
- {
105
- if( is_string( $default ) && !empty( $default ) && in_array( strtolower( $default ), $this->defaults ) )
106
  $this->default = strtolower( $default );
 
107
  }
108
 
109
 
7
  * @subpackage Main
8
  */
9
 
10
+ // exit if accessed directly
11
+ if ( ! defined( 'ABSPATH' ) ) {
12
+ exit;
13
+ }
14
+
15
  // Declare our class
16
+ if ( ! class_exists( 'Attachments_Field' ) ) :
17
 
18
  /**
19
  * Attachments_Field
20
  */
21
+ abstract class Attachments_Field {
22
+
23
  public $instance; // the instance this field is used within
24
  public $name; // the user-defined field name
25
  public $field_name; // the name attribute to be used
40
  * @param string $label Field label
41
  * @param mixed $value Field value
42
  */
43
+ function __construct( $name = 'name', $label = 'Name', $value = null, $meta = array() ) {
 
44
  $this->name = sanitize_title( $name );
45
  $this->label = __( esc_attr( $label) );
46
  $this->value = $value;
57
  * @param string $instance The instance name
58
  * @param Attachments_Field $field The field object
59
  */
60
+ function set_field_instance( $instance, $field ) {
 
61
  $field->instance = $instance;
62
  }
63
 
68
  * @param Attachments_Field $field The field object
69
  * @param string $uid Existing UID if applicable
70
  */
71
+ function set_field_identifiers( $field, $uid = null ) {
 
72
  // we MUST have an instance
73
+ if ( empty( $field->instance ) ) {
74
  return false;
75
+ }
76
 
77
  // if we're pulling an existing Attachment (field has a value) we're going to use
78
  // a PHP uniqid to set up our array flags but if we're setting up our Underscore
79
  // template we need to use a variable flag to be processed later
80
+ $this->uid = ! is_null( $uid ) ? $uid : '{{ attachments.attachment_uid }}';
81
 
82
  // set the name
83
  $field->field_name = "attachments[$field->instance][$this->uid][fields][$field->name]";
92
  * Sets the field type of the field
93
  * @param string $field_type Registered field type name
94
  */
95
+ function set_field_type( $field_type ) {
 
96
  $this->type = $field_type;
97
  }
98
 
102
  * Sets the WordPress meta attribute to be used as the default
103
  * @param string $default One of the approved defauls (title, caption, alt, description)
104
  */
105
+ function set_field_default( $default = '' ) {
106
+ if ( is_string( $default ) && ! empty( $default ) && in_array( strtolower( $default ), $this->defaults ) ) {
 
107
  $this->default = strtolower( $default );
108
+ }
109
  }
110
 
111
 
classes/fields/class.field.select.php CHANGED
@@ -7,8 +7,7 @@
7
  * @subpackage Main
8
  */
9
 
10
- class Attachments_Field_Select extends Attachments_Field
11
- {
12
 
13
  private $allow_null; // whether null is allowed
14
  private $multiple; // whether it's a multiple <select>
@@ -21,8 +20,7 @@ class Attachments_Field_Select extends Attachments_Field
21
  * @param string $label Field label
22
  * @param mixed $value Field value
23
  */
24
- function __construct( $name = 'name', $label = 'Name', $value = null, $meta = array() )
25
- {
26
  $defaults = array(
27
  'options' => array(), // no <option>s by default
28
  'allow_null' => false,
@@ -45,8 +43,7 @@ class Attachments_Field_Select extends Attachments_Field
45
  * @param Attachments_Field $field The field object
46
  * @return void
47
  */
48
- function html( $field )
49
- {
50
  ?>
51
  <select name="<?php esc_attr_e( $field->field_name ); ?><?php if( $this->multiple ) : ?>[]<?php endif; ?>" id="<?php esc_attr_e( $field->field_id ); ?>" class="attachments attachments-field attachments-field-<?php esc_attr_e( $field->field_name ); ?> attachments-field-<?php esc_attr_e( $field->field_id ); ?>"<?php if( $this->multiple ) : ?> multiple<?php endif; ?>>
52
  <?php if( $this->allow_null && !$this->multiple ) : ?><option value="">&mdash;</option><?php endif; ?>
@@ -54,18 +51,18 @@ class Attachments_Field_Select extends Attachments_Field
54
  <?php
55
  $selected = selected( $field->value, $option_value ) ? ' selected' : '';
56
 
57
- if( is_array( $field->value ) )
58
  $selected = in_array( $option_value, $field->value ) ? ' selected' : '';
 
59
 
60
- if( is_object( $field->value ) )
61
- {
62
  $values = get_object_vars( $field->value );
63
  $selected = in_array( $option_value, $values ) ? ' selected' : '';
64
  }
65
 
66
  ?>
67
  <option value="<?php esc_attr_e( $option_value ); ?>"<?php echo $selected; ?>>
68
- <?php echo $option_label; ?>
69
  </option>
70
  <?php endforeach; ?>
71
  </select>
@@ -80,8 +77,7 @@ class Attachments_Field_Select extends Attachments_Field
80
  * @param Attachments_field $field The field object
81
  * @return string The formatted value
82
  */
83
- function format_value_for_input( $value, $field = null )
84
- {
85
  return $value;
86
  }
87
 
@@ -91,8 +87,7 @@ class Attachments_Field_Select extends Attachments_Field
91
  * Fires once per field type per instance and outputs any additional assets (e.g. external JavaScript)
92
  * @return void
93
  */
94
- public function assets()
95
- {
96
  return;
97
  }
98
 
@@ -102,9 +97,8 @@ class Attachments_Field_Select extends Attachments_Field
102
  * Hook into WordPress' init action
103
  * @return void
104
  */
105
- function init()
106
- {
107
  return;
108
  }
109
 
110
- }
7
  * @subpackage Main
8
  */
9
 
10
+ class Attachments_Field_Select extends Attachments_Field {
 
11
 
12
  private $allow_null; // whether null is allowed
13
  private $multiple; // whether it's a multiple <select>
20
  * @param string $label Field label
21
  * @param mixed $value Field value
22
  */
23
+ function __construct( $name = 'name', $label = 'Name', $value = null, $meta = array() ) {
 
24
  $defaults = array(
25
  'options' => array(), // no <option>s by default
26
  'allow_null' => false,
43
  * @param Attachments_Field $field The field object
44
  * @return void
45
  */
46
+ function html( $field ) {
 
47
  ?>
48
  <select name="<?php esc_attr_e( $field->field_name ); ?><?php if( $this->multiple ) : ?>[]<?php endif; ?>" id="<?php esc_attr_e( $field->field_id ); ?>" class="attachments attachments-field attachments-field-<?php esc_attr_e( $field->field_name ); ?> attachments-field-<?php esc_attr_e( $field->field_id ); ?>"<?php if( $this->multiple ) : ?> multiple<?php endif; ?>>
49
  <?php if( $this->allow_null && !$this->multiple ) : ?><option value="">&mdash;</option><?php endif; ?>
51
  <?php
52
  $selected = selected( $field->value, $option_value ) ? ' selected' : '';
53
 
54
+ if( is_array( $field->value ) ) {
55
  $selected = in_array( $option_value, $field->value ) ? ' selected' : '';
56
+ }
57
 
58
+ if ( is_object( $field->value ) ) {
 
59
  $values = get_object_vars( $field->value );
60
  $selected = in_array( $option_value, $values ) ? ' selected' : '';
61
  }
62
 
63
  ?>
64
  <option value="<?php esc_attr_e( $option_value ); ?>"<?php echo $selected; ?>>
65
+ <?php echo esc_html( $option_label ); ?>
66
  </option>
67
  <?php endforeach; ?>
68
  </select>
77
  * @param Attachments_field $field The field object
78
  * @return string The formatted value
79
  */
80
+ function format_value_for_input( $value, $field = null ) {
 
81
  return $value;
82
  }
83
 
87
  * Fires once per field type per instance and outputs any additional assets (e.g. external JavaScript)
88
  * @return void
89
  */
90
+ public function assets() {
 
91
  return;
92
  }
93
 
97
  * Hook into WordPress' init action
98
  * @return void
99
  */
100
+ function init() {
 
101
  return;
102
  }
103
 
104
+ }
classes/fields/class.field.text.php CHANGED
@@ -7,8 +7,7 @@
7
  * @subpackage Main
8
  */
9
 
10
- class Attachments_Field_Text extends Attachments_Field
11
- {
12
 
13
  /**
14
  * Constructor
@@ -16,8 +15,7 @@ class Attachments_Field_Text extends Attachments_Field
16
  * @param string $label Field label
17
  * @param mixed $value Field value
18
  */
19
- function __construct( $name = 'name', $label = 'Name', $value = null, $meta = array() )
20
- {
21
  parent::__construct( $name, $label, $value, $meta );
22
  }
23
 
@@ -28,8 +26,7 @@ class Attachments_Field_Text extends Attachments_Field
28
  * @param Attachments_Field $field The field object
29
  * @return void
30
  */
31
- function html( $field )
32
- {
33
  ?>
34
  <input type="text" name="<?php esc_attr_e( $field->field_name ); ?>" id="<?php esc_attr_e( $field->field_id ); ?>" class="attachments attachments-field attachments-field-<?php esc_attr_e( $field->field_name ); ?> attachments-field-<?php esc_attr_e( $field->field_id ); ?>" value="<?php esc_attr_e( $field->value ); ?>" data-default="<?php esc_attr_e( $field->default ); ?>" />
35
  <?php
@@ -43,8 +40,7 @@ class Attachments_Field_Text extends Attachments_Field
43
  * @param Attachments_field $field The field object
44
  * @return string The formatted value
45
  */
46
- function format_value_for_input( $value, $field = null )
47
- {
48
  return htmlspecialchars( $value, ENT_QUOTES );
49
  }
50
 
@@ -54,8 +50,7 @@ class Attachments_Field_Text extends Attachments_Field
54
  * Fires once per field type per instance and outputs any additional assets (e.g. external JavaScript)
55
  * @return void
56
  */
57
- public function assets()
58
- {
59
  return;
60
  }
61
 
@@ -65,8 +60,7 @@ class Attachments_Field_Text extends Attachments_Field
65
  * Hook into WordPress' init action
66
  * @return void
67
  */
68
- function init()
69
- {
70
  return;
71
  }
72
 
7
  * @subpackage Main
8
  */
9
 
10
+ class Attachments_Field_Text extends Attachments_Field {
 
11
 
12
  /**
13
  * Constructor
15
  * @param string $label Field label
16
  * @param mixed $value Field value
17
  */
18
+ function __construct( $name = 'name', $label = 'Name', $value = null, $meta = array() ) {
 
19
  parent::__construct( $name, $label, $value, $meta );
20
  }
21
 
26
  * @param Attachments_Field $field The field object
27
  * @return void
28
  */
29
+ function html( $field ) {
 
30
  ?>
31
  <input type="text" name="<?php esc_attr_e( $field->field_name ); ?>" id="<?php esc_attr_e( $field->field_id ); ?>" class="attachments attachments-field attachments-field-<?php esc_attr_e( $field->field_name ); ?> attachments-field-<?php esc_attr_e( $field->field_id ); ?>" value="<?php esc_attr_e( $field->value ); ?>" data-default="<?php esc_attr_e( $field->default ); ?>" />
32
  <?php
40
  * @param Attachments_field $field The field object
41
  * @return string The formatted value
42
  */
43
+ function format_value_for_input( $value, $field = null ) {
 
44
  return htmlspecialchars( $value, ENT_QUOTES );
45
  }
46
 
50
  * Fires once per field type per instance and outputs any additional assets (e.g. external JavaScript)
51
  * @return void
52
  */
53
+ public function assets() {
 
54
  return;
55
  }
56
 
60
  * Hook into WordPress' init action
61
  * @return void
62
  */
63
+ function init() {
 
64
  return;
65
  }
66
 
classes/fields/class.field.textarea.php CHANGED
@@ -7,8 +7,7 @@
7
  * @subpackage Main
8
  */
9
 
10
- class Attachments_Field_Textarea extends Attachments_Field
11
- {
12
 
13
  /**
14
  * Constructor
@@ -16,8 +15,7 @@ class Attachments_Field_Textarea extends Attachments_Field
16
  * @param string $label Field label
17
  * @param mixed $value Field value
18
  */
19
- function __construct( $name = 'name', $label = 'Name', $value = null, $meta = array() )
20
- {
21
  parent::__construct( $name, $label, $value, $meta );
22
  }
23
 
@@ -28,8 +26,7 @@ class Attachments_Field_Textarea extends Attachments_Field
28
  * @param Attachments_Field $field The field object
29
  * @return void
30
  */
31
- function html( $field )
32
- {
33
  ?>
34
  <textarea type="text" name="<?php esc_attr_e( $field->field_name ); ?>" id="<?php esc_attr_e( $field->field_id ); ?>" class="attachments attachments-field attachments-field-<?php esc_attr_e( $field->field_name ); ?> attachments-field-<?php esc_attr_e( $field->field_id ); ?>" data-default="<?php esc_attr_e( $field->default ); ?>"><?php echo esc_textarea( $field->value ); ?></textarea>
35
  <?php
@@ -43,8 +40,7 @@ class Attachments_Field_Textarea extends Attachments_Field
43
  * @param Attachments_field $field The field object
44
  * @return string The formatted value
45
  */
46
- function format_value_for_input( $value, $field = null )
47
- {
48
  return $value;
49
  }
50
 
@@ -54,8 +50,7 @@ class Attachments_Field_Textarea extends Attachments_Field
54
  * Fires once per field type per instance and outputs any additional assets (e.g. external JavaScript)
55
  * @return void
56
  */
57
- public function assets()
58
- {
59
  return;
60
  }
61
 
@@ -65,8 +60,7 @@ class Attachments_Field_Textarea extends Attachments_Field
65
  * Hook into WordPress' init action
66
  * @return void
67
  */
68
- function init()
69
- {
70
  return;
71
  }
72
 
7
  * @subpackage Main
8
  */
9
 
10
+ class Attachments_Field_Textarea extends Attachments_Field {
 
11
 
12
  /**
13
  * Constructor
15
  * @param string $label Field label
16
  * @param mixed $value Field value
17
  */
18
+ function __construct( $name = 'name', $label = 'Name', $value = null, $meta = array() ) {
 
19
  parent::__construct( $name, $label, $value, $meta );
20
  }
21
 
26
  * @param Attachments_Field $field The field object
27
  * @return void
28
  */
29
+ function html( $field ) {
 
30
  ?>
31
  <textarea type="text" name="<?php esc_attr_e( $field->field_name ); ?>" id="<?php esc_attr_e( $field->field_id ); ?>" class="attachments attachments-field attachments-field-<?php esc_attr_e( $field->field_name ); ?> attachments-field-<?php esc_attr_e( $field->field_id ); ?>" data-default="<?php esc_attr_e( $field->default ); ?>"><?php echo esc_textarea( $field->value ); ?></textarea>
32
  <?php
40
  * @param Attachments_field $field The field object
41
  * @return string The formatted value
42
  */
43
+ function format_value_for_input( $value, $field = null ) {
 
44
  return $value;
45
  }
46
 
50
  * Fires once per field type per instance and outputs any additional assets (e.g. external JavaScript)
51
  * @return void
52
  */
53
+ public function assets() {
 
54
  return;
55
  }
56
 
60
  * Hook into WordPress' init action
61
  * @return void
62
  */
63
+ function init() {
 
64
  return;
65
  }
66
 
classes/fields/class.field.wysiwyg.php CHANGED
@@ -7,8 +7,7 @@
7
  * @subpackage Main
8
  */
9
 
10
- class Attachments_Field_WYSIWYG extends Attachments_Field
11
- {
12
 
13
  /**
14
  * Constructor
@@ -16,8 +15,7 @@ class Attachments_Field_WYSIWYG extends Attachments_Field
16
  * @param string $label Field label
17
  * @param mixed $value Field value
18
  */
19
- function __construct( $name = 'name', $label = 'Name', $value = null, $meta = array() )
20
- {
21
  parent::__construct( $name, $label, $value, $meta );
22
 
23
  add_filter( 'wp_default_editor', array( $this, 'wp_default_editor' ) );
@@ -29,16 +27,16 @@ class Attachments_Field_WYSIWYG extends Attachments_Field
29
  * Hook into WordPress' init action
30
  * @return void
31
  */
32
- function init()
33
- {
34
  global $post;
35
 
36
  // ensure we've got TinyMCE to work with
37
  $has_editor = post_type_supports( $post->post_type, 'editor' );
38
  add_post_type_support( $post->post_type, 'editor' );
39
 
40
- if( !$has_editor )
41
  echo '<style type="text/css">#poststuff .postarea { display:none; }</style>';
 
42
  }
43
 
44
 
@@ -48,8 +46,7 @@ class Attachments_Field_WYSIWYG extends Attachments_Field
48
  * @param Attachments_Field $field The field object
49
  * @return void
50
  */
51
- function html( $field )
52
- {
53
  ?>
54
  <div class="wp-editor-wrap attachments-field-wysiwyg-editor-wrap">
55
  <div class="wp-editor-container">
@@ -65,9 +62,8 @@ class Attachments_Field_WYSIWYG extends Attachments_Field
65
  * Fires once per field type per instance and outputs any additional assets (e.g. external JavaScript)
66
  * @return void
67
  */
68
- function assets()
69
- {
70
- if( 'true' == get_user_meta( get_current_user_id(), 'rich_editing', true ) ) :
71
  ?>
72
  <style type="text/css">
73
  .attachments-field-wysiwyg-editor-wrap { background:#fff; }
@@ -138,8 +134,7 @@ class Attachments_Field_WYSIWYG extends Attachments_Field
138
  * @param Attachments_field $field The field object
139
  * @return string The formatted value
140
  */
141
- function format_value_for_input( $value, $field = null )
142
- {
143
  return wp_richedit_pre( $value );
144
  }
145
 
@@ -149,8 +144,7 @@ class Attachments_Field_WYSIWYG extends Attachments_Field
149
  * Callback for 'wp_default_editor' action in constructor. Sets the default editor to TinyMCE.
150
  * @return string Editor name
151
  */
152
- function wp_default_editor()
153
- {
154
  return 'tinymce'; // html or tinymce
155
  }
156
 
7
  * @subpackage Main
8
  */
9
 
10
+ class Attachments_Field_WYSIWYG extends Attachments_Field {
 
11
 
12
  /**
13
  * Constructor
15
  * @param string $label Field label
16
  * @param mixed $value Field value
17
  */
18
+ function __construct( $name = 'name', $label = 'Name', $value = null, $meta = array() ) {
 
19
  parent::__construct( $name, $label, $value, $meta );
20
 
21
  add_filter( 'wp_default_editor', array( $this, 'wp_default_editor' ) );
27
  * Hook into WordPress' init action
28
  * @return void
29
  */
30
+ function init() {
 
31
  global $post;
32
 
33
  // ensure we've got TinyMCE to work with
34
  $has_editor = post_type_supports( $post->post_type, 'editor' );
35
  add_post_type_support( $post->post_type, 'editor' );
36
 
37
+ if ( ! $has_editor ) {
38
  echo '<style type="text/css">#poststuff .postarea { display:none; }</style>';
39
+ }
40
  }
41
 
42
 
46
  * @param Attachments_Field $field The field object
47
  * @return void
48
  */
49
+ function html( $field ) {
 
50
  ?>
51
  <div class="wp-editor-wrap attachments-field-wysiwyg-editor-wrap">
52
  <div class="wp-editor-container">
62
  * Fires once per field type per instance and outputs any additional assets (e.g. external JavaScript)
63
  * @return void
64
  */
65
+ function assets() {
66
+ if ( 'true' == get_user_meta( get_current_user_id(), 'rich_editing', true ) ) :
 
67
  ?>
68
  <style type="text/css">
69
  .attachments-field-wysiwyg-editor-wrap { background:#fff; }
134
  * @param Attachments_field $field The field object
135
  * @return string The formatted value
136
  */
137
+ function format_value_for_input( $value, $field = null ) {
 
138
  return wp_richedit_pre( $value );
139
  }
140
 
144
  * Callback for 'wp_default_editor' action in constructor. Sets the default editor to TinyMCE.
145
  * @return string Editor name
146
  */
147
+ function wp_default_editor() {
 
148
  return 'tinymce'; // html or tinymce
149
  }
150
 
deprecated/attachments.php CHANGED
@@ -21,12 +21,15 @@
21
  */
22
 
23
  // Exit if accessed directly
24
- if( !defined( 'ABSPATH' ) ) exit;
 
 
25
 
26
 
27
  // constant definition
28
- if( !defined( 'IS_ADMIN' ) )
29
  define( 'IS_ADMIN', is_admin() );
 
30
 
31
  define( 'ATTACHMENTS_PREFIX', 'attachments_' );
32
  define( 'ATTACHMENTS_VERSION', '1.6.2.1' );
@@ -42,31 +45,26 @@ global $wpdb;
42
 
43
  // environment check
44
  $wp_version = get_bloginfo( 'version' );
45
- if( !version_compare( PHP_VERSION, '5.2', '>=' ) || !version_compare( $wp_version, '3.0', '>=' ) )
46
- {
47
- if( IS_ADMIN && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) )
48
- {
49
  require_once ABSPATH.'/wp-admin/includes/plugin.php';
50
  deactivate_plugins( __FILE__ );
51
  wp_die( __('Attachments requires PHP 5.2 or higher, as does WordPress 3.2+. Attachments has been automatically deactivated.') );
52
- }
53
- else
54
- {
55
  return;
56
  }
57
  }
58
 
59
 
60
  // we moved all attachments_get_attachments() functions to an external file in version 3.0
61
- include_once 'get-attachments.php';
62
 
63
 
64
  // =========
65
  // = HOOKS =
66
  // =========
67
 
68
- if( IS_ADMIN )
69
- {
70
 
71
  // pre-flight check
72
  add_action( 'init', 'attachments_pre_init' );
@@ -93,20 +91,19 @@ if( IS_ADMIN )
93
  }
94
 
95
 
96
- function attachments_localization()
97
- {
98
  load_plugin_textdomain( 'attachments', false, ATTACHMENTS_DIR . '/languages/' );
99
  }
100
 
101
 
102
 
103
- function attachments_enqueues( $hook )
104
- {
105
 
106
  wp_enqueue_style( 'attachments', trailingslashit( ATTACHMENTS_URL ) . 'css/attachments.css' );
107
 
108
- if( 'edit.php' != $hook && 'post.php' != $hook && 'post-new.php' != $hook )
109
  return;
 
110
 
111
  wp_enqueue_script( 'handlebars', trailingslashit( ATTACHMENTS_URL ) . 'js/handlebars.js', null, '1.0.beta.6', false );
112
  wp_enqueue_script( 'attachments', trailingslashit( ATTACHMENTS_URL ) . 'js/attachments.js', array( 'handlebars', 'jquery', 'thickbox' ), ATTACHMENTS_VERSION, true );
@@ -121,11 +118,9 @@ function attachments_enqueues( $hook )
121
  // = FUNCTIONS =
122
  // =============
123
 
124
- function attachments_pre_init()
125
- {
126
  // as of version 1.6 we'll be storing a proper settings array
127
- if( !get_option( ATTACHMENTS_PREFIX . 'settings' ) )
128
- {
129
  $settings = array();
130
 
131
  // we've got a version < 1.6 and therefore no real settings
@@ -133,13 +128,10 @@ function attachments_pre_init()
133
 
134
  $post_parent = get_option( 'attachments_store_native' );
135
 
136
- if( $post_parent === false )
137
- {
138
  // it wasn't set
139
  $settings['post_parent'] = false;
140
- }
141
- else
142
- {
143
  $settings['post_parent'] = true;
144
  }
145
 
@@ -159,19 +151,14 @@ function attachments_pre_init()
159
  $post_types['page']->labels->name = 'Pages';
160
  $post_types['page']->name = 'page';
161
 
162
- if( count( $post_types ) )
163
- {
164
- foreach( $post_types as $post_type )
165
- {
166
  $post_parent = get_option( 'attachments_cpt_' . $post_type->name );
167
 
168
- if( $post_parent === false )
169
- {
170
  // it wasn't set
171
  $settings['post_types'][$post_type->name] = false;
172
- }
173
- else
174
- {
175
  $settings['post_types'][$post_type->name] = true;
176
  }
177
  }
@@ -183,8 +170,7 @@ function attachments_pre_init()
183
  }
184
 
185
 
186
- function attachments_register_settings()
187
- {
188
  // flag our settings
189
  register_setting(
190
  ATTACHMENTS_PREFIX . 'settings',
@@ -218,17 +204,15 @@ function attachments_register_settings()
218
  );
219
  }
220
 
221
- function attachments_edit_options()
222
- { }
223
 
224
- function attachments_validate_settings($input)
225
- {
226
  $input['version'] = ATTACHMENTS_VERSION;
 
227
  return $input;
228
  }
229
 
230
- function attachments_edit_post_parent()
231
- {
232
  $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
233
  ?>
234
  <div>
@@ -238,8 +222,7 @@ function attachments_edit_post_parent()
238
  </div>
239
  <?php }
240
 
241
- function attachments_edit_post_types()
242
- {
243
  $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
244
  $args = array(
245
  'public' => true,
@@ -256,10 +239,10 @@ function attachments_edit_post_types()
256
  $post_types['page']->labels->name = 'Pages';
257
  $post_types['page']->name = 'page';
258
 
259
- if( count( $post_types ) ) : foreach($post_types as $post_type) : ?>
260
  <div>
261
  <label for="<?php echo ATTACHMENTS_PREFIX; ?>settings[post_types][<?php echo $post_type->name; ?>]">
262
- <input name="<?php echo ATTACHMENTS_PREFIX; ?>settings[post_types][<?php echo $post_type->name; ?>]" type="checkbox" id="<?php echo ATTACHMENTS_PREFIX; ?>settings[post_types][<?php echo $post_type->name; ?>]" value="1"<?php if( isset( $settings['post_types'][$post_type->name] ) && $settings['post_types'][$post_type->name] ) : ?> checked="checked"<?php endif; ?> /> <?php echo $post_type->labels->name; ?>
263
  </label>
264
  </div>
265
  <?php endforeach; endif; ?>
@@ -272,9 +255,8 @@ function attachments_edit_post_types()
272
  * @return void
273
  * @author Jonathan Christopher
274
  */
275
- function attachments_options()
276
- {
277
- include 'attachments.options.php';
278
  }
279
 
280
 
@@ -284,8 +266,7 @@ function attachments_options()
284
  * @return void
285
  * @author Jonathan Christopher
286
  */
287
- function attachments_menu()
288
- {
289
  add_options_page('Settings', 'Attachments', 'manage_options', __FILE__, 'attachments_options');
290
  }
291
 
@@ -296,8 +277,7 @@ function attachments_menu()
296
  * @return void
297
  * @author Jonathan Christopher
298
  */
299
- function attachments_add()
300
- { ?>
301
 
302
  <div id="attachments-inner">
303
 
@@ -315,25 +295,20 @@ function attachments_add()
315
  </ul>
316
 
317
  <div id="attachments-list">
318
- <input type="hidden" name="attachments_nonce" id="attachments_nonce" value="<?php echo wp_create_nonce( plugin_basename(__FILE__) ); ?>" />
319
  <ul>
320
  <?php
321
- if( !empty($_GET['post']) )
322
- {
323
  // get all attachments
324
  $existing_attachments = attachments_get_attachments( intval( $_GET['post'] ) );
325
 
326
- if( is_array($existing_attachments) && !empty($existing_attachments) )
327
- {
328
- foreach ($existing_attachments as $attachment)
329
- {
330
  // TODO: Better handle this when examining Handlebars template
331
- if( empty( $attachment['title'] ) )
332
- {
333
  $attachment['title'] = ' ';
334
  }
335
- if( empty( $attachment['caption'] ) )
336
- {
337
  $attachment['caption'] = ' ';
338
  }
339
  attachments_attachment_markup( $attachment['name'], $attachment['title'], $attachment['caption'], $attachment['id'], $attachment['order'] );
@@ -350,15 +325,20 @@ function attachments_add()
350
  <?php }
351
 
352
 
353
- function attachments_attachment_markup( $name = null, $title = null, $caption = null, $id = null, $order = null )
354
- { ?>
 
 
 
 
 
355
  <li class="attachments-file">
356
  <h2>
357
  <a href="#" class="attachment-handle">
358
- <span class="attachment-handle-icon"><img src="<?php echo WP_PLUGIN_URL; ?>/attachments/deprecated/images/handle.gif" alt="Drag" /></span>
359
  </a>
360
- <span class="attachment-name"><?php echo empty( $name ) ? '{{name}}' : $name; ?></span>
361
- <span class="attachment-delete"><a href="#"><?php _e("Delete", "attachments")?></a></span>
362
  </h2>
363
  <div class="attachments-fields">
364
  <div class="textfield" id="field_attachment_title_<?php echo empty( $id ) ? '{{id}}' : $id; ?>">
@@ -395,11 +375,9 @@ function attachments_attachment_markup( $name = null, $title = null, $caption =
395
  * @return void
396
  * @author Jonathan Christopher
397
  */
398
- function attachments_meta_box()
399
- {
400
  // for custom post types
401
- if( function_exists( 'get_post_types' ) )
402
- {
403
  $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
404
 
405
  $args = array(
@@ -410,10 +388,8 @@ function attachments_meta_box()
410
  $operator = 'and';
411
  $post_types = get_post_types( $args, $output, $operator );
412
 
413
- foreach($post_types as $post_type)
414
- {
415
- if( isset( $settings['post_types'][$post_type->name] ) && $settings['post_types'][$post_type->name] )
416
- {
417
  add_meta_box( 'attachments_list', __( 'Attachments', 'attachments' ), 'attachments_add', $post_type->name, 'normal' );
418
  }
419
  }
@@ -430,7 +406,7 @@ function attachments_meta_box()
430
  function attachments_init_js()
431
  {
432
  echo '<script type="text/javascript" charset="utf-8">';
433
- echo ' var attachments_base = "' . WP_PLUGIN_URL . '/attachments"; ';
434
  echo ' var attachments_media = ""; ';
435
  echo '</script>';
436
  }
@@ -444,39 +420,30 @@ function attachments_init_js()
444
  * @author Jonathan Christopher
445
  * @author JR Tashjian
446
  */
447
- function attachments_save($post_id)
448
- {
449
  // verify this came from the our screen and with proper authorization,
450
  // because save_post can be triggered at other times
451
- if( !isset( $_POST['attachments_nonce'] ) )
452
- {
453
  return $post_id;
454
  }
455
 
456
- if( !wp_verify_nonce( $_POST['attachments_nonce'], plugin_basename(__FILE__) ) )
457
- {
458
  return $post_id;
459
  }
460
 
461
  // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
462
  // to do anything
463
- if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
464
- {
465
  return $post_id;
466
  }
467
 
468
  // Check permissions
469
- if( 'page' == $_POST['post_type'] )
470
- {
471
- if( !current_user_can( 'edit_page', $post_id ) )
472
- {
473
  return $post_id;
474
  }
475
- }
476
- else
477
- {
478
- if( !current_user_can( 'edit_post', $post_id ) )
479
- {
480
  return $post_id;
481
  }
482
  }
@@ -492,25 +459,19 @@ function attachments_save($post_id)
492
  $attachment_ids = array();
493
 
494
  // We'll build our array of attachments
495
- foreach( $_POST as $key => $data )
496
- {
497
  // Arbitrarily using the id
498
- if( substr($key, 0, 14) == 'attachment_id_' )
499
- {
500
  array_push( $attachment_ids, substr( $key, 14, strlen( $key ) ) );
501
  }
502
 
503
  }
504
 
505
  // If we have attachments, there's work to do
506
- if( !empty( $attachment_ids ) )
507
- {
508
-
509
- foreach ( $attachment_ids as $i )
510
- {
511
- if( !empty( $_POST['attachment_id_' . $i] ) )
512
- {
513
- $attachment_id = intval( $_POST['attachment_id_' . $i] );
514
 
515
  $attachment_details = array(
516
  'id' => $attachment_id,
@@ -527,13 +488,11 @@ function attachments_save($post_id)
527
 
528
  // save native Attach
529
  $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
530
- if( isset( $settings['post_parent'] ) && $settings['post_parent'] )
531
- {
532
  // need to first check to make sure we're not overwriting a native Attach
533
  $attach_post_ref = get_post( $attachment_id );
534
 
535
- if( $attach_post_ref->post_parent == 0 )
536
- {
537
  // no current Attach, we can add ours
538
  $attach_post = array();
539
  $attach_post['ID'] = $attachment_id;
@@ -548,4 +507,4 @@ function attachments_save($post_id)
548
 
549
  }
550
 
551
- }
21
  */
22
 
23
  // Exit if accessed directly
24
+ if ( ! defined( 'ABSPATH' ) ) {
25
+ exit;
26
+ }
27
 
28
 
29
  // constant definition
30
+ if ( ! defined( 'IS_ADMIN' ) ) {
31
  define( 'IS_ADMIN', is_admin() );
32
+ }
33
 
34
  define( 'ATTACHMENTS_PREFIX', 'attachments_' );
35
  define( 'ATTACHMENTS_VERSION', '1.6.2.1' );
45
 
46
  // environment check
47
  $wp_version = get_bloginfo( 'version' );
48
+ if ( ! version_compare( PHP_VERSION, '5.2', '>=' ) || !version_compare( $wp_version, '3.0', '>=' ) ) {
49
+ if ( IS_ADMIN && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
 
 
50
  require_once ABSPATH.'/wp-admin/includes/plugin.php';
51
  deactivate_plugins( __FILE__ );
52
  wp_die( __('Attachments requires PHP 5.2 or higher, as does WordPress 3.2+. Attachments has been automatically deactivated.') );
53
+ } else {
 
 
54
  return;
55
  }
56
  }
57
 
58
 
59
  // we moved all attachments_get_attachments() functions to an external file in version 3.0
60
+ include_once dirname( __FILE__ ) . '/get-attachments.php';
61
 
62
 
63
  // =========
64
  // = HOOKS =
65
  // =========
66
 
67
+ if ( is_admin() ) {
 
68
 
69
  // pre-flight check
70
  add_action( 'init', 'attachments_pre_init' );
91
  }
92
 
93
 
94
+ function attachments_localization() {
 
95
  load_plugin_textdomain( 'attachments', false, ATTACHMENTS_DIR . '/languages/' );
96
  }
97
 
98
 
99
 
100
+ function attachments_enqueues( $hook ) {
 
101
 
102
  wp_enqueue_style( 'attachments', trailingslashit( ATTACHMENTS_URL ) . 'css/attachments.css' );
103
 
104
+ if ( 'edit.php' != $hook && 'post.php' != $hook && 'post-new.php' != $hook ) {
105
  return;
106
+ }
107
 
108
  wp_enqueue_script( 'handlebars', trailingslashit( ATTACHMENTS_URL ) . 'js/handlebars.js', null, '1.0.beta.6', false );
109
  wp_enqueue_script( 'attachments', trailingslashit( ATTACHMENTS_URL ) . 'js/attachments.js', array( 'handlebars', 'jquery', 'thickbox' ), ATTACHMENTS_VERSION, true );
118
  // = FUNCTIONS =
119
  // =============
120
 
121
+ function attachments_pre_init() {
 
122
  // as of version 1.6 we'll be storing a proper settings array
123
+ if ( ! get_option( ATTACHMENTS_PREFIX . 'settings' ) ) {
 
124
  $settings = array();
125
 
126
  // we've got a version < 1.6 and therefore no real settings
128
 
129
  $post_parent = get_option( 'attachments_store_native' );
130
 
131
+ if( $post_parent === false ) {
 
132
  // it wasn't set
133
  $settings['post_parent'] = false;
134
+ } else {
 
 
135
  $settings['post_parent'] = true;
136
  }
137
 
151
  $post_types['page']->labels->name = 'Pages';
152
  $post_types['page']->name = 'page';
153
 
154
+ if ( count( $post_types ) ) {
155
+ foreach( $post_types as $post_type ) {
 
 
156
  $post_parent = get_option( 'attachments_cpt_' . $post_type->name );
157
 
158
+ if( $post_parent === false ) {
 
159
  // it wasn't set
160
  $settings['post_types'][$post_type->name] = false;
161
+ } else {
 
 
162
  $settings['post_types'][$post_type->name] = true;
163
  }
164
  }
170
  }
171
 
172
 
173
+ function attachments_register_settings() {
 
174
  // flag our settings
175
  register_setting(
176
  ATTACHMENTS_PREFIX . 'settings',
204
  );
205
  }
206
 
207
+ function attachments_edit_options() {}
 
208
 
209
+ function attachments_validate_settings($input) {
 
210
  $input['version'] = ATTACHMENTS_VERSION;
211
+
212
  return $input;
213
  }
214
 
215
+ function attachments_edit_post_parent() {
 
216
  $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
217
  ?>
218
  <div>
222
  </div>
223
  <?php }
224
 
225
+ function attachments_edit_post_types() {
 
226
  $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
227
  $args = array(
228
  'public' => true,
239
  $post_types['page']->labels->name = 'Pages';
240
  $post_types['page']->name = 'page';
241
 
242
+ if ( count( $post_types ) ) : foreach( $post_types as $post_type ) : ?>
243
  <div>
244
  <label for="<?php echo ATTACHMENTS_PREFIX; ?>settings[post_types][<?php echo $post_type->name; ?>]">
245
+ <input name="<?php echo ATTACHMENTS_PREFIX; ?>settings[post_types][<?php echo $post_type->name; ?>]" type="checkbox" id="<?php echo ATTACHMENTS_PREFIX; ?>settings[post_types][<?php echo $post_type->name; ?>]" value="1"<?php if( isset( $settings['post_types'][$post_type->name] ) && $settings['post_types'][$post_type->name] ) : ?> checked="checked"<?php endif; ?> /> <?php echo esc_html( $post_type->labels->name ); ?>
246
  </label>
247
  </div>
248
  <?php endforeach; endif; ?>
255
  * @return void
256
  * @author Jonathan Christopher
257
  */
258
+ function attachments_options() {
259
+ include dirname( __FILE__ ) . '/attachments.options.php';
 
260
  }
261
 
262
 
266
  * @return void
267
  * @author Jonathan Christopher
268
  */
269
+ function attachments_menu() {
 
270
  add_options_page('Settings', 'Attachments', 'manage_options', __FILE__, 'attachments_options');
271
  }
272
 
277
  * @return void
278
  * @author Jonathan Christopher
279
  */
280
+ function attachments_add() { ?>
 
281
 
282
  <div id="attachments-inner">
283
 
295
  </ul>
296
 
297
  <div id="attachments-list">
298
+ <input type="hidden" name="attachments_nonce" id="attachments_nonce" value="<?php echo esc_attr( wp_create_nonce( plugin_basename(__FILE__) ) ); ?>" />
299
  <ul>
300
  <?php
301
+ if ( ! empty( $_GET['post'] ) ) {
 
302
  // get all attachments
303
  $existing_attachments = attachments_get_attachments( intval( $_GET['post'] ) );
304
 
305
+ if ( is_array( $existing_attachments ) && ! empty( $existing_attachments ) ) {
306
+ foreach ( $existing_attachments as $attachment ) {
 
 
307
  // TODO: Better handle this when examining Handlebars template
308
+ if ( empty( $attachment['title'] ) ) {
 
309
  $attachment['title'] = ' ';
310
  }
311
+ if ( empty( $attachment['caption'] ) ) {
 
312
  $attachment['caption'] = ' ';
313
  }
314
  attachments_attachment_markup( $attachment['name'], $attachment['title'], $attachment['caption'], $attachment['id'], $attachment['order'] );
325
  <?php }
326
 
327
 
328
+ function attachments_attachment_markup( $name = null, $title = null, $caption = null, $id = null, $order = null ) { ?>
329
+ <?php
330
+ $name = esc_attr( $name );
331
+ $title = esc_attr( $title );
332
+ $id = esc_attr( $id );
333
+ $order = absint( $order );
334
+ ?>
335
  <li class="attachments-file">
336
  <h2>
337
  <a href="#" class="attachment-handle">
338
+ <span class="attachment-handle-icon"><img src="<?php echo esc_url( WP_PLUGIN_URL ); ?>/attachments/deprecated/images/handle.gif" alt="Drag" /></span>
339
  </a>
340
+ <span class="attachment-name"><?php echo empty( $name ) ? '{{name}}' : esc_html( $name ); ?></span>
341
+ <span class="attachment-delete"><a href="#"><?php _e( "Delete", "attachments" )?></a></span>
342
  </h2>
343
  <div class="attachments-fields">
344
  <div class="textfield" id="field_attachment_title_<?php echo empty( $id ) ? '{{id}}' : $id; ?>">
375
  * @return void
376
  * @author Jonathan Christopher
377
  */
378
+ function attachments_meta_box() {
 
379
  // for custom post types
380
+ if ( function_exists( 'get_post_types' ) ) {
 
381
  $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
382
 
383
  $args = array(
388
  $operator = 'and';
389
  $post_types = get_post_types( $args, $output, $operator );
390
 
391
+ foreach( $post_types as $post_type ) {
392
+ if ( isset( $settings['post_types'][$post_type->name] ) && $settings['post_types'][ $post_type->name ] ) {
 
 
393
  add_meta_box( 'attachments_list', __( 'Attachments', 'attachments' ), 'attachments_add', $post_type->name, 'normal' );
394
  }
395
  }
406
  function attachments_init_js()
407
  {
408
  echo '<script type="text/javascript" charset="utf-8">';
409
+ echo ' var attachments_base = "' . esc_url( WP_PLUGIN_URL ) . '/attachments"; ';
410
  echo ' var attachments_media = ""; ';
411
  echo '</script>';
412
  }
420
  * @author Jonathan Christopher
421
  * @author JR Tashjian
422
  */
423
+ function attachments_save($post_id) {
 
424
  // verify this came from the our screen and with proper authorization,
425
  // because save_post can be triggered at other times
426
+ if ( ! isset( $_POST['attachments_nonce'] ) ) {
 
427
  return $post_id;
428
  }
429
 
430
+ if ( ! wp_verify_nonce( $_POST['attachments_nonce'], plugin_basename(__FILE__) ) ) {
 
431
  return $post_id;
432
  }
433
 
434
  // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
435
  // to do anything
436
+ if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
 
437
  return $post_id;
438
  }
439
 
440
  // Check permissions
441
+ if ( 'page' == $_POST['post_type'] ) {
442
+ if ( ! current_user_can( 'edit_page', $post_id ) ) {
 
 
443
  return $post_id;
444
  }
445
+ } else {
446
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
 
 
 
447
  return $post_id;
448
  }
449
  }
459
  $attachment_ids = array();
460
 
461
  // We'll build our array of attachments
462
+ foreach ( $_POST as $key => $data ) {
 
463
  // Arbitrarily using the id
464
+ if( substr($key, 0, 14) == 'attachment_id_' ) {
 
465
  array_push( $attachment_ids, substr( $key, 14, strlen( $key ) ) );
466
  }
467
 
468
  }
469
 
470
  // If we have attachments, there's work to do
471
+ if ( !empty( $attachment_ids ) ) {
472
+ foreach ( $attachment_ids as $i ) {
473
+ if ( ! empty( $_POST['attachment_id_' . $i] ) ) {
474
+ $attachment_id = intval( $_POST['attachment_id_' . $i] );
 
 
 
 
475
 
476
  $attachment_details = array(
477
  'id' => $attachment_id,
488
 
489
  // save native Attach
490
  $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
491
+ if ( isset( $settings['post_parent'] ) && $settings['post_parent'] ) {
 
492
  // need to first check to make sure we're not overwriting a native Attach
493
  $attach_post_ref = get_post( $attachment_id );
494
 
495
+ if ( $attach_post_ref->post_parent == 0 ) {
 
496
  // no current Attach, we can add ours
497
  $attach_post = array();
498
  $attach_post['ID'] = $attachment_id;
507
 
508
  }
509
 
510
+ }
deprecated/get-attachments.php CHANGED
@@ -1,7 +1,9 @@
1
  <?php
2
 
3
  // Exit if accessed directly
4
- if( !defined( 'ABSPATH' ) ) exit;
 
 
5
 
6
  /**
7
  * Compares two array values with the same key "order"
@@ -11,21 +13,15 @@ if( !defined( 'ABSPATH' ) ) exit;
11
  * @return int
12
  * @author Jonathan Christopher
13
  */
14
- function attachments_cmp($a, $b)
15
- {
16
  $a = intval( $a['order'] );
17
  $b = intval( $b['order'] );
18
 
19
- if( $a < $b )
20
- {
21
  return -1;
22
- }
23
- else if( $a > $b )
24
- {
25
  return 1;
26
- }
27
- else
28
- {
29
  return 0;
30
  }
31
  }
@@ -41,10 +37,10 @@ function attachments_cmp($a, $b)
41
  function attachments_get_filesize_formatted( $path = NULL )
42
  {
43
  $formatted = '0 bytes';
44
- if( file_exists( $path ) )
45
- {
46
  $formatted = size_format( @filesize( $path ) );
47
  }
 
48
  return $formatted;
49
  }
50
 
@@ -58,27 +54,22 @@ function attachments_get_filesize_formatted( $path = NULL )
58
  * @author JR Tashjian
59
  */
60
 
61
- function attachments_get_attachments( $post_id=null )
62
- {
63
  global $post;
64
 
65
- if( $post_id==null )
66
- {
67
  $post_id = $post->ID;
68
  }
69
 
70
  // get all attachments
71
- $existing_attachments = get_post_meta( $post_id, '_attachments', false );
72
 
73
  // We can now proceed as normal, all legacy data should now be upgraded
74
 
75
  $post_attachments = array();
76
 
77
- if( is_array( $existing_attachments ) && count( $existing_attachments ) > 0 )
78
- {
79
-
80
- foreach ($existing_attachments as $attachment)
81
- {
82
  // decode and unserialize the data
83
  $data = unserialize( base64_decode( $attachment ) );
84
 
@@ -95,11 +86,10 @@ function attachments_get_attachments( $post_id=null )
95
  }
96
 
97
  // sort attachments
98
- if( count( $post_attachments ) > 1 )
99
- {
100
  usort( $post_attachments, "attachments_cmp" );
101
  }
102
  }
103
 
104
  return $post_attachments;
105
- }
1
  <?php
2
 
3
  // Exit if accessed directly
4
+ if ( !defined( 'ABSPATH' ) ) {
5
+ exit;
6
+ }
7
 
8
  /**
9
  * Compares two array values with the same key "order"
13
  * @return int
14
  * @author Jonathan Christopher
15
  */
16
+ function attachments_cmp($a, $b) {
 
17
  $a = intval( $a['order'] );
18
  $b = intval( $b['order'] );
19
 
20
+ if ( $a < $b ) {
 
21
  return -1;
22
+ } else if( $a > $b ) {
 
 
23
  return 1;
24
+ } else {
 
 
25
  return 0;
26
  }
27
  }
37
  function attachments_get_filesize_formatted( $path = NULL )
38
  {
39
  $formatted = '0 bytes';
40
+ if ( file_exists( $path ) ) {
 
41
  $formatted = size_format( @filesize( $path ) );
42
  }
43
+
44
  return $formatted;
45
  }
46
 
54
  * @author JR Tashjian
55
  */
56
 
57
+ function attachments_get_attachments( $post_id = null ) {
 
58
  global $post;
59
 
60
+ if ( $post_id == null ) {
 
61
  $post_id = $post->ID;
62
  }
63
 
64
  // get all attachments
65
+ $existing_attachments = get_post_meta( absint( $post_id ), '_attachments', false );
66
 
67
  // We can now proceed as normal, all legacy data should now be upgraded
68
 
69
  $post_attachments = array();
70
 
71
+ if ( is_array( $existing_attachments ) && count( $existing_attachments ) > 0 ) {
72
+ foreach ( $existing_attachments as $attachment ) {
 
 
 
73
  // decode and unserialize the data
74
  $data = unserialize( base64_decode( $attachment ) );
75
 
86
  }
87
 
88
  // sort attachments
89
+ if ( count( $post_attachments ) > 1 ) {
 
90
  usort( $post_attachments, "attachments_cmp" );
91
  }
92
  }
93
 
94
  return $post_attachments;
95
+ }
deprecated/index.php CHANGED
@@ -1 +1,3 @@
1
- <?php
 
 
1
+ <?php
2
+
3
+ // silence is golden
deprecated/js/index.php CHANGED
@@ -1 +1,3 @@
1
  <?php
 
 
1
  <?php
2
+
3
+ // silence is golden
index.php CHANGED
@@ -6,7 +6,7 @@
6
  * Description: Attachments gives the ability to append any number of Media Library items to Pages, Posts, and Custom Post Types
7
  * Author: Jonathan Christopher
8
  * Author URI: http://mondaybynoon.com/
9
- * Version: 3.5.4
10
  * Text Domain: attachments
11
  * Domain Path: /languages/
12
  * License: GPLv2 or later
@@ -14,34 +14,17 @@
14
  */
15
 
16
  // Exit if accessed directly
17
- if( !defined( 'ABSPATH' ) ) exit;
18
-
19
- // Store whether or not we're in the admin
20
- if( !defined( 'IS_ADMIN' ) ) define( 'IS_ADMIN', is_admin() );
21
-
22
- // Environment check
23
- $wp_version = get_bloginfo( 'version' );
24
-
25
- if( !version_compare( PHP_VERSION, '5.2', '>=' ) && IS_ADMIN && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) )
26
- {
27
- // failed PHP requirement
28
- require_once ABSPATH . '/wp-admin/includes/plugin.php';
29
- deactivate_plugins( __FILE__ );
30
- wp_die( esc_attr( __( 'Attachments requires PHP 5.2+. Attachments has been automatically deactivated.' ) ) );
31
  }
32
- else
33
- {
34
- if( ( defined( 'ATTACHMENTS_LEGACY' ) && ATTACHMENTS_LEGACY === true ) || version_compare( $wp_version, '3.5', '<' ) )
35
- {
36
- // load deprecated version of Attachments
37
- require_once 'deprecated/attachments.php';
38
- }
39
- else
40
- {
41
- define( 'ATTACHMENTS_DIR', plugin_dir_path( __FILE__ ) );
42
- define( 'ATTACHMENTS_URL', plugin_dir_url( __FILE__ ) );
43
 
44
- // load current version of Attachments
45
- require_once 'classes/class.attachments.php';
46
- }
 
 
 
 
 
 
47
  }
6
  * Description: Attachments gives the ability to append any number of Media Library items to Pages, Posts, and Custom Post Types
7
  * Author: Jonathan Christopher
8
  * Author URI: http://mondaybynoon.com/
9
+ * Version: 3.5.5
10
  * Text Domain: attachments
11
  * Domain Path: /languages/
12
  * License: GPLv2 or later
14
  */
15
 
16
  // Exit if accessed directly
17
+ if ( ! defined( 'ABSPATH' ) ) {
18
+ exit;
 
 
 
 
 
 
 
 
 
 
 
 
19
  }
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ if( ( defined( 'ATTACHMENTS_LEGACY' ) && ATTACHMENTS_LEGACY === true ) || version_compare( $wp_version, '3.5', '<' ) ) {
22
+ // load deprecated version of Attachments
23
+ require_once dirname( __FILE__ ) . '/deprecated/attachments.php';
24
+ } else {
25
+ define( 'ATTACHMENTS_DIR', plugin_dir_path( __FILE__ ) );
26
+ define( 'ATTACHMENTS_URL', plugin_dir_url( __FILE__ ) );
27
+
28
+ // load current version of Attachments
29
+ require_once dirname( __FILE__ ) . '/classes/class.attachments.php';
30
  }
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: jchristopher
3
  Donate link: http://mondaybynoon.com/donate/
4
  Tags: post, page, posts, pages, images, PDF, doc, Word, image, jpg, jpeg, picture, pictures, photos, attachment
5
  Requires at least: 3.0
6
- Tested up to: 3.9
7
- Stable tag: 3.5.4
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -91,6 +91,9 @@ Please see [Issues on GitHub](https://github.com/jchristopher/attachments/issues
91
 
92
  Please see [Attachments' changelog on GitHub](https://github.com/jchristopher/attachments/docs/changelog.md)
93
 
 
 
 
94
  = 3.5.4 =
95
  * Fixed assumption of field keys (props bukka)
96
  * Improved documentation (props Lane Goldberg, Roman Kokarev, Ore Landau)
3
  Donate link: http://mondaybynoon.com/donate/
4
  Tags: post, page, posts, pages, images, PDF, doc, Word, image, jpg, jpeg, picture, pictures, photos, attachment
5
  Requires at least: 3.0
6
+ Tested up to: 4.2
7
+ Stable tag: 3.5.5
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
91
 
92
  Please see [Attachments' changelog on GitHub](https://github.com/jchristopher/attachments/docs/changelog.md)
93
 
94
+ = 3.5.5 =
95
+ * Fixed an issue where field values were improperly overwritten when the instance was set to prepend in some cases
96
+
97
  = 3.5.4 =
98
  * Fixed assumption of field keys (props bukka)
99
  * Improved documentation (props Lane Goldberg, Roman Kokarev, Ore Landau)
views/options.php CHANGED
@@ -7,19 +7,19 @@
7
  include_once( ATTACHMENTS_DIR . '/classes/class.attachments.legacy.php' );
8
  $legacy = new AttachmentsLegacyHandler();
9
 
10
- if( isset( $_GET['dismiss'] ) )
11
- {
12
- if( !wp_verify_nonce( $_GET['nonce'], 'attachments-dismiss') )
13
  wp_die( __( 'Invalid request', 'attachments' ) );
 
14
 
15
  // set our flag that the user wants to ignore the migration message
16
  add_option( 'attachments_ignore_migration', true, '', 'no' );
17
  }
18
 
19
- if( isset( $_GET['dismiss-pro'] ) )
20
- {
21
- if( !wp_verify_nonce( $_GET['nonce'], 'attachments-pro-dismiss') )
22
  wp_die( __( 'Invalid request', 'attachments' ) );
 
23
 
24
  // set our flag that the user wants to ignore the migration message
25
  add_option( 'attachments_pro_ignore_migration', true, '', 'no' );
@@ -32,7 +32,7 @@
32
 
33
  <h2><?php _e( 'Attachments', 'attachments' ); ?></h2>
34
 
35
- <?php if( isset( $_GET['overview'] ) ) : ?>
36
 
37
  <div class="message updated" id="message">
38
  <p><?php _e( "<strong>Attachments has changed significantly since it's last update.</strong> These changes <em>will affect your themes and plugins</em>.", 'attachments' ); ?></p>
@@ -74,10 +74,8 @@
74
 
75
  <?php
76
  // check to see if we're migrating
77
- if( isset( $_GET['migrate'] ) )
78
- {
79
- switch( intval( $_GET['migrate'] ) )
80
- {
81
  case 1:
82
  $migrator->prepare_migration();
83
  break;
@@ -86,11 +84,8 @@
86
  $migrator->init_migration();
87
  break;
88
  }
89
- }
90
- elseif( isset( $_GET['migrate-pro'] ) )
91
- {
92
- switch( intval( $_GET['migrate-pro'] ) )
93
- {
94
  case 1:
95
  $migrator->prepare_pro_migration();
96
  break;
@@ -99,15 +94,13 @@
99
  $migrator->init_pro_migration();
100
  break;
101
  }
102
- }
103
- else
104
- { ?>
105
 
106
- <?php if( false == get_option( 'attachments_migrated' ) && $legacy->legacy ) : ?>
107
  <h2><?php _e( 'Migrate legacy Attachments data', 'attachments' ); ?></h2>
108
  <p><?php _e( 'Attachments has found records from version 1.x. Would you like to migrate them to version 3?', 'attachments' ); ?></p>
109
  <p><a href="?page=attachments&amp;migrate=1&amp;nonce=<?php echo wp_create_nonce( 'attachments-migrate-1' ); ?>" class="button-primary button"><?php _e( 'Migrate legacy data', 'attachments' ); ?></a></p>
110
- <?php elseif( true == get_option( 'attachments_migrated' ) ) : ?>
111
  <p>
112
  <?php _e( 'You have already migrated your legacy Attachments data.', 'attachments' ); ?>
113
  <a class="attachments-toggle-trigger" href="#migrated-legacy"><?php _e( 'View more', 'attachments' ); ?></a>
@@ -117,20 +110,20 @@
117
  </div>
118
  <?php endif; ?>
119
 
120
- <?php if( false == get_option( 'attachments_pro_migrated' ) && $legacy->legacy_pro ) : ?>
121
  <h2><?php _e( 'Migrate Attachments Pro data', 'attachments' ); ?></h2>
122
  <p><?php _e( 'Attachments has found records stored in Attachments Pro. Would you like to migrate them to Attachments?', 'attachments' ); ?></p>
123
  <p><a href="?page=attachments&amp;migrate-pro=1&amp;nonce=<?php echo wp_create_nonce( 'attachments-pro-migrate-1' ); ?>" class="button-primary button"><?php _e( 'Migrate Attachments Pro data', 'attachments' ); ?></a></p>
124
- <?php elseif( true == get_option( 'attachments_pro_migrated' ) ) : ?>
125
  <p>
126
  <?php _e( 'You have already migrated your Attachments Pro data.', 'attachments' ); ?>
127
  <a class="attachments-toggle-trigger" href="#migrated-legacy-pro"><?php _e( 'View more', 'attachments' ); ?></a>
128
  </p>
129
  <div id="migrated-legacy-pro" class="attachments-toggle-target" style="display:none;">
130
  <h2><?php _e( 'Moving from Attachments Pro: required code for your functions.php', 'attachments' ); ?></h2>
131
- <textarea style="display:block; width:100%; font-family:monospace; height:300px;"><?php echo get_option( 'attachments_pro_functions '); ?></textarea>
132
  <h2><?php _e( 'Moving from Attachments Pro: starter code for your theme templates', 'attachments' ); ?></h2>
133
- <textarea style="display:block; width:100%; font-family:monospace; height:200px;"><?php echo get_option( 'attachments_pro_template '); ?></textarea>
134
  </div>
135
  <?php endif; ?>
136
 
7
  include_once( ATTACHMENTS_DIR . '/classes/class.attachments.legacy.php' );
8
  $legacy = new AttachmentsLegacyHandler();
9
 
10
+ if ( isset( $_GET['dismiss'] ) ) {
11
+ if ( ! wp_verify_nonce( $_GET['nonce'], 'attachments-dismiss') ) {
 
12
  wp_die( __( 'Invalid request', 'attachments' ) );
13
+ }
14
 
15
  // set our flag that the user wants to ignore the migration message
16
  add_option( 'attachments_ignore_migration', true, '', 'no' );
17
  }
18
 
19
+ if ( isset( $_GET['dismiss-pro'] ) ) {
20
+ if ( ! wp_verify_nonce( $_GET['nonce'], 'attachments-pro-dismiss') ) {
 
21
  wp_die( __( 'Invalid request', 'attachments' ) );
22
+ }
23
 
24
  // set our flag that the user wants to ignore the migration message
25
  add_option( 'attachments_pro_ignore_migration', true, '', 'no' );
32
 
33
  <h2><?php _e( 'Attachments', 'attachments' ); ?></h2>
34
 
35
+ <?php if ( isset( $_GET['overview'] ) ) : ?>
36
 
37
  <div class="message updated" id="message">
38
  <p><?php _e( "<strong>Attachments has changed significantly since it's last update.</strong> These changes <em>will affect your themes and plugins</em>.", 'attachments' ); ?></p>
74
 
75
  <?php
76
  // check to see if we're migrating
77
+ if ( isset( $_GET['migrate'] ) ) {
78
+ switch( intval( $_GET['migrate'] ) ) {
 
 
79
  case 1:
80
  $migrator->prepare_migration();
81
  break;
84
  $migrator->init_migration();
85
  break;
86
  }
87
+ } elseif( isset( $_GET['migrate-pro'] ) ) {
88
+ switch( intval( $_GET['migrate-pro'] ) ) {
 
 
 
89
  case 1:
90
  $migrator->prepare_pro_migration();
91
  break;
94
  $migrator->init_pro_migration();
95
  break;
96
  }
97
+ } else { ?>
 
 
98
 
99
+ <?php if ( false == get_option( 'attachments_migrated' ) && $legacy->legacy ) : ?>
100
  <h2><?php _e( 'Migrate legacy Attachments data', 'attachments' ); ?></h2>
101
  <p><?php _e( 'Attachments has found records from version 1.x. Would you like to migrate them to version 3?', 'attachments' ); ?></p>
102
  <p><a href="?page=attachments&amp;migrate=1&amp;nonce=<?php echo wp_create_nonce( 'attachments-migrate-1' ); ?>" class="button-primary button"><?php _e( 'Migrate legacy data', 'attachments' ); ?></a></p>
103
+ <?php elseif ( true == get_option( 'attachments_migrated' ) ) : ?>
104
  <p>
105
  <?php _e( 'You have already migrated your legacy Attachments data.', 'attachments' ); ?>
106
  <a class="attachments-toggle-trigger" href="#migrated-legacy"><?php _e( 'View more', 'attachments' ); ?></a>
110
  </div>
111
  <?php endif; ?>
112
 
113
+ <?php if ( false == get_option( 'attachments_pro_migrated' ) && $legacy->legacy_pro ) : ?>
114
  <h2><?php _e( 'Migrate Attachments Pro data', 'attachments' ); ?></h2>
115
  <p><?php _e( 'Attachments has found records stored in Attachments Pro. Would you like to migrate them to Attachments?', 'attachments' ); ?></p>
116
  <p><a href="?page=attachments&amp;migrate-pro=1&amp;nonce=<?php echo wp_create_nonce( 'attachments-pro-migrate-1' ); ?>" class="button-primary button"><?php _e( 'Migrate Attachments Pro data', 'attachments' ); ?></a></p>
117
+ <?php elseif ( true == get_option( 'attachments_pro_migrated' ) ) : ?>
118
  <p>
119
  <?php _e( 'You have already migrated your Attachments Pro data.', 'attachments' ); ?>
120
  <a class="attachments-toggle-trigger" href="#migrated-legacy-pro"><?php _e( 'View more', 'attachments' ); ?></a>
121
  </p>
122
  <div id="migrated-legacy-pro" class="attachments-toggle-target" style="display:none;">
123
  <h2><?php _e( 'Moving from Attachments Pro: required code for your functions.php', 'attachments' ); ?></h2>
124
+ <textarea style="display:block; width:100%; font-family:monospace; height:300px;"><?php echo esc_textarea( get_option( 'attachments_pro_functions' ) ); ?></textarea>
125
  <h2><?php _e( 'Moving from Attachments Pro: starter code for your theme templates', 'attachments' ); ?></h2>
126
+ <textarea style="display:block; width:100%; font-family:monospace; height:200px;"><?php echo esc_textarea( get_option( 'attachments_pro_template' ) ); ?></textarea>
127
  </div>
128
  <?php endif; ?>
129