Attachments - Version 1.6

Version Description

  • Updated settings to use the Settings API
  • Tested with WordPress 3.3
  • Removed support for extremely legacy Attachments storage. If you have upgraded from a version before 1.0.7, please downgrade to 1.5.10 and let me know.
Download this release

Release Info

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

Code changes from version 1.5.10 to 1.6

Files changed (3) hide show
  1. attachments.options.php +6 -59
  2. attachments.php +167 -47
  3. readme.txt +7 -2
attachments.options.php CHANGED
@@ -1,17 +1,14 @@
1
  <?php
2
-
3
  if( isset( $_GET['dismisspro'] ) )
4
  {
5
  update_option( '_attachments_dismiss_pro', 1 );
6
- // wp_redirect( 'options-general.php?page=attachments/attachments.php' );
7
- // die();
8
  }
9
-
10
  ?>
11
 
12
  <div class="wrap">
13
 
14
  <div id="icon-options-general" class="icon32"><br /></div>
 
15
  <h2>Attachments Options</h2>
16
 
17
  <?php $attachments_dismiss_pro = get_option( '_attachments_dismiss_pro' ); if( !$attachments_dismiss_pro ) : ?>
@@ -21,62 +18,12 @@
21
  <?php endif; ?>
22
 
23
  <form action="options.php" method="post">
24
- <?php wp_nonce_field('update-options'); ?>
25
-
26
- <?php if( function_exists( 'get_post_types' ) ) : ?>
27
-
28
- <?php
29
- $args = array(
30
- 'public' => true,
31
- 'show_ui' => true,
32
- '_builtin' => false
33
- );
34
- $output = 'objects';
35
- $operator = 'and';
36
- $post_types = get_post_types( $args, $output, $operator );
37
-
38
- // we also want to optionally enable Pages and Posts
39
- $post_types['post']->labels->name = 'Posts';
40
- $post_types['post']->name = 'post';
41
- $post_types['page']->labels->name = 'Pages';
42
- $post_types['page']->name = 'page';
43
-
44
- ?>
45
-
46
- <?php if( count( $post_types ) ) : ?>
47
-
48
- <h3><?php _e("Post Type Settings", "attachments"); ?></h3>
49
- <p><?php _e("Include Attachments in the following Post Types:", "attachments"); ?></p>
50
- <?php foreach($post_types as $post_type) : ?>
51
-
52
- <div class="attachments_checkbox">
53
- <input type="checkbox" name="attachments_cpt_<?php echo $post_type->name; ?>" id="attachments_cpt_<?php echo $post_type->name; ?>" value="true"<?php if (get_option('attachments_cpt_' . $post_type->name)=='true') : ?> checked="checked"<?php endif ?> />
54
- <label for="attachments_cpt_<?php echo $post_type->name; ?>"><?php echo $post_type->labels->name; ?></label>
55
- </div>
56
-
57
- <?php endforeach ?>
58
-
59
- <?php else: ?>
60
-
61
- <p><?php _e('Attachments can be integrated with your Custom Post Types. Unfortunately, there are none to work with at this time.', 'attachments'); ?></p>
62
-
63
- <?php endif ?>
64
-
65
- <?php endif ?>
66
-
67
- <h3><?php _e("Miscellaneous", "attachments"); ?></h3>
68
- <div class="attachments_checkbox">
69
- <input type="checkbox" name="attachments_store_native" id="attachments_store_native" value="true"<?php if (get_option('attachments_store_native')=='true') : ?> checked="checked"<?php endif ?> />
70
- <label for="attachments_store_native">Make WordPress-level attachment relationships</label>
71
- <p class="note">If checked, Attachments will tell WordPress that all Attachments for the entry should be marked as such as though it were included in the main editor. The association will be made as though it were. Changing this option <strong>will not</strong> update existing Attachments, it only effects future saves.</p>
72
  </div>
73
-
74
- <input type="hidden" name="action" value="update" />
75
- <input type="hidden" name="page_options" value="attachments_store_native,<?php if( !empty( $post_types ) ) : foreach( $post_types as $post_type ) : ?>attachments_cpt_<?php echo $post_type->name; ?>,<?php endforeach; endif; ?>" />
76
  <p class="submit">
77
- <input type="submit" class="button-primary" value="<?php _e("Save", "attachments");?>" />
78
  </p>
79
-
80
  </form>
81
-
82
- </div>
1
  <?php
 
2
  if( isset( $_GET['dismisspro'] ) )
3
  {
4
  update_option( '_attachments_dismiss_pro', 1 );
 
 
5
  }
 
6
  ?>
7
 
8
  <div class="wrap">
9
 
10
  <div id="icon-options-general" class="icon32"><br /></div>
11
+
12
  <h2>Attachments Options</h2>
13
 
14
  <?php $attachments_dismiss_pro = get_option( '_attachments_dismiss_pro' ); if( !$attachments_dismiss_pro ) : ?>
18
  <?php endif; ?>
19
 
20
  <form action="options.php" method="post">
21
+ <div id="poststuff" class="metabox-holder">
22
+ <?php settings_fields( 'attachments_settings' ); ?>
23
+ <?php do_settings_sections( 'attachments_options' ); ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  </div>
 
 
 
25
  <p class="submit">
26
+ <input type="submit" class="button-primary" value="Save Options" />
27
  </p>
 
28
  </form>
29
+ </div>
 
attachments.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Attachments
4
  Plugin URI: http://mondaybynoon.com/wordpress-attachments/
5
  Description: Attachments gives the ability to append any number of Media Library items to Pages, Posts, and Custom Post Types
6
- Version: 1.5.10
7
  Author: Jonathan Christopher
8
  Author URI: http://mondaybynoon.com/
9
  */
@@ -30,6 +30,8 @@
30
  if( !defined( 'IS_ADMIN' ) )
31
  define( 'IS_ADMIN', is_admin() );
32
 
 
 
33
 
34
 
35
  // ===========
@@ -49,7 +51,7 @@ if( !version_compare( PHP_VERSION, '5.2', '>=' ) || !version_compare( $wp_versio
49
  {
50
  require_once ABSPATH.'/wp-admin/includes/plugin.php';
51
  deactivate_plugins( __FILE__ );
52
- wp_die( __('Attachments requires PHP 5.2 or higher, as will WordPress 3.2 and higher. It has been automatically deactivated.') );
53
  }
54
  else
55
  {
@@ -65,13 +67,16 @@ if( !version_compare( PHP_VERSION, '5.2', '>=' ) || !version_compare( $wp_versio
65
 
66
  if( IS_ADMIN )
67
  {
68
- add_action( 'admin_menu', 'attachments_init' );
69
- add_action( 'admin_head', 'attachments_init_js' );
70
- add_action( 'save_post', 'attachments_save' );
71
- add_action( 'admin_menu', 'attachments_menu' );
72
- add_action( 'admin_footer', 'attachments_footer_js' );
73
- add_action( 'in_plugin_update_message-attachments/attachments.php', 'attachments_update_message' );
74
- add_filter( 'plugin_row_meta', 'attachments_filter_plugin_row_meta', 10, 2 );
 
 
 
75
 
76
  load_plugin_textdomain( 'attachments', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
77
  }
@@ -82,6 +87,150 @@ if( IS_ADMIN )
82
  // = FUNCTIONS =
83
  // =============
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  /**
86
  * Includes our plugin update message
87
  *
@@ -246,6 +395,8 @@ function attachments_meta_box()
246
  // for custom post types
247
  if( function_exists( 'get_post_types' ) )
248
  {
 
 
249
  $args = array(
250
  'public' => true,
251
  'show_ui' => true
@@ -256,7 +407,7 @@ function attachments_meta_box()
256
 
257
  foreach($post_types as $post_type)
258
  {
259
- if (get_option('attachments_cpt_' . $post_type->name)=='true')
260
  {
261
  add_meta_box( 'attachments_list', __( 'Attachments', 'attachments' ), 'attachments_add', $post_type->name, 'normal' );
262
  }
@@ -273,8 +424,6 @@ function attachments_meta_box()
273
  */
274
  function attachments_init_js()
275
  {
276
- global $pagenow;
277
-
278
  echo '<script type="text/javascript" charset="utf-8">';
279
  echo ' var attachments_base = "' . WP_PLUGIN_URL . '/attachments"; ';
280
  echo ' var attachments_media = ""; ';
@@ -372,7 +521,8 @@ function attachments_save($post_id)
372
  add_post_meta( $post_id, '_attachments', $attachment_serialized );
373
 
374
  // save native Attach
375
- if( get_option( 'attachments_store_native' ) == 'true' )
 
376
  {
377
  // need to first check to make sure we're not overwriting a native Attach
378
  $attach_post_ref = get_post( $attachment_id );
@@ -439,36 +589,6 @@ function attachments_get_attachments( $post_id=null )
439
  // get all attachments
440
  $existing_attachments = get_post_meta( $post_id, '_attachments', false );
441
 
442
- if( !empty( $existing_attachments ) )
443
- {
444
- try
445
- {
446
- $legacy_existing_attachments = unserialize( $existing_attachments[0] );
447
- }
448
- catch( Exception $e )
449
- {
450
- // unserialization failed
451
- }
452
- }
453
-
454
- // Check for legacy attachments
455
- if( isset( $legacy_existing_attachments ) )
456
- {
457
- if( is_array( $legacy_existing_attachments ) )
458
- {
459
- $tmp_legacy_attachments = array();
460
-
461
- // Legacy attachments (single serialized record)
462
- foreach ( $legacy_existing_attachments as $legacy_attachment )
463
- {
464
- array_push( $tmp_legacy_attachments, base64_encode( serialize( $legacy_attachment ) ) );
465
- }
466
-
467
- $existing_attachments = $tmp_legacy_attachments;
468
- }
469
- }
470
-
471
-
472
  // We can now proceed as normal, all legacy data should now be upgraded
473
 
474
  $post_attachments = array();
@@ -533,8 +653,6 @@ function attachments_footer_js()
533
  */
534
  function attachments_init()
535
  {
536
- global $pagenow;
537
-
538
  wp_enqueue_script( 'jquery-ui-core' );
539
  wp_enqueue_script( 'thickbox' );
540
 
@@ -560,7 +678,9 @@ function attachments_init()
560
  /**
561
  * Modifies the plugin meta line on the WP Plugins page
562
  *
563
- * @return $plugin_meta Array of plugin meta data
 
 
564
  * @author Jonathan Christopher
565
  */
566
  function attachments_filter_plugin_row_meta( $plugin_meta, $plugin_file )
@@ -575,4 +695,4 @@ function attachments_filter_plugin_row_meta( $plugin_meta, $plugin_file )
575
  {
576
  return $plugin_meta;
577
  }
578
- }
3
  Plugin Name: Attachments
4
  Plugin URI: http://mondaybynoon.com/wordpress-attachments/
5
  Description: Attachments gives the ability to append any number of Media Library items to Pages, Posts, and Custom Post Types
6
+ Version: 1.6
7
  Author: Jonathan Christopher
8
  Author URI: http://mondaybynoon.com/
9
  */
30
  if( !defined( 'IS_ADMIN' ) )
31
  define( 'IS_ADMIN', is_admin() );
32
 
33
+ define( 'ATTACHMENTS_PREFIX', 'attachments_' );
34
+ define( 'ATTACHMENTS_VERSION', '1.6' );
35
 
36
 
37
  // ===========
51
  {
52
  require_once ABSPATH.'/wp-admin/includes/plugin.php';
53
  deactivate_plugins( __FILE__ );
54
+ wp_die( __('Attachments requires PHP 5.2 or higher, as does WordPress 3.2+. Attachments has been automatically deactivated.') );
55
  }
56
  else
57
  {
67
 
68
  if( IS_ADMIN )
69
  {
70
+ add_action( 'init', 'attachments_pre_init' );
71
+
72
+ add_action( 'admin_menu', 'attachments_init' );
73
+ add_action( 'admin_head', 'attachments_init_js' );
74
+ add_action( 'save_post', 'attachments_save' );
75
+ add_action( 'admin_menu', 'attachments_menu' );
76
+ add_action( 'admin_footer', 'attachments_footer_js' );
77
+ add_action( 'in_plugin_update_message-attachments/attachments.php', 'attachments_update_message' );
78
+ add_filter( 'plugin_row_meta', 'attachments_filter_plugin_row_meta', 10, 2 );
79
+ add_action( 'admin_init', 'attachments_register_settings' );
80
 
81
  load_plugin_textdomain( 'attachments', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
82
  }
87
  // = FUNCTIONS =
88
  // =============
89
 
90
+ function attachments_pre_init()
91
+ {
92
+ // as of version 1.6 we'll be storing a proper settings array
93
+ if( !get_option( ATTACHMENTS_PREFIX . 'settings' ) )
94
+ {
95
+ $settings = array();
96
+
97
+ // we've got a version < 1.6 and therefore no real settings
98
+ $settings['version'] = ATTACHMENTS_VERSION;
99
+
100
+ $post_parent = get_option( 'attachments_store_native' );
101
+
102
+ if( $post_parent === false )
103
+ {
104
+ // it wasn't set
105
+ $settings['post_parent'] = false;
106
+ }
107
+ else
108
+ {
109
+ $settings['post_parent'] = true;
110
+ }
111
+
112
+ // grab our custom post types
113
+ $args = array(
114
+ 'public' => true,
115
+ 'show_ui' => true,
116
+ '_builtin' => false
117
+ );
118
+ $output = 'objects';
119
+ $operator = 'and';
120
+ $post_types = get_post_types( $args, $output, $operator );
121
+
122
+ // we also want to optionally enable Pages and Posts
123
+ $post_types['post']->labels->name = 'Posts';
124
+ $post_types['post']->name = 'post';
125
+ $post_types['page']->labels->name = 'Pages';
126
+ $post_types['page']->name = 'page';
127
+
128
+ if( count( $post_types ) )
129
+ {
130
+ foreach( $post_types as $post_type )
131
+ {
132
+ $post_parent = get_option( 'attachments_cpt_' . $post_type->name );
133
+
134
+ if( $post_parent === false )
135
+ {
136
+ // it wasn't set
137
+ $settings['post_types'][$post_type->name] = false;
138
+ }
139
+ else
140
+ {
141
+ $settings['post_types'][$post_type->name] = true;
142
+ }
143
+ }
144
+ }
145
+
146
+ // save our settings
147
+ update_option( ATTACHMENTS_PREFIX . 'settings', $settings );
148
+ }
149
+ }
150
+
151
+
152
+ function attachments_register_settings()
153
+ {
154
+ // flag our settings
155
+ register_setting(
156
+ ATTACHMENTS_PREFIX . 'settings', // group
157
+ ATTACHMENTS_PREFIX . 'settings', // name of options
158
+ 'attachments_validate_settings' // validation callback
159
+ );
160
+
161
+ add_settings_section(
162
+ ATTACHMENTS_PREFIX . 'options', // section ID
163
+ 'Post Type Settings', // title
164
+ 'attachments_edit_options', // display callback
165
+ 'attachments_options' // page name (do_settings_sections)
166
+ );
167
+
168
+ // post types
169
+ add_settings_field(
170
+ ATTACHMENTS_PREFIX . 'post_types', // unique field ID
171
+ 'Post Types', // title
172
+ 'attachments_edit_post_types', // input box display callback
173
+ 'attachments_options', // page name (as above)
174
+ ATTACHMENTS_PREFIX . 'options' // first arg to add_settings_section
175
+ );
176
+
177
+ // post_parent
178
+ add_settings_field(
179
+ ATTACHMENTS_PREFIX . 'post_parent', // unique field ID
180
+ 'Set Post Parent', // title
181
+ 'attachments_edit_post_parent', // input box display callback
182
+ 'attachments_options', // page name (as above)
183
+ ATTACHMENTS_PREFIX . 'options' // first arg to add_settings_section
184
+ );
185
+ }
186
+
187
+ function attachments_edit_options()
188
+ { }
189
+
190
+ function attachments_validate_settings($input)
191
+ {
192
+ $input['version'] = ATTACHMENTS_VERSION;
193
+ return $input;
194
+ }
195
+
196
+ function attachments_edit_post_parent()
197
+ {
198
+ $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
199
+ ?>
200
+ <div>
201
+ <label for="<?php echo ATTACHMENTS_PREFIX; ?>settings[post_parent]">
202
+ <input name="<?php echo ATTACHMENTS_PREFIX; ?>settings[post_parent]" type="checkbox" id="<?php echo ATTACHMENTS_PREFIX; ?>settings[post_parent]" value="1"<?php if( isset( $settings['post_parent'] ) && $settings['post_parent'] ) : ?> checked="checked"<?php endif; ?> /> Set the <code>post_parent</code> when Attachments are saved
203
+ </label>
204
+ </div>
205
+ <?php }
206
+
207
+ function attachments_edit_post_types()
208
+ {
209
+ $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
210
+ $args = array(
211
+ 'public' => true,
212
+ 'show_ui' => true,
213
+ '_builtin' => false
214
+ );
215
+ $output = 'objects';
216
+ $operator = 'and';
217
+ $post_types = get_post_types( $args, $output, $operator );
218
+
219
+ // we also want to optionally enable Pages and Posts
220
+ $post_types['post']->labels->name = 'Posts';
221
+ $post_types['post']->name = 'post';
222
+ $post_types['page']->labels->name = 'Pages';
223
+ $post_types['page']->name = 'page';
224
+
225
+ if( count( $post_types ) ) : foreach($post_types as $post_type) : ?>
226
+ <div>
227
+ <label for="<?php echo ATTACHMENTS_PREFIX; ?>settings[post_types][<?php echo $post_type->name; ?>]">
228
+ <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; ?>
229
+ </label>
230
+ </div>
231
+ <?php endforeach; endif; ?>
232
+ <?php }
233
+
234
  /**
235
  * Includes our plugin update message
236
  *
395
  // for custom post types
396
  if( function_exists( 'get_post_types' ) )
397
  {
398
+ $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
399
+
400
  $args = array(
401
  'public' => true,
402
  'show_ui' => true
407
 
408
  foreach($post_types as $post_type)
409
  {
410
+ if( isset( $settings['post_types'][$post_type->name] ) && $settings['post_types'][$post_type->name] )
411
  {
412
  add_meta_box( 'attachments_list', __( 'Attachments', 'attachments' ), 'attachments_add', $post_type->name, 'normal' );
413
  }
424
  */
425
  function attachments_init_js()
426
  {
 
 
427
  echo '<script type="text/javascript" charset="utf-8">';
428
  echo ' var attachments_base = "' . WP_PLUGIN_URL . '/attachments"; ';
429
  echo ' var attachments_media = ""; ';
521
  add_post_meta( $post_id, '_attachments', $attachment_serialized );
522
 
523
  // save native Attach
524
+ $settings = get_option( ATTACHMENTS_PREFIX . 'settings' );
525
+ if( isset( $settings['post_parent'] ) && $settings['post_parent'] )
526
  {
527
  // need to first check to make sure we're not overwriting a native Attach
528
  $attach_post_ref = get_post( $attachment_id );
589
  // get all attachments
590
  $existing_attachments = get_post_meta( $post_id, '_attachments', false );
591
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
592
  // We can now proceed as normal, all legacy data should now be upgraded
593
 
594
  $post_attachments = array();
653
  */
654
  function attachments_init()
655
  {
 
 
656
  wp_enqueue_script( 'jquery-ui-core' );
657
  wp_enqueue_script( 'thickbox' );
658
 
678
  /**
679
  * Modifies the plugin meta line on the WP Plugins page
680
  *
681
+ * @param $plugin_meta
682
+ * @param $plugin_file
683
+ * @return array $plugin_meta Array of plugin meta data
684
  * @author Jonathan Christopher
685
  */
686
  function attachments_filter_plugin_row_meta( $plugin_meta, $plugin_file )
695
  {
696
  return $plugin_meta;
697
  }
698
+ }
readme.txt CHANGED
@@ -4,7 +4,7 @@ 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.3
7
- Stable tag: 1.5.10
8
 
9
  Attachments allows you to simply append any number of items from your WordPress Media Library to Posts, Pages, and Custom Post Types
10
 
@@ -62,6 +62,11 @@ Attachments uses WordPress' built in Media library for uploads and storage.
62
 
63
  == Changelog ==
64
 
 
 
 
 
 
65
  = 1.5.10 =
66
  * WordPress 3.3 compatibility
67
  * Updated Polish translation
@@ -221,4 +226,4 @@ Here is a basic implementation:
221
  <?php endfor; ?>
222
  </ul>
223
  <?php endif; ?>
224
- <?php } ?>`
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.3
7
+ Stable tag: 1.6
8
 
9
  Attachments allows you to simply append any number of items from your WordPress Media Library to Posts, Pages, and Custom Post Types
10
 
62
 
63
  == Changelog ==
64
 
65
+ = 1.6 =
66
+ * Updated settings to use the Settings API
67
+ * Tested with WordPress 3.3
68
+ * Removed support for extremely legacy Attachments storage. If you have upgraded from a version before 1.0.7, please downgrade to 1.5.10 and let me know.
69
+
70
  = 1.5.10 =
71
  * WordPress 3.3 compatibility
72
  * Updated Polish translation
226
  <?php endfor; ?>
227
  </ul>
228
  <?php endif; ?>
229
+ <?php } ?>`