我可以为理论用户0设置用户META吗?

时间:2019-08-04 作者:Matthew Brown aka Lord Matt

我有一个用例,需要为包括来宾在内的所有用户存储user\\u meta。反序列化为一个值只是一个相当复杂的数组。我的直觉是尝试为user=0(理论上已注销的用户)存储一个guest user\\u meta数组,其中包含一个cookie键值和一个date值,这样我就可以清理过期的guest meta。这样做的想法是,如果客人创建帐户或登录,就可以跨多个服务器复制数据。

WordPress会允许我这样做吗?我有什么理由不这样做(或者这样做有任何危险)?我有没有考虑过更好的方法?

Update: Here is a gist 我要去哪里。看起来滥用user=0不是一个好主意。如果我有更好的想法,我会更新要点。

Update 2: 我通过将元数据系统扩展到客人身上解决了这个问题。它100%使用内部WordPress方法,并按照相同的元数据命名约定添加了四个新函数。Here is my untested draft.

1 个回复
最合适的回答,由SO网友:Matthew Brown aka Lord Matt 整理而成

事实证明(通过评论),(ab)使用用户0是一个非常糟糕的主意。然而,WordPress的构建允许新事物获取元表。这让我将来宾元作为插件。

在上提供GitHub as a gist 以及以下当前形式。我必须指出,这是一个概念性的解决方案,而不是经过充分测试的解决方案。请谨慎使用我的代码(背景原则非常合理)。

  <?php
  /*
  Plugin Name: Guest Meta
  Plugin URI: <https://gist.github.com/lordmatt/3bd8f7787fbbe0950f9228dec92f2f8a>
  Description: Enable storing meta data for guests. Keeps cookies small and simple.
  Author: Matthew Brown
  Author URI: http://matthewdbrown.authorbuzz.co.uk/
  Version: 1.1.0
  License: GPLv3 or later
  Text Domain: guest_meta
  */
  /**
   * This is an idea I had to store meta data about guest users as well as regular
   * registered users. In theory, installing this as a plugin will give your other
   * plugins and themes the ability to store guest metadata against a cookie value
   * and not in the cookie which means the data is tamper proof and more trustable
   * in the main.
   * 
   * This untested draft assumes everything I read in the WordPress\' documentation 
   * is 100% accurate.
   * 
   * @package     guest_meta
   * @author      Matthew Brown <https://twitter.com/lordmatt>
   * @copyright   (C) 2019 Matthew Brown.
   * @license     http://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 
   * @used-by       storyteller.datastore <https://gist.github.com/lordmatt/e6323ae00a39373841344ebb0b49b8fd> 
   * 
   * VERSION: 1.1.0
   * 
   * =======
   * History
   * =======
   * 1.1.0 - Added pass FALSE or 0 as guest-ID and the ID will autoload
   * 1.0.0 - First version. Not tested, yet.
   * 
   * =======
   * License
   * =======
   * 
   * This file is part of guest_meta.
   * 
   * guest_meta is free software: you can redistribute it and/or modify it under 
   * the terms of the GNU General Public License as published by the Free Software 
   * Foundation, either version 2 of the License, or (at your option) any later 
   * version.
   * 
   * guest_meta is distributed in the hope that it will be useful, but WITHOUT ANY 
   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   * 
   * You should have received a copy of the GNU General Public License along with 
   * guest_meta. If not, see http://www.gnu.org/licenses/.
   */
  function guest_meta_install(){
      global $wpdb;
      $table_name = $wpdb->prefix . "guestmeta"; 
      $charset_collate = $wpdb->get_charset_collate();
      $sql = "CREATE TABLE $table_name (
        `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        `object_id` bigint(20) unsigned NOT NULL DEFAULT \'0\',
        `meta_key` varchar(255) DEFAULT NULL,
        `meta_value` longtext,
        `ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY  (meta_id)
      ) $charset_collate;";
      $sql2 = "ALTER TABLE $table_name
        ADD KEY `comment_id` (`object_id`),
        ADD KEY `meta_key` (`meta_key`(191));";
      require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
      dbDelta( $sql );
      dbDelta( $sql2 );
  }
  register_activation_hook( __FILE__, \'guest_meta_install\' );
  add_action( \'init\', \'guest_meta_cookie\' );
  function guest_meta_cookie() {
      setcookie( \'guest_meta\', guest_meta_get_object_id(), 30 * DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
  }
  function guest_meta_get_object_id(){
      if(!isset($_COOKIE[\'guest_meta\'])) {
          $_COOKIE[\'guest_meta\'] = guest_meta_make_id();
      }
      return $_COOKIE[\'guest_meta\'];
  }
  function guest_meta_make_id(){
      global $wpdb;
      $table_name = $wpdb->prefix . "guestmeta";
      guest_meta_drop_old_rows(); // clean up old stuff first
      $results = $wpdb->get_results( "SELECT MAX(object_id) AS almost FROM {$table_name};", OBJECT );
      $newish = $results->almost+1;
      return $newish;
  }
  function guest_meta_drop_old_rows(){
      global $wpdb;
      $table_name = $wpdb->prefix . "guestmeta";
      $sql = "DELETE FROM $table_name WHERE `ts` < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 28 DAY))";
      require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
      dbDelta( $sql );
  }
  function add_guest_meta($object_id=FALSE, $meta_key=FALSE, $meta_value=FALSE, $unique=FALSE){
      if($object_id==FALSE||$object_id<1){
          $object_id = guest_meta_get_object_id();
      }
      return add_metadata( \'guest\', $object_id, $meta_key, $meta_value, $unique );
  }
  function get_guest_meta($object_id=FALSE, $meta_key=FALSE, $single=FALSE){
      if($object_id==FALSE||$object_id<1){
          $object_id = guest_meta_get_object_id();
      }
      get_metadata(\'guest\', $object_id, $meta_key, $single);
  }
  function update_guest_meta($object_id=FALSE, $meta_key=FALSE, $meta_value=FALSE, $prev_value=\'\'){
      if($object_id==FALSE||$object_id<1){
          $object_id = guest_meta_get_object_id();
      }
      update_metadata( \'guest\', $object_id, $meta_key, $meta_value, $prev_value );
  }
  function delete_guest_meta($object_id=FALSE, $meta_key=FALSE, $meta_value=FALSE, $delete_all=FALSE){
      if($object_id==FALSE||$object_id<1){
          $object_id = guest_meta_get_object_id();
      }
      delete_metadata ( \'guest\', $object_id, $meta_key, $meta_value, $delete_all );
  }

相关推荐