Skip to content

Commit

Permalink
Merge pull request #132 from TangibleInc/feature/object-cache-preproc…
Browse files Browse the repository at this point in the history
…essed-template-posts

Start optional object cache for parsed and preprocessed template posts
  • Loading branch information
eliot-akira authored Oct 24, 2024
2 parents a03208a + 4736f29 commit a65d68e
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 6 deletions.
22 changes: 20 additions & 2 deletions admin/settings/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
'default_value' => false,
],

[
'name' => 'object_cache_processed_template_post',
'field_type' => 'checkbox',
'label' => 'Object cache for parsed and pre-processed template posts (Experimental)',
'default_value' => false,
'beta' => true,
],

[
'name' => 'sass_in_browser',
'field_type' => 'checkbox',
Expand Down Expand Up @@ -106,13 +114,23 @@ function get_settings( $field_name = null, $default_value = null ) {
return $settings;
}

function set_settings( $settings ) {
// Alias
function get_setting( $field_name = null, $default_value = null ) {
return get_settings($field_name, $default_value);
}

function set_settings( $settings ) {
update_option( template_system::$state->settings_key, $settings );

return $settings;
}

function set_setting( $key, $value ) {
$settings = template_system\get_settings();
if (!is_array($settings)) $settings = [];
$settings[ $key ] = $value;
return template_system\set_settings( $settings );
}

function settings_page() {
require_once __DIR__.'/page.php';
};
Expand Down
1 change: 1 addition & 0 deletions admin/settings/page.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace tangible\template_system;

use tangible\api;
use tangible\template_system;

Expand Down
73 changes: 73 additions & 0 deletions admin/template-post/cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Object cache for pre-processed template posts
*
* > Use the Transients API if you need to guarantee that your data will be cached. If persistent caching is configured, then the transients functions will use the wp_cache_* functions described in this document. However if persistent caching has not been enabled, then the data will instead be cached to the options table.
*
* @see https://developer.wordpress.org/apis/transients
* @see https://developer.wordpress.org/reference/classes/wp_object_cache/
* @see https://developer.wordpress.org/advanced-administration/performance/cache/
*/
namespace tangible\template_system;

use tangible\html;
use tangible\template_system;

function is_processed_template_post_cache_enabled() {
// See /admin/settings
return template_system\get_setting('object_cache_processed_template_post');
}

function get_template_post_cache_key($post) {
$type = $post->post_type;
$id = $post->ID;
return 'tangible_template_post_' . $type . '_' . $id;
}

function get_processed_template_post_with_cache( $post ) {

$cache_key = template_system\get_template_post_cache_key($post);

// \tangible\see( $post);

if (($processed = get_transient($cache_key))!==false) {
return $processed;
}

return template_system\process_and_cache_template_post($post, $cache_key);
}

/**
* Process and cache post content
*/
function process_and_cache_template_post( $post, $cache_key = null ) {

if (empty($cache_key)) {
$cache_key = template_system\get_template_post_cache_key($post);
}

$metadata = [
'time' => gmdate('Y-m-d H:i:s'),
'type' => $post->post_type,
'id' => $post->ID
];
$prefix = '<!-- Parsed and cached: ' . json_encode($metadata) . " -->\n";
$content = html\parse( $prefix . ($post->post_content) );
$processed = [
'parsed_content' => $content,
// ..Possibly other pre-processed template data
] + $metadata;

set_transient($cache_key, $processed);

return $processed;
}

/**
* Flush cache
*/
function delete_processed_template_post_cache( $post ) {
return delete_transient(
template_system\get_template_post_cache_key($post)
);
}
1 change: 1 addition & 0 deletions admin/template-post/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

require_once __DIR__ . '/cache.php';
require_once __DIR__ . '/data.php';
require_once __DIR__ . '/fields.php';
require_once __DIR__ . '/save.php';
Expand Down
28 changes: 24 additions & 4 deletions admin/template-post/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @see /admin/template-post/script
* @see /admin/template-assets/variable
*/
use tangible\html;
use tangible\template_system;

$plugin->render_template_post = function(
Expand All @@ -16,6 +17,9 @@

$is_post = true;

// Capture any style, script, or unexpected output
ob_start();

// Direct post data
if (is_array($post) && isset($post['content'])) {

Expand All @@ -24,12 +28,28 @@

} else {
if (is_numeric( $post )) $post = get_post( $post );
if ( ! is_a( $post, 'WP_Post' )) return;
if ( ! is_a( $post, 'WP_Post' )) {
ob_end_clean();
return;
}

$content = $post->post_content;
}

// Capture any style, script, or unexpected output
ob_start();
/**
* Post content - Optionally processed and cached
* Passed to `$html->render_with_catch_exit()` which accepts string
* or parsed nodes.
* @see ./cache
*/
if (template_system\is_processed_template_post_cache_enabled()) {

// Delete cache for testing
// template_system\delete_processed_template_post_cache($post);

$processed = template_system\get_processed_template_post_with_cache( $post );
$content = $processed['parsed_content'] ?? $content;
}
}

/**
* Local variable scope
Expand Down
15 changes: 15 additions & 0 deletions admin/template-post/save.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
use tangible\template_system;

$plugin->is_saving_template_post = false;

Expand Down Expand Up @@ -97,6 +98,13 @@
if ( isset( $fields['style'] ) ) {
$plugin->maybe_save_style_compiled( $post, $fields['style'] );
}

/**
* Optionally pre-process template post on save
*/
if (template_system\is_processed_template_post_cache_enabled()) {
template_system\process_and_cache_template_post( $post );
}
}

return $result;
Expand Down Expand Up @@ -146,6 +154,13 @@
}
}

/**
* Optionally pre-process template post on save
*/
if (template_system\is_processed_template_post_cache_enabled()) {
template_system\process_and_cache_template_post( $post );
}

}, 10, 3 );

/**
Expand Down

0 comments on commit a65d68e

Please sign in to comment.