| 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', __('Images and Attachments') . ' - WP Attachments', |
| 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') |
| 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 |
$attachments = new WP_Query(array( |
| 139 |
'post_parent' => $post->ID, |
| 140 |
'post_type' => 'attachment', |
| 141 |
'post_status' => 'any', |
| 142 |
'orderby' => 'menu_order', |
| 143 |
'order' => 'ASC' |
| 144 |
)); |
| 145 |
|
| 146 |
include IJ_POST_ATTACHMENTS_DIR . '/html/metabox.php'; |
| 147 |
} |
| 148 |
//</editor-fold> |
| 149 |
|
| 150 |
//<editor-fold desc="Attachment Edition Screen"> |
| 151 |
/** |
| 152 |
* Output the script/link tags needed by the edit iframe |
| 153 |
* |
| 154 |
* @since 0.0.1a |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
public function attachmentEditHeadIframe() |
| 158 |
{ |
| 159 |
global $wp_scripts; |
| 160 |
wp_default_scripts($wp_scripts); |
| 161 |
|
| 162 |
// I don't know if all these scripts are really needed by the media edit screen. |
| 163 |
// They're just there, so they'll be here too :P |
| 164 |
include IJ_POST_ATTACHMENTS_DIR . '/html/attachmentEditHead.php'; |
| 165 |
|
| 166 |
// Add the needed vars to set the thumbnail :) |
| 167 |
$wp_scripts->localize('set-post-thumbnail', 'post_id', $_GET['post_id']); |
| 168 |
$wp_scripts->print_extra_script('set-post-thumbnail'); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Echoes the content of the edit iframe |
| 173 |
* |
| 174 |
* @since 0.0.1a |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
public function attachmentEditIframe() |
| 178 |
{ |
| 179 |
$url = admin_url('media.php'); |
| 180 |
$id = $_REQUEST['attachment_id']; |
| 181 |
include IJ_POST_ATTACHMENTS_DIR . '/html/attachmentEditIframe.php'; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Initialize the attachment edit pop-up. |
| 186 |
* |
| 187 |
* @since 0.0.1a |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
public function wp_ajax_ij_attachment_edit() |
| 191 |
{ |
| 192 |
add_action('admin_head-media-upload-popup', array($this, 'attachmentEditHeadIframe')); |
| 193 |
wp_iframe(array($this, 'attachmentEditIframe')); |
| 194 |
|
| 195 |
// Without the line below, the WP AJAX caller (admin-ajax.php) would print an '0' |
| 196 |
// at the end of the request. |
| 197 |
die; |
| 198 |
} |
| 199 |
//</editor-fold> |
| 200 |
|
| 201 |
//<editor-fold desc="Attachment sorting"> |
| 202 |
/** |
| 203 |
* Re-align attachments. |
| 204 |
* |
| 205 |
* @since 0.0.1a |
| 206 |
* @return void |
| 207 |
*/ |
| 208 |
public function wp_ajax_ij_realign() |
| 209 |
{ |
| 210 |
header('Content-Type: text/plain'); |
| 211 |
|
| 212 |
$alignment = $_REQUEST['alignment']; |
| 213 |
if (!is_array($alignment)) |
| 214 |
$alignment = array_map('trim', explode(',', $alignment)); |
| 215 |
|
| 216 |
$alignment = array_values($alignment); |
| 217 |
$count = count($alignment); |
| 218 |
|
| 219 |
for ($i = 0; $i < $count; $i++) { |
| 220 |
if (!is_numeric($alignment[$i])) |
| 221 |
continue; |
| 222 |
|
| 223 |
$attachment = get_post($alignment[$i]); |
| 224 |
$attachment->menu_order = $i; |
| 225 |
wp_update_post($attachment); |
| 226 |
} |
| 227 |
} |
| 228 |
//</editor-fold> |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
$IJ_Post_Attachments = IJ_Post_Attachments::getInstance(); |
| 233 |
|