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

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