簡體   English   中英

功能覆蓋問題 wordpress

[英]function override issue wordpress

我的 WordPress function.php文件中有圖片上傳功能,但我的function.php文件是用 ioncube 編碼器編碼的,所以我無法編輯該功能。

但我有我的主題作者提供的該功能的完整代碼。 和作者告訴我覆蓋該功能它會起作用。

我的函數名稱是pinc_upload_pin()並且添加它像add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');

現在我需要覆蓋上面的函數。

我嘗試過創建一個單獨的文件名 function_new.php 並將其包含在header.php就像使用require_once 我已經使用remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');刪除了上述功能remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');

並添加我自己的函數並鈎住它。 下面是我的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');

但我的這個新函數無法調用。 它仍然調用舊函數pinc_upload_pin();

請指導我在哪里我遺漏了什么

您如何以比舊函數更高的優先級鈎住新函數,然后刪除該函數中的前一個鈎子,以便只調用您的新函數。

未經測試,但它可以通過為您的鈎子提供優先級 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;
}

對於任何人,如果上述答案不適合您,那么這里是一種刪除主題添加的動作掛鈎的替代方法。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM