Created
August 2, 2014 21:36
-
-
Save tatemz/9c960357b0946f898d3e to your computer and use it in GitHub Desktop.
How to Optimize Your WordPress Metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Page_Object { | |
public $ID; | |
public $original_post; | |
public $data = array(); | |
public function __construct( $id ) { | |
if ( $id <= 0 || !is_int( $id ) ) { | |
return false; | |
} | |
$this->original_post = get_post( $id ); | |
$this->ID = $this->original_post->ID; | |
add_filter( 'get_post_metadata', array( $this, 'get_meta' ), -1, 4 ); | |
add_filter( 'update_post_metadata', array( $this, 'update_meta' ), -1, 5 ); | |
add_action( 'shutdown', array( $this, 'sync_data' ) ); | |
} | |
public function get_meta( $value, $page_id, $meta_key, $single ) { | |
if ( $page_id != $this->ID ) { | |
return $value; | |
} | |
if ( !array_key_exists( $meta_key, $this->data ) ) { | |
remove_filter( 'get_post_metadata', array( $this, 'get_meta' ), -1, 4 ); | |
$this->data[ $meta_key ] = get_metadata( 'post', $this->ID, $meta_key, false ); | |
add_filter( 'get_post_metadata', array( $this, 'get_meta' ), -1, 4 ); | |
} elseif ( !$meta_key ) { | |
$this->data = get_metadata( 'post', $this->ID ); | |
} | |
$value = $this->data[ $meta_key ]; | |
if ( $single && is_array( $value ) ) { | |
return $value[0]; | |
} else { | |
return $value; | |
} | |
} | |
public function update_meta( $value, $page_id, $meta_key, $meta_value, $prev_value ) { | |
if ( $page_id != $this->ID ) { | |
return $value; | |
} | |
$this->data[ $meta_key ] = $meta_value; | |
return true; | |
} | |
public function sync_data() { | |
remove_filter( 'update_post_metadata', array( $this, 'update_meta' ), -1, 5 ); | |
foreach ( $this->data as $meta_key => $meta_value ) { | |
update_metadata( 'post', $this->ID, $meta_key, $meta_value ); | |
} | |
add_filter( 'update_post_metadata', array( $this, 'update_meta' ), -1, 5 ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WCFay 2014 | |
Description: Organizing and optimizing custom metadata. | |
Plugin URI: http://wcfay.com | |
Author: A. Tate Barber | |
Author URI: http://tatemz.com | |
Version: 1.0 | |
*/ | |
class WCFay { | |
public function __construct() { | |
// Register install function to create our tables | |
register_activation_hook( __FILE__, array( $this, 'create_meta_tables' ) ); | |
// init our plugin | |
add_action( 'init', array( $this, 'init' ) ); | |
// filter our content to show session meta | |
add_filter( 'the_content', array( $this, 'display_sessionmeta' ) ); | |
} | |
public function init() { | |
global $wpdb; | |
// Add our new tables to the $wpdb | |
$wpdb->sessions = $wpdb->prefix . 'sessions'; | |
$wpdb->tables[] = 'sessions'; | |
$wpdb->sessionmeta = $wpdb->prefix . 'sessionmeta'; | |
$wpdb->tables[] = 'sessionmeta'; | |
} | |
public function create_meta_tables() { | |
global $wpdb; | |
$collate = ''; | |
// Prepare dbDelta to create some meta tables! | |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
// set $collate... | |
if ( $wpdb->has_cap( 'collation' ) ) { | |
if ( ! empty($wpdb->charset ) ) { | |
$collate .= "DEFAULT CHARACTER SET $wpdb->charset"; | |
} | |
if ( ! empty($wpdb->collate ) ) { | |
$collate .= " COLLATE $wpdb->collate"; | |
} | |
} | |
// create table mysql query | |
$new_tables = " | |
CREATE TABLE {$wpdb->prefix}sessions ( | |
session_id bigint(20) NOT NULL auto_increment, | |
session_name longtext NOT NULL DEFAULT '', | |
session_speaker longtext NOT NULL DEFAULT '', | |
PRIMARY KEY (session_id) | |
) $collate; | |
CREATE TABLE {$wpdb->prefix}sessionmeta ( | |
meta_id bigint(20) NOT NULL auto_increment, | |
session_id bigint(20) NOT NULL, | |
meta_key varchar(255) NULL, | |
meta_value longtext NULL, | |
PRIMARY KEY (meta_id), | |
KEY session_id (session_id), | |
KEY meta_key (meta_key) | |
) $collate;"; | |
// Run that query | |
dbDelta( $new_tables ); | |
} | |
public function get_session( $id = 0 ) { | |
global $wpdb; | |
if ( $id <= 0 ) { | |
return false; | |
} | |
// Get our session from the database | |
$session = $wpdb->get_row( $wpdb->prepare( " | |
SELECT * | |
FROM {$wpdb->sessions} | |
WHERE session_id = %d | |
", $id ) ); | |
return $session; | |
} | |
public function display_sessionmeta( $content ) { | |
// Only modify this page's content | |
if ( get_the_title() == 'How to Optimize Your WordPress Metadata' ) { | |
// There's probably a better way to get the session_id ... post_meta? | |
$session_id = 1; | |
// Get the session | |
$session = $this->get_session( $session_id ); | |
// Get the session metadata | |
$session_meta = get_metadata( 'session', $session_id ); | |
if ( $session_meta ) { | |
$content .= '<table><thead><th>meta_key</th><th>meta_value</th></thead><tbody>'; | |
// Add our metadata to a display table | |
foreach ( $session_meta as $meta_key => $meta_value ) { | |
$content .= sprintf( '<tr><td>%s</td><td>%s</td></tr>', $meta_key, $meta_value[0] ); | |
} | |
$content .= '</tbody></table>'; | |
} | |
} | |
$this->test_meta_optimization(); | |
// Return the original or modified content | |
return $content; | |
} | |
public function test_meta_optimization() { | |
include_once( 'class-page-object.php' ); | |
$entries = 3000; | |
$our_optimal_page = new Page_Object( get_the_id() ); | |
$id = get_the_id(); | |
// $id = $our_optimal_page->ID; | |
$time_start = microtime( true ); | |
for ( $i = 0; $i < $entries ; $i++ ) { | |
update_post_meta( $id, '_wcfay_meta_' . $i, 'optimized-GOOD' ); | |
} | |
$time_end = microtime( true ); | |
$time = $time_end - $time_start; | |
d( 'Update ' . $entries . ' meta data entries: ' . $time ); | |
} | |
} | |
$GLOBALS['wcfay'] = new WCFay; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment