php 修改上传到WP插件文件夹的插件

x8goxv8g  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(88)

我无法从插件作者那里获得任何信息或帮助,但它目前所做的是在Woocommerce结帐页面上提供签名框。提交订单后,它会将签名保存并上传到/wp-content/uploads/文件夹,并将其显示在Woocommerce订单页面上,并将其添加到wp-admin中的媒体库。
我想做的是保存签名到订单页面,但添加到媒体库,因为我们没有必要为媒体库内的签名1000的。
此外,如果我可以改变它保存到一个子文件夹(即图像的位置。/wp-content/uploads/signatures/),这将是非常有帮助的。

代码在下面。如有任何帮助,我们将不胜感激!

add_action('woocommerce_after_order_notes', 'DSCFW_add_signature_field', 50);
function DSCFW_add_signature_field($post)
{

?>
    <div class="validate-required validate-signature" style="position: relative;">
        <p><?php echo esc_html('Please sign below confirming that you understand you are purchasing a subcription which will renew automatically.', 'digital-signature-checkout-for-woocommerce'); ?> <span class="bp-required-field-label"><abbr class="required" title="required">*</abbr></span></p>

        <canvas id="dscfw_sign" name="signaturefield" width="300" height="100" required></canvas>

        <input class="clearButton" type="button" value="X">
        <input id="signPadInput" type="hidden" required name="signpad" value="">
    </div>

<?php
}

add_action('woocommerce_checkout_update_order_meta', 'DSCFW_product_get_data');
function DSCFW_product_get_data($post)
{     
    if (is_string($_REQUEST['signpad']) && strrpos($_REQUEST['signpad'], "data:image/png;base64", -strlen($_REQUEST['signpad'])) !== FALSE) {

        $signature_imgid = DSCFW_save_image($_REQUEST['signpad'], 'fff');
    }

    if (isset($_REQUEST['signpad'])) {
        update_post_meta($post, 'signpad', $signature_imgid);
    }
}

function DSCFW_save_image($base64_img, $title)
{

    // Upload dir.
    $upload_dir  = wp_upload_dir();
    $upload_path = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir['path']) . DIRECTORY_SEPARATOR;

    $img             = str_replace('data:image/png;base64,', '', $base64_img);
    $img             = str_replace(' ', '+', $img);
    $decoded         = base64_decode($img);
    $filename        = $title . '.jpeg';
    $file_type       = 'image/jpeg';
    $hashed_filename = md5($filename . microtime()) . '_' . $filename;

    // Save the image in the uploads directory.
    $upload_file = file_put_contents($upload_path . $hashed_filename, $decoded);

    $attachment = array(
        'post_mime_type' => $file_type,
        'post_title'     => preg_replace('/\.[^.]+$/', '', basename($hashed_filename)),
        'post_content'   => '',
        'post_status'    => 'inherit',
        'guid'           => $upload_dir['url'] . '/' . basename($hashed_filename)
    );

    $attach_id = wp_insert_attachment($attachment, $upload_dir['path'] . '/' . $hashed_filename);
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    return $attach_id;
}

add_action('woocommerce_admin_order_data_after_billing_address', 'DSCFW_checkout_field_display_admin_order_meta', 10, 1);
function DSCFW_checkout_field_display_admin_order_meta($order)
{
    $post_attach_file = $order->get_meta('signpad');
    $signimage = wp_get_attachment_url($post_attach_file, true);
?>  
    <p><strong style="display:block;">User Signature:</strong>
    <a href="<?Php echo esc_url($signimage); ?>" target="_blank"><img id="signature" src="<?Php echo esc_url($signimage); ?>" width="100"></a></p>
    <script>
        jQuery(document).ready(function($) {
            function resize() {
                var signatureBlock = document.querySelector('#signature');
                var checkoutContainer = document.querySelector('.order_data_column');
                checkoutWidth = $(checkoutContainer).width();
                signatureBlock.width = checkoutWidth;
            }
            $(window).on("resize", resize);
            $(window).on("load", resize);

        });
    </script>
<?php
}

我尝试了下面的代码,只是将/signatures/ sub文件夹添加到$upload_path中,这确实会移动图像上传的位置,但是在Woocommerce订单页面中,图像不会显示,因为它说我没有正确的权限查看它。

function DSCFW_save_image($base64_img, $title)
{

    // Upload dir.
    $upload_dir  = wp_upload_dir();
    $upload_path = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir['path']). '/signatures/' . DIRECTORY_SEPARATOR;

    $img             = str_replace('data:image/png;base64,', '', $base64_img);
    $img             = str_replace(' ', '+', $img);
    $decoded         = base64_decode($img);
    $filename        = $title . '.jpeg';
    $file_type       = 'image/jpeg';
    $hashed_filename = md5($filename . microtime()) . '_' . $filename;

    // Save the image in the uploads directory.
    $upload_file = file_put_contents($upload_path . $hashed_filename, $decoded);

    $attachment = array(
        'post_mime_type' => $file_type, 
        'post_title'     => preg_replace('/\.[^.]+$/', '', basename($hashed_filename)),
        'post_content'   => '',
        'post_status'    => 'inherit',
        'guid'           => $upload_dir['url'] . '/signatures/' . basename($hashed_filename)
    );

    $attach_id = wp_insert_attachment($attachment, $upload_dir['path'] . '/signatures/' . $hashed_filename);
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    return $attach_id;
}
r7xajy2e

r7xajy2e1#

我还没有测试过这个,但它可能会工作,保存文件url到订单Meta,而不是一个附件id

function DSCFW_save_image($base64_img, $title)
{

    // Upload dir.
    $upload_dir  = wp_upload_dir();
    $upload_path = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir['path']) . DIRECTORY_SEPARATOR . 'signatures' . DITECTORY_SEPARATOR;

    $img             = str_replace('data:image/png;base64,', '', $base64_img);
    $img             = str_replace(' ', '+', $img);
    $decoded         = base64_decode($img);
    $filename        = $title . '.jpeg';
    $file_type       = 'image/jpeg';
    $hashed_filename = md5($filename . microtime()) . '_' . $filename;

    // Save the image in the uploads directory.
    $upload_file = file_put_contents($upload_path . $hashed_filename, $decoded);

    
    return $upload_dir['url'] . '/signatures/' . $hashed_filename;
}

add_action('woocommerce_admin_order_data_after_billing_address', 'DSCFW_checkout_field_display_admin_order_meta', 10, 1);
function DSCFW_checkout_field_display_admin_order_meta($order)
{
    $signimage = $order->get_meta('signpad');
?>  
    <p><strong style="display:block;">User Signature:</strong>
    <a href="<?Php echo esc_url($signimage); ?>" target="_blank"><img id="signature" src="<?Php echo esc_url($signimage); ?>" width="100"></a></p>
    
...rest of code

相关问题