Skip to content

Commit

Permalink
Cache queries in transients (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion authored Oct 16, 2023
2 parents 20fe74b + 9cb3cd7 commit a3045e3
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 92 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ft-issue-gardening.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Issue gardening for figuren.theater

on:
issues:
types:
- opened
jobs:
call-workflow-ft-issue-gardening:
uses: figuren-theater/.github/.github/workflows/issue-gardening.yml@main
secrets: inherit
99 changes: 68 additions & 31 deletions ft-network-sourcelinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
* @package Ft_Network_Sourcelinks
*/

const TRANSIENT_KEY = 'ft_ns_urls';


/**
* This class handles all the major use-cases for external URLs in WordPress
*
Expand Down Expand Up @@ -81,11 +84,11 @@ public static function get_subscribed_events() : Array



// TEMP DISABLED, until sync works, read on down the code
// @todo #32 Re-ENABLE - TEMP DISABLED; until sync works, read on down the code
// 'after_setup_theme' => 'enable__on_setup_theme', // working


//
//
// 'admin_menu' => 'enable__on_admin', //
);
}
Expand All @@ -95,13 +98,13 @@ public static function get_subscribed_events() : Array
public function enable() : void
{

\load_plugin_textdomain(
'ft-network-sourcelinks',
\load_plugin_textdomain(
'ft-network-sourcelinks',
false,
dirname( \plugin_basename( __FILE__ ) ) . '/languages'
// $this->plugin_dir_path . '/languages'
);


// 1. Register our post_type 'ft_link'
\Figuren_Theater\API::get('PT')->add(
Expand Down Expand Up @@ -146,17 +149,17 @@ public function custom_query_block_attributes( $parsed_block ) {
if ( 'core/query' === $parsed_block['blockName'] ) {
// If the block has a `taxQuery` attribute, then find the corresponding cat ID and set the `categoryIds` attribute.
// TODO: support multiple?
if (
if (
isset( $parsed_block[ 'attrs' ][ 'query' ][ 'search' ] )
&&
strpos( $parsed_block[ 'attrs' ][ 'query' ][ 'search' ], 'link_category:' )
) {
// die(var_dump($parsed_block));
#if ( is_string( $parsed_block[ 'attrs' ][ 'query' ][ 'taxQuery' ]['link_category'][0] ) )
$_link_cat = array_flip( explode(':', $parsed_block[ 'attrs' ][ 'query' ][ 'search' ] ) );
$tax_term = get_term_by(
$tax_term = get_term_by(
'slug',
$_link_cat[0],
'link_category',
Expand All @@ -176,13 +179,13 @@ public function custom_query_block_attributes( $parsed_block ) {
public function i18n()
{

\load_plugin_textdomain(
'ft-network-sourcelinks',
\load_plugin_textdomain(
'ft-network-sourcelinks',
false,
dirname( \plugin_basename( __FILE__ ) ) . '/languages'
// $this->plugin_dir_path . '/languages'
);

/*
\wp_set_script_translations(
'figurentheater-figurentheater-production-duration-editor-script',
Expand All @@ -198,15 +201,16 @@ public function i18n()
*/
}



/**
* @todo TEMPORARILY DISABLED
* @todo #32
* TEMPORARILY DISABLED
* but needed when sync/importing starts
* so we can assign different post_formats based on the
* so we can assign different post_formats based on the
* source_links post_format
*
*
*
* Note that you must call 'add_theme_support()' before the init hook gets called!
*
* A good hook to use is the after_setup_theme hook.
Expand All @@ -224,18 +228,11 @@ public function enable__on_admin() : void
{
$this->debug();
}*/


public static function get_urls() : Array
{
// minimal caching
if( isset( self::$urls ) && !empty( self::$urls ) )
return self::$urls;

public static function query_urls() : Array {
$ft_query = \Figuren_Theater\FT_Query::init();

//
return self::$urls = $ft_query->find_many_by_type(
return $ft_query->find_many_by_type(
Post_Types\Post_Type__ft_link::NAME,
'publish',
[
Expand All @@ -252,9 +249,49 @@ public static function get_urls() : Array
'update_post_term_cache' => false,

// 'suppress_filters' => true,
'no_found_rows' => true, // Useful when pagination is not needed.
'posts_per_page' => 25,
]
);
}

/**
* Get stored labels from the database.
*
* Uses transients to cache simplified WP_Query results.
*
* @return Array An array of Label objects.
*/
public static function get_stored_urls() : array {

// Check if the value is already stored.
$stored_urls = \get_transient( TRANSIENT_KEY );

if ( empty( $stored_urls ) ) {

// Retrieve posts from DB.
$stored_urls = static::query_urls();

// Store for long,
// because this will beflushed with every new (and updated) 'wp_block' post.
\set_transient(
TRANSIENT_KEY,
$stored_urls,
\WEEK_IN_SECONDS
);
}

return $stored_urls;
}


public static function get_urls() : Array
{
// minimal caching
if( isset( self::$urls ) && !empty( self::$urls ) )
return self::$urls;

return self::$urls = static::get_stored_urls();
}


Expand All @@ -274,13 +311,13 @@ protected function debug()
{

# \do_action( 'qm/debug', $this->get_urls() );

# \do_action( 'qm/info', '{fn}: {value}', [
# 'fn' => "get_taxonomy( 'link_category' )",
# 'value' => var_export( \get_taxonomy( 'link_category' ), true ),
# ] );
#

# \do_action( 'qm/info', '{fn}: {value}', [
# 'fn' => "\get_post_type( 'link' )",
# 'value' => var_export( \get_post_type( 'link' ), true ),
Expand Down Expand Up @@ -316,20 +353,20 @@ protected function debug()


// runs once, on activation
//
//
// after reading a loooong thread at
// https://core.trac.wordpress.org/ticket/14170
// I know now, that we should follow a new path
// because of multisite vs. register_activation_hook
//
//
// let us now do this from within the taxonomy,
// when visiting the links edit.php
/*
\register_activation_hook( __FILE__, function(){
// create 'link_category' taxonomy terms
// create first 'ft_link' using site_url()
// add_option('default_link_category')
} );*/
14 changes: 7 additions & 7 deletions inc/Network/Options/Preset__wpseo_social.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ function( $v, $k ) use ( &$_urls, $_social_links ) {
// clean up url as pseudo label
// so this makes
// https://instagram.com -> instagram
// @TODO

// @todo #25
// we have this string-cleaning now 3 times
//
//
// - plugins\ft-network-sourcelinks\src\block-editor\blocks\filtered-links\index.php#L90
// - plugins\ft-network-sourcelinks\inc\Network\Post_Types\Post_Type__ft_link.php#L330
// - plugins\ft-network-sourcelinks\inc\Network\Options\Preset__wpseo_social.php#L142
//
//
//
//
// cleanup html
// especially from wp_auto_p
$_url = \wp_kses( $v->post_content, [] );
Expand Down Expand Up @@ -136,7 +136,7 @@ function( $v, $k ) use ( &$_urls, $_social_links ) {
/**
* [pre_option_wpseo_social description]
*
* 2022.06.08: Added Array|bool for situations´,
* 2022.06.08: Added Array|bool for situations´,
* where 'wpseo_social' is not set and/or not filtered yet.
* Just to prevent fatals ;).
*
Expand Down Expand Up @@ -179,7 +179,7 @@ public function pre_option_wpseo_social( array|bool $wpseo_social ) : array {

if ( isset( $_urls['pinterest'] ) && ( !isset( $wpseo_social['pinterest_url'] ) || empty( $wpseo_social['pinterest_url'] ))) {
$wpseo_social['pinterest_url'] = $_urls['pinterest'];
// @TODO add handling for post_meta 'pinterestverify'
// @TODO #26 add handling for post_meta 'pinterestverify'
}

if ( isset( $_urls['twitter'] ) && ( !isset( $wpseo_social['twitter_site'] ) || empty( $wpseo_social['twitter_site'] ))) {
Expand Down
42 changes: 23 additions & 19 deletions inc/Network/Post_Types/Post_Type__ft_link.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Figuren_Theater\inc\EventManager;
use Figuren_Theater\Network\Taxonomies;
use Figuren_Theater\Network\Users;
use Figuren_Theater\Network\Sources;

use WP_Post;

Expand Down Expand Up @@ -72,7 +73,7 @@ public static function get_subscribed_events() : array {
public function get_post_data() : Array
{
return [
'post_author' => ( isset( $this->arguments['user_id'] ) ) ? $this->arguments['user_id'] : Users\ft_bot::id(),
'post_author' => ( isset( $this->arguments['user_id'] ) ) ? $this->arguments['user_id'] : Users\ft_bot::id(),
'post_title' => $this->arguments['new_post_title'],
'post_content' => $this->arguments['new_post_content'],
'post_status' => 'publish', // start with private, switch to publish on later point
Expand All @@ -96,9 +97,9 @@ public function get_post_meta() : Array
}

/**
* Get all taxonomies and its terms (IDs)
* as multidimesnional array,
* properly prepared to be used
* Get all taxonomies and its terms (IDs)
* as multidimesnional array,
* properly prepared to be used
* as part of wp_insert_post.
*
* Structural Example:
Expand Down Expand Up @@ -184,10 +185,10 @@ protected function register_post_type__default_args() : Array

/**
* Localiced Labels
*
* ExtendedCPTs generates the default labels in English for your post type.
* If you need to allow your post type labels to be localized,
* then you must explicitly provide all of the labels (in the labels parameter)
*
* ExtendedCPTs generates the default labels in English for your post type.
* If you need to allow your post type labels to be localized,
* then you must explicitly provide all of the labels (in the labels parameter)
* so the strings can be translated. There is no shortcut for this.
*
* @source https://github.com/johnbillion/extended-cpts/pull/5#issuecomment-33756474
Expand Down Expand Up @@ -326,15 +327,15 @@ public static function get_readable_link( WP_Post|null $post = null ) {
if( null === $post )
global $post;

// @TODO

// @todo #25
// we have this string-cleaning now 3 times
//
//
// - plugins\ft-network-sourcelinks\src\block-editor\blocks\filtered-links\index.php#L90
// - plugins\ft-network-sourcelinks\inc\Network\Post_Types\Post_Type__ft_link.php#L330
// - plugins\ft-network-sourcelinks\inc\Network\Options\Preset__wpseo_social.php#L142
//
//
//
//
// cleanup html
// especially from wp_auto_p
$_url = \wp_kses( $post->post_content, [] );
Expand All @@ -343,7 +344,7 @@ public static function get_readable_link( WP_Post|null $post = null ) {
// last check
if ( ! $_url = \esc_url( $_url ) )
return '';

$_short_url = \url_shorten( $_url );
return "<a href='$_url'>$_short_url</a>";
}
Expand Down Expand Up @@ -401,7 +402,7 @@ public function permalink_source_url( string $permalink, WP_Post $post ) : strin
if ( static::NAME !== $post->post_type ) {
return $permalink;
}

$url = \esc_url( $post->post_content );
return $url ?? $permalink;
}
Expand All @@ -420,8 +421,8 @@ public static function get_instance()


/**
* @todo Everything.
*
* @todo #27 Refactor __is_ft_link_privacy_relevant()
*
* [__is_ft_link_privacy_relevant description]
*
* @package [package]
Expand All @@ -444,10 +445,13 @@ function ( string $service_url ) use ( $url_short ) {
return false;
}
*/

// Create a simple function to delete our transient
public static function delete_transient() {
// Has all our links with 'OWN' taxonomy-term.
\delete_transient( 'ft_link_own_q' );
// Has all our links.
\delete_transient( Sources\TRANSIENT_KEY );
}


Expand All @@ -460,7 +464,7 @@ public function get_queried_urls() : array {
}
return $ft_link_query;
}

public function query_urls() : array {

// get all IDs of 'Links' in our 'Own' 'link_category' now,
Expand Down
Loading

0 comments on commit a3045e3

Please sign in to comment.