Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.2 kB
1
Indexable
Never

// Enqueue the script and add inline JavaScript
function b2bking_offer_enqueue_inline_script() {

    if (is_account_page()) { 
        // Query all b2bking_offer posts
        $offers = new WP_Query(array(
            'post_type' => 'b2bking_offer',
            'posts_per_page' => -1,
        ));

        $offers_data = array();
        if ($offers->have_posts()) {
            while ($offers->have_posts()) {
                $offers->the_post();
                $offers_data[get_the_ID()] = get_post_meta(get_the_ID(), '_b2bking_offer_url', true);
            }
            wp_reset_postdata();
        }

        ?>
        <Script>
        	jQuery(document).ready(function(){
        		setTimeout(function(){
	        		window.b2bking_offers = <?php echo json_encode($offers_data); ?>;

	        		// Save the existing handlers for the 'click' event on the body for .b2bking_offer_download
				    var existingHandlers = [];
				    if (jQuery._data(jQuery('body')[0], 'events') && jQuery._data(jQuery('body')[0], 'events').click) {
				        jQuery._data(jQuery('body')[0], 'events').click.forEach(function(handler) {
				            if (handler.selector === '.b2bking_offer_download') {
				                existingHandlers.push(handler);
				            }
				        });
				    }

				    // Remove all click handlers for .b2bking_offer_download on the body
				    jQuery('body').off('click', '.b2bking_offer_download');

				    // Bind your click handler first
				    jQuery('body').on('click', '.b2bking_offer_download', function() {
				        let offerid = jQuery(this).val();
				        let logovar = window.b2bking_offers[offerid];

				        b2bking_display_settings.offers_logo = logovar;
				        jQuery('#b2bking_img_logo').attr('src', logovar);
				    });

				    // Reattach the previously removed handlers
			        existingHandlers.forEach(function(handler) {
			            jQuery('body').on('click', handler.selector, function(event) {
			                setTimeout(function() {
			                    handler.handler.call(event.target, event);
			                }, 200);
			            });
			        });
        		}, 200);
        		
        	});
        </Script>
        <?php
    }
}
add_action('wp_head', 'b2bking_offer_enqueue_inline_script');


// Add the metabox
function b2bking_offer_add_custom_metabox() {
    add_meta_box(
        'b2bking_offer_metabox', // Metabox ID
        'Offer Logo URL',             // Metabox Title
        'b2bking_offer_metabox_callback', // Callback function
        'b2bking_offer',         // Post type
        'side',                  // Context (side, normal, advanced)
        'default'                // Priority
    );
}
add_action('add_meta_boxes', 'b2bking_offer_add_custom_metabox');

// Metabox callback function
function b2bking_offer_metabox_callback($post) {
    // Add a nonce field for security
    wp_nonce_field('b2bking_offer_save_metabox', 'b2bking_offer_metabox_nonce');

    // Retrieve the existing value from the database
    $value = get_post_meta($post->ID, '_b2bking_offer_url', true);

    // Display the metabox form field
    echo '<label for="b2bking_offer_url">URL: </label>';
    echo '<input type="url" id="b2bking_offer_url" name="b2bking_offer_url" value="' . esc_attr($value) . '" size="25" />';
}

// Save the metabox data
function b2bking_offer_save_metabox($post_id) {
    // Check if our nonce is set
    if (!isset($_POST['b2bking_offer_metabox_nonce'])) {
        return $post_id;
    }
    $nonce = $_POST['b2bking_offer_metabox_nonce'];

    // Verify that the nonce is valid
    if (!wp_verify_nonce($nonce, 'b2bking_offer_save_metabox')) {
        return $post_id;
    }

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    if ('b2bking_offer' == $_POST['post_type']) {
        if (!current_user_can('edit_post', $post_id)) {
            return $post_id;
        }
    } else {
        return $post_id;
    }

    // Sanitize the user input
    $new_value = sanitize_text_field($_POST['b2bking_offer_url']);

    // Update the meta field in the database
	update_post_meta($post_id, '_b2bking_offer_url', $new_value);
}
add_action('save_post', 'b2bking_offer_save_metabox');
Leave a Comment