Skip to content

Commit 1ba6245

Browse files
committed
append comments for each php function / minor code clean up
1 parent b4ad4df commit 1ba6245

File tree

1 file changed

+107
-42
lines changed

1 file changed

+107
-42
lines changed

netlify-webhook-deploy.php

Lines changed: 107 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Plugin Name: Webhook Netlify Deploy
88
Plugin URI: http://github.com/lukethacoder/wp-webhook-netlify-deploy
99
Description: Adds a Build Website button that sends a webhook request to build a netlify hosted website when clicked
10-
Version: 1.0.0
10+
Version: 1.1.0
1111
Author: Luke Secomb
1212
Author URI: https://lukesecomb.digital
1313
License: GPLv3 or later
@@ -33,6 +33,11 @@
3333

3434
class deployWebhook {
3535

36+
/**
37+
* Constructor
38+
*
39+
* @since 1.0.0
40+
**/
3641
public function __construct() {
3742
// Hook into the admin menu
3843
add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) );
@@ -44,6 +49,11 @@ public function __construct() {
4449
add_action( 'admin_bar_menu', array( $this, 'add_to_admin_bar' ), 90 );
4550
}
4651

52+
/**
53+
* Main Plugin Page markup
54+
*
55+
* @since 1.0.0
56+
**/
4757
public function plugin_settings_page_content() {?>
4858
<div class="wrap">
4959
<h2>Webhook Netlify Deploy</h2>
@@ -60,21 +70,18 @@ public function plugin_settings_page_content() {?>
6070
<button id="status_button" class="button button-primary" name="submit" type="submit" style="margin: 0 0 16px;">Get Deploys Status</button>
6171

6272
<div style="margin: 0 0 16px;">
63-
<a id="build_img_link" href="https://app.netlify.com/sites/dvlp-haus/deploys">
73+
<a id="build_img_link" href="">
6474
<img id="build_img" src=""/>
6575
</a>
6676
</div>
6777
<div>
6878
<!-- <p id="deploy_status"></p> -->
6979
<p id="deploy_id"></p>
70-
<!-- <p id="deploy_current_time"></p> -->
7180
<div style="display: flex;"><p id="deploy_finish_time"></p><p id="deploy_loading"></p></div>
7281
<p id="deploy_ssl_url"></p>
7382
</div>
7483

75-
<div id="deploy_preview">
76-
77-
</div>
84+
<div id="deploy_preview"></div>
7885

7986
<hr>
8087

@@ -84,6 +91,11 @@ public function plugin_settings_page_content() {?>
8491
</div> <?php
8592
}
8693

94+
/**
95+
* Developer Settings (subpage) markup
96+
*
97+
* @since 1.0.0
98+
**/
8799
public function plugin_settings_subpage_content() {?>
88100
<div class="wrap">
89101
<h1>Developer Settings</h1>
@@ -111,9 +123,21 @@ public function plugin_settings_subpage_content() {?>
111123
</div> <?php
112124
}
113125

126+
127+
/**
128+
* The Mighty JavaScript
129+
*
130+
* @since 1.0.0
131+
**/
114132
public function run_the_mighty_javascript() {
133+
// TODO: split up javascript to allow to be dynamically imported as needed
134+
// $screen = get_current_screen();
135+
// if ( $screen && $screen->parent_base != 'deploy_webhook_fields' && $screen->parent_base != 'deploy_webhook_fields_sub' ) {
136+
// return;
137+
// }
115138
?>
116139
<script type="text/javascript" >
140+
console.log('run_the_mighty_javascript');
117141
jQuery(document).ready(function($) {
118142
var _this = this;
119143
$( ".webhook-deploy_page_deploy_webhook_fields_sub td > input" ).css( "width", "100%");
@@ -152,7 +176,7 @@ function getAllPreviousBuilds() {
152176
deploy_preview_url = data.deploy_url
153177
}
154178
$('#previous_deploys_container').append(
155-
'<li style="margin: 0 auto 16px"><hr><h3>No: ' + buildNo + ' - ' + item.name + '</h3><h4>Created at: ' + item.created_at + '</h4><h4>' + item.title + '</h4><p>Id: ' + item.id + '</p><p>Deploy Time: ' + item.deploy_time + '</p><p>Branch: ' + item.branch + '</p><a href="' + item.deploy_preview_url + '">Preview Build</a></li>'
179+
'<li style="margin: 0 auto 16px"><hr><h3>No: ' + buildNo + ' - ' + item.name + '</h3><h4>Created at: ' + new Date(item.created_at.toString()).toLocaleString() + '</h4><h4>' + item.title + '</h4><p>Id: ' + item.id + '</p><p>Deploy Time: ' + item.deploy_time + '</p><p>Branch: ' + item.branch + '</p><a href="' + item.deploy_preview_url + '">Preview Build</a></li>'
156180
);
157181
buildNo++;
158182
})
@@ -163,7 +187,6 @@ function getAllPreviousBuilds() {
163187
}
164188

165189
function runSecondFunc() {
166-
167190
$.ajax({
168191
type: "GET",
169192
url: req_url
@@ -184,15 +207,11 @@ function appendStatusData(data) {
184207
var yo = new Date(data.created_at);
185208
var created = yo.toLocaleString();
186209
var current_state = data.state;
210+
187211
if (data.state === 'ready') {
188212
current_state = "Success"
189213
}
190214

191-
// $( "#deploy_id" ).html( "ID: " + data.id + "" );
192-
// $( "#build_img_link" ).attr("href", data.admin_url);
193-
// $( "#build_img" ).attr("src", data.admin_url);
194-
// $( "#deploy_status" ).html( "Status: " + current_state );
195-
// $( "#deploy_current_time" ).html( "Current Date/Time: " + p );
196215
if (data.state !== 'ready') {
197216
$( "#deploy_finish_time" ).html( "Building Site" );
198217
$( "#build_img" ).attr("src", `https://api.netlify.com/api/v1/badges/${ netlify_site_id }/deploy-status`);
@@ -208,11 +227,13 @@ function appendStatusData(data) {
208227
500);
209228
} else {
210229
var deploy_preview_url = '';
230+
211231
if (data.deploy_ssl_url) {
212232
deploy_preview_url = data.deploy_ssl_url
213233
} else {
214234
deploy_preview_url = data.deploy_url
215235
}
236+
216237
$( "#deploy_id" ).html( "ID: " + data.id + "" );
217238
$( "#deploy_finish_time" ).html( "Build Completed: " + created );
218239
$( "#build_img" ).attr("src", `https://api.netlify.com/api/v1/badges/${ netlify_site_id }/deploy-status`);
@@ -307,77 +328,110 @@ function netlifyDeploy() {
307328
</script> <?php
308329
}
309330

331+
/**
332+
* Plugin Menu Items Setup
333+
*
334+
* @since 1.0.0
335+
**/
310336
public function create_plugin_settings_page() {
311-
// Add the menu item and page
312-
$page_title = 'Deploy to Netlify';
313-
$menu_title = 'Webhook Deploy';
314-
$capability = 'manage_options';
315-
$slug = 'deploy_webhook_fields';
316-
$callback = array( $this, 'plugin_settings_page_content' );
317-
$icon = 'dashicons-admin-plugins';
318-
$position = 100;
319-
320-
$sub_page_title = 'Developer Settings';
321-
$sub_menu_title = 'Developer Settings';
322-
$sub_capability = 'manage_options';
323-
$sub_slug = 'deploy_webhook_fields_sub';
324-
$sub_callback = array( $this, 'plugin_settings_subpage_content' );
325-
$sub_icon = 'dashicons-admin-plugins';
326-
$sub_position = 100;
327-
328-
329-
add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
330-
add_submenu_page( $slug, $sub_page_title, $sub_menu_title, $sub_capability, $sub_slug, $sub_callback, $sub_icon, $sub_position );
337+
$run_deploys = apply_filters( 'netlify_deploy_capability', 'manage_options' );
338+
$adjust_settings = apply_filters( 'netlify_adjust_settings_capability', 'manage_options' );
339+
340+
if ( current_user_can( $run_deploys ) ) {
341+
$page_title = 'Deploy to Netlify';
342+
$menu_title = 'Webhook Deploy';
343+
$capability = 'manage_options';
344+
$slug = 'deploy_webhook_fields';
345+
$callback = array( $this, 'plugin_settings_page_content' );
346+
$icon = 'dashicons-admin-plugins';
347+
$position = 100;
348+
349+
add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
350+
}
351+
if ( current_user_can( $adjust_settings ) ) {
352+
$sub_page_title = 'Developer Settings';
353+
$sub_menu_title = 'Developer Settings';
354+
$sub_capability = 'manage_options';
355+
$sub_slug = 'deploy_webhook_fields_sub';
356+
$sub_callback = array( $this, 'plugin_settings_subpage_content' );
357+
$sub_icon = 'dashicons-admin-plugins';
358+
$sub_position = 100;
359+
360+
add_submenu_page( $slug, $sub_page_title, $sub_menu_title, $sub_capability, $sub_slug, $sub_callback, $sub_icon, $sub_position );
361+
362+
}
363+
364+
331365
}
332366

367+
/**
368+
* Notify Admin on Successful Update
369+
*
370+
* @since 1.0.0
371+
**/
333372
public function admin_notice() { ?>
334373
<div class="notice notice-success is-dismissible">
335374
<p>Your settings have been updated!</p>
336375
</div><?php
337376
}
338377

378+
/**
379+
* Setup Sections
380+
*
381+
* @since 1.0.0
382+
**/
339383
public function setup_sections() {
340-
add_settings_section( 'our_first_section', 'Webhook Settings', array( $this, 'section_callback' ), 'deploy_webhook_fields' );
384+
add_settings_section( 'main_section', 'Webhook Settings', array( $this, 'section_callback' ), 'deploy_webhook_fields' );
341385
}
342-
386+
387+
/**
388+
* Check it wont break on build and deploy
389+
*
390+
* @since 1.0.0
391+
**/
343392
public function section_callback( $arguments ) {
344393
switch( $arguments['id'] ){
345-
case 'our_first_section':
394+
case 'main_section':
346395
echo 'The build and deploy status will not work without these fields entered corrently';
347396
break;
348397
}
349398
}
350-
399+
400+
/**
401+
* Fields used for developer input data
402+
*
403+
* @since 1.0.0
404+
**/
351405
public function setup_fields() {
352406
$fields = array(
353407
array(
354408
'uid' => 'webhook_address',
355409
'label' => 'Webhook Build URL',
356-
'section' => 'our_first_section',
410+
'section' => 'main_section',
357411
'type' => 'text',
358412
'placeholder' => 'https://',
359413
'default' => '',
360414
),
361415
array(
362416
'uid' => 'netlify_site_id',
363417
'label' => 'Netlify site_id',
364-
'section' => 'our_first_section',
418+
'section' => 'main_section',
365419
'type' => 'text',
366420
'placeholder' => 'e.g. 5b8e927e-82e1-4786-4770-a9a8321yes43',
367421
'default' => '',
368422
),
369423
array(
370424
'uid' => 'netlify_api_key',
371425
'label' => 'Netlify API Key',
372-
'section' => 'our_first_section',
426+
'section' => 'main_section',
373427
'type' => 'text',
374428
'placeholder' => 'GET O-AUTH TOKEN',
375429
'default' => '',
376430
),
377431
array(
378432
'uid' => 'netlify_user_agent',
379433
'label' => 'User-Agent Site Value',
380-
'section' => 'our_first_section',
434+
'section' => 'main_section',
381435
'type' => 'text',
382436
'placeholder' => 'Website Name (and-website-url.netlify.com)',
383437
'default' => '',
@@ -389,11 +443,17 @@ public function setup_fields() {
389443
}
390444
}
391445

446+
/**
447+
* Field callback for handling multiple field types
448+
*
449+
* @since 1.0.0
450+
* @param $arguments
451+
**/
392452
public function field_callback( $arguments ) {
393453

394454
$value = get_option( $arguments['uid'] );
395455

396-
if( ! $value ) {
456+
if ( !$value ) {
397457
$value = $arguments['default'];
398458
}
399459

@@ -435,6 +495,11 @@ public function field_callback( $arguments ) {
435495
}
436496
}
437497

498+
/**
499+
* Add Deploy Button and Deployment Status to admin bar
500+
*
501+
* @since 1.1.0
502+
**/
438503
public function add_to_admin_bar( $admin_bar ) {
439504

440505
$see_deploy_status = apply_filters( 'netlify_status_capability', 'manage_options' );

0 commit comments

Comments
 (0)