| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_WPLR_Sync_Admin extends Meow_WPLR_Sync_RPC { |
| 4 |
|
| 5 |
public function __construct() { |
| 6 |
parent::__construct(); |
| 7 |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 8 |
} |
| 9 |
|
| 10 |
function admin_menu() { |
| 11 |
if ( WP_DEBUG ) { |
| 12 |
add_submenu_page( 'tools.php', 'WP/LR Sync', 'WP/LR Sync', 'manage_options', 'wplrsync_tools', array( $this, 'tools' ) ); |
| 13 |
} |
| 14 |
add_media_page( 'WP/LR Sync', 'WP/LR Sync', 'manage_options', 'wplrsync', array( $this, 'wplrsync_media' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
function display_image_box( $wpid, $image = null ) { |
| 18 |
$metadata = wp_get_attachment_image_src( $wpid, "full", false ); |
| 19 |
$filename = $metadata ? $metadata[0] : ""; |
| 20 |
echo "<div id='wplr-image-box-$wpid' class='wplr-image-box'>"; |
| 21 |
echo "<span class='wplr-title-wpid'>Media ID #<a target='_blank' href='post.php?post=$wpid&action=edit'>$wpid</a></span><br />"; |
| 22 |
echo "<a target='_blank' href='$filename'>" . wp_get_attachment_image( $wpid ) . "</a>"; |
| 23 |
echo "<div style='clear: both;'></div>"; |
| 24 |
echo "<span class='wplr-actions'>"; |
| 25 |
if ( $image ) { |
| 26 |
echo "<a style='background: #B34A4A;' href='upload.php?page=wplrsync&show=duplicates&action=unlink&wpid=$wpid&lrid=$image->lr_id'>Unlink</a>"; |
| 27 |
echo "<a style='' href='upload.php?page=wplrsync&show=duplicates&action=delete&wpid=$wpid&lrid=$image->lr_id'>Delete</a>"; |
| 28 |
} |
| 29 |
else { |
| 30 |
echo "<div> |
| 31 |
LR ID: |
| 32 |
<input type='text' class='wplr-sync-lrid-input' id='wplrsync-link-" . $wpid . "'></input> |
| 33 |
<a href='#' style='background: #3E79BB;' onclick='wplrsync_link($wpid)'>Link</a> |
| 34 |
</div>"; |
| 35 |
} |
| 36 |
echo "</span>"; |
| 37 |
|
| 38 |
|
| 39 |
echo "</div>"; |
| 40 |
} |
| 41 |
|
| 42 |
function wplrsync_media() { |
| 43 |
$images = array(); |
| 44 |
global $wpdb; |
| 45 |
$table_name = $wpdb->prefix . "lrsync"; |
| 46 |
$show = null; |
| 47 |
if ( isset( $_GET['action'] ) ) { |
| 48 |
$wpid = isset( $_GET['wpid'] ) ? $_GET['wpid'] : null; |
| 49 |
$lrid = isset( $_GET['lrid'] ) ? $_GET['lrid'] : null; |
| 50 |
if ( $_GET['action'] == "link" ) { |
| 51 |
$this->link_media( $lrid, $wpid ); |
| 52 |
} |
| 53 |
else if ( $_GET['action'] == "unlink" ) { |
| 54 |
$this->unlink_media( $lrid, $wpid ); |
| 55 |
} |
| 56 |
else if ( $_GET['action'] == "delete" ) { |
| 57 |
$this->unlink_media( $lrid, $wpid ); |
| 58 |
wp_delete_attachment( $wpid ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if ( isset( $_GET['show'] ) && $_GET['show'] == "duplicates" ) { |
| 63 |
$show = "duplicates"; |
| 64 |
$images = $wpdb->get_results( |
| 65 |
"SELECT lr.lr_id, lr.lr_file, GROUP_CONCAT(lr.wp_id SEPARATOR ',') as wpids |
| 66 |
FROM $wpdb->posts p, $table_name lr |
| 67 |
WHERE p.ID = lr.wp_id AND lr.lr_id != 0 |
| 68 |
GROUP BY lr.lr_id |
| 69 |
HAVING COUNT(p.ID) > 1 |
| 70 |
ORDER BY lr.lr_id DESC", OBJECT ); |
| 71 |
} |
| 72 |
else { |
| 73 |
$show = "unlinked"; |
| 74 |
$images = $this->list_unlinks( true ); |
| 75 |
} |
| 76 |
|
| 77 |
?> |
| 78 |
<style> |
| 79 |
|
| 80 |
.wplr-title-lrid { |
| 81 |
background: #515151; |
| 82 |
color: white; |
| 83 |
padding: 0px 2px; |
| 84 |
margin-left: 5px; |
| 85 |
} |
| 86 |
|
| 87 |
.wplr-duplicates-box { |
| 88 |
padding: 5px; |
| 89 |
border: 2px solid #515151; |
| 90 |
border-radius: 5px; |
| 91 |
margin-bottom: 10px; |
| 92 |
} |
| 93 |
|
| 94 |
.wplr-image-box { |
| 95 |
width: 150px; |
| 96 |
margin-right: 5px; |
| 97 |
float: left; |
| 98 |
font-size: 10px; |
| 99 |
} |
| 100 |
|
| 101 |
.wplr-title-wpid { |
| 102 |
font-size: 11px; |
| 103 |
line-height: 8px; |
| 104 |
} |
| 105 |
|
| 106 |
.wplr-title-filename { |
| 107 |
line-height: 8px; |
| 108 |
} |
| 109 |
|
| 110 |
.wplr-title-wpid a { |
| 111 |
text-decoration: none; |
| 112 |
} |
| 113 |
|
| 114 |
.wplr-actions a { |
| 115 |
margin-right: 5px; |
| 116 |
padding: 1px 3px; |
| 117 |
background: rgb(92, 92, 92); |
| 118 |
color: white; |
| 119 |
} |
| 120 |
|
| 121 |
</style> |
| 122 |
|
| 123 |
<div class='wrap'> |
| 124 |
<div id="icon-upload" class="icon32"><br></div> |
| 125 |
<h2>WP/LR Sync</h2> |
| 126 |
<p>You can easily link your unlinked photos through this screen. You can also check if you have any duplicate links (1 LR photo = more than 1 WP photo). Be careful, those actions are <u>instantaneous</u> and <u>unrecoverable</u>.</p> |
| 127 |
<ul class="subsubsub"> |
| 128 |
<li><a class="<?php echo $show == "unlinked" ? "current" : "" ?>" href="upload.php?page=wplrsync&show=unlinked">Unlinked Photos</a> |</li> |
| 129 |
<li><a class="<?php echo $show == "duplicates" ? "current" : "" ?>" href="upload.php?page=wplrsync&show=duplicates">Duplicated Links</a></li> |
| 130 |
</ul> |
| 131 |
<div style='clear: both; margin-bottom: 18px; margin-top: -5px;'></div> |
| 132 |
|
| 133 |
<?php |
| 134 |
|
| 135 |
if ( count( $images ) == 0 && $show == "unlinked" ) |
| 136 |
echo "<div class='updated'><p>There are no unlinked photos. Good job!</p></div>"; |
| 137 |
else if ( count( $images ) == 0 && $show == "duplicates" ) |
| 138 |
echo "<div class='updated'><p>There are no duplicated links.</p></div>"; |
| 139 |
else { |
| 140 |
foreach ( $images as $image ) { |
| 141 |
if ( $show == "duplicates" ) { |
| 142 |
echo "<b class='wplr-title-lrid'>LR ID " . $image->lr_id . "</b> Filename: " . ( $image->lr_file ? $image->lr_file : "Unknown" ); |
| 143 |
echo "<div class='wplr-duplicates-box'>"; |
| 144 |
$wpids = explode( ',', $image->wpids ); |
| 145 |
foreach ( $wpids as $wpid ) { |
| 146 |
$this->display_image_box( $wpid, $image ); |
| 147 |
} |
| 148 |
echo "<div style='clear: both;'></div>"; |
| 149 |
echo "</div>"; |
| 150 |
} |
| 151 |
else if ( $show == "unlinked" ) { |
| 152 |
$this->display_image_box( $image->ID ); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
echo "</div>"; |
| 158 |
} |
| 159 |
|
| 160 |
function tools() { |
| 161 |
if ( !current_user_can( 'manage_options' ) ) { |
| 162 |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
| 163 |
} |
| 164 |
|
| 165 |
// HEADER |
| 166 |
?> |
| 167 |
<div class="wrap" > |
| 168 |
|
| 169 |
<style> |
| 170 |
|
| 171 |
.left { |
| 172 |
width: 450px; |
| 173 |
} |
| 174 |
|
| 175 |
.right { |
| 176 |
position: absolute; |
| 177 |
left: 460px; |
| 178 |
} |
| 179 |
|
| 180 |
h3 { |
| 181 |
margin-bottom: 5px; |
| 182 |
} |
| 183 |
|
| 184 |
p { |
| 185 |
margin-top: 0px; |
| 186 |
margin-bottom: 10px; |
| 187 |
} |
| 188 |
|
| 189 |
.wplrsync-form { |
| 190 |
border: 1px solid lightgrey; |
| 191 |
padding: 5px; |
| 192 |
background: white; |
| 193 |
border-radius: 5px; |
| 194 |
width: 420px; |
| 195 |
} |
| 196 |
|
| 197 |
th { |
| 198 |
text-align: left; |
| 199 |
} |
| 200 |
|
| 201 |
#wpfooter { |
| 202 |
display: none; |
| 203 |
} |
| 204 |
|
| 205 |
</style> |
| 206 |
|
| 207 |
<h2> |
| 208 |
WP/LR Sync |
| 209 |
<span> |
| 210 |
<form style="float: right;" method="post" action=""> |
| 211 |
<input type="hidden" name="action" value="reset"> |
| 212 |
<input type="submit" name="submit" id="submit" class="button button-primary" value="Reset"> |
| 213 |
</form> |
| 214 |
<form style="float: right; margin-right: 5px;" method="post" action=""> |
| 215 |
<input type="hidden" name="action" value="duplicates"> |
| 216 |
<input type="submit" name="submit" id="submit" class="button button-primary" value="Get Duplicates"> |
| 217 |
</form> |
| 218 |
<form style="float: right; margin-right: 5px;" method="post" action=""> |
| 219 |
<input type="hidden" name="action" value="scan"> |
| 220 |
<input type="submit" name="submit" id="submit" class="button button-primary" value="Get Unlinked"> |
| 221 |
</form> |
| 222 |
<form style="float: right; margin-right: 5px;" method="post" action=""> |
| 223 |
<input type="hidden" name="action" value="list"> |
| 224 |
<input type="submit" name="submit" id="submit" class="button button-primary" value="Get Linked"> |
| 225 |
</form> |
| 226 |
</span> |
| 227 |
</h2> |
| 228 |
<?php |
| 229 |
|
| 230 |
global $wpdb; |
| 231 |
$table_name = $wpdb->prefix . "lrsync"; |
| 232 |
|
| 233 |
if ( isset( $_POST['action'] ) ) { |
| 234 |
|
| 235 |
$action = $_POST['action']; |
| 236 |
if ( $action == "reset" ) { |
| 237 |
$this->reset_db(); |
| 238 |
echo "<div class='updated'><p>The database was reset.</p></div>"; |
| 239 |
} |
| 240 |
else if ( $action == "duplicates" ) { |
| 241 |
echo "<div class='updated'><p>Checked duplicates.</p></div>"; |
| 242 |
} |
| 243 |
else if ( $action == "scan" ) { |
| 244 |
$unlinks = $this->list_unlinks(); |
| 245 |
} |
| 246 |
else if ( $action == "list" ) { |
| 247 |
$list = $this->list_sync_media(); |
| 248 |
} |
| 249 |
else if ( $action == "link" ) { |
| 250 |
if ( $this->link_media( $_POST['lr_id'], $_POST['wp_id'] ) ) { |
| 251 |
echo "<div class='updated'><p>Link successful.</p></div>"; |
| 252 |
} |
| 253 |
else { |
| 254 |
echo "<div class='error'><p>" . $this->get_error()->message . "</p></div>"; |
| 255 |
} |
| 256 |
} |
| 257 |
else if ( $action == "linkinfo_upload" ) { |
| 258 |
if ( isset( $_FILES['file'], $_FILES['file']['tmp_name'] ) && !empty( $_FILES['file']['tmp_name'] ) ) { |
| 259 |
$tmp_path = $_FILES['file']['tmp_name']; |
| 260 |
$linkinfo = $this->linkinfo_upload( $tmp_path, null ); |
| 261 |
} |
| 262 |
else { |
| 263 |
$linkinfo = $this->linkinfo_media( $_POST['wp_id'] ); |
| 264 |
} |
| 265 |
if ( $linkinfo ) |
| 266 |
echo "<div class='updated'><p>Link info retrieved.</p></div>"; |
| 267 |
else |
| 268 |
echo "<div class='error'><p>" . $this->get_error()->message . "</p></div>"; |
| 269 |
} |
| 270 |
else if ( $action == "sync" ) { |
| 271 |
$tmp_path = $_FILES['file']['tmp_name']; |
| 272 |
$lrinfo = new Meow_WPLR_LRInfo(); |
| 273 |
$lrinfo->lr_id = $_POST['lr_id'] == "" ? -1 : $_POST['lr_id']; |
| 274 |
$lrinfo->lr_file = $_FILES['file']['name']; |
| 275 |
$lrinfo->lr_title = isset( $_POST['title'] ) ? $_POST['title'] : ""; |
| 276 |
$lrinfo->lr_caption = $_POST['caption']; |
| 277 |
$lrinfo->lr_desc = isset( $_POST['desc'] ) ? $_POST['desc'] : ""; |
| 278 |
$lrinfo->lr_alt_text = isset( $_POST['altText'] ) ? $_POST['altText'] : ""; |
| 279 |
$lrinfo->sync_title = isset( $_POST['syncTitle'] ) && $_POST['syncTitle'] == 'on'; |
| 280 |
$lrinfo->sync_caption = isset( $_POST['syncCaption'] ) && $_POST['syncCaption'] == 'on'; |
| 281 |
$lrinfo->sync_desc = isset( $_POST['syncDesc'] ) && $_POST['syncDesc'] == 'on'; |
| 282 |
$lrinfo->sync_alt_text = isset( $_POST['syncAltText'] ) && $_POST['syncAltText'] == 'on'; |
| 283 |
$lrinfo->type = $_FILES['file']['type']; |
| 284 |
if ( $this->sync_media( $lrinfo, $tmp_path ) ) |
| 285 |
echo "<div class='updated'><p>Lr ID " . $_POST['lr_id'] . " was synchronized with the attachment.</p></div>"; |
| 286 |
else |
| 287 |
echo "<div class='error'><p>" . $this->get_error()->message . "</p></div>"; |
| 288 |
|
| 289 |
} |
| 290 |
else if ( $action == "unlink" ) { |
| 291 |
if ( $this->unlink_media( $_POST['lr_id'], $_POST['wp_id'] ) ) |
| 292 |
echo "<div class='updated'><p>Media unlinked.</p></div>"; |
| 293 |
else |
| 294 |
echo "<div class='error'><p>" . $this->get_error()->message . "</p></div>"; |
| 295 |
} |
| 296 |
else if ( $action == "remove" ) { |
| 297 |
if ( $this->delete_media( $_POST['lr_id'] ) ) |
| 298 |
echo "<div class='updated'><p>Media removed.</p></div>"; |
| 299 |
else |
| 300 |
echo "<div class='error'><p>" . $this->get_error()->message . "</p></div>"; |
| 301 |
} |
| 302 |
else { |
| 303 |
echo "<div class='error'<p>Unknown action.</p></div>"; |
| 304 |
} |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
$link_files = $this->list_sync_media(); |
| 309 |
$unlink_files = $this->list_unlinks(); |
| 310 |
$link_files_count = count( $link_files ); |
| 311 |
$unlink_files_count = count( $unlink_files ); |
| 312 |
|
| 313 |
// CONTENT & FORMS |
| 314 |
?> |
| 315 |
<div class="right"> |
| 316 |
<?php |
| 317 |
if ( isset( $_POST['action'] ) && $action == "scan" ) { |
| 318 |
echo '<pre>'; |
| 319 |
print_r( $unlinks ); |
| 320 |
echo '</pre>'; |
| 321 |
} |
| 322 |
else if ( isset( $_POST['action'] ) && $action == "list" ) { |
| 323 |
echo '<pre>'; |
| 324 |
print_r( $list ); |
| 325 |
echo '</pre>'; |
| 326 |
} |
| 327 |
else if ( isset( $_POST['action'] ) && $action == "linkinfo_upload" ) { |
| 328 |
echo '<pre>'; |
| 329 |
print_r( $linkinfo ); |
| 330 |
echo '</pre>'; |
| 331 |
} |
| 332 |
else if ( isset( $_POST['duplicates'] ) && $action == "list" ) { |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
echo '<pre>'; |
| 337 |
print_r( $list ); |
| 338 |
echo '</pre>'; |
| 339 |
} |
| 340 |
?> |
| 341 |
</div> |
| 342 |
|
| 343 |
<div class="left"> |
| 344 |
<div style="font-weight: bold;"><?php echo $link_files_count ?> linked files out of <?php echo ( $link_files_count + $unlink_files_count ) ?> media files.</div> |
| 345 |
|
| 346 |
<h3>Link</h3> |
| 347 |
<p>Will link the WP Media ID to the Lr ID.</p> |
| 348 |
<form class="wplrsync-form" method="post" action="" enctype="multipart/form-data"> |
| 349 |
<input type="hidden" name="action" value="link"> |
| 350 |
<table> |
| 351 |
<tr> |
| 352 |
<th scope="row"><label for="lr_id">Lr ID</label></th> |
| 353 |
<td><input name="lr_id" type="text" id="lr_id" value="" class="regular-text code"></td> |
| 354 |
</tr> |
| 355 |
<tr> |
| 356 |
<th scope="row"><label for="wp_id">Media ID</label></th> |
| 357 |
<td><input name="wp_id" type="text" id="wp_id" value="" class="regular-text code"></td> |
| 358 |
</tr> |
| 359 |
<tr> |
| 360 |
<th scope="row"></th> |
| 361 |
<td><input type="submit" name="submit" id="submit" class="button button-primary" value="Link Media ID + Lr ID"></td> |
| 362 |
</tr> |
| 363 |
</table> |
| 364 |
|
| 365 |
</form> |
| 366 |
|
| 367 |
<h3>Unlink</h3> |
| 368 |
<p>Will unlink the media.</p> |
| 369 |
<form class="wplrsync-form" method="post" action=""> |
| 370 |
<input type="hidden" name="action" value="unlink"> |
| 371 |
<table> |
| 372 |
<tr> |
| 373 |
<th scope="row"><label for="lr_id">Lr ID</label></th> |
| 374 |
<td><input name="lr_id" type="text" id="lr_id" value="" class="regular-text code"></td> |
| 375 |
</tr> |
| 376 |
<tr> |
| 377 |
<th scope="row"><label for="wp_id">Media ID</label></th> |
| 378 |
<td><input name="wp_id" type="text" id="wp_id" value="" class="regular-text code"></td> |
| 379 |
</tr> |
| 380 |
<tr> |
| 381 |
<th scope="row"></th> |
| 382 |
<td><input type="submit" name="submit" id="submit" class="button button-primary" value="Unlink"></td> |
| 383 |
</tr> |
| 384 |
</table> |
| 385 |
</form> |
| 386 |
|
| 387 |
<h3>Sync</h3> |
| 388 |
<p>Will create the entry if doesn't exist, will update it if exists.</p> |
| 389 |
<form class="wplrsync-form" method="post" action="" enctype="multipart/form-data"> |
| 390 |
<input type="hidden" name="action" value="sync"> |
| 391 |
<table> |
| 392 |
<tr> |
| 393 |
<th scope="row"><label for="lr_id">Lr ID</label></th> |
| 394 |
<td><input name="lr_id" type="text" id="lr_id" value="" class="regular-text code"></td> |
| 395 |
</tr> |
| 396 |
<tr> |
| 397 |
<th scope="row"><label for="title">Title</label></th> |
| 398 |
<td><input name="title" type="text" id="title" class="regular-text code"></td> |
| 399 |
</tr> |
| 400 |
<tr> |
| 401 |
<th scope="row"><label for="title">Caption</label></th> |
| 402 |
<td><input name="caption" type="text" id="caption" class="regular-text code"></td> |
| 403 |
</tr> |
| 404 |
<tr> |
| 405 |
<th scope="row"><label for="title">Desc</label></th> |
| 406 |
<td><input name="desc" type="text" id="desc" class="regular-text code"></td> |
| 407 |
</tr> |
| 408 |
<tr> |
| 409 |
<th scope="row"><label for="title">Alt</label></th> |
| 410 |
<td><input name="altText" type="text" id="altText" class="regular-text code"></td> |
| 411 |
</tr> |
| 412 |
<tr> |
| 413 |
<th scope="row"><label for="file">File</label></th> |
| 414 |
<td><input name="file" type="file" id="file"></td> |
| 415 |
</tr> |
| 416 |
<tr> |
| 417 |
<th scope="row"><label for="sync">Sync</label></th> |
| 418 |
<td> |
| 419 |
<label><input type="checkbox" id="syncTitle" name="syncTitle">Title</label> |
| 420 |
<label><input type="checkbox" id="syncCaption" name="syncCaption">Caption</label> |
| 421 |
<label><input type="checkbox" id="syncDesc" name="syncDesc">Desc</label> |
| 422 |
<label><input type="checkbox" id="syncAltText" name="syncAltText">Alt</label> |
| 423 |
</td> |
| 424 |
</tr> |
| 425 |
<tr> |
| 426 |
<th scope="row"></th> |
| 427 |
<td><input type="submit" name="submit" id="submit" class="button button-primary" value="Sync Media"></td> |
| 428 |
</tr> |
| 429 |
</table> |
| 430 |
</form> |
| 431 |
|
| 432 |
<h3>Remove</h3> |
| 433 |
<p>Will remove the media.</p> |
| 434 |
<form class="wplrsync-form" method="post" action=""> |
| 435 |
<input type="hidden" name="action" value="remove"> |
| 436 |
<table> |
| 437 |
<tr> |
| 438 |
<th scope="row"><label for="lr_id">Lr ID</label></th> |
| 439 |
<td><input name="lr_id" type="text" id="lr_id" value="" class="regular-text code"></td> |
| 440 |
</tr> |
| 441 |
<tr> |
| 442 |
<th scope="row"></th> |
| 443 |
<td><input type="submit" name="submit" id="submit" class="button button-primary" value="Remove Media"></td> |
| 444 |
</tr> |
| 445 |
</table> |
| 446 |
</form> |
| 447 |
|
| 448 |
<h3>Link info</h3> |
| 449 |
<p>Link info for either a WP ID <b>or</b> the Uploaded File.</p> |
| 450 |
<form class="wplrsync-form" method="post" action="" enctype="multipart/form-data"> |
| 451 |
<input type="hidden" name="action" value="linkinfo_upload"> |
| 452 |
<table> |
| 453 |
<tr> |
| 454 |
<th scope="row"><label for="wp_id">WP ID</label></th> |
| 455 |
<td><input name="wp_id" type="text" id="wp_id" value="" class="regular-text code"></td> |
| 456 |
</tr> |
| 457 |
<tr> |
| 458 |
<th scope="row"><label for="file">File</label></th> |
| 459 |
<td><input name="file" type="file" id="file"></td> |
| 460 |
</tr> |
| 461 |
<tr> |
| 462 |
<th scope="row"></th> |
| 463 |
<td><input type="submit" name="submit" id="submit" class="button button-primary" value="Check"></td> |
| 464 |
</tr> |
| 465 |
</table> |
| 466 |
</form> |
| 467 |
|
| 468 |
</div> |
| 469 |
</div> |
| 470 |
</div> |
| 471 |
<?php |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
?> |