Skip to content

Instantly share code, notes, and snippets.

@lordmatt
Last active August 11, 2019 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lordmatt/e6323ae00a39373841344ebb0b49b8fd to your computer and use it in GitHub Desktop.
Save lordmatt/e6323ae00a39373841344ebb0b49b8fd to your computer and use it in GitHub Desktop.
A work in progress abstraction that will allow user meta for all users including guests. Please contribute.
<?php
namespace storyteller;
/**
* This is a class designed to be used in WordPress Plugins and Themes where for
* whatever reason, the designer wishes to track a huge ammount of per-user data
* and keep it well ordered and accessable.
*
* I am posting this in an incomplete state; in the hope that further developers
* might have ideas that will finish the project for the good of the community.
*
* This is part of a wider project to use WordPress for interactive fiction as a
* form of storytelling. Thus the namespace.
*
* Datastore is an abstraction layer to store data within user_meta or elsewhere
* against guest_cookie (in site_meta) which requires garbage collection.
*
* datastore is intended to query the data quickly. For example via a number of
* shortcodes and assorted API yet to be created. It should be possible to ask a
* question about the data without any awareness beyond getting TRUE or FALSE as
* a return value. Thus, datastore::test_var(...);
*
* @package storyteller
* @subpackage storyteller.datastore
* @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
*
* VERSION: 0.1
*
* =======
* History
* =======
*
* 0.1 - the basic IO into and out oft he array has been covered but more work's
* needed for the store and load methods.
*
* =================
* Unsolved problems
* =================
*
* Where to put guest user data?
* -----------------------------
*
* All signs point to abusing existing API as a very bad idea.
* <https://wordpress.stackexchange.com/q/344323/109240>
*
* This may require the creation of a session meta_table of some kind.
*
*
* Useful reading
* --------------
* <https://codex.wordpress.org/Metadata_API>
*
* =======
* License
* =======
*
* This file is part of storyteller/datastore.
*
* datastore 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.
*
* datastore 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
* datastore. If not, see http://www.gnu.org/licenses/.
*/
class datastore {
private $data=array();
protected function update_flag_group_count($group){
$count = count($this->data['flags'][$group]['flags']);
$this->data['flags'][$group]['count'] = $count;
}
public function set_flag($flag,$group='global'){
$this->data['flags'][$group]['flags'][$flag]=TRUE;
$this->update_flag_group_count($group);
}
public function get_flag_group_count($group='global'){
$this->update_flag_group_count($group);
return $this->data['flags'][$group]['count'];
}
public function add_to_var($var,$change=1,$group='global'){
if(!isset($this->data['vars'][$group][$var])){
$this->data['vars'][$group][$var]=0;
}
$old = $this->data['vars'][$group][$var];
$this->data['vars'][$group][$var]=$old+$change;
}
public function get_var($var,$group='global'){
if(!isset($this->data['vars'][$group][$var])){
$this->data['vars'][$group][$var]=0;
}
return $this->data['vars'][$group][$var];
}
public function flag_is_set($flag,$group='global'){
return isset($this->data['flags'][$group]['flags'][$flag]);
}
public function test_var($var,$val,$comp='=',$group='global'){
// $comp <= < = > >= !=
switch (trim($comp)) {
case "!=": return $var != $val;
case ">=": return $var >= $val;
case "<=": return $var <= $val;
case ">": return $var > $val;
case "<": return $var < $val;
case '==':
case '===':
case "=": return $var == $val;
default: return false;
}
}
public function load_data(){
# if user logged in
# load user_meta('story')
# if guest , get cookie key , find store from ????
# I think that I have cracked it: <https://gist.github.com/lordmatt/3bd8f7787fbbe0950f9228dec92f2f8a>
}
public function save_data(){
# if user logged in
# set user_meta('story')
# if guest , get cookie key , find store in ???? update ????
# I think that I have cracked it: <https://gist.github.com/lordmatt/3bd8f7787fbbe0950f9228dec92f2f8a>
}
}
@lordmatt
Copy link
Author

I think I have an answer in the form of a one-file plugin that I wrote called Guest Meta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment