Skip to content

Commit 25480df

Browse files
committed
init
1 parent 7c817f3 commit 25480df

10 files changed

+765
-2
lines changed

Diff for: .gitignore

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Node template
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
yarn-debug.log*
8+
yarn-error.log*
9+
10+
# Runtime data
11+
pids
12+
*.pid
13+
*.seed
14+
*.pid.lock
15+
16+
# Directory for instrumented libs generated by jscoverage/JSCover
17+
lib-cov
18+
19+
# Coverage directory used by tools like istanbul
20+
coverage
21+
22+
# nyc test coverage
23+
.nyc_output
24+
25+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
26+
.grunt
27+
28+
# Bower dependency directory (https://bower.io/)
29+
bower_components
30+
31+
# node-waf configuration
32+
.lock-wscript
33+
34+
# Compiled binary addons (https://nodejs.org/api/addons.html)
35+
build/Release
36+
37+
# Dependency directories
38+
node_modules/
39+
jspm_packages/
40+
41+
# TypeScript v1 declaration files
42+
typings/
43+
44+
# Optional npm cache directory
45+
.npm
46+
47+
# Optional eslint cache
48+
.eslintcache
49+
50+
# Optional REPL history
51+
.node_repl_history
52+
53+
# Output of 'npm pack'
54+
*.tgz
55+
56+
# Yarn Integrity file
57+
.yarn-integrity
58+
59+
# dotenv environment variables file
60+
.env
61+
62+
# parcel-bundler cache (https://parceljs.org/)
63+
.cache
64+
65+
# next.js build output
66+
.next
67+
68+
# nuxt.js build output
69+
.nuxt
70+
71+
# Nuxt generate
72+
dist
73+
74+
# vuepress build output
75+
.vuepress/dist
76+
77+
# Serverless directories
78+
.serverless
79+
80+
# IDE
81+
.idea
82+
83+
# Service worker
84+
sw.*
85+
86+
# Environment Vars
87+
.env
88+
89+
# OS Junk
90+
.DS_Store
91+
92+
# Storybook
93+
.nuxt-storybook
94+
storybook-static

Diff for: README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
# netlify-deploy
2-
Trigger a deploy to Netlify either automatically or with a button in the dashboard.
1+
# Netlify Deploy
2+
3+
Trigger a deploy to Netlify either automatically or with a button in the dashboard.
4+
5+
## Setup
6+
7+
1. Under `Settings > Netlify Deploy` you'll need to config the following:
8+
1. A "Build hook" URL found in the Netlify admin under `Site Settings > Build & Deploy > Build hooks` settings page. Recommended to name this hook "WordPress" but it's optional.
9+
1. The "Deploy status badge" URL found in the Netlify admin under `Site Settings > General > Deploy status badge` settings page. Note, you only need to enter the URL, not the full badge markdown code.
10+
11+
## Auto Deploy
12+
13+
The "Auto deploy on publish" setting will trigger a deploy whenever the following occurs:
14+
15+
1. A post/page/CPT is created or updated,
16+
1. Attachment updated
17+
1. Menu item changes
18+
1. Nested Pages plugin changes page order

Diff for: css/main.css

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Admin bar deploy button */
2+
#wpadminbar .nd-deploy-button .dashicons {
3+
font-family: 'dashicons';
4+
font-size: 20px;
5+
}
6+
.nd-deploy-button .msg {
7+
display: none;
8+
}
9+
.nd-status-success .msg-success,
10+
.nd-status-building .msg-building,
11+
.nd-status-failed .msg-failed {
12+
display: inline-block;
13+
}

Diff for: includes/admin-bar.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* Add the deply button to the admin bar at top of WP dashboard
5+
*/
6+
function nd_toolbar_item( $wp_admin_bar ) {
7+
$options = get_option('nd_settings');
8+
$status = nd_get_build_status();
9+
10+
$title = '
11+
<span class="msg msg-success"><span class="dashicons dashicons-admin-site"></span> New Deploy</span>
12+
<span class="msg msg-building"><span class="dashicons dashicons-backup"></span> Deploying</span>
13+
<span class="msg msg-failed"><span class="dashicons dashicons-dismiss"></span> Deploy failed</span>
14+
';
15+
16+
$args = [
17+
'id' => 'nd-deploy-button',
18+
'title' => $title,
19+
'href' => admin_url() . 'options-general.php?page=netlify-deploy&deploy=1',
20+
'meta' => [
21+
'class' => 'nd-deploy-button nd-status-' . $status
22+
]
23+
];
24+
25+
if( !empty($options['build_hook_url']) ) {
26+
$wp_admin_bar->add_node( $args );
27+
}
28+
}
29+
add_action( 'admin_bar_menu', 'nd_toolbar_item', 999 );

Diff for: includes/api.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/**
4+
* Checks if the user is logged into WordPress and can publish a post. Used as permission_callback of WP JSON.
5+
*
6+
* @return Boolean|WP_Error true if valid token, otherwise error
7+
*/
8+
function nd_check_user_can_publish($request)
9+
{
10+
// For this to work, generate a nonce using: `wp_create_nonce('wp_rest')` and set it as a HTTP header `X-WP-Nonce`.
11+
// Must also include valid WP logged in cookies in request headers too (credentials: include and CORS on)
12+
//return current_user_can('publish_posts');
13+
return true;
14+
}
15+
16+
/**
17+
* Post to the Netlify build hook
18+
*/
19+
function nd_deploy_post() {
20+
$options = get_option('nd_settings');
21+
22+
$url = $options['build_hook_url'] ?? '';
23+
24+
if($url) {
25+
// Attempt to POST to Netlify build
26+
$response = wp_remote_post($url);
27+
} else {
28+
// Return error if no URL set in admin
29+
return new WP_Error(
30+
'invalid_url',
31+
'You need to define a Build hook url on the settings page.',
32+
[
33+
'status' => 422,
34+
]
35+
);
36+
}
37+
38+
// If there was an error trying to hit Netlify
39+
if( is_wp_error($response) ) {
40+
return new WP_Error(
41+
'network_error',
42+
'Something went wrong with the network request.',
43+
[
44+
'status' => 400,
45+
]
46+
);
47+
}
48+
49+
// Build return data
50+
$timestamp = gmdate('Y-m-d H:i:s');
51+
$data = [
52+
'success' => true,
53+
'datetimestamp' => $timestamp,
54+
'nonce' => wp_create_nonce('wp_rest'),
55+
];
56+
57+
// Save last deploy time
58+
$options['last_deploy'] = $timestamp;
59+
update_option('nd_settings', $options);
60+
61+
return new WP_REST_Response($data, 200 );
62+
}
63+
64+
/*
65+
* Get the build status from Netlify
66+
*/
67+
function nd_build_get() {
68+
69+
// Build output
70+
$data = [
71+
'success' => true,
72+
'data' => [
73+
'status' => nd_get_build_status($content_header)
74+
],
75+
'nonce' => wp_create_nonce('wp_rest'),
76+
];
77+
78+
return new WP_REST_Response($data, 200);
79+
}

Diff for: includes/auto-deploy.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/*
3+
* Deounce requests to deploy
4+
*/
5+
function nd_debounce_deploy($object_id) {
6+
$options = get_option('nd_settings');
7+
8+
// Abort early
9+
if( empty($options['auto_deploy']) || empty($options['build_hook_url']) ) {
10+
return false;
11+
}
12+
13+
$deploy_time = get_transient('nd_deploy_time');
14+
15+
if($deploy_time) {
16+
// Try again in 60 seconds
17+
wp_clear_scheduled_hook('nd_run_auto_deploy')
18+
wp_schedule_single_event($deploy_time, 'nd_run_auto_deploy');
19+
20+
} else {
21+
nd_deploy_post();
22+
set_transient('nd_deploy_time', time() + 59, 60);
23+
}
24+
25+
}
26+
add_action('wp_update_nav_menu', 'nd_debounce_deploy');
27+
add_action('save_post', 'nd_debounce_deploy');
28+
add_action('attachment_updated', 'nd_debounce_deploy');
29+
add_action('nestedpages_post_order_updated', 'nd_debounce_deploy');
30+
31+
32+
33+
/*
34+
* Run an auto deploy
35+
*/
36+
function nd_run_auto_deploy() {
37+
nd_deploy_post();
38+
}
39+
add_action( 'nd_run_auto_deploy', 'nd_run_auto_deploy' );

0 commit comments

Comments
 (0)