GithubHelp home page GithubHelp logo

inpsyde / menu-cache Goto Github PK

View Code? Open in Web Editor NEW
72.0 11.0 5.0 38 KB

Easily cache rendered menus using the Transients API.

Home Page: https://packagist.org/packages/inpsyde/menu-cache

License: GNU General Public License v2.0

PHP 100.00%
cached-menus transients-api wordpress-plugin wordpress

menu-cache's Introduction

Inpsyde Menu Cache

Version Status Build Downloads License

Easily cache rendered menus using the Transients API.

Introduction

The wp_nav_menu() function calls _wp_menu_item_classes_by_context(), which again, depending on the context, calls wp_get_object_terms(), which is not cached, multiple times. With lots of taxonomies, terms and menu items, this can lead to a fair amount of (totally redundant) database queries.

This plugin lets you cache rendered menus (assuming they don't have dynamic components) for re-use.

Installation

Install with Composer:

$ composer require inpsyde/menu-cache

Requirements

This package requires PHP 5.4 or higher.

Usage

Once activated, the plugin caches all menus, by default for five minutes. The menus to be cached, as well as the expiration, can be customized by using the appropriate filter.

Filters

Need to customize anything? Just use the provided filters.

Please note: when you use the below class constants for the filters, make sure that the class is actually available. This can be as easy as guarding your customization with if ( class_exists( 'Inpsyde\MenuCache\MenuCache' ) ).

Inpsyde\MenuCache\MenuCache::FILTER_EXPIRATION

The Inpsyde\MenuCache\MenuCache::FILTER_EXPIRATION filter allows you to define the expiration for all cached menus. The default value is 300, which is 5 minutes.

Arguments:

  • int $expiration: Expiration in seconds.

Usage Example:

<?php

add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_EXPIRATION, function () {

	// Cache menus for 10 minutes.
	return 600;
} );

Inpsyde\MenuCache\MenuCache::FILTER_KEY

The Inpsyde\MenuCache\MenuCache::FILTER_KEY filter allows you to customize the cache key on a per-menu basis. The default value is constructed using a predfined prefix and the MD5 hash of the serialized args object.

Arguments:

  • string $key: Current key.
  • object $args: Menu args.

Usage Example:

<?php

add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_KEY, function ( $key, $args ) {

	// Construct the key based on the theme location only.
	return "cached_menu_{$args->theme_location}";
}, 10, 2 );

Inpsyde\MenuCache\MenuCache::FILTER_KEY_ARGUMENT

The Inpsyde\MenuCache\MenuCache::FILTER_KEY_ARGUMENT filter allows you to customize the menu argument name that is used to store the menu key (for later look-up).

Arguments:

  • string $key_argument: Current key argument name.

Usage Example:

<?php

add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_KEY_ARGUMENT, function () {

	// Use argument name with a leading underscore.
	return '_menu_key';
} );

Inpsyde\MenuCache\MenuCache::FILTER_SHOULD_CACHE_MENU

The Inpsyde\MenuCache\MenuCache::FILTER_SHOULD_CACHE_MENU filter allows you to customize caching on a per-menu basis.

Arguments:

  • bool $key: Whether or not the menu should be cached.
  • object $args: Menu args.

Usage Example:

<?php

add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_SHOULD_CACHE_MENU, function ( $should_cache_menu, $args ) {

	// Cache all menus for a bunch of dynamically created theme locations.
	return 0 === strpos( $args->theme_location, 'some_prefix_here_' );
}, 10, 2 );

Inpsyde\MenuCache\MenuCache::FILTER_THEME_LOCATIONS

The Inpsyde\MenuCache\MenuCache::FILTER_THEME_LOCATIONS filter allows you to define theme locations to restrict caching menus to.

Arguments:

  • string|string[] $theme_locations: One or more theme locations.

Usage Example:

<?php

add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_THEME_LOCATIONS, function () {

	// Cache the menus for the "primary" theme location only.
	return 'primary';
} );

License

Copyright (c) 2017 Inpsyde GmbH

This code is licensed under the GPLv2+ License.

menu-cache's People

Contributors

bueltge avatar gmazzap avatar jrfnl avatar tfrommen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

menu-cache's Issues

Proper way to install menu-cache plugin?

I created a composer.json in my wordpress directory (/var/www/mysite.com):

{
    "require": {
        "inpsyde/menu-cache": "^1.3"
    }
}

When I run composer install, the plugin is installed in /var/www/mysite.com/vendor instead of /var/www/mysite.com/wp-content/plugins.

What is the correct way to install this plugin?

1.4.0 breaks deployment

Version Information

  • PHP: 7.1
  • WordPress: 4.8.2

Steps to Reproduce

  1. Set up a composer-based WP installation
  2. Require menu-cache 1.4.0
  3. Deploy

What I Expected

Deployment to Mittwald through Envoyer to go through without issue

What Happened Instead

Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 106 installs, 0 updates, 0 removals
  - Installing composer/installers (v1.4.0) Loading from cache
  - Installing johnpbloch/wordpress-core-installer (1.0.0) Loading from cache

 [...]

  - Installing squizlabs/php_codesniffer (3.1.1) Loading from cache
  - Installing wp-coding-standards/wpcs (0.13.1) Loading from cache
  - Installing wimg/php-compatibility (8.0.1) Loading from cache
  - Installing dealerdirect/phpcodesniffer-composer-installer (v0.4.3) Loading from cache
Plugin installation failed, rolling back
  - Removing dealerdirect/phpcodesniffer-composer-installer (v0.4.3)

                                                                                                                                           
  [Symfony\Component\Process\Exception\ProcessFailedException]                                                                             
  The command "'/files/deployments/releases/20171024101214/public/wp-content/vendor/bin/phpcs' '--config-show' 'installed_paths'" failed.  
  Exit Code: 3(Unknown error)                                                                                                              
  Working directory: /files/deployments/releases/20171024101214                                                                            
  Output:                                                                                                                                  
  ================                                                                                                                         
  Content-type: text/html; charset=UTF-8                                                                                                   
  ERROR: You must supply at least one file or directory to process.                                                                        
  Run "phpcs --help" for usage information                                                                                                 
  Error Output:                                                                                                                            
  ================                                                                                                                         
                         

It appears that during the installation, phpcs --config-set installed_paths is unset, resulting in phpcs throwing an error.

It it a requirement to use phpcs in production? Could this stuff be a dev-dependency? Maybe you should try to set the path as a post-install-cmd. Anyway, I am not sufficiently aware of the workflows surrounding the usage of the phpcs/wpcs packages within the development of menu-cache, so you probably know better.

Custom plugin with add_filter fails to activate

Version Information

  • PHP: 7.1
  • WordPress: 4.8.2

Steps to Reproduce

  1. Create custom plugin modifying menu caching period
  2. Activate custom plugin
<?php
add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_EXPIRATION, function () {
  return 3600;
} );
?>

What I Expected

Custom plugin would activate and filter would modify menu caching period.

What Happened Instead

Custom plugin fails to activate:

[27-Sep-2017 05:14:53 UTC] PHP Fatal error:  Uncaught Error: Class 'Inpsyde\MenuCache\MenuCache' not found in /var/www/mysite.com/wp-content/plugins/customplugin-menu-cache/customplugin-menu-cache.php:14
Stack trace:
#0 /var/www/mysite.com/wp-admin/includes/plugin.php(560): include_once()
#1 /var/www/mysite.com/wp-admin/plugins.php(42): activate_plugin('customplugin-menu-...', 'https://www.mysite.com...', true)
#2 /var/www/mysite.com/wp-admin/network/plugins.php(13): require('/var/www/www.mysite.com...')
#3 {main}
  thrown in /var/www/mysite.com/wp-content/plugins/customplugin-menu-cache/customplugin-menu-cache.php on line 14

Menu key argument clarity?

Documentation is a bit unclear on how or why would you actually use MenuCache::FILTER_KEY_ARGUMENT?

On surface, it seems like working with MenuCache::FILTER_KEY is always better.

Anything I'm missing?

Incompatible with Multisite Shared Menu

Version Information

  • PHP: 7.1
  • WordPress: 4.8

Steps to Reproduce

  1. Add both Menu Cache and Multisite Shared Menu on a Multisite installation
  2. Configure Multisite Shared Menu so that it draws the menu from some site
  3. Go to any other site, fill the cache and reload

What I Expected

A functioning sub-site

What Happened Instead

The site is switched to the menu origin's site, so content and permalinks and loads of other stuff is broken

Why does this happen

Multisite Shared Menu hooks into pre_wp_nav_menu, just like Menu Cache. It uses this hook to switch_to_blog() and restores the current blog in wp_nav_menu_items

The problem is that it never gets there if Menu Cache returned the cached HTML, so we end up with a rogue site-switch.
I'm not blaming this on Menu Cache, mind you. But since wp_nav_menu is not implemented in a way that would allow us to hook into it safely, stuff like this is bound to happen.

If Menu Cache was more aggressive about dealing with its hooke, this particular incompatibility could be avoided, however. I will post a PR for you to review

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.