├── .github └── workflows │ └── workflow.yml ├── .gitignore ├── .wordpress-org ├── icon.svg └── screenshot-1.png ├── CHANGELOG.md ├── LICENSE ├── README.md ├── assets └── admin.js ├── composer.json ├── index.php ├── readme.txt ├── src ├── App.php ├── Field.php ├── Settings.php ├── UI │ └── SettingsScreen.php ├── WebhookTrigger.php └── functions.php ├── uninstall.php └── wp-jamstack-deployments.php /.github/workflows/workflow.yml: -------------------------------------------------------------------------------- 1 | name: Deploy to WordPress.org 2 | on: 3 | push: 4 | tags: 5 | - "*" 6 | jobs: 7 | tag: 8 | name: New tag 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@master 12 | - name: WordPress Plugin Deploy 13 | uses: 10up/action-wordpress-plugin-deploy@master 14 | env: 15 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 16 | SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }} 17 | SVN_USERNAME: ${{ secrets.SVN_USERNAME }} 18 | SLUG: wp-jamstack-deployments 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | .DS_Store 3 | Thumbs.db 4 | -------------------------------------------------------------------------------- /.wordpress-org/icon.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.wordpress-org/screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crgeary/wp-jamstack-deployments/6734ccc4c30cf2474dda3fb92867b6ed4c7dcbc1/.wordpress-org/screenshot-1.png -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.1.1 2 | 3 | - Add width/height to admin bar SVG to fix FOUC 4 | 5 | ## 1.1.0 6 | 7 | - Add settings to allow user to specify "post statuses" that trigger deployments 8 | - Add support for deploying when ACF options pages are updated 9 | 10 | ## 1.0.0 11 | 12 | - No longer uses `save_post` and `transition_post_status` to detect updates. Only uses `transition_post_status` 13 | - Fix undefined offset error when options not yet saved 14 | 15 | ## 0.4.1 16 | 17 | - No longer uses `sanitize_text_field()` on webhook url 18 | 19 | ## 0.4.0 20 | 21 | - Add support for `transition_post_status` to better handle post changes 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JAMstack Deployments 2 | 3 | A WordPress plugin for JAMstack deployments on Netlify (and other platforms). 4 | 5 | ## Description 6 | 7 | This plugin provides a way to fire off a request to a webhook when a post, page or custom post type has been created, udpated or deleted. You're also able to fire off a request manually at the click of a button, or programmatically via a WordPress action. 8 | 9 | ## Screenshots 10 | 11 | ![Settings Screen](.wordpress-org/screenshot-1.png) 12 | 13 | ## Installing the Plugin 14 | 15 | Clone the contents of this repository to your WordPress plugins folder and activate the plugin via the installed plugins page. 16 | 17 | ## Configuration 18 | 19 | The plugin attempts to trigger builds when you update your content, and has settings that you can use to define what post types & taxonomies should be monitored. 20 | 21 | You can access the plugin's settings in WordPress by accessing the 'Settings' panel on the left hand side of the dashboard and then clicking 'Deployments'. 22 | 23 | From this screen you can configure the following: 24 | 25 | - **Webhook URL** - The webhook URL that you have created to trigger a deployment. For more information on webhooks with Netlify [visit the Netlify documentation](https://www.netlify.com/docs/webhooks/). 26 | - **Webhook Method** - This is the required method for the webhook request. The available options are `GET` or `POST`. By default the plugin will automatically select `POST`. 27 | - **Badge Image URL** - An optional field to specify the `src` of a badge, for services that support badges. 28 | - **Badge Link** - An optional field to specify the `href` of a badge, for services that support badges. 29 | - **Post Types** - A list of selectable post types that will trigger a Netlify deployment when created, updated or deleted. Note that only selected post types will trigger a deployment. 30 | - **Taxonomies** - A list of selectable taxonomies that will trigger a Netlify deployment when created, updated or deleted. Note that only selected taxonomies will trigger a deployment. 31 | 32 | **If you need more control, there are actions & filters you can use to get the job done.** 33 | 34 | ### Post Types 35 | 36 | You can choose which posts types should trigger builds from the plugin settings. However, you may require more control, or need to overwrite the settings, you can do so using the `jamstack_deployments_post_types` filter. By default, this filter contains an array of post types that we monitor. You can add or remove them as required. 37 | 38 | For example, if you want to force the plugin to trigger builds for the `'post'` post type regardless of the settings, you can do so with the following code: 39 | 40 | ```php 41 | add_filter('jamstack_deployments_post_types', function ($post_types, $post_id, $post) { 42 | if (!in_array($post->post_type, $post_types, true)) { 43 | $post_types[] = 'post'; 44 | } 45 | return $post_types; 46 | }, 10, 3); 47 | ``` 48 | 49 | ### Taxonomies 50 | 51 | Like post types, you can choose which taxonmies should trigger builds from the plugin settings. But there may be times you need more control. For this, you can use the `jamstack_deployments_taxonomies` filter. By default, this filter contains an array of taxonomies that we monitor. 52 | 53 | For example, if you want to force the plugin to trigger builds for the `'post_tag'` taxonomy regardless of the settings, you can do so with the following code: 54 | 55 | ```php 56 | add_filter('jamstack_deployments_taxonomies', function ($taxonomies, $term_id, $tax_id) { 57 | $tax = get_taxonomy($tax_id); 58 | if (!in_array($tax->name, $taxonomies, true)) { 59 | $taxonomies[] = 'post_tag'; 60 | } 61 | return $taxonomies; 62 | }, 10, 3); 63 | ``` 64 | 65 | ### Post Statuses 66 | 67 | You can use the `jamstack_deployments_post_statuses` filter to change which post statuses we monitor. The default is to monitor `'publish'`, `'private'` and `'trash'`. 68 | 69 | Here is an example that adds `'review'` to the array of post statuses that we monitor & will trigger builds for. 70 | 71 | ```php 72 | add_filter('jamstack_deployments_post_statuses', function ($statuses, $post_id, $post) { 73 | $statuses[] = 'review'; 74 | return $statuses; 75 | }, 10, 3); 76 | ``` 77 | 78 | ## Custom Actions 79 | 80 | The `jamstack_deployments_fire_webhook` action can be used to fire the webhook and trigger a build at a custom point that you specify. For example, if you want to fire the webhook when a user registers, then you can use: 81 | 82 | ```php 83 | add_action('user_register', 'jamstack_deployments_fire_webhook'); 84 | ``` 85 | 86 | ## Running Code Before & After Webhooks 87 | 88 | You can run code directly before or after you fire the webhook using the following actions: 89 | 90 | * Before: `jamstack_deployments_before_fire_webhook` 91 | * After: `jamstack_deployments_after_fire_webhook` 92 | 93 | ## Changing Webhook Request Arguments 94 | 95 | You can modify the arguments sent to the `wp_remote_safe_*` functions using the `jamstack_deployments_webhook_request_args` filter. 96 | 97 | ## License 98 | [GPL-3.0](LICENSE.md) 99 | -------------------------------------------------------------------------------- /assets/admin.js: -------------------------------------------------------------------------------- 1 | ;(function ($, window, document, undefined) { 2 | $(function () { 3 | 4 | var image = $('.wp-jamstack-deployments-badge img'); 5 | var imageSrc = image.prop('src'); 6 | var refreshTimout = null; 7 | 8 | var updateNetlifyBadgeUrl = function () { 9 | if (!image.length) { 10 | return; 11 | } 12 | var d = new Date(); 13 | image.prop('src', imageSrc + '?v=s_' + d.getTime()); 14 | refreshTimout = setTimeout(updateNetlifyBadgeUrl, 15000); 15 | }; 16 | 17 | refreshTimout = setTimeout(updateNetlifyBadgeUrl, 15000); 18 | 19 | $('.wp-jamstack-deployments-button').click(function (e) { 20 | e.preventDefault(); 21 | $.ajax({ 22 | type: 'POST', 23 | url: wpjd.ajaxurl, 24 | data: { 25 | action: 'wp_jamstack_deployments_manual_trigger', 26 | security: wpjd.deployment_button_nonce, 27 | }, 28 | dataType: 'json', 29 | success: updateNetlifyBadgeUrl, 30 | }); 31 | clearTimeout(refreshTimout); 32 | }); 33 | 34 | }); 35 | })(jQuery, window, document); 36 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "crgeary/wp-jamstack-deployments", 3 | "description": "A WordPress plugin for JAMstack deployments", 4 | "type": "wordpress-plugin", 5 | "license": "GPL-3.0+", 6 | "version": "1.1.1", 7 | "authors": [ 8 | { 9 | "name": "Christopher Geary", 10 | "email": "hello@crgeary.com" 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | Deployments screen 16 | -------------------------------------------------------------------------------- /src/App.php: -------------------------------------------------------------------------------- 1 | constants(); 40 | $this->includes(); 41 | $this->hooks(); 42 | } 43 | 44 | /** 45 | * Register constants 46 | * 47 | * @return void 48 | */ 49 | protected function constants() 50 | { 51 | define('CRGEARY_JAMSTACK_DEPLOYMENTS_OPTIONS_KEY', 'wp_jamstack_deployments'); 52 | } 53 | 54 | /** 55 | * Include/require files 56 | * 57 | * @return void 58 | */ 59 | protected function includes() 60 | { 61 | require_once (CRGEARY_JAMSTACK_DEPLOYMENTS_PATH.'/src/UI/SettingsScreen.php'); 62 | 63 | require_once (CRGEARY_JAMSTACK_DEPLOYMENTS_PATH.'/src/Settings.php'); 64 | require_once (CRGEARY_JAMSTACK_DEPLOYMENTS_PATH.'/src/WebhookTrigger.php'); 65 | require_once (CRGEARY_JAMSTACK_DEPLOYMENTS_PATH.'/src/Field.php'); 66 | 67 | require_once (CRGEARY_JAMSTACK_DEPLOYMENTS_PATH.'/src/functions.php'); 68 | } 69 | 70 | /** 71 | * Register actions & filters 72 | * 73 | * @return void 74 | */ 75 | protected function hooks() 76 | { 77 | register_activation_hook(CRGEARY_JAMSTACK_DEPLOYMENTS_FILE, [$this, 'activation']); 78 | register_deactivation_hook(CRGEARY_JAMSTACK_DEPLOYMENTS_FILE, [$this, 'deactivation']); 79 | 80 | SettingsScreen::init(); 81 | Settings::init(); 82 | WebhookTrigger::init(); 83 | } 84 | 85 | /** 86 | * Fires on plugin activation 87 | * 88 | * @return void 89 | */ 90 | public function activation() 91 | { 92 | 93 | } 94 | 95 | /** 96 | * Fires on plugin deactivation 97 | * 98 | * @return void 99 | */ 100 | public function deactivation() 101 | { 102 | 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/Field.php: -------------------------------------------------------------------------------- 1 |
16 | 17 | {$args['description']}

" : ''; ?> 18 |
30 | 35 | {$args['description']}

" : ''; ?> 36 |
50 | 51 | $v) : ?> 52 |
60 | 61 | {$args['description']}

" : ''; ?> 62 |
"{$key}[webhook_url]", 35 | 'value' => jamstack_deployments_get_webhook_url(), 36 | 'description' => sprintf( __( 'Your Build Hook URL. This is the URL that is pinged to start building/deploying the JAMstack site. See Netlify docs or see Zeit docs.', 'wp-jamstack-deployments' ), 'https://docs.netlify.com/configure-builds/build-hooks/', 'https://zeit.co/docs/v2/advanced/deploy-hooks/' ) 37 | ]); 38 | 39 | add_settings_field('webhook_method', __( 'Hook Method', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'select'], $key, 'general', [ 40 | 'name' => "{$key}[webhook_method]", 41 | 'value' => jamstack_deployments_get_webhook_method(), 42 | 'choices' => [ 43 | 'post' => 'POST', 44 | 'get' => 'GET' 45 | ], 46 | 'default' => 'post', 47 | 'description' => __( 'Set either GET or POST for the build hook request. Defaults to POST.', 'wp-jamstack-deployments' ) 48 | ]); 49 | 50 | add_settings_field('deployment_badge_url', __( 'Badge Image URL', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'url'], $key, 'general', [ 51 | 'name' => "{$key}[deployment_badge_url]", 52 | 'value' => self::getBadgeImageUrl($option), 53 | 'description' => sprintf( __( 'Your Badge URL. Input the URL to display a badge with the current site status on your WordPress back end. See Netlify docs.', 'wp-jamstack-deployments' ), 'https://docs.netlify.com/monitor-sites/status-badges/#add-status-badges' ) 54 | ]); 55 | 56 | add_settings_field('deployment_badge_link_url', __( 'Badge Link', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'url'], $key, 'general', [ 57 | 'name' => "{$key}[deployment_badge_link_url]", 58 | 'value' => isset($option['deployment_badge_link_url']) ? $option['deployment_badge_link_url'] : '', 59 | 'description' => sprintf( __( 'The link to your deployments. See Netlify docs.', 'wp-jamstack-deployments' ), 'https://www.netlify.com/docs/continuous-deployment/' ) 60 | ]); 61 | 62 | add_settings_field('webhook_post_types', __( 'Post Types', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'checkboxes'], $key, 'general', [ 63 | 'name' => "{$key}[webhook_post_types]", 64 | 'value' => isset($option['webhook_post_types']) ? $option['webhook_post_types'] : [], 65 | 'choices' => self::getPostTypes(), 66 | 'description' => __( 'Only selected post types will trigger a deployment when created, updated or deleted.', 'wp-jamstack-deployments' ), 67 | 'legend' => 'Post Types' 68 | ]); 69 | 70 | add_settings_field('webhook_taxonomies', __( 'Taxonomies', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'checkboxes'], $key, 'general', [ 71 | 'name' => "{$key}[webhook_taxonomies]", 72 | 'value' => isset($option['webhook_taxonomies']) ? $option['webhook_taxonomies'] : [], 73 | 'choices' => self::getTaxonomies(), 74 | 'description' => __( 'Only selected taxonomies will trigger a deployment when their terms are created, updated or deleted.', 'wp-jamstack-deployments' ), 75 | 'legend' => 'Taxonomies' 76 | ]); 77 | 78 | 79 | add_settings_field('webhook_post_statuses', __('Post Statuses', 'wp-jamstack-deployments'), ['Crgeary\JAMstackDeployments\Field', 'checkboxes'], $key, 'general', [ 80 | 'name' => "{$key}[webhook_post_statuses]", 81 | 'value' => isset($option['webhook_post_statuses']) ? $option['webhook_post_statuses'] : ['private', 'publish', 'trash'], 82 | 'choices' => self::getStatuses(), 83 | 'description' => __('Only posts with the selected statuses will trigger a deployment.', 'wp-jamstack-deployments'), 84 | 'legend' => 'Post Statuses' 85 | ]); 86 | 87 | add_settings_field('webhook_acf', __( 'ACF', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'checkboxes'], $key, 'general', [ 88 | 'name' => "{$key}[webhook_acf]", 89 | 'value' => isset($option['webhook_acf']) ? $option['webhook_acf'] : [], 90 | 'choices' => [ 91 | 'options' => __('Options Page', 'wp-jamstack-deployments'), 92 | ], 93 | 'description' => __( 'Only selected ACF locations will trigger a deployment when they\'re saved.', 'wp-jamstack-deployments' ), 94 | 'legend' => 'ACF' 95 | ]); 96 | } 97 | 98 | /** 99 | * Get the badge image URL, has fallback to old option name 100 | * 101 | * @param array $option 102 | * @return string 103 | */ 104 | protected static function getBadgeImageUrl($option) 105 | { 106 | if (!empty($option['deployment_badge_url'])) { 107 | return $option['deployment_badge_url']; 108 | } 109 | 110 | return !empty($option['netlify_badge_url']) ? $option['netlify_badge_url'] : ''; 111 | } 112 | 113 | /** 114 | * Get an array of post types in name > label format 115 | * 116 | * @return array 117 | */ 118 | protected static function getPostTypes() 119 | { 120 | $return = []; 121 | 122 | foreach (get_post_types(null, 'objects') as $choice) { 123 | $return[$choice->name] = $choice->labels->name; 124 | } 125 | 126 | return $return; 127 | } 128 | 129 | /** 130 | * Get an array of taxonomies in name > label format 131 | * 132 | * @return array 133 | */ 134 | protected static function getTaxonomies() 135 | { 136 | $return = []; 137 | 138 | foreach (get_taxonomies(null, 'objects') as $choice) { 139 | $return[$choice->name] = $choice->labels->name; 140 | } 141 | 142 | return $return; 143 | } 144 | 145 | /** 146 | * Get an array of statuses in name > label format 147 | * 148 | * @return array 149 | */ 150 | protected static function getStatuses() 151 | { 152 | return array_merge(get_post_statuses(), ['trash' => __('Trash', 'wp-jamstack-deployments')]); 153 | } 154 | 155 | /** 156 | * Sanitize user input 157 | * 158 | * @var array $input 159 | * @return array 160 | */ 161 | public static function sanitize($input) 162 | { 163 | if (!empty($input['webhook_url'])) { 164 | $input['webhook_url'] = trim($input['webhook_url']); 165 | } 166 | 167 | if (isset($input['webhook_method']) && !in_array($input['webhook_method'], ['get', 'post'])) { 168 | $input['webhook_method'] = 'post'; 169 | } 170 | 171 | if (!isset($input['webhook_post_types']) || !is_array($input['webhook_post_types'])) { 172 | $input['webhook_post_types'] = []; 173 | } 174 | 175 | if (!isset($input['webhook_taxonomies']) || !is_array($input['webhook_taxonomies'])) { 176 | $input['webhook_taxonomies'] = []; 177 | } 178 | 179 | if (!isset($input['webhook_post_statuses']) || !is_array($input['webhook_post_statuses'])) { 180 | $input['webhook_post_statuses'] = []; 181 | } 182 | 183 | return $input; 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /src/UI/SettingsScreen.php: -------------------------------------------------------------------------------- 1 |
41 | 42 |

43 | 44 |
45 | 59 | 60 |

61 | 62 | 63 |
64 | 65 |
admin_url('admin-ajax.php'), 182 | 'deployment_button_nonce' => $button_nonce, 183 | ]); 184 | } 185 | 186 | /** 187 | * Add a "trigger webhook" button to the admin bar 188 | * 189 | * @param object $bar 190 | * @return void 191 | */ 192 | public static function adminBarTriggerButton($bar) 193 | { 194 | $option = jamstack_deployments_get_options(); 195 | $image = ''; 196 | 197 | if (!empty($option['deployment_badge_url'])) { 198 | $image = $option['deployment_badge_url']; 199 | } else if (!empty($option['netlify_badge_url'])) { 200 | $image = $option['netlify_badge_url']; 201 | } 202 | 203 | if (!empty($image)) { 204 | $bar->add_node([ 205 | 'id' => 'wp-jamstack-deployments-netlify-badge', 206 | 'title' => sprintf('', $image), 207 | 'href' => empty($option['deployment_badge_link_url']) ? 'javascript:void(0)' : $option['deployment_badge_link_url'], 208 | 'parent' => 'top-secondary', 209 | 'meta' => [ 210 | 'class' => 'wp-jamstack-deployments-badge', 211 | 'target' => empty($option['deployment_badge_link_url']) ? '_self' : '_blank', 212 | ] 213 | ]); 214 | } 215 | 216 | $bar->add_node([ 217 | 'id' => 'wp-jamstack-deployments', 218 | 'title' => 'Deploy Website ', 219 | 'parent' => 'top-secondary', 220 | 'href' => 'javascript:void(0)', 221 | 'meta' => [ 222 | 'class' => 'wp-jamstack-deployments-button' 223 | ] 224 | ]); 225 | } 226 | 227 | /** 228 | * Trigger a request manually from the admin settings 229 | * 230 | * @return void 231 | */ 232 | public static function trigger() 233 | { 234 | if (!isset($_GET['action']) || 'jamstack-deployment-trigger' !== $_GET['action']) { 235 | return; 236 | } 237 | 238 | check_admin_referer('crgeary_jamstack_deployment_trigger', 'crgeary_jamstack_deployment_trigger'); 239 | 240 | self::fireWebhook(); 241 | 242 | wp_redirect(admin_url('admin.php?page=wp-jamstack-deployments-settings')); 243 | exit; 244 | } 245 | 246 | /** 247 | * Trigger a webhook when a post transitions to published 248 | * 249 | * @param string $new 250 | * @param string $old 251 | * @param WP_Post $post 252 | * 253 | * @return void 254 | */ 255 | public static function triggerPostTransition($new, $old, $post) 256 | { 257 | $id = $post->ID; 258 | $option = jamstack_deployments_get_options(); 259 | 260 | $saved_post_types = isset($option['webhook_post_types']) ? $option['webhook_post_types'] : []; 261 | $post_types = apply_filters('jamstack_deployments_post_types', $saved_post_types, $id, $post); 262 | 263 | if (!in_array(get_post_type($id), $post_types, true)) { 264 | return; 265 | } 266 | 267 | $saved_post_statuses = isset($option['webhook_post_statuses']) ? $option['webhook_post_statuses'] : ['publish', 'private', 'trash']; 268 | $statuses = apply_filters('jamstack_deployments_post_statuses', $saved_post_statuses, $id, $post); 269 | 270 | if (!in_array(get_post_status($id), $statuses, true)) { 271 | return; 272 | } 273 | 274 | self::fireWebhook(); 275 | } 276 | 277 | /** 278 | * Trigger a request manually from the admin settings 279 | * 280 | * @return void 281 | */ 282 | public static function ajaxTrigger() 283 | { 284 | check_ajax_referer('wp-jamstack-deployments-button-nonce', 'security'); 285 | 286 | self::fireWebhook(); 287 | 288 | echo 1; 289 | exit; 290 | } 291 | 292 | /** 293 | * Fire off a request to the webhook 294 | * 295 | * @return WP_Error|array 296 | */ 297 | public static function fireWebhook() 298 | { 299 | $webhook = jamstack_deployments_get_webhook_url(); 300 | 301 | if (!$webhook) { 302 | return; 303 | } 304 | 305 | if (false === filter_var($webhook, FILTER_VALIDATE_URL)) { 306 | return; 307 | } 308 | 309 | $args = apply_filters('jamstack_deployments_webhook_request_args', [ 310 | 'blocking' => false 311 | ]); 312 | 313 | $method = jamstack_deployments_get_webhook_method(); 314 | 315 | do_action('jamstack_deployments_before_fire_webhook'); 316 | 317 | if ($method === 'get') { 318 | $return = wp_safe_remote_get($webhook, $args); 319 | } else { 320 | $return = wp_safe_remote_post($webhook, $args); 321 | } 322 | 323 | do_action('jamstack_deployments_after_fire_webhook'); 324 | 325 | return $return; 326 | } 327 | } 328 | -------------------------------------------------------------------------------- /src/functions.php: -------------------------------------------------------------------------------- 1 |