| 1 |
<?php |
| 2 |
|
| 3 |
////////////////////////////////////////////////////////////// |
| 4 |
//=========================================================== |
| 5 |
// template.php |
| 6 |
//=========================================================== |
| 7 |
// PAGELAYER |
| 8 |
// Inspired by the DESIRE to be the BEST OF ALL |
| 9 |
// ---------------------------------------------------------- |
| 10 |
// Started by: Pulkit Gupta |
| 11 |
// Date: 23rd Jan 2017 |
| 12 |
// Time: 23:00 hrs |
| 13 |
// Site: http://pagelayer.com/wordpress (PAGELAYER) |
| 14 |
// ---------------------------------------------------------- |
| 15 |
// Please Read the Terms of use at http://pagelayer.com/tos |
| 16 |
// ---------------------------------------------------------- |
| 17 |
//=========================================================== |
| 18 |
// (c)Pagelayer Team |
| 19 |
//=========================================================== |
| 20 |
////////////////////////////////////////////////////////////// |
| 21 |
|
| 22 |
// Are we being accessed directly ? |
| 23 |
if(!defined('PAGELAYER_VERSION')) { |
| 24 |
exit('Hacking Attempt !'); |
| 25 |
} |
| 26 |
|
| 27 |
//function is called first to select the route |
| 28 |
function pagelayer_replace_page(){ |
| 29 |
|
| 30 |
global $pl_error; |
| 31 |
|
| 32 |
if(!current_user_can('upload_files')){ |
| 33 |
wp_die(esc_html__('You do not have permission to upload files.', 'pagelayer')); |
| 34 |
} |
| 35 |
|
| 36 |
$post_id = (int) $_GET['id']; |
| 37 |
|
| 38 |
if(empty($post_id)){ |
| 39 |
wp_die(esc_html__('ID not found .', 'pagelayer')); |
| 40 |
} |
| 41 |
|
| 42 |
// Load the attachment |
| 43 |
$post = get_post($post_id); |
| 44 |
|
| 45 |
if(empty($post) || is_wp_error($post)){ |
| 46 |
wp_die(esc_html__('ID not found .', 'pagelayer')); |
| 47 |
} |
| 48 |
|
| 49 |
// Authorization check |
| 50 |
if(!current_user_can('edit_post', $post_id)){ |
| 51 |
wp_die(esc_html__('You do not have permission to edit this attachment.', 'pagelayer')); |
| 52 |
} |
| 53 |
|
| 54 |
// Process the POST ! |
| 55 |
if(isset($_FILES['userfile'])){ |
| 56 |
|
| 57 |
if(!check_admin_referer()){ |
| 58 |
wp_die('Invalid Nonce'); |
| 59 |
} |
| 60 |
|
| 61 |
/** Check if file is uploaded properly **/ |
| 62 |
if(!is_uploaded_file($_FILES['userfile']['tmp_name'])){ |
| 63 |
$pl_error['upload_error'] = __('No file was uploaded ! Please try again.'); |
| 64 |
pagelayer_media_replace_theme(); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if(isset($_FILES['userfile']['error']) && $_FILES['userfile']['error'] > 0){ |
| 69 |
$pl_error['upload_error'] = __('There was some error uploading the file ! Please try again.'); |
| 70 |
pagelayer_media_replace_theme(); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$filedata = wp_check_filetype_and_ext($_FILES['userfile']['tmp_name'], $_FILES['userfile']['name']); |
| 75 |
|
| 76 |
if ($filedata['ext'] == false){ |
| 77 |
$pl_error['ext_error'] = __('The File type could not be determined. Please upload a permitted file type.'); |
| 78 |
pagelayer_media_replace_theme(); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$result = pagelayer_replace_attachment($_FILES['userfile']['tmp_name'], $post_id, $err); |
| 83 |
|
| 84 |
if(empty($result)){ |
| 85 |
$pl_error['replace_error'] = $err; |
| 86 |
pagelayer_media_replace_theme(); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$redirect_success = admin_url('post.php'); |
| 91 |
$redirect_success = add_query_arg(array( |
| 92 |
'action' => 'edit', |
| 93 |
'post' => $post_id, |
| 94 |
), $redirect_success); |
| 95 |
|
| 96 |
echo '<meta http-equiv="refresh" content="0;url='.$redirect_success.'" />'; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
// Show the theme |
| 101 |
pagelayer_media_replace_theme(); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
// Theme of the page |
| 106 |
function pagelayer_media_replace_theme(){ |
| 107 |
|
| 108 |
global $pl_error; |
| 109 |
|
| 110 |
pagelayer_report_error($pl_error);echo '<br />'; |
| 111 |
|
| 112 |
$id = (int) $_GET['id']; |
| 113 |
|
| 114 |
// Authorization check |
| 115 |
if(!current_user_can('edit_post', $id)){ |
| 116 |
wp_die(esc_html__('You do not have permission to edit this attachment.', 'pagelayer')); |
| 117 |
} |
| 118 |
?> |
| 119 |
<div class="wrap"> |
| 120 |
<h1><?php echo esc_html__("Replace Media File", 'pagelayer'); ?></h1> |
| 121 |
<form enctype="multipart/form-data" method="POST"> |
| 122 |
<div class="editor-wrapper"> |
| 123 |
<section class="image_chooser wrapper"> |
| 124 |
<input type="hidden" name="ID" id="ID" value="<?php echo $id ?>" /> |
| 125 |
<p><?php echo esc_html__("Choose a file to upload from your computer", 'pagelayer'); ?></p> |
| 126 |
<div class="drop-wrapper"> |
| 127 |
<p><input type="file" name="userfile" id="userfile" /></p> |
| 128 |
<?php wp_nonce_field(); ?> |
| 129 |
</div> |
| 130 |
</section> |
| 131 |
<section class="form_controls wrapper"> |
| 132 |
<input id="submit" type="submit" class="button button-primary" name="submit" value="<?php echo esc_attr__("Upload", 'pagelayer');?>" /> |
| 133 |
</section> |
| 134 |
</div> |
| 135 |
</form> |
| 136 |
<?php |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
// Replace the uploaded media with the new one |
| 141 |
function pagelayer_replace_attachment($file, $post_id, &$error = ''){ |
| 142 |
|
| 143 |
if(function_exists('wp_get_original_image_path')){ |
| 144 |
$targetFile = wp_get_original_image_path($post_id); |
| 145 |
}else{ |
| 146 |
$targetFile = trim(get_attached_file($post_id, apply_filters( 'pagelayer_unfiltered_get_attached_file', true ))); |
| 147 |
} |
| 148 |
|
| 149 |
$fileparts = pathinfo($targetFile); |
| 150 |
$filePath = isset($fileparts['dirname']) ? trailingslashit($fileparts['dirname']) : ''; |
| 151 |
$fileName = isset($fileparts['basename']) ? $fileparts['basename'] : ''; |
| 152 |
$filedata = wp_check_filetype_and_ext($targetFile, $fileName); |
| 153 |
$fileMime = (isset($filedata['type'])) ? $filedata['type'] : false; |
| 154 |
|
| 155 |
if(empty($targetFile)){ |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
if(empty($filePath)){ |
| 160 |
$error = 'No folder for the target found !'; |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
// Remove the files of the original attachment |
| 165 |
pagelayer_remove_attahment_files($post_id); |
| 166 |
|
| 167 |
$result_moved = move_uploaded_file($file, $targetFile); |
| 168 |
|
| 169 |
if (false === $result_moved){ |
| 170 |
$error = sprintf( esc_html__('The uploaded file could not be moved to %1$s. This is most likely an issue with permissions, or upload failed.', 'pagelayer'), $targetFile ); |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
$permissions = fileperms($targetFile) & 0777; |
| 175 |
if ($permissions > 0){ |
| 176 |
chmod( $targetFile, $permissions ); // restore permissions |
| 177 |
} |
| 178 |
|
| 179 |
$updated = update_attached_file($post_id, $targetFile); |
| 180 |
|
| 181 |
$target_url = wp_get_attachment_url($post_id); |
| 182 |
|
| 183 |
// Run the filter, so other plugins can hook if needed. |
| 184 |
$filtered = apply_filters( 'wp_handle_upload', array( |
| 185 |
'file' => $targetFile, |
| 186 |
'url' => $target_url, |
| 187 |
'type' => $fileMime, |
| 188 |
), 'sideload'); |
| 189 |
|
| 190 |
// Check if file changed during filter. Set changed to attached file meta properly. |
| 191 |
if (isset($filtered['file']) && $filtered['file'] != $targetFile ){ |
| 192 |
update_attached_file($post_id, $filtered['file']); |
| 193 |
} |
| 194 |
|
| 195 |
$metadata = wp_generate_attachment_metadata($post_id, $targetFile); |
| 196 |
wp_update_attachment_metadata($post_id, $metadata); |
| 197 |
|
| 198 |
return true; |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
function pagelayer_remove_attahment_files($post_id){ |
| 203 |
|
| 204 |
$meta = wp_get_attachment_metadata( $post_id ); |
| 205 |
|
| 206 |
if (function_exists('wp_get_original_image_path')){ // WP 5.3+ |
| 207 |
$fullfilepath = wp_get_original_image_path($post_id); |
| 208 |
}else{ |
| 209 |
$fullFilePath = trim(get_attached_file($post_id, apply_filters( 'pagelayer_unfiltered_get_attached_file', true ))); |
| 210 |
} |
| 211 |
|
| 212 |
$backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true ); |
| 213 |
$file = $fullFilePath; |
| 214 |
$result = wp_delete_attachment_files($post_id, $meta, $backup_sizes, $file ); |
| 215 |
|
| 216 |
// If attached file is not the same path as file, this indicates a -scaled images is in play. |
| 217 |
$attached_file = get_attached_file($post_id); |
| 218 |
|
| 219 |
if ($file !== $attached_file && file_exists($attached_file)){ |
| 220 |
@unlink($attached_file); |
| 221 |
} |
| 222 |
} |
| 223 |
|