Plugin Directory

Changeset 3097616


Ignore:
Timestamp:
06/04/2024 09:28:14 PM (10 months ago)
Author:
zapier
Message:

Add updated post hook

Location:
zapier
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • zapier/tags/1.5.0/readme.txt

    r3089542 r3097616  
    55Tested up to: 6.5
    66Requires PHP: 7.4
    7 Stable tag: 1.4.0
     7Stable tag: 1.5.0
    88License: Expat (MIT License)
    99License URI: https://spdx.org/licenses/MIT.html
     
    119119
    120120* Add support for webhooks & add user updated hook
     121
     122= 1.5.0 =
     123
     124* Add updated post hook
  • zapier/tags/1.5.0/zapier.php

    r3089542 r3097616  
    44 * Plugin Name:       Zapier for WordPress
    55 * Description:       Zapier enables you to automatically share your posts to social media, create WordPress posts from Mailchimp newsletters, and much more. Visit https://zapier.com/apps/wordpress/integrations for more details.
    6  * Version:           1.4.0
     6 * Version:           1.5.0
    77 * Author:            Zapier
    88 * Author URI:        https://zapier.com
     
    8181        $this->loader->add_plugin_filter('rest_pre_dispatch', $this, 'rest_pre_dispatch');
    8282        $this->loader->add_plugin_filter('determine_current_user', $this, 'determine_current_user');
     83
     84        // Webhooks
    8385        $this->loader->add_plugin_action('wp_update_user', $this, 'updated_user');
     86        $this->loader->add_plugin_action('post_updated', $this, 'updated_post', 10, 3);
    8487    }
    8588
     
    217220        }
    218221
    219         $ALLOWED_ACTIONS = array('wp_update_user');
     222        $ALLOWED_ACTIONS = array('wp_update_user','post_updated');
    220223
    221224        $action = $request->get_param("action");
     
    288291            $response = wp_remote_post($hook, array(
    289292                'body' => json_encode(array('user_id' => $user_id)),
     293                'headers' => array('Content-Type' => 'application/json'),
     294            ));
     295        }
     296    }
     297
     298    public function updated_post($post_id, $post_after, $post_before) {
     299        $option_key = "zapier_hooks_post_updated";
     300       
     301        $rest_base = get_post_type_object($post_after->post_type)->rest_base;
     302        $changed_properties = $this->compareObjects($post_after, $post_before);
     303       
     304        $hooks = get_option($option_key, []);
     305
     306        foreach($hooks as $hook) {
     307            $response = wp_remote_post($hook, array(
     308                'body' => json_encode(array(
     309                    'post_id' => $post_id,
     310                    'rest_base' => $rest_base,
     311                    'post_after_status' => $post_after->post_status,
     312                    'post_before_status' => $post_before->post_status,
     313                    'post_changed_properties' => $changed_properties
     314                )),
    290315                'headers' => array('Content-Type' => 'application/json'),
    291316            ));
     
    356381        return $request;
    357382    }
     383
     384    private function compareObjects($obj1, $obj2) {
     385        $reflect1 = new ReflectionClass($obj1);
     386        $reflect2 = new ReflectionClass($obj2);
     387
     388        if ($reflect1->getName() !== $reflect2->getName()) {
     389            throw new Exception('Objects must be instances of the same class');
     390        }
     391
     392        $properties1 = $reflect1->getProperties();
     393        $changed_properties = [];
     394
     395        foreach ($properties1 as $property) {
     396            // Make property accessible if it's private or protected
     397            $property->setAccessible(true);
     398
     399            $value1 = $property->getValue($obj1);
     400            $value2 = $property->getValue($obj2);
     401
     402            if ($value1 !== $value2) {
     403                $changed_properties[] = str_replace('post_', '', $property->getName());
     404            }
     405        }
     406
     407        return $changed_properties;
     408    }
    358409}
    359410
  • zapier/trunk/readme.txt

    r3089542 r3097616  
    55Tested up to: 6.5
    66Requires PHP: 7.4
    7 Stable tag: 1.4.0
     7Stable tag: 1.5.0
    88License: Expat (MIT License)
    99License URI: https://spdx.org/licenses/MIT.html
     
    119119
    120120* Add support for webhooks & add user updated hook
     121
     122= 1.5.0 =
     123
     124* Add updated post hook
  • zapier/trunk/zapier.php

    r3089542 r3097616  
    44 * Plugin Name:       Zapier for WordPress
    55 * Description:       Zapier enables you to automatically share your posts to social media, create WordPress posts from Mailchimp newsletters, and much more. Visit https://zapier.com/apps/wordpress/integrations for more details.
    6  * Version:           1.4.0
     6 * Version:           1.5.0
    77 * Author:            Zapier
    88 * Author URI:        https://zapier.com
     
    8181        $this->loader->add_plugin_filter('rest_pre_dispatch', $this, 'rest_pre_dispatch');
    8282        $this->loader->add_plugin_filter('determine_current_user', $this, 'determine_current_user');
     83
     84        // Webhooks
    8385        $this->loader->add_plugin_action('wp_update_user', $this, 'updated_user');
     86        $this->loader->add_plugin_action('post_updated', $this, 'updated_post', 10, 3);
    8487    }
    8588
     
    217220        }
    218221
    219         $ALLOWED_ACTIONS = array('wp_update_user');
     222        $ALLOWED_ACTIONS = array('wp_update_user','post_updated');
    220223
    221224        $action = $request->get_param("action");
     
    288291            $response = wp_remote_post($hook, array(
    289292                'body' => json_encode(array('user_id' => $user_id)),
     293                'headers' => array('Content-Type' => 'application/json'),
     294            ));
     295        }
     296    }
     297
     298    public function updated_post($post_id, $post_after, $post_before) {
     299        $option_key = "zapier_hooks_post_updated";
     300       
     301        $rest_base = get_post_type_object($post_after->post_type)->rest_base;
     302        $changed_properties = $this->compareObjects($post_after, $post_before);
     303       
     304        $hooks = get_option($option_key, []);
     305
     306        foreach($hooks as $hook) {
     307            $response = wp_remote_post($hook, array(
     308                'body' => json_encode(array(
     309                    'post_id' => $post_id,
     310                    'rest_base' => $rest_base,
     311                    'post_after_status' => $post_after->post_status,
     312                    'post_before_status' => $post_before->post_status,
     313                    'post_changed_properties' => $changed_properties
     314                )),
    290315                'headers' => array('Content-Type' => 'application/json'),
    291316            ));
     
    356381        return $request;
    357382    }
     383
     384    private function compareObjects($obj1, $obj2) {
     385        $reflect1 = new ReflectionClass($obj1);
     386        $reflect2 = new ReflectionClass($obj2);
     387
     388        if ($reflect1->getName() !== $reflect2->getName()) {
     389            throw new Exception('Objects must be instances of the same class');
     390        }
     391
     392        $properties1 = $reflect1->getProperties();
     393        $changed_properties = [];
     394
     395        foreach ($properties1 as $property) {
     396            // Make property accessible if it's private or protected
     397            $property->setAccessible(true);
     398
     399            $value1 = $property->getValue($obj1);
     400            $value2 = $property->getValue($obj2);
     401
     402            if ($value1 !== $value2) {
     403                $changed_properties[] = str_replace('post_', '', $property->getName());
     404            }
     405        }
     406
     407        return $changed_properties;
     408    }
    358409}
    359410
Note: See TracChangeset for help on using the changeset viewer.