Skip to content

Commit 0000641

Browse files
committed
Add Netlify status badge and deploy button the admin bar
1 parent 35a337d commit 0000641

File tree

1 file changed

+97
-17
lines changed

1 file changed

+97
-17
lines changed

netlify-webhook-deploy.php

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Text Domain: webhook-netlify-deploy
1515
*/
1616

17-
/*
17+
/*
1818
This program is free software: you can redistribute it and/or modify
1919
it under the terms of the GNU General Public License as published by
2020
the Free Software Foundation, either version 3 of the License, or
@@ -41,6 +41,7 @@ public function __construct() {
4141
add_action( 'admin_init', array( $this, 'setup_sections' ) );
4242
add_action( 'admin_init', array( $this, 'setup_fields' ) );
4343
add_action( 'admin_footer', array( $this, 'run_the_mighty_javascript' ) );
44+
add_action( 'admin_bar_menu', array( $this, 'add_to_admin_bar' ), 90 );
4445
}
4546

4647
public function plugin_settings_page_content() {?>
@@ -52,12 +53,12 @@ public function plugin_settings_page_content() {?>
5253
<button id="build_button" class="button button-primary" name="submit" type="submit">Build Site</button><br>
5354
<p id="build_status" style="font-size: 12px; margin: 0;"></p>
5455
<p style="font-size: 12px">*Do not abuse the Build Site button*</p><br>
55-
56+
5657
<hr>
5758

5859
<h3>Deploy Status</h3>
5960
<button id="status_button" class="button button-primary" name="submit" type="submit" style="margin: 0 0 16px;">Get Deploys Status</button>
60-
61+
6162
<div style="margin: 0 0 16px;">
6263
<a id="build_img_link" href="https://app.netlify.com/sites/dvlp-haus/deploys">
6364
<img id="build_img" src=""/>
@@ -72,7 +73,7 @@ public function plugin_settings_page_content() {?>
7273
</div>
7374

7475
<div id="deploy_preview">
75-
76+
7677
</div>
7778

7879
<hr>
@@ -116,7 +117,7 @@ public function run_the_mighty_javascript() {
116117
jQuery(document).ready(function($) {
117118
var _this = this;
118119
$( "td > input" ).css( "width", "100%");
119-
120+
120121
var webhook_url = '<?php echo(get_option('webhook_address')) ?>';
121122
var netlify_user_agent = '<?php echo(get_option('netlify_user_agent')) ?>';
122123
var netlify_api_key = '<?php echo(get_option('netlify_api_key'))?>'
@@ -218,8 +219,19 @@ function appendStatusData(data) {
218219
$( "#deploy_ssl_url" ).html( "Deploy URL: <a href='" + deploy_preview_url + "'>" + data.deploy_ssl_url + "</a>");
219220
$( "#deploy_preview" ).html( `<iframe style="width: 100%; min-height: 540px" id="frameLeft" src="${deploy_preview_url}"/>`)
220221
}
221-
222-
222+
223+
224+
}
225+
226+
function netlifyDeploy() {
227+
return $.ajax({
228+
type: "POST",
229+
url: webhook_url,
230+
dataType: "json",
231+
header: {
232+
"User-Agent": netlify_user_agent
233+
}
234+
});
223235
}
224236

225237
$("#status_button").on("click", function(e) {
@@ -243,14 +255,8 @@ function appendStatusData(data) {
243255
$('#deploy_preview').html('');
244256

245257
e.preventDefault();
246-
$.ajax({
247-
type: "POST",
248-
url: webhook_url,
249-
dataType: "json",
250-
header: {
251-
"User-Agent": netlify_user_agent
252-
}
253-
}).done(function() {
258+
259+
netlifyDeploy().done(function() {
254260
console.log("success")
255261
getDeployData();
256262
$( "#build_status" ).html('Deploy building');
@@ -260,6 +266,43 @@ function appendStatusData(data) {
260266
$( "#build_status" ).html('There seems to be an error with the build', this);
261267
})
262268
});
269+
270+
$(document).on('click', '#wp-admin-bar-netlify-deploy-button', function(e) {
271+
e.preventDefault();
272+
273+
var $button = $(this),
274+
$buttonContent = $button.find('.ab-item:first');
275+
276+
if ($button.hasClass('deploying') || $button.hasClass('running')) {
277+
return false;
278+
}
279+
280+
$button.addClass('running').css('opacity', '0.5');
281+
282+
netlifyDeploy().done(function() {
283+
var $badge = $('#admin-bar-netlify-deploy-status-badge');
284+
285+
$button.removeClass('running');
286+
$button.addClass('deploying');
287+
288+
$buttonContent.find('.ab-label').text('Deploying…');
289+
290+
if ($badge.length) {
291+
if (!$badge.data('original-src')) {
292+
$badge.data('original-src', $badge.attr('src'));
293+
}
294+
295+
$badge.attr('src', $badge.data('original-src') + '?updated=' + Date.now());
296+
}
297+
})
298+
.fail(function() {
299+
$button.removeClass('running').css('opacity', '1');
300+
$buttonContent.find('.dashicons-hammer')
301+
.removeClass('dashicons-hammer').addClass('dashicons-warning');
302+
303+
console.error("error res => ", this)
304+
})
305+
});
263306
});
264307
</script> <?php
265308
}
@@ -281,12 +324,12 @@ public function create_plugin_settings_page() {
281324
$sub_callback = array( $this, 'plugin_settings_subpage_content' );
282325
$sub_icon = 'dashicons-admin-plugins';
283326
$sub_position = 100;
284-
327+
285328

286329
add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
287330
add_submenu_page( $slug, $sub_page_title, $sub_menu_title, $sub_capability, $sub_slug, $sub_callback, $sub_icon, $sub_position );
288331
}
289-
332+
290333
public function admin_notice() { ?>
291334
<div class="notice notice-success is-dismissible">
292335
<p>Your settings have been updated!</p>
@@ -392,6 +435,43 @@ public function field_callback( $arguments ) {
392435
}
393436
}
394437

438+
public function add_to_admin_bar( $admin_bar ) {
439+
440+
$see_deploy_status = apply_filters( 'netlify_status_capability', 'manage_options' );
441+
$run_deploys = apply_filters( 'netlify_deploy_capability', 'manage_options' );
442+
443+
if ( current_user_can( $see_deploy_status ) ) {
444+
$netlify_site_id = get_option( 'netlify_site_id' );
445+
446+
if ( $netlify_site_id ) {
447+
$badge = array(
448+
'id' => 'netlify-deploy-status-badge',
449+
'parent' => 'top-secondary',
450+
'title' => sprintf( '<div style="display: flex; height: 100%%; align-items: center;">
451+
<img id="admin-bar-netlify-deploy-status-badge" src="https://api.netlify.com/api/v1/badges/%s/deploy-status" alt="Netlify deply status" style="width: auto; height: 16px;" />
452+
</div>', $netlify_site_id )
453+
);
454+
455+
$admin_bar->add_node( $badge );
456+
}
457+
}
458+
459+
if ( current_user_can( $run_deploys ) ) {
460+
$webhook_address = get_option( 'webhook_address' );
461+
462+
if ( $webhook_address ) {
463+
$button = array(
464+
'id' => 'netlify-deploy-button',
465+
'parent' => 'top-secondary',
466+
'title' => '<span class="ab-icon dashicons
467+
dashicons-hammer"></span> <span class="ab-label">Deploy site</span>'
468+
);
469+
470+
$admin_bar->add_node( $button );
471+
}
472+
}
473+
}
474+
395475
}
396476

397477
new deployWebhook;

0 commit comments

Comments
 (0)