Plugin Directory

Changeset 3476146


Ignore:
Timestamp:
03/06/2026 07:49:42 AM (4 weeks ago)
Author:
jamesdlow
Message:

1.5.2

  • Latest version of restlib library
Location:
pageapp
Files:
18 added
3 edited

Legend:

Unmodified
Added
Removed
  • pageapp/trunk/inc/restlib.php

    r3469357 r3476146  
    22/*
    33 * File: restlib.php
    4  * Date: 20250225
     4 * Date: 20250306
    55 * Author: James D. Low
    66 * URL: http://jameslow.com
     
    1414    public $route = 'wordpress';
    1515    public $is_wordpress = false;
     16    public $cookie = false;
    1617
    1718    /* Constructor */
    18     function __construct($apikeys = null, $cache = 60, $prefix = null, $route = 'wordpress') {
     19    function __construct($apikeys = null, $cache = 60, $prefix = null, $route = 'wordpress', $cookie = false) {
    1920        $this->apikeys = $apikeys ? (is_array($apikeys) ? $apikeys : array($apikeys)) : array();
    2021        $this->cache = $cache !== null ? $cache : $this->cache;
    2122        $this->prefix = $prefix !== null ? $prefix : get_class($this);
    2223        $this->route = $route !== null ? $route : $this->route;
     24        $this->cookie = $cookie;
    2325        $this->add_hooks();
    2426    }
     
    151153                }
    152154            }*/
    153             return is_user_logged_in(); //This works with oauth server
     155            if (is_user_logged_in()) {
     156                return true;
     157            }
     158            return $this->check_cookie();
    154159        } else {
    155160            return false;
    156161        }
     162    }
     163
     164    public function check_cookie() {
     165        if ($this->cookie) {
     166             if (empty($_COOKIE)) {
     167                return false; // No cookies sent
     168            }
     169
     170            // Look for the standard WordPress logged-in cookie
     171            $cookie = null;
     172            foreach ($_COOKIE as $key => $value) {
     173                if (strpos($key, 'wordpress_logged_in_') === 0) {
     174                    $cookie = $value;
     175                    break;
     176                }
     177            }
     178
     179            if (!$cookie) {
     180                return false; // No login cookie found
     181            }
     182
     183            // Parse cookie: format is user|expiration|token|hash
     184            list($username, $expiration, $token, $hmac) = explode('|', $cookie);
     185
     186            // Get the user by login
     187            $user = get_user_by('login', $username);
     188            if (!$user) {
     189                return false; // Invalid username
     190            }
     191
     192            // Validate cookie using WordPress core function
     193            require_once ABSPATH . 'wp-includes/pluggable.php';
     194            $check = wp_validate_auth_cookie($cookie, 'logged_in');
     195            if (!$check) {
     196                return false; // Cookie invalid
     197            }
     198
     199            // Cookie valid — set the current user and return true
     200            wp_set_current_user($user->ID);
     201
     202            return true;
     203        }
     204        return false;
    157205    }
    158206
     
    268316
    269317    public function add_hooks() {
    270         $this->is_wordpress = true;
    271         //add_filter('rest_url_prefix', function () { return 'api'; }); //Doing in htaccess since it lets us have plain /api/
    272         add_action('rest_api_init', array($this, 'register_rest_route'));
    273         add_filter('rest_pre_dispatch', array($this, 'rest_pre_dispatch'), 10, 3);
     318        if (function_exists('add_action')) {
     319            $this->is_wordpress = true;
     320            //add_filter('rest_url_prefix', function () { return 'api'; }); //Doing in htaccess since it lets us have plain /api/
     321            add_action('rest_api_init', array($this, 'register_rest_route'));
     322            add_filter('rest_pre_dispatch', array($this, 'rest_pre_dispatch'), 10, 3);
     323        }
    274324    }
    275325}
  • pageapp/trunk/pageapp.php

    r3469357 r3476146  
    44Plugin URI: https://wordpress.org/plugins/pageapp/
    55Description: Extensions to Wordpress wp-json for the PageApp API and mobile framework
    6 Version: 1.5.1
     6Version: 1.5.2
    77Author: PageApp
    88Author URI: https://www.thirteen.com/
  • pageapp/trunk/readme.txt

    r3469357 r3476146  
    44Requires at least: 4.0
    55Tested up to: 6.9
    6 Stable tag: 1.5.1
     6Stable tag: 1.5.2
    77License: MIT
    88Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=K6VKWB3HZB2T2&item_name=Donation%20to%20jameslow%2ecom&currency_code=USD&bn=PP%2dDonationsBF&charset=UTF%2d8
     
    2828
    2929== Changelog ==
     30
     31= 1.5.2 =
     32* Latest version of restlib library
    3033
    3134= 1.5.1 =
Note: See TracChangeset for help on using the changeset viewer.