PluginProbe
WP Attachments / 3.5.4
WP Attachments v3.5.4
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.4, at ij-post-attachments.php

240 lines 5.4 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 $pid = isset($_GET['post']) ? $_GET['post'] : 0;
87 global $pagenow;
88 if (!(in_array( $pagenow, array( 'post-new.php' ) )) && ($pid == '0' || $pid == NULL)) {
89 return; //Additional check to avoid showing the metabox in not-intended pages (ex. Members Plugin)
90 }
91
92 add_meta_box(
93 'ij-post-attachments', __('Media'),
94 array($this, 'printMetaBox'), null, 'normal', 'high'
95 );
96 }
97
98 /**
99 * Enqueue the JS files needed by the plugin
100 *
101 * @since 0.0.1a
102 * @return void
103 */
104 public function admin_enqueue_scripts()
105 {
106 global $hook_suffix;
107 if ($hook_suffix != 'post.php')
108 return;
109
110 wp_enqueue_script('syoHint', IJ_POST_ATTACHMENTS_URL . 'scripts/jquery.syoHint.js', array('jquery'), '1.0.10');
111 wp_enqueue_script(
112 'ij-post-attachments', IJ_POST_ATTACHMENTS_URL . 'scripts/ij-post-attachments.js',
113 array('syoHint', 'jquery-ui-sortable'), IJ_POST_ATTACHMENTS_VER
114 );
115
116 wp_localize_script('ij-post-attachments', 'IJ_Post_Attachments_Vars', array(
117 'editMedia' => __('Edit Media'),
118 'postID' => isset($_GET['post']) ? $_GET['post'] : 0
119 ));
120 }
121
122 /**
123 * Enqueue the plugin CSS
124 *
125 * @since 0.0.1a
126 * @return void
127 */
128 public function admin_print_styles()
129 {
130 global $hook_suffix;
131 if ($hook_suffix == 'post.php' || $hook_suffix == 'post-new.php' )
132 wp_enqueue_style('ij-post-attachments', IJ_POST_ATTACHMENTS_URL . 'styles/ij-post-attachments.css', array(), IJ_POST_ATTACHMENTS_VER);
133 }
134
135 /**
136 * Create the meta box below the post editor and list the files
137 *
138 * @since 0.0.1a
139 * @param object $post
140 * @return void
141 */
142 public function printMetaBox($post)
143 {
144
145 $attachments = new WP_Query(array(
146 'post_parent' => $post->ID,
147 'post_type' => 'attachment',
148 'post_status' => 'any',
149 'orderby' => 'menu_order',
150 'order' => 'ASC'
151 ));
152
153 include IJ_POST_ATTACHMENTS_DIR . '/html/metabox.php';
154 }
155 //</editor-fold>
156
157 //<editor-fold desc="Attachment Edition Screen">
158 /**
159 * Output the script/link tags needed by the edit iframe
160 *
161 * @since 0.0.1a
162 * @return void
163 */
164 public function attachmentEditHeadIframe()
165 {
166 global $wp_scripts;
167 wp_default_scripts($wp_scripts);
168
169 // I don't know if all these scripts are really needed by the media edit screen.
170 // They're just there, so they'll be here too :P
171 include IJ_POST_ATTACHMENTS_DIR . '/html/attachmentEditHead.php';
172
173 // Add the needed vars to set the thumbnail :)
174 $wp_scripts->localize('set-post-thumbnail', 'post_id', $_GET['post_id']);
175 $wp_scripts->print_extra_script('set-post-thumbnail');
176 }
177
178 /**
179 * Echoes the content of the edit iframe
180 *
181 * @since 0.0.1a
182 * @return void
183 */
184 public function attachmentEditIframe()
185 {
186 $url = admin_url('media.php');
187 $id = $_REQUEST['attachment_id'];
188 include IJ_POST_ATTACHMENTS_DIR . '/html/attachmentEditIframe.php';
189 }
190
191 /**
192 * Initialize the attachment edit pop-up.
193 *
194 * @since 0.0.1a
195 * @return void
196 */
197 public function wp_ajax_ij_attachment_edit()
198 {
199 add_action('admin_head-media-upload-popup', array($this, 'attachmentEditHeadIframe'));
200 wp_iframe(array($this, 'attachmentEditIframe'));
201
202 // Without the line below, the WP AJAX caller (admin-ajax.php) would print an '0'
203 // at the end of the request.
204 die;
205 }
206 //</editor-fold>
207
208 //<editor-fold desc="Attachment sorting">
209 /**
210 * Re-align attachments.
211 *
212 * @since 0.0.1a
213 * @return void
214 */
215 public function wp_ajax_ij_realign()
216 {
217 header('Content-Type: text/plain');
218
219 $alignment = $_REQUEST['alignment'];
220 if (!is_array($alignment))
221 $alignment = array_map('trim', explode(',', $alignment));
222
223 $alignment = array_values($alignment);
224 $count = count($alignment);
225
226 for ($i = 0; $i < $count; $i++) {
227 if (!is_numeric($alignment[$i]))
228 continue;
229
230 $attachment = get_post($alignment[$i]);
231 $attachment->menu_order = $i;
232 wp_update_post($attachment);
233 }
234 }
235 //</editor-fold>
236
237 }
238
239 $IJ_Post_Attachments = IJ_Post_Attachments::getInstance();
240