├── readme.txt └── wp-netlify-updater.php /readme.txt: -------------------------------------------------------------------------------- 1 | === WordPress Netfily Updater === 2 | Contributors: yahsan2 3 | Donate link: https://github.com/yahsan2/wp-netlify-updater 4 | Tags: netlify, wordpress 5 | Requires at least: 1.0 6 | Tested up to: 4.9.4 7 | Stable tag: 0.0.1 8 | License: GPLv2 or later 9 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 10 | 11 | == Description == 12 | WordPress plugins Netlify build hook when updating posts 13 | 14 | == Installation == 15 | 0. cd path/to/wp-content/plugins/ && git clone https://github.com/yahsan2/wp-netlify-updater 16 | 1. Activate Wp Netlify Updater plugins 17 | 2. Set up Netfily incoming webhooks on admin page > Options > Wp Netlify Updater 18 | 19 | == Changelog == 20 | 2018/04/29 First Commit -------------------------------------------------------------------------------- /wp-netlify-updater.php: -------------------------------------------------------------------------------- 1 | version = '0.1'; 13 | $this->name = 'Wp Netlify Updater'; 14 | $this->slug = 'wp-netlify-updater'; 15 | $this->prefix = 'wpnu_'; 16 | 17 | $this->set_options(); 18 | 19 | if(is_admin()){ 20 | add_action('save_post', array($this, 'wp_save_post')); 21 | add_action('edit_term', array($this, 'wp_edit_term')); 22 | add_action('admin_menu', array($this, 'add_menu')); 23 | } 24 | } 25 | 26 | function wp_save_post( $post_id ) { 27 | $title = get_the_title($post_id); 28 | $modified_user = $this->get_modified_author($post_id); 29 | $this->netlify_webhooks( $title . ' updated by ' . $modified_user->display_name , 'Triggered by wp hook: save_post'); 30 | } 31 | 32 | function wp_edit_term( $term_id, $tt_id, $taxonomy ) { 33 | $term = get_term($term_id, $taxonomy); 34 | $this->netlify_webhooks( $term->name, 'Triggered by wp hook: edit_term' ); 35 | } 36 | 37 | function get_modified_author($post_id) { 38 | $last_id = get_post_meta( $post_id, '_edit_last', true ); 39 | if ( $last_id ) { 40 | return get_userdata( $last_id ); 41 | } 42 | } 43 | 44 | function netlify_webhooks($title, $hook = 'Triggered by hook: WordPress') { 45 | if(isset($this->webhook_url) && $this->webhook_url){ 46 | $parsed_url = parse_url($this->webhook_url); 47 | $base_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path']; 48 | $title = $title ? $title : get_bloginfo('name'); 49 | $output['trigger_title'] = $hook. ' | ' . $title; 50 | $url = $base_url . '?' . http_build_query($output); 51 | 52 | $output = array(); 53 | $curl = curl_init($url); 54 | curl_setopt($curl, CURLOPT_POST, true); 55 | curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query( $output )); 56 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 57 | $response = curl_exec($curl); 58 | curl_close($curl); 59 | } 60 | } 61 | 62 | function add_menu() { 63 | add_submenu_page('options-general.php', $this->name, $this->name, 'level_8', __FILE__, array($this,'option_page'), ''); 64 | } 65 | 66 | function set_options(){ 67 | $opt = get_option( $this->prefix.'option'); 68 | if(isset($opt)){ 69 | $this->webhook_url = $opt['webhook_url']; 70 | }else{ 71 | $this->webhook_url = null; 72 | } 73 | } 74 | 75 | function option_page() { 76 | if ( isset($_POST[ $this->prefix.'option'])): ?> 77 | slug ); 79 | $opt = $_POST[ $this->prefix.'option']; 80 | update_option( $this->prefix.'option', $opt); 81 | ?> 82 |

83 | 84 |
85 |

name ?>

86 |
87 | slug ); 89 | $this->set_options(); 90 | ?> 91 | 92 | 93 | 97 | 101 | 102 |
94 | 95 |

Docs: https://www.netlify.com/docs/webhooks/

96 |
98 |

URL:

99 | 100 |
103 |

104 |
105 |
106 | 107 |