PluginProbe
WP Attachments / 3.5.2
WP Attachments v3.5.2
6.0.2 6.0.3 trunk 2.0 3 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.2 3.1.3 3.1.4 3.2 3.2.1 3.2.2 3.2.3 3.2.4 3.3 3.4 3.5 3.5.1 3.5.2 3.5.3 3.5.4 All 52 releases
wp-attachments / ij-post-attachments.php

ij-post-attachments.php in WP Attachments 3.5.2, at ij-post-attachments.php

234 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 BASED ON IJ POST ATTACHMENTS BY GUSTAVO HENKE - http://www.injoin.com.br
4 */
5
6 //<editor-fold desc="Constants">
7 define('IJ_POST_ATTACHMENTS_DIR', dirname(__FILE__));
8 define('IJ_POST_ATTACHMENTS_URL', plugin_dir_url(__FILE__));
9 define('IJ_POST_ATTACHMENTS_VER', '0.1.0');
10 //</editor-fold>
11
12 class IJ_Post_Attachments
13 {
14
15 //<editor-fold desc="Properties">
16 /**
17 * List of methods that are WP actions.
18 *
19 * @since 0.0.1a
20 * @var array
21 */
22 private $actions = array(
23 'add_meta_boxes', 'admin_print_styles', 'admin_enqueue_scripts',
24 'wp_ajax_ij_realign', 'wp_ajax_ij_attachment_edit'
25 );
26
27 /**
28 * The singleton instance of this class
29 *
30 * @since 0.0.1a
31 * @var IJ_Post_Attachments
32 */
33 private static $instance;
34 //</editor-fold>
35
36 //<editor-fold desc="Basic methods">
37 /**
38 * Constructor
39 *
40 * @since 0.0.1a
41 * @return IJ_Post_Attachments
42 */
43 private function __construct()
44 {
45 foreach ($this->actions as $action)
46 add_action($action, array($this, $action));
47 }
48
49 /**
50 * Singleton access for the class
51 *
52 * @static
53 * @since 0.0.1a
54 * @return IJ_Post_Attachments
55 */
56 static public function getInstance()
57 {
58 if (!isset(self::$instance))
59 self::$instance = new IJ_Post_Attachments();
60
61 return self::$instance;
62 }
63
64 /**
65 * Disallows cloning this class
66 *
67 * @since 0.0.1a
68 * @throws Exception
69 * @return void
70 */
71 public function __clone()
72 {
73 throw new Exception("Clone is disallowed.");
74 }
75 //</editor-fold>
76
77 //<editor-fold desc="Metabox">
78 /**
79 * Add the plugin meta box
80 *
81 * @since 0.0.1a
82 * @return void
83 */
84 public function add_meta_boxes()
85 {
86 add_meta_box(
87 'ij-post-attachments', __('Media'),
88 array($this, 'printMetaBox'), null, 'normal', 'high'
89 );
90 }
91
92 /**
93 * Enqueue the JS files needed by the plugin
94 *
95 * @since 0.0.1a
96 * @return void
97 */
98 public function admin_enqueue_scripts()
99 {
100 global $hook_suffix;
101 if ($hook_suffix != 'post.php')
102 return;
103
104 wp_enqueue_script('syoHint', IJ_POST_ATTACHMENTS_URL . 'scripts/jquery.syoHint.js', array('jquery'), '1.0.10');
105 wp_enqueue_script(
106 'ij-post-attachments', IJ_POST_ATTACHMENTS_URL . 'scripts/ij-post-attachments.js',
107 array('syoHint', 'jquery-ui-sortable'), IJ_POST_ATTACHMENTS_VER
108 );
109
110 wp_localize_script('ij-post-attachments', 'IJ_Post_Attachments_Vars', array(
111 'editMedia' => __('Edit Media'),
112 'postID' => isset($_GET['post']) ? $_GET['post'] : 0
113 ));
114 }
115
116 /**
117 * Enqueue the plugin CSS
118 *
119 * @since 0.0.1a
120 * @return void
121 */
122 public function admin_print_styles()
123 {
124 global $hook_suffix;
125 if ($hook_suffix == 'post.php' || $hook_suffix == 'post-new.php' )
126 wp_enqueue_style('ij-post-attachments', IJ_POST_ATTACHMENTS_URL . 'styles/ij-post-attachments.css', array(), IJ_POST_ATTACHMENTS_VER);
127 }
128
129 /**
130 * Create the meta box below the post editor and list the files
131 *
132 * @since 0.0.1a
133 * @param object $post
134 * @return void
135 */
136 public function printMetaBox($post)
137 {
138
139 $attachments = new WP_Query(array(
140 'post_parent' => $post->ID,
141 'post_type' => 'attachment',
142 'post_status' => 'any',
143 'orderby' => 'menu_order',
144 'order' => 'ASC'
145 ));
146
147 include IJ_POST_ATTACHMENTS_DIR . '/html/metabox.php';
148 }
149 //</editor-fold>
150
151 //<editor-fold desc="Attachment Edition Screen">
152 /**
153 * Output the script/link tags needed by the edit iframe
154 *
155 * @since 0.0.1a
156 * @return void
157 */
158 public function attachmentEditHeadIframe()
159 {
160 global $wp_scripts;
161 wp_default_scripts($wp_scripts);
162
163 // I don't know if all these scripts are really needed by the media edit screen.
164 // They're just there, so they'll be here too :P
165 include IJ_POST_ATTACHMENTS_DIR . '/html/attachmentEditHead.php';
166
167 // Add the needed vars to set the thumbnail :)
168 $wp_scripts->localize('set-post-thumbnail', 'post_id', $_GET['post_id']);
169 $wp_scripts->print_extra_script('set-post-thumbnail');
170 }
171
172 /**
173 * Echoes the content of the edit iframe
174 *
175 * @since 0.0.1a
176 * @return void
177 */
178 public function attachmentEditIframe()
179 {
180 $url = admin_url('media.php');
181 $id = $_REQUEST['attachment_id'];
182 include IJ_POST_ATTACHMENTS_DIR . '/html/attachmentEditIframe.php';
183 }
184
185 /**
186 * Initialize the attachment edit pop-up.
187 *
188 * @since 0.0.1a
189 * @return void
190 */
191 public function wp_ajax_ij_attachment_edit()
192 {
193 add_action('admin_head-media-upload-popup', array($this, 'attachmentEditHeadIframe'));
194 wp_iframe(array($this, 'attachmentEditIframe'));
195
196 // Without the line below, the WP AJAX caller (admin-ajax.php) would print an '0'
197 // at the end of the request.
198 die;
199 }
200 //</editor-fold>
201
202 //<editor-fold desc="Attachment sorting">
203 /**
204 * Re-align attachments.
205 *
206 * @since 0.0.1a
207 * @return void
208 */
209 public function wp_ajax_ij_realign()
210 {
211 header('Content-Type: text/plain');
212
213 $alignment = $_REQUEST['alignment'];
214 if (!is_array($alignment))
215 $alignment = array_map('trim', explode(',', $alignment));
216
217 $alignment = array_values($alignment);
218 $count = count($alignment);
219
220 for ($i = 0; $i < $count; $i++) {
221 if (!is_numeric($alignment[$i]))
222 continue;
223
224 $attachment = get_post($alignment[$i]);
225 $attachment->menu_order = $i;
226 wp_update_post($attachment);
227 }
228 }
229 //</editor-fold>
230
231 }
232
233 $IJ_Post_Attachments = IJ_Post_Attachments::getInstance();
234