[英]function override issue wordpress
I have image upload function in my WordPress function.php
file but my function.php
file is encoded with an ioncube encoder so I can't edit that function.我的 WordPress
function.php
文件中有图片上传功能,但我的function.php
文件是用 ioncube 编码器编码的,所以我无法编辑该功能。
but I have a complete code of that function given by my theme author.但我有我的主题作者提供的该功能的完整代码。 and author told me to override that function it will work.
和作者告诉我覆盖该功能它会起作用。
my function name is pinc_upload_pin()
and it is added like add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
我的函数名称是
pinc_upload_pin()
并且添加它像add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
now I need to override that above function.现在我需要覆盖上面的函数。
i have tried like create one separate file name function_new.php and include it in header.php
like using require_once
.我尝试过创建一个单独的文件名 function_new.php 并将其包含在
header.php
就像使用require_once
。 i have remove above function using remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
我已经使用
remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
删除了上述功能remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
and add my own function and hook it.并添加我自己的函数并钩住它。 below is my
function_new.php
code下面是我的
function_new.php
代码
remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
function pinc_upload_pin_new(){
check_ajax_referer('upload_pin', 'ajax-nonce');
do_action('pinc_before_upload_pin', $_POST);
$minWidth = 2;
$minHeight = 2;
$minWidth = apply_filters('pinc_minwidth', $minWidth);
$minHeight = apply_filters('pinc_minheight', $minHeight);
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
if ($_POST['mode'] == 'computer') {
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$imageTypes = array (
1, //IMAGETYPE_GIF
2, //IMAGETYPE_JPEG
3 //IMAGETYPE_PNG
);
$imageinfo = getimagesize($_FILES[$file]['tmp_name']);
$width = @$imageinfo[0];
$height = @$imageinfo[1];
$type = @$imageinfo[2];
$mime = @$imageinfo['mime'];
if (!in_array($type, $imageTypes)) {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
}
if ($width < $minWidth || $height < $minWidth) {
@unlink($_FILES[$file]['tmp_name']);
echo 'errorsize';
die();
}
if($mime != 'image/gif' && $mime != 'image/jpeg' && $mime != 'image/png') {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
}
switch($type) {
case 1:
$ext = '.gif';
//check if is animated gif
$frames = 0;
if(($fh = @fopen($_FILES[$file]['tmp_name'], 'rb')) && $error != 'error') {
while(!feof($fh) && $frames < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$frames += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}
}
fclose($fh);
break;
case 2:
$ext = '.jpg';
break;
case 3:
$ext = '.png';
break;
}
$transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja');
$fname = $_FILES[$file]['name'];
$fname = str_replace(array_keys($transliterationTable), array_values($transliterationTable), $fname);
$filename = time() . str_shuffle('pcl48');
$original_filename = preg_replace('/[^(\x20|\x61-\x7A)]*/', '', strtolower(str_ireplace($ext, '', $fname))); //preg_replace('/[^(\x48-\x7A)]*/' strips non-utf character. Ref: http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
$_FILES[$file]['name'] = strtolower(substr($original_filename, 0, 100)) . '-' . $filename . $ext;
$attach_id = media_handle_upload($file, $post_id);
if (is_wp_error($attach_id)) {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
} else {
if ($frames > 1) {
update_post_meta($attach_id, 'a_gif', 'yes');
}
}
}
}
update_post_meta($attach_id, 'pinc_unattached', 'yes');
$return = array();
$thumbnail = wp_get_attachment_image_src($attach_id, 'medium');
$return['thumbnail'] = $thumbnail[0];
$return['id'] = $attach_id;
do_action('pinc_after_upload_pin_computer', $attach_id);
echo json_encode($return);
} else if ($_POST['mode'] == 'web') {
$url = esc_url_raw($_POST['pin_upload_web']);
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$image = curl_exec($ch);
curl_close($ch);
} elseif (ini_get('allow_url_fopen')) {
$image = file_get_contents($url, false, $context);
}
if (!$image) {
echo 'error';
die();
}
$filename = time() . str_shuffle('pcl48');
$file_array['tmp_name'] = WP_CONTENT_DIR . "/" . $filename . '.tmp';
$filetmp = file_put_contents($file_array['tmp_name'], $image);
if (!$filetmp) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
$imageTypes = array (
1, //IMAGETYPE_GIF
2, //IMAGETYPE_JPEG
3 //IMAGETYPE_PNG
);
$imageinfo = getimagesize($file_array['tmp_name']);
$width = @$imageinfo[0];
$height = @$imageinfo[1];
$type = @$imageinfo[2];
$mime = @$imageinfo['mime'];
if (!in_array ($type, $imageTypes)) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
if ($width < $minWidth || $height < $minWidth) {
@unlink($file_array['tmp_name']);
echo 'errorsize';
die();
}
if($mime != 'image/gif' && $mime != 'image/jpeg' && $mime != 'image/png') {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
switch($type) {
case 1:
$ext = '.gif';
//check if is animated gif
$frame = 0;
if(($fh = @fopen($file_array['tmp_name'], 'rb')) && $error != 'error') {
while(!feof($fh) && $frames < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$frames += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}
}
fclose($fh);
break;
case 2:
$ext = '.jpg';
break;
case 3:
$ext = '.png';
break;
}
$original_filename = preg_replace('/[^(\x20|\x61-\x7A)]*/', '', strtolower(str_ireplace($ext, '', basename($url)))); //preg_replace('/[^(\x48-\x7A)]*/' strips non-utf character. Ref: http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
$file_array['name'] = strtolower(substr($original_filename, 0, 100)) . '-' . $filename . $ext;
$attach_id = media_handle_sideload($file_array, $post_id);
if (is_wp_error($attach_id)) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
} else {
if ($frames > 1) {
update_post_meta($attach_id, 'a_gif', 'yes');
}
}
update_post_meta($attach_id, 'pinc_unattached', 'yes');
$return = array();
$thumbnail = wp_get_attachment_image_src($attach_id, 'medium');
$return['thumbnail'] = $thumbnail[0];
$return['id'] = $attach_id;
do_action('pinc_after_upload_pin_web', $attach_id);
echo json_encode($return);
}
exit;
}
add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin_new');
but my this new function could not call.但我的这个新函数无法调用。 it still call old function
pinc_upload_pin();
它仍然调用旧函数
pinc_upload_pin();
please guide me where i am missing something请指导我在哪里我遗漏了什么
How about you hook you new function with higher priority than your old function and then remove the previous hook inside that function so that only your new function is called.您如何以比旧函数更高的优先级钩住新函数,然后删除该函数中的前一个钩子,以便只调用您的新函数。
Not tested but it might work by giving your hook the priority 9( any digit less than 10 because default priority level is 10 if not specified) as below:未经测试,但它可以通过为您的钩子提供优先级 9(任何小于 10 的数字,因为如果未指定默认优先级为 10)来工作,如下所示:
add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin_new',9); // High priority
add_action('wp_ajax_nopriv_pinc-upload-pin', 'pinc_upload_pin_new',9); // Use this line only if you need this hook for non-logged in users as well
function pinc_upload_pin_new(){
remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
check_ajax_referer('upload_pin', 'ajax-nonce');
do_action('pinc_before_upload_pin', $_POST);
$minWidth = 2;
$minHeight = 2;
$minWidth = apply_filters('pinc_minwidth', $minWidth);
$minHeight = apply_filters('pinc_minheight', $minHeight);
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
if ($_POST['mode'] == 'computer') {
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$imageTypes = array (
1, //IMAGETYPE_GIF
2, //IMAGETYPE_JPEG
3 //IMAGETYPE_PNG
);
$imageinfo = getimagesize($_FILES[$file]['tmp_name']);
$width = @$imageinfo[0];
$height = @$imageinfo[1];
$type = @$imageinfo[2];
$mime = @$imageinfo['mime'];
if (!in_array($type, $imageTypes)) {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
}
if ($width < $minWidth || $height < $minWidth) {
@unlink($_FILES[$file]['tmp_name']);
echo 'errorsize';
die();
}
if($mime != 'image/gif' && $mime != 'image/jpeg' && $mime != 'image/png') {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
}
switch($type) {
case 1:
$ext = '.gif';
//check if is animated gif
$frames = 0;
if(($fh = @fopen($_FILES[$file]['tmp_name'], 'rb')) && $error != 'error') {
while(!feof($fh) && $frames < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$frames += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}
}
fclose($fh);
break;
case 2:
$ext = '.jpg';
break;
case 3:
$ext = '.png';
break;
}
$transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja');
$fname = $_FILES[$file]['name'];
$fname = str_replace(array_keys($transliterationTable), array_values($transliterationTable), $fname);
$filename = time() . str_shuffle('pcl48');
$original_filename = preg_replace('/[^(\x20|\x61-\x7A)]*/', '', strtolower(str_ireplace($ext, '', $fname))); //preg_replace('/[^(\x48-\x7A)]*/' strips non-utf character. Ref: http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
$_FILES[$file]['name'] = strtolower(substr($original_filename, 0, 100)) . '-' . $filename . $ext;
$attach_id = media_handle_upload($file, $post_id);
if (is_wp_error($attach_id)) {
@unlink($_FILES[$file]['tmp_name']);
echo 'error';
die();
} else {
if ($frames > 1) {
update_post_meta($attach_id, 'a_gif', 'yes');
}
}
}
}
update_post_meta($attach_id, 'pinc_unattached', 'yes');
$return = array();
$thumbnail = wp_get_attachment_image_src($attach_id, 'medium');
$return['thumbnail'] = $thumbnail[0];
$return['id'] = $attach_id;
do_action('pinc_after_upload_pin_computer', $attach_id);
echo json_encode($return);
} else if ($_POST['mode'] == 'web') {
$url = esc_url_raw($_POST['pin_upload_web']);
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$image = curl_exec($ch);
curl_close($ch);
} elseif (ini_get('allow_url_fopen')) {
$image = file_get_contents($url, false, $context);
}
if (!$image) {
echo 'error';
die();
}
$filename = time() . str_shuffle('pcl48');
$file_array['tmp_name'] = WP_CONTENT_DIR . "/" . $filename . '.tmp';
$filetmp = file_put_contents($file_array['tmp_name'], $image);
if (!$filetmp) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
$imageTypes = array (
1, //IMAGETYPE_GIF
2, //IMAGETYPE_JPEG
3 //IMAGETYPE_PNG
);
$imageinfo = getimagesize($file_array['tmp_name']);
$width = @$imageinfo[0];
$height = @$imageinfo[1];
$type = @$imageinfo[2];
$mime = @$imageinfo['mime'];
if (!in_array ($type, $imageTypes)) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
if ($width < $minWidth || $height < $minWidth) {
@unlink($file_array['tmp_name']);
echo 'errorsize';
die();
}
if($mime != 'image/gif' && $mime != 'image/jpeg' && $mime != 'image/png') {
@unlink($file_array['tmp_name']);
echo 'error';
die();
}
switch($type) {
case 1:
$ext = '.gif';
//check if is animated gif
$frame = 0;
if(($fh = @fopen($file_array['tmp_name'], 'rb')) && $error != 'error') {
while(!feof($fh) && $frames < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$frames += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}
}
fclose($fh);
break;
case 2:
$ext = '.jpg';
break;
case 3:
$ext = '.png';
break;
}
$original_filename = preg_replace('/[^(\x20|\x61-\x7A)]*/', '', strtolower(str_ireplace($ext, '', basename($url)))); //preg_replace('/[^(\x48-\x7A)]*/' strips non-utf character. Ref: http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
$file_array['name'] = strtolower(substr($original_filename, 0, 100)) . '-' . $filename . $ext;
$attach_id = media_handle_sideload($file_array, $post_id);
if (is_wp_error($attach_id)) {
@unlink($file_array['tmp_name']);
echo 'error';
die();
} else {
if ($frames > 1) {
update_post_meta($attach_id, 'a_gif', 'yes');
}
}
update_post_meta($attach_id, 'pinc_unattached', 'yes');
$return = array();
$thumbnail = wp_get_attachment_image_src($attach_id, 'medium');
$return['thumbnail'] = $thumbnail[0];
$return['id'] = $attach_id;
do_action('pinc_after_upload_pin_web', $attach_id);
echo json_encode($return);
}
exit;
}
For anyone if the above answer does not fits you then here is an alterntive method to remove the action hook added by themes.对于任何人,如果上述答案不适合您,那么这里是一种删除主题添加的动作挂钩的替代方法。
function wpso682524_remove_action()
{
//Remove the unwanted previous hook added by your theme.
remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
//Now hook your new function to that hook.
add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin_new',9);
}
add_action('after_setup_theme', 'wpso682524_remove_action');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.