Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
scottopolis committed Aug 17, 2019
1 parent 6970c55 commit 7691fbe
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 2 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# wp-netlify-deploy
WP Netlify Deploy
# WP Netlify Deploy

This plugin triggers a Netlify deploy when a WordPress post is updated or created. It is meant to be used for headless WordPress with Gatsby as a front end.

Full tutorial for usage located here: [https://scottbolinger.com/headless-wordpress-with-gatsby/](https://scottbolinger.com/headless-wordpress-with-gatsby/)

## Usage

* Clone this repository, unzip and add to wp-content/plugins
* Active the plugin
* Create a new webook on Netlify under Build and Deploy => Continuous deployment, build hooks.
* Coming soon...Add a deploy notification under Build and Deploy => Deploy Notifications. Click Add notification => Outgoing Webhook, enter this url: https://YOURSITEDOMAIN.com/wp-json/wp-netlify-deploy/notifications
* Add your build hooks to the plugin settings and save.
* Select the hooks you want to trigger a rebuild in the plugin settings, and save.
145 changes: 145 additions & 0 deletions includes/class-wp-netlify-deploy-admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php
/**
* WooCommerce UPC Admin
* @since 0.1.0
*/


// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;

if( !class_exists( 'WP_Netlify_Deploy_Admin' ) ) {

/**
* WP_Netlify_Deploy_Admin class
*
* @since 0.2.0
*/
class WP_Netlify_Deploy_Admin {

/**
* @var WP_Netlify_Deploy_Admin $instance The one true WP_Netlify_Deploy_Admin
* @since 0.2.0
*/
private static $instance;
public static $errorpath = '../php-error-log.php';
public static $active = array();
// sample: error_log("meta: " . $meta . "\r\n",3,self::$errorpath);

/**
* Get active instance
*
* @access public
* @since 0.2.0
* @return object self::$instance The one true WP_Netlify_Deploy_Admin
*/
public static function instance() {
if( !self::$instance ) {
self::$instance = new WP_Netlify_Deploy_Admin();
self::$instance->hooks();
}

return self::$instance;
}


/**
* Include necessary files
*
* @access private
* @since 0.2.0
* @return void
*/
private function hooks() {

add_action( 'admin_menu', array( $this, 'settings_page' ) );
add_action( 'save_post', array( $this, 'maybe_send_webhook' ), 10, 3 );

}

/**
* Add settings menu item
*
* @access public
* @since 0.1
*/
public function settings_page() {

add_options_page(
'WP Netlify Deploy',
'WP Netlify Deploy',
'manage_options',
'wp_netlify_deploy',
array(
$this,
'render_settings'
)
);

}

/**
* Settings page output
*
* @access public
* @since 0.1
*/
public function render_settings() {

if( isset( $_POST['wpnd_webhook'] ) ) {
update_option( 'wpnd_webhook', sanitize_text_field( $_POST['wpnd_webhook'] ) );
}

?>
<div class="wrap">

<h2><?php _e('Settings', 'wp-netlify-deploy'); ?></h2>

<?php
if( get_option('wp_netlify_deploy_error') ) {
echo '<p style="color:red">' . get_option('wp_netlify_deploy_error') . '</p>';
} ?>

<form method="post">

<h3><?php _e('Webhook url', 'wp-netlify-deploy'); ?></h3>

<input id="wpnd_webhook" name="wpnd_webhook" value="<?php echo esc_html( get_option( 'wpnd_webhook' ) ); ?>" placeholder="Webhook url" type="text" size="50" /><br/>

<?php submit_button(); ?>

</form>

</div>
<?php

}

/**
* If conditions are met, trigger deployment on Netlify
*
* @access public
* @since 0.1
*/
public function maybe_send_webhook( $post_id, $post, $update ) {

if( $post->post_type != 'post' )
return;

$url = get_option('wpnd_webhook');
if( $url) {
$response = wp_remote_post( $url );
}

if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
update_option('wp_netlify_deploy_error', "Something went wrong with your Netlify deployment: $error_message" );
}
}

}

$WP_Netlify_Deploy_Admin = new WP_Netlify_Deploy_Admin();
$WP_Netlify_Deploy_Admin->instance();

} // end class_exists check
31 changes: 31 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== WP Netlify Deploy ===

Contributors: scottopolis
Tags: netlify, gatsby
Requires at least: 4.5
Tested up to: 5.2.0
Stable tag: 0.41.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Trigger a deployment (or Gatsby rebuild) on Netlify when posts or pages are created or updated.

== Description ==

This plugin calls a Netlify webhook on WordPress actions you choose, such as updating or creating a post or page.

For example, if you are using Gatsby, you can trigger a rebuild on Netlify which will update your static front end.

== Installation ==

* Install and activate the plugin.
* Create a new webook on Netlify under Build and Deploy => Continuous deployment, build hooks.
* Add a deploy notification under Build and Deploy => Deploy Notifications. Click Add notification => Outgoing Webhook, enter this url: https://YOURSITEDOMAIN.com/wp-json/wp-netlify-deploy/notifications
* Add your build hooks to the plugin settings and save.
* Select the hooks you want to trigger a rebuild in the plugin settings, and save.

== Changelog ==

= 0.1.0 =

* Beta release
132 changes: 132 additions & 0 deletions wp-netlify-deploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Plugin Name: WP Netlify Deploy
* Plugin URI: https://github.com/scottopolis/
* Description: Trigger a deployment (or Gatsby rebuild) on Netlify when posts or pages are created or updated.
* Version: 0.1.0
* Author: Scott Bolinger
* Text Domain: wp-netlify-deploy
*
* @author Scott Bolinger
* @copyright Copyright (c) Scott Bolinger 2019
*
*/


// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;

if( !class_exists( 'WP_Netlify_Deploy' ) ) {

/**
* Main WP_Netlify_Deploy class
*
* @since 0.1.0
*/
class WP_Netlify_Deploy {

/**
* @var WP_Netlify_Deploy $instance The one true WP_Netlify_Deploy
* @since 0.1.0
*/
private static $instance;


/**
* Get active instance
*
* @access public
* @since 0.1.0
* @return self The one true WP_Netlify_Deploy
*/
public static function instance() {
if( !self::$instance ) {
self::$instance = new WP_Netlify_Deploy();
self::$instance->setup_constants();
self::$instance->includes();
self::$instance->load_textdomain();
}

return self::$instance;
}


/**
* Setup plugin constants
*
* @access private
* @since 0.1.0
* @return void
*/
private function setup_constants() {
// Plugin version
define( 'WP_Netlify_Deploy_VER', '0.1.0' );

// Plugin path
define( 'WP_Netlify_Deploy_DIR', plugin_dir_path( __FILE__ ) );

// Plugin URL
define( 'WP_Netlify_Deploy_URL', plugin_dir_url( __FILE__ ) );

}


/**
* Include necessary files
*
* @access private
* @since 0.1.0
* @return void
*/
private function includes() {

if( is_admin() )
require_once WP_Netlify_Deploy_DIR . 'includes/class-wp-netlify-deploy-admin.php';

}


/**
* Internationalization
*
* @access public
* @since 0.1.0
* @return void
*/
public function load_textdomain() {

load_plugin_textdomain( 'wp-netlify-deploy' );

}

}
} // End if class_exists check


/**
* The main function responsible for returning the one true
* instance to functions everywhere
*
* @since 0.1.0
* @return \WP_Netlify_Deploy The one true WP_Netlify_Deploy
*
*/
function WP_Netlify_Deploy_load() {
return WP_Netlify_Deploy::instance();
}
add_action( 'plugins_loaded', 'WP_Netlify_Deploy_load' );


/**
* The activation hook is called outside of the singleton because WordPress doesn't
* register the call from within the class, since we are preferring the plugins_loaded
* hook for compatibility, we also can't reference a function inside the plugin class
* for the activation function. If you need an activation function, put it here.
*
* @since 0.1.0
* @return void
*/
function WP_Netlify_Deploy_activation() {
/* Activation functions here */
}
// register_activation_hook( __FILE__, 'WP_Netlify_Deploy_activation' );

0 comments on commit 7691fbe

Please sign in to comment.