Plugin Directory

Changeset 3060628


Ignore:
Timestamp:
03/28/2024 03:07:29 PM (2 years ago)
Author:
EdwardBock
Message:

release 2.2.2

Location:
headless
Files:
3 deleted
9 edited
64 copied

Legend:

Unmodified
Added
Removed
  • headless/tags/2.2.2/README.txt

    r3033669 r3060628  
    66Tested up to: 6.4.3
    77Requires PHP: 8.0
    8 Stable tag: 2.2.0
     8Stable tag: 2.2.2
    99License: GPLv3
    1010License URI: http://www.gnu.org/licenses/gpl
     
    2727
    2828== Changelog ==
     29
     30= 2.2.2 =
     31* Feature: revalidate comments on schedule
     32* Bugfix: hl_post_type filter with any now working
     33
    2934
    3035= 2.2.0 =
  • headless/tags/2.2.2/classes/Ajax.php

    r3033669 r3060628  
    5656        if(isset($_GET[self::GET_POST_ID]) && !empty($_GET[self::GET_POST_ID])){
    5757            $postId = intval($_GET[self::GET_POST_ID]);
    58             $result = $this->plugin->revalidate->revalidateByPostId($frontends[$frontendIndex], $postId);
     58            $result = $this->plugin->revalidate->revalidateByPathByPostId($frontends[$frontendIndex], $postId);
    5959            if($result instanceof \WP_Error){
    6060                wp_send_json_error($result);
  • headless/tags/2.2.2/classes/Dashboard.php

    r3033669 r3060628  
    3939
    4040            <p>Pending posts to be revalidated: <?= $this->plugin->dbRevalidation->countPendingPosts(); ?></p>
     41            <p>Pending comments to be revalidated: <?= $this->plugin->dbRevalidation->countPendingComments(); ?></p>
    4142
    4243            <button class="button button-secondary" id="headless-revalidate-pending">Revalidate pending</button>
  • headless/tags/2.2.2/classes/Query.php

    r2748985 r3060628  
    2929    public static function getRequestPostTypes( \WP_REST_Request $request ) {
    3030        $post_types = $request->get_param( static::POST_TYPE );
     31        if(in_array("any",$post_types)){
     32            return ["any"];
     33        }
    3134        if ( empty( $post_types ) ) {
    3235            return [];
  • headless/tags/2.2.2/classes/Revalidate.php

    r2901812 r3060628  
    1010    public function onCreate() {
    1111        parent::onCreate();
    12         add_action('save_post', [$this, 'save_post']);
    13         add_action('edit_comment', [$this, 'edit_comment']);
     12        add_action('save_post', [$this, 'on_post_change']);
     13        add_action('edit_comment', [$this, 'on_comment_change']);
     14        add_action('wp_insert_comment', [$this, 'on_comment_change']);
    1415    }
    1516
    16     public function save_post($post_id){
     17    public function on_post_change($post_id){
    1718        if(wp_is_post_revision($post_id)) return;
    1819        $this->plugin->dbRevalidation->addPost($post_id);
    1920    }
    2021
    21     public function edit_comment($comment_id){
     22    public function on_comment_change($comment_id){
    2223        $comment = get_comment($comment_id);
    2324        $this->plugin->dbRevalidation->addPost($comment->comment_post_ID);
     25        $this->plugin->dbRevalidation->addComment($comment_id);
     26    }
     27
     28    function revalidateComments($post_id){
     29        $frontends = $this->plugin->headquarter->getFrontends();
     30        $results = [];
     31        foreach ($frontends as $frontend){
     32            $results[] = $this->revalidateByTag(
     33                $frontend,
     34                apply_filters(Plugin::FILTER_REVALIDATE_COMMENTS_BY_TAG, "post-$post_id-comments")
     35            );
     36        }
     37        return $results;
    2438    }
    2539
     
    3347        $results = [];
    3448        foreach ($frontends as $frontend){
    35             $results[] = $this->revalidateByPostId($frontend, $post_id);
     49            $results[] = $this->revalidateByTag($frontend, "post-$post_id");
     50            $results[] = $this->revalidateByPathByPostId($frontend, $post_id);
    3651        }
    3752        return $results;
    3853    }
    3954
    40     function revalidateByPostId(Frontend $frontend, $post_id) {
     55    function revalidateByPathByPostId(Frontend $frontend, $post_id) {
    4156        $permalink = get_permalink($post_id);
    4257        $path = parse_url($permalink, PHP_URL_PATH);
     
    5267        return $this->executeRavalidation(
    5368            apply_filters(Plugin::FILTER_REVALIDATE_BY_PATH_URL, $url, $path, $frontend)
     69        );
     70    }
     71
     72    function revalidateByTag(Frontend $frontend, string $tag) {
     73        $baseUrl = $frontend->getBaseUrl();
     74        $url = untrailingslashit($baseUrl)."/api/revalidate?secret_token=".HEADLESS_SECRET_TOKEN."&tag=".urlencode($tag);
     75        return $this->executeRavalidation(
     76            apply_filters(Plugin::FILTER_REVALIDATE_BY_TAG_URL, $url, $tag, $frontend)
    5477        );
    5578    }
  • headless/tags/2.2.2/classes/Schedule.php

    r2901812 r3060628  
    3636        $now = time();
    3737
     38        $commentIds = $this->plugin->dbRevalidation->getPendingComments();
     39        foreach ($commentIds as $id){
     40            $comment = get_comment($id);
     41            $postId = $comment->comment_post_ID;
     42            $results = $this->plugin->revalidate->revalidateComments($postId);
     43
     44            $success = true;
     45            foreach ($results as $result){
     46                if($result instanceof \WP_Error){
     47                    $this->plugin->log->warning($result->get_error_message());
     48                    $title = get_the_title($id);
     49                    $this->plugin->log->warning("revalidate comment id: $id ; post: $postId $title");
     50                    $success = false;
     51                }
     52            }
     53
     54            if($success){
     55                $this->plugin->dbRevalidation->setCommentState($id);
     56            } else {
     57                $this->plugin->dbRevalidation->setCommentState($id, "error");
     58            }
     59        }
     60
    3861        $postIds = $this->plugin->dbRevalidation->getPendingPosts();
    3962
     
    6184        }
    6285
     86
     87
     88
    6389        // do stuff like revalidating landingpages
    6490        do_action(Plugin::ACTION_REVALIDATION_SIDE_EFFECT, $postIds);
     91
     92
    6593
    6694        $this->setLastRevalidationRun($now);
  • headless/tags/2.2.2/classes/Store/RevalidationDatabase.php

    r2977228 r3060628  
    1212class RevalidationDatabase extends Database {
    1313
     14    const TYPE_POST = "post";
     15    const TYPE_COMMENT = "comment";
     16
    1417    function init() {
    1518        $this->table = $this->wpdb->prefix . "headless_revalidate";
    1619    }
    1720
    18     /**
    19      * @param int $post_id
    20      * @return bool|int
    21      */
    22     public function addPost( $post_id ) {
     21    private function addContent(string $id, string $type) {
    2322        return $this->wpdb->replace(
    2423            $this->table,
    2524            [
    26                 "content_id"    => $post_id,
    27                 "content_type"  => "post",
     25                "content_id" => $id,
     26                "content_type" => $type,
    2827                "revalidated_at" => null,
    2928                "revalidation_state" => "pending",
     
    3231    }
    3332
     33    public function addPost(int $post_id) {
     34        return $this->addContent($post_id, self::TYPE_POST);
     35    }
     36
     37    public function addComment(string $comment_id) {
     38        return $this->addContent($comment_id, self::TYPE_COMMENT);
     39    }
     40
     41
    3442    /**
    35      * @param int $after
     43     * @return Int[]
     44     */
     45    private function getPendingContents(string $type): array {
     46        $sql = $this->wpdb->prepare(
     47            "SELECT content_id FROM $this->table WHERE content_type = '%s' AND revalidation_state = 'pending'",
     48            $type
     49        );
     50        return $this->wpdb->get_col($sql);
     51    }
     52
     53    /**
    3654     *
    3755     * @return Int[]
    3856     */
    39     public function getPendingPosts() {
    40         $sql = "SELECT content_id FROM $this->table WHERE content_type = 'post' AND revalidation_state = 'pending'";
    41         return  $this->wpdb->get_col( $sql);
     57    public function getPendingPosts(): array {
     58        return $this->getPendingContents(self::TYPE_POST);
    4259    }
    4360
    44     /**
    45      * @param int $after
    46      *
    47      * @return int
    48      */
    49     public function countPendingPosts(): int{
    50         $sql = "SELECT count(content_id) FROM $this->table WHERE content_type = 'post' AND revalidation_state = 'pending'";
     61    public function getPendingComments(): array {
     62        return $this->getPendingContents(self::TYPE_COMMENT);
     63    }
     64
     65
     66    private function countPendingContents(string $type): int {
     67        $sql = $this->wpdb->prepare(
     68            "SELECT count(content_id) FROM $this->table WHERE content_type = '%s' AND revalidation_state = 'pending'",
     69            $type
     70        );
    5171        return intval($this->wpdb->get_var($sql));
    5272    }
    5373
    54     public function setPostState(int $post_id, $state = "revalidated") {
     74    public function countPendingPosts(): int {
     75        return $this->countPendingContents(self::TYPE_POST);
     76    }
     77
     78    public function countPendingComments(): int {
     79        return $this->countPendingContents(self::TYPE_COMMENT);
     80    }
     81
     82    public function setContentState(int $id, string $type, $state = "revalidated") {
    5583        return $this->wpdb->update(
    5684            $this->table,
    5785            [
    58                 "revalidated_at" => current_time( 'mysql' ),
     86                "revalidated_at" => current_time('mysql'),
    5987                "revalidation_state" => $state,
    6088            ],
    6189            [
    62                 "content_id" => $post_id,
    63                 "content_type" => "post",
     90                "content_id" => $id,
     91                "content_type" => $type,
    6492            ],
    6593            ["%s", "%s"],
     
    6896    }
    6997
     98    public function setPostState(int $post_id, $state = "revalidated") {
     99        return $this->setContentState($post_id, self::TYPE_POST, $state);
     100    }
     101
     102    public function setCommentState(int $comment_id, $state = "revalidated") {
     103        return $this->setContentState($comment_id, self::TYPE_COMMENT, $state);
     104    }
     105
    70106    public function createTables() {
    71107        parent::createTables();
    72         \dbDelta( "CREATE TABLE IF NOT EXISTS $this->table
     108        \dbDelta("CREATE TABLE IF NOT EXISTS $this->table
    73109            (
    74110             id bigint(20) unsigned auto_increment,
  • headless/tags/2.2.2/headless.php

    r3033669 r3060628  
    55 * Plugin URI: https://github.com/palasthotel/headless
    66 * Description: Adds features to use WordPress as headless CMS
    7  * Version: 2.2.0
     7 * Version: 2.2.2
    88 * Author: Palasthotel (Edward Bock) <edward.bock@palasthotel.de>
    99 * Author URI: http://www.palasthotel.de
     
    9494    const FILTER_FRONTENDS = "headless_frontends";
    9595    const FILTER_REVALIDATE_BY_PATH_URL = "headless_revalidate_by_path_url";
     96    const FILTER_REVALIDATE_BY_TAG_URL = "headless_revalidate_by_tag_url";
     97    const FILTER_REVALIDATE_COMMENTS_BY_TAG = "headless_revalidate_comments_by_tag";
    9698    const FILTER_REVALIDATE_PERMALINK_PATH = "headless_revalidate_permalink_path";
    9799    const OPTION_LAST_REVALIDATION_RUN = "headless_last_revalidation_run";
  • headless/tags/2.2.2/vendor/composer/installed.php

    r3033669 r3060628  
    44        'pretty_version' => 'dev-main',
    55        'version' => 'dev-main',
    6         'reference' => 'dbd1b93a4b371dbbe1368ba0301bf88e86cb9723',
     6        'reference' => '1c6b6a45575b82a64578c37f33ec77db281733fc',
    77        'type' => 'library',
    88        'install_path' => __DIR__ . '/../../',
     
    1414            'pretty_version' => 'dev-main',
    1515            'version' => 'dev-main',
    16             'reference' => 'dbd1b93a4b371dbbe1368ba0301bf88e86cb9723',
     16            'reference' => '1c6b6a45575b82a64578c37f33ec77db281733fc',
    1717            'type' => 'library',
    1818            'install_path' => __DIR__ . '/../../',
  • headless/trunk/README.txt

    r3033669 r3060628  
    66Tested up to: 6.4.3
    77Requires PHP: 8.0
    8 Stable tag: 2.2.0
     8Stable tag: 2.2.2
    99License: GPLv3
    1010License URI: http://www.gnu.org/licenses/gpl
     
    2727
    2828== Changelog ==
     29
     30= 2.2.2 =
     31* Feature: revalidate comments on schedule
     32* Bugfix: hl_post_type filter with any now working
     33
    2934
    3035= 2.2.0 =
  • headless/trunk/classes/Ajax.php

    r3033669 r3060628  
    5656        if(isset($_GET[self::GET_POST_ID]) && !empty($_GET[self::GET_POST_ID])){
    5757            $postId = intval($_GET[self::GET_POST_ID]);
    58             $result = $this->plugin->revalidate->revalidateByPostId($frontends[$frontendIndex], $postId);
     58            $result = $this->plugin->revalidate->revalidateByPathByPostId($frontends[$frontendIndex], $postId);
    5959            if($result instanceof \WP_Error){
    6060                wp_send_json_error($result);
  • headless/trunk/classes/Dashboard.php

    r3033669 r3060628  
    3939
    4040            <p>Pending posts to be revalidated: <?= $this->plugin->dbRevalidation->countPendingPosts(); ?></p>
     41            <p>Pending comments to be revalidated: <?= $this->plugin->dbRevalidation->countPendingComments(); ?></p>
    4142
    4243            <button class="button button-secondary" id="headless-revalidate-pending">Revalidate pending</button>
  • headless/trunk/classes/Query.php

    r2748985 r3060628  
    2929    public static function getRequestPostTypes( \WP_REST_Request $request ) {
    3030        $post_types = $request->get_param( static::POST_TYPE );
     31        if(in_array("any",$post_types)){
     32            return ["any"];
     33        }
    3134        if ( empty( $post_types ) ) {
    3235            return [];
  • headless/trunk/classes/Revalidate.php

    r2901812 r3060628  
    1010    public function onCreate() {
    1111        parent::onCreate();
    12         add_action('save_post', [$this, 'save_post']);
    13         add_action('edit_comment', [$this, 'edit_comment']);
     12        add_action('save_post', [$this, 'on_post_change']);
     13        add_action('edit_comment', [$this, 'on_comment_change']);
     14        add_action('wp_insert_comment', [$this, 'on_comment_change']);
    1415    }
    1516
    16     public function save_post($post_id){
     17    public function on_post_change($post_id){
    1718        if(wp_is_post_revision($post_id)) return;
    1819        $this->plugin->dbRevalidation->addPost($post_id);
    1920    }
    2021
    21     public function edit_comment($comment_id){
     22    public function on_comment_change($comment_id){
    2223        $comment = get_comment($comment_id);
    2324        $this->plugin->dbRevalidation->addPost($comment->comment_post_ID);
     25        $this->plugin->dbRevalidation->addComment($comment_id);
     26    }
     27
     28    function revalidateComments($post_id){
     29        $frontends = $this->plugin->headquarter->getFrontends();
     30        $results = [];
     31        foreach ($frontends as $frontend){
     32            $results[] = $this->revalidateByTag(
     33                $frontend,
     34                apply_filters(Plugin::FILTER_REVALIDATE_COMMENTS_BY_TAG, "post-$post_id-comments")
     35            );
     36        }
     37        return $results;
    2438    }
    2539
     
    3347        $results = [];
    3448        foreach ($frontends as $frontend){
    35             $results[] = $this->revalidateByPostId($frontend, $post_id);
     49            $results[] = $this->revalidateByTag($frontend, "post-$post_id");
     50            $results[] = $this->revalidateByPathByPostId($frontend, $post_id);
    3651        }
    3752        return $results;
    3853    }
    3954
    40     function revalidateByPostId(Frontend $frontend, $post_id) {
     55    function revalidateByPathByPostId(Frontend $frontend, $post_id) {
    4156        $permalink = get_permalink($post_id);
    4257        $path = parse_url($permalink, PHP_URL_PATH);
     
    5267        return $this->executeRavalidation(
    5368            apply_filters(Plugin::FILTER_REVALIDATE_BY_PATH_URL, $url, $path, $frontend)
     69        );
     70    }
     71
     72    function revalidateByTag(Frontend $frontend, string $tag) {
     73        $baseUrl = $frontend->getBaseUrl();
     74        $url = untrailingslashit($baseUrl)."/api/revalidate?secret_token=".HEADLESS_SECRET_TOKEN."&tag=".urlencode($tag);
     75        return $this->executeRavalidation(
     76            apply_filters(Plugin::FILTER_REVALIDATE_BY_TAG_URL, $url, $tag, $frontend)
    5477        );
    5578    }
  • headless/trunk/classes/Schedule.php

    r2901812 r3060628  
    3636        $now = time();
    3737
     38        $commentIds = $this->plugin->dbRevalidation->getPendingComments();
     39        foreach ($commentIds as $id){
     40            $comment = get_comment($id);
     41            $postId = $comment->comment_post_ID;
     42            $results = $this->plugin->revalidate->revalidateComments($postId);
     43
     44            $success = true;
     45            foreach ($results as $result){
     46                if($result instanceof \WP_Error){
     47                    $this->plugin->log->warning($result->get_error_message());
     48                    $title = get_the_title($id);
     49                    $this->plugin->log->warning("revalidate comment id: $id ; post: $postId $title");
     50                    $success = false;
     51                }
     52            }
     53
     54            if($success){
     55                $this->plugin->dbRevalidation->setCommentState($id);
     56            } else {
     57                $this->plugin->dbRevalidation->setCommentState($id, "error");
     58            }
     59        }
     60
    3861        $postIds = $this->plugin->dbRevalidation->getPendingPosts();
    3962
     
    6184        }
    6285
     86
     87
     88
    6389        // do stuff like revalidating landingpages
    6490        do_action(Plugin::ACTION_REVALIDATION_SIDE_EFFECT, $postIds);
     91
     92
    6593
    6694        $this->setLastRevalidationRun($now);
  • headless/trunk/classes/Store/RevalidationDatabase.php

    r2977228 r3060628  
    1212class RevalidationDatabase extends Database {
    1313
     14    const TYPE_POST = "post";
     15    const TYPE_COMMENT = "comment";
     16
    1417    function init() {
    1518        $this->table = $this->wpdb->prefix . "headless_revalidate";
    1619    }
    1720
    18     /**
    19      * @param int $post_id
    20      * @return bool|int
    21      */
    22     public function addPost( $post_id ) {
     21    private function addContent(string $id, string $type) {
    2322        return $this->wpdb->replace(
    2423            $this->table,
    2524            [
    26                 "content_id"    => $post_id,
    27                 "content_type"  => "post",
     25                "content_id" => $id,
     26                "content_type" => $type,
    2827                "revalidated_at" => null,
    2928                "revalidation_state" => "pending",
     
    3231    }
    3332
     33    public function addPost(int $post_id) {
     34        return $this->addContent($post_id, self::TYPE_POST);
     35    }
     36
     37    public function addComment(string $comment_id) {
     38        return $this->addContent($comment_id, self::TYPE_COMMENT);
     39    }
     40
     41
    3442    /**
    35      * @param int $after
     43     * @return Int[]
     44     */
     45    private function getPendingContents(string $type): array {
     46        $sql = $this->wpdb->prepare(
     47            "SELECT content_id FROM $this->table WHERE content_type = '%s' AND revalidation_state = 'pending'",
     48            $type
     49        );
     50        return $this->wpdb->get_col($sql);
     51    }
     52
     53    /**
    3654     *
    3755     * @return Int[]
    3856     */
    39     public function getPendingPosts() {
    40         $sql = "SELECT content_id FROM $this->table WHERE content_type = 'post' AND revalidation_state = 'pending'";
    41         return  $this->wpdb->get_col( $sql);
     57    public function getPendingPosts(): array {
     58        return $this->getPendingContents(self::TYPE_POST);
    4259    }
    4360
    44     /**
    45      * @param int $after
    46      *
    47      * @return int
    48      */
    49     public function countPendingPosts(): int{
    50         $sql = "SELECT count(content_id) FROM $this->table WHERE content_type = 'post' AND revalidation_state = 'pending'";
     61    public function getPendingComments(): array {
     62        return $this->getPendingContents(self::TYPE_COMMENT);
     63    }
     64
     65
     66    private function countPendingContents(string $type): int {
     67        $sql = $this->wpdb->prepare(
     68            "SELECT count(content_id) FROM $this->table WHERE content_type = '%s' AND revalidation_state = 'pending'",
     69            $type
     70        );
    5171        return intval($this->wpdb->get_var($sql));
    5272    }
    5373
    54     public function setPostState(int $post_id, $state = "revalidated") {
     74    public function countPendingPosts(): int {
     75        return $this->countPendingContents(self::TYPE_POST);
     76    }
     77
     78    public function countPendingComments(): int {
     79        return $this->countPendingContents(self::TYPE_COMMENT);
     80    }
     81
     82    public function setContentState(int $id, string $type, $state = "revalidated") {
    5583        return $this->wpdb->update(
    5684            $this->table,
    5785            [
    58                 "revalidated_at" => current_time( 'mysql' ),
     86                "revalidated_at" => current_time('mysql'),
    5987                "revalidation_state" => $state,
    6088            ],
    6189            [
    62                 "content_id" => $post_id,
    63                 "content_type" => "post",
     90                "content_id" => $id,
     91                "content_type" => $type,
    6492            ],
    6593            ["%s", "%s"],
     
    6896    }
    6997
     98    public function setPostState(int $post_id, $state = "revalidated") {
     99        return $this->setContentState($post_id, self::TYPE_POST, $state);
     100    }
     101
     102    public function setCommentState(int $comment_id, $state = "revalidated") {
     103        return $this->setContentState($comment_id, self::TYPE_COMMENT, $state);
     104    }
     105
    70106    public function createTables() {
    71107        parent::createTables();
    72         \dbDelta( "CREATE TABLE IF NOT EXISTS $this->table
     108        \dbDelta("CREATE TABLE IF NOT EXISTS $this->table
    73109            (
    74110             id bigint(20) unsigned auto_increment,
  • headless/trunk/headless.php

    r3033669 r3060628  
    55 * Plugin URI: https://github.com/palasthotel/headless
    66 * Description: Adds features to use WordPress as headless CMS
    7  * Version: 2.2.0
     7 * Version: 2.2.2
    88 * Author: Palasthotel (Edward Bock) <edward.bock@palasthotel.de>
    99 * Author URI: http://www.palasthotel.de
     
    9494    const FILTER_FRONTENDS = "headless_frontends";
    9595    const FILTER_REVALIDATE_BY_PATH_URL = "headless_revalidate_by_path_url";
     96    const FILTER_REVALIDATE_BY_TAG_URL = "headless_revalidate_by_tag_url";
     97    const FILTER_REVALIDATE_COMMENTS_BY_TAG = "headless_revalidate_comments_by_tag";
    9698    const FILTER_REVALIDATE_PERMALINK_PATH = "headless_revalidate_permalink_path";
    9799    const OPTION_LAST_REVALIDATION_RUN = "headless_last_revalidation_run";
  • headless/trunk/vendor/composer/installed.php

    r3033669 r3060628  
    44        'pretty_version' => 'dev-main',
    55        'version' => 'dev-main',
    6         'reference' => 'dbd1b93a4b371dbbe1368ba0301bf88e86cb9723',
     6        'reference' => '1c6b6a45575b82a64578c37f33ec77db281733fc',
    77        'type' => 'library',
    88        'install_path' => __DIR__ . '/../../',
     
    1414            'pretty_version' => 'dev-main',
    1515            'version' => 'dev-main',
    16             'reference' => 'dbd1b93a4b371dbbe1368ba0301bf88e86cb9723',
     16            'reference' => '1c6b6a45575b82a64578c37f33ec77db281733fc',
    1717            'type' => 'library',
    1818            'install_path' => __DIR__ . '/../../',
Note: See TracChangeset for help on using the changeset viewer.