Viewing 6 replies - 1 through 6 (of 6 total)
  • This isn’t entirely what you’re asking, but it’s a start. This will give you a link to the Cloudflare settings page in the Admin Bar, and also indicate whether dev_mode is on or not.

    I’ll probably keep messing with this function because I’m sure it’s possible to actually toggle dev mode using this method.

    //Add a link to Cloudflare Options on the Admin Bar (and indicate dev mode)
    	add_action('admin_bar_menu', 'gearside_admin_bar_cloudflare', 100);
    	function gearside_admin_bar_cloudflare($wp_admin_bar){
    
    		if ( get_dev_mode_status(get_option('cloudflare_api_key'), get_option('cloudflare_api_email'), get_option('cloudflare_zone_name')) == 'on' ){
    			$cloudflare_dev_mode_text = '(Dev Mode)';
    		}
    
    		$wp_admin_bar->add_node(array(
    			'id' => 'gearside-cloudflare',
    			'title' => 'Cloudflare ' . $cloudflare_dev_mode_text,
    			'href' => get_admin_url() . 'options-general.php?page=cloudflare'
    		));
    	}

    Alright, I got it working. You can paste this code into your functions.php file and it will toggle Cloudflare Dev Mode from the WordPress Admin Bar. This isn’t the prettiest code, but it should do the trick. If we were to push into the plugin, I’d want to optimize it a bit.

    This would be something that would be quite easy to implement into the Cloudflare plugin. In my personal implementation I added some status indicator icons for better UX during the AJAX call. If anyone on the plugin dev team would like a pull request for this, I’m happy to share my full code.

    //Add a link to Cloudflare Options on the Admin Bar and indicate dev_mode
    add_action('admin_bar_menu', 'gearside_admin_bar_cloudflare', 100);
    function gearside_admin_bar_cloudflare($wp_admin_bar){
    	$cloudflare_dev_mode_status = get_dev_mode_status(get_option('cloudflare_api_key'), get_option('cloudflare_api_email'), get_option('cloudflare_zone_name'));
    	$cloudflare_dev_mode_text = '';
    	$cloudflare_dev_mode_title = 'Enable Dev Mode';
    	$cloudflare_dev_mode_opposite = 'on';
    	if ( $cloudflare_dev_mode_status == 'on' ){
    		$cloudflare_dev_mode_text = '(Dev Mode)';
    		$cloudflare_dev_mode_title = 'Disable Dev Mode';
    		$cloudflare_dev_mode_opposite = 'off';
    	}
    
    	$wp_admin_bar->add_node(array(
    		'id' => 'gearside-cloudflare',
    		'title' => 'Cloudflare ' . cloudflare_dev_mode_text,
    		'href' => get_admin_url() . 'options-general.php?page=cloudflare',
    		'meta' => array('target' => '_blank')
    	));
    
    	$wp_admin_bar->add_node(array(
    		'parent' => 'gearside-cloudflare',
    		'id' => 'gearside-cloudflare-devmode',
    		'title' => $cloudflare_dev_mode_title,
    		'href' => get_admin_url() . 'options-general.php?page=cloudflare',
    		'meta' => array('onclick' => 'jQuery.ajax({type: "POST", url: bloginfo["admin_ajax"], data: {action: "gearside_cloudflare_dev_mode_toggle", data: "' . $cloudflare_dev_mode_opposite . '",}, success: function(response){window.location.reload();}, timeout: 60000}); return false;')
    	));
    }
    
    add_action('wp_ajax_gearside_cloudflare_dev_mode_toggle', 'gearside_cloudflare_dev_mode_toggle');
    add_action('wp_ajax_gearside_cloudflare_dev_mode_toggle', 'gearside_cloudflare_dev_mode_toggle');
    function gearside_cloudflare_dev_mode_toggle(){
    	if ( $_POST['data'] ){
    		$action = ( $_POST['data'] == 'off' )? 0 : 1;
    	}
    	set_dev_mode(get_option('cloudflare_api_key'), get_option('cloudflare_api_email'), get_option('cloudflare_zone_name'), $action);
    	exit;
    }
    Thread Starter Marty

    (@bozzmedia)

    Nice work, GreatBlakes! I’ll give it a go. Thank you.

    Cloudflare plugin devs, any thoughts on incorporating GreatBlakes’ work into the core plugin?

    Cheers!

    Nice to see some community involvement! 🙂

    I’ve gone ahead and created a feature request internally to get this implemented. I can’t guarantee when (or if) it will get done, but it exists, and will be tracked.

    That’s great to hear- I’ve create a Gist of the more advanced functionality I mentioned for reference. Like I said earlier- it can definitely be optimized quite a bit, but is working great on my server.

    https://gist.github.com/chrisblakley/75f797364d2c314897bf

    This is just the kind of thing I would love to see get put into the plugin. Been trying to get CloudFlare to support the plugin on Github so community improvements such as this can get added.

    https://wordpress.org/support/topic/support-plugin-on-github

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘It would be rad if this plugin had a Dev mode toggle via toolbar’ is closed to new replies.