Skip to content

Commit 6416e03

Browse files
committed
adds lots of features, docs, and more!
0 parents  commit 6416e03

File tree

132 files changed

+20363
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+20363
-0
lines changed

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# NETLIFY_WEBHOOK_DEVELOPMENT=https://api.netlify.com/build_hooks/########################
2+
# NETLIFY_WEBHOOK_STAGING=https://api.netlify.com/build_hooks/########################
3+
# NETLIFY_WEBHOOK_PRODUCTION=https://api.netlify.com/build_hooks/########################

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
!vendor

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Tiny Pixel Collective
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

NetlifyDeploy/Error.php

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace TinyPixel\NetlifyDeploy;
4+
5+
use \TinyPixel\NetlifyDeployPlugin as Plugin;
6+
use function \admin_url;
7+
8+
class Error
9+
{
10+
public function __construct($plugin)
11+
{
12+
$this->plugin = $plugin;
13+
}
14+
15+
/**
16+
* Deactivates plugin and displays errors
17+
* @param array $error
18+
*/
19+
public static function throw($error = null)
20+
{
21+
// this runs too late for deactivation
22+
// so we just manually include the function
23+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
24+
25+
// if plugin is activated, deactivate it
26+
is_plugin_active(Plugin::getInstance()->name) &&
27+
deactivate_plugins(Plugin::getInstance()->name);
28+
29+
// if error was set cast it to an object
30+
if (!is_null($error)) {
31+
$error = is_array($error) ? (object) $error : $error;
32+
}
33+
34+
// get formatted error variables
35+
$message = (object) [
36+
'title' => isset($error->title) ? $error->title : self::defaultTitle(),
37+
'subtitle' => isset($error->subtitle) ? $error->subtitle : self::defaultSubtitle(),
38+
'body' => isset($error->body) ? $error->body : self::defaultBody(),
39+
'footer' => isset($error->footer) ? $error->footer : self::defaultFooter(),
40+
'link' => isset($error->link) ? $error->link : self::defaultLink(),
41+
];
42+
43+
// prepare the liturgy
44+
$dirge = sprintf(
45+
"<h1>%s<br><small>%s</small></h1><p>%s</p><p>%s</p>",
46+
$message->title,
47+
$message->subtitle,
48+
$message->body,
49+
$message->footer
50+
);
51+
52+
// bear the bad news
53+
wp_die($dirge, $message->title, $message->subtitle);
54+
}
55+
56+
public static function defaultTitle()
57+
{
58+
return __(
59+
'Netlify Deploy Runtime Error',
60+
'netlify-deploy',
61+
);
62+
}
63+
64+
public static function defaultSubtitle()
65+
{
66+
return __(
67+
'There is a problem with the plugin',
68+
'netlify-deploy',
69+
);
70+
}
71+
72+
public static function defaultBody()
73+
{
74+
return __(
75+
'There was a problem with the plugin.',
76+
'netlify-deploy',
77+
);
78+
}
79+
80+
public static function defaultFooter()
81+
{
82+
return __(
83+
'The plugin has been deactivated.',
84+
'netlify-deploy'
85+
);
86+
}
87+
88+
public static function defaultLink()
89+
{
90+
return [
91+
'link_text' => __('Plugin Administration ⌫', 'netlify-deploy'),
92+
'link_url' => admin_url('plugins.php')
93+
];
94+
}
95+
}

NetlifyDeploy/Netlify.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace TinyPixel\NetlifyDeploy;
4+
5+
use \GuzzleHttp\Client;
6+
7+
class Netlify
8+
{
9+
/**
10+
* Construct
11+
*/
12+
public function __construct($plugin)
13+
{
14+
$this->plugin = $plugin;
15+
16+
return $this;
17+
}
18+
19+
/**
20+
* Initializes http client for webhook request
21+
* @return self $this
22+
*/
23+
public function setupClient()
24+
{
25+
// instantiate guzzle for POST req
26+
$this->client = new Client(['headers' => [
27+
'Content-Type' => 'application/json',
28+
]]);
29+
30+
return $this;
31+
}
32+
33+
/**
34+
* Return hook for a given environment
35+
*/
36+
public static function getNetlifyHook($environment)
37+
{
38+
return (object) [
39+
'key' => "NETLIFY_WEBHOOK_{$environment}",
40+
'value' => env("NETLIFY_WEBHOOK_{$environment}")
41+
];
42+
}
43+
44+
/**
45+
* Sets webhook URLs for POSTing
46+
* @return self $this
47+
*/
48+
public function setWebhooks()
49+
{
50+
$this->hooks = (object) [
51+
'development' => self::getNetlifyHook('DEVELOPMENT')->value,
52+
'staging' => self::getNetlifyHook('STAGING')->value,
53+
'production' => self::getNetlifyHook('PRODUCTION')->value,
54+
];
55+
56+
return $this;
57+
}
58+
59+
public function filterWebhooks()
60+
{
61+
has_filter('netlify_webhooks') &&
62+
apply_filters('netlify_webhooks', $this->hooks);
63+
64+
return $this;
65+
}
66+
67+
/**
68+
* Hooks into WordPress lifecycle
69+
*/
70+
public function addActions()
71+
{
72+
// mile high
73+
foreach ($this->postTypes as $type) {
74+
add_action("publish_{$type}", [
75+
$this, 'onPublish'
76+
], 10, 2);
77+
}
78+
79+
return $this;
80+
}
81+
82+
/**
83+
* Fields PostTypes that are set to trigger webhook
84+
* @param array postTypes
85+
* @return self $this
86+
*/
87+
public function usePostTypes()
88+
{
89+
has_filter('netlify_posttypes') &&
90+
apply_filters('netlify_posttypes', $this->plugin->postTypes);
91+
92+
$this->postTypes = $this->plugin->postTypes;
93+
94+
return $this;
95+
}
96+
97+
/**
98+
* POSTs to appropriate webhook on publish actions
99+
*/
100+
public function onPublish()
101+
{
102+
// set the netlify hook based on envvar
103+
switch ($this->plugin->runtime->env) :
104+
case 'DEVELOPMENT':
105+
$this->hook = $this->hooks->development;
106+
break;
107+
108+
case 'STAGING':
109+
$this->hook = $this->hooks->staging;
110+
break;
111+
112+
case 'PRODUCTION':
113+
$this->hook = $this->hooks->production;
114+
break;
115+
endswitch;
116+
117+
// make the run
118+
$this->hook &&
119+
$this->client->post($this->hook);
120+
}
121+
}

0 commit comments

Comments
 (0)