Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Widget Posts functionality #12

Merged
merged 35 commits into from
May 28, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
85da60f
Introduce Widget Posts functionality.
westonruter May 20, 2015
99a0cb7
Update wp-dev-lib 7f29252...b3b6c2a: Merge pull request xwp/wp-dev-li…
westonruter May 23, 2015
8fe6a53
Checkout PHPUnit tests
westonruter May 23, 2015
e5e5ce3
Add unit test to run Tests_WP_Customize_Widgets w/ Widget Posts active
westonruter May 23, 2015
92b20d5
Update wp-dev-lib b3b6c2a...fc1c489: Fix grep for PHP and JS files
westonruter May 23, 2015
7c2ec85
Revert change to .jscsrc in 92b20d52f
westonruter May 23, 2015
53bda21
Tweak Travis before_script to fix unit test failure
westonruter May 23, 2015
3d75e53
Add Efficient_Multidimensional_Setting_Sanitizing to Core test
westonruter May 23, 2015
f500d2c
Fix applying plugin during core unit tests; fix bugs revealed
westonruter May 23, 2015
d25ab21
Add CLI commands
westonruter May 23, 2015
8aca737
Only call update_option in set_widget_number if changed; prevent widg…
westonruter May 23, 2015
5f43beb
Ensure widget numbers less than 2 are not used
westonruter May 23, 2015
d9a36a7
Improve show CLI command to warn if not exists
westonruter May 23, 2015
3672308
Ensure post type gets registered when required, along with requisite …
westonruter May 23, 2015
4da8ad0
Fix unit tests by ensuring registered post type gets cleaned up
westonruter May 23, 2015
ce02f60
Compare literal arrays for identity in filter_pre_update_option_widge…
westonruter May 23, 2015
9221f9b
Have Widget_Settings::offsetGet() return empty array when instance da…
westonruter May 23, 2015
439b1e0
Fix filters for short-circuiting widget settings
westonruter May 24, 2015
d61f76d
Handle deletion of widget posts via unsetting from Widget_Settings
westonruter May 24, 2015
44a6497
Store widget instances as base64-encoded PHP-serialized strings to pr…
westonruter May 24, 2015
4a880aa
Fix unit tests after introduction of Widget_Posts::parse_post_content…
westonruter May 24, 2015
ad24505
Add support for WXR import/export
westonruter May 24, 2015
ace6811
Add is_enabled(), enable(), disable() methods; use yes/no value
westonruter May 24, 2015
8ca601d
Store pretty-printed JSON instance in post_content for search indexin…
westonruter May 24, 2015
66d7013
Stop calling sanitization update callback unnecessarily
westonruter May 24, 2015
065a49c
Fix update_widget() to preserve post ID and thus actually update the …
westonruter May 24, 2015
c6ec296
Remove try/catch since get_widget_instance_data no longer throws exce…
westonruter May 24, 2015
3baa6e3
Make wp widget-posts show an alias for get command
westonruter May 25, 2015
9f65de0
Add import CLI command; improve migrate and import routines
westonruter May 25, 2015
84040ba
Introduce get_post_content_filtered() wrapper for parse_post_content_…
westonruter May 25, 2015
a532454
Ensure that post_content gets populated with pretty JSON on WXR import
westonruter May 25, 2015
1bc04d1
Update wp-dev-lib fc1c489...2875eff: Fix filter_php_files
westonruter May 26, 2015
d5fbcfa
Add support for importing from exports from WP Options Importer on VIP
westonruter May 26, 2015
f4901a4
Make method private to prevent WP-CLI from exposing as command
westonruter May 27, 2015
c095337
Debug travis unit tests
westonruter May 28, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Introduce Widget Posts functionality.
  • Loading branch information
westonruter committed May 23, 2015
commit 85da60f0ea11502e53029a26fabf33a1b7a88eb1
22 changes: 22 additions & 0 deletions php/class-efficient-multidimensional-setting-sanitizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class Efficient_Multidimensional_Setting_Sanitizing {
*/
public $disabled_filtering_pre_option_widget_settings = false;

/**
* @var \WP_Widget[]
*/
public $widget_objs;

/**
* @param Plugin $plugin
* @param \WP_Customize_Manager $manager
Expand All @@ -60,7 +65,9 @@ function __construct( Plugin $plugin, \WP_Customize_Manager $manager ) {
$this->manager->efficient_multidimensional_setting_sanitizing = $this;

add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_dynamic_setting_class' ), 10, 2 );
add_action( 'widgets_init', array( $this, 'save_widget_objs' ), 90 );

// Note that customize_register happens at wp_loaded, so we register the settings just before
$priority = has_action( 'wp_loaded', array( $this->manager, 'wp_loaded' ) );
$priority -= 1;
add_action( 'wp_loaded', array( $this, 'register_widget_instance_settings_early' ), $priority );
Expand All @@ -69,6 +76,21 @@ function __construct( Plugin $plugin, \WP_Customize_Manager $manager ) {
add_action( 'customize_save_after', array( $this, 'enable_filtering_pre_option_widget_settings' ) );
}

/**
* Since at widgets_init,100 the single instances of widgets get copied out
* to the many instances in $wp_registered_widgets, we capture all of the
* registered widgets up front so we don't have to search through the big
* list later.
*
* @see WP_Customize_Widget_Setting::__construct()
*/
function save_widget_objs() {
foreach ( $this->plugin->widget_factory->widgets as $widget_obj ) {
/** @var \WP_Widget $widget_obj */
$this->widget_objs[ $widget_obj->id_base ] = $widget_obj;
}
}

/**
* Ensure that dynamic settings for widgets use the proper class.
*
Expand Down
111 changes: 111 additions & 0 deletions php/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class Plugin extends Plugin_Base {
*/
public $https_resource_proxy;

/**
* @var Widget_Posts
*/
public $widget_posts;

/**
* @var Efficient_Multidimensional_Setting_Sanitizing
*/
Expand Down Expand Up @@ -73,9 +78,14 @@ public function __construct( $config = array() ) {

'widget_number_incrementing' => true,
'https_resource_proxy' => true,
'widget_posts' => true,
'efficient_multidimensional_setting_sanitizing' => true,
),
'https_resource_proxy' => HTTPS_Resource_Proxy::default_config(),
'widget_posts' => Widget_Posts::default_config(),

'memory_limit' => '256M',
'max_memory_usage_percentage' => 0.75,
);

$this->config = array_merge( $default_config, $config );
Expand Down Expand Up @@ -103,6 +113,13 @@ function init() {
$this->widget_factory = $wp_widget_factory;
$this->config = apply_filters( 'customize_widgets_plus_plugin_config', $this->config, $this );

// Handle conflicting modules.
if ( $this->config['active_modules']['widget_posts'] ) {
$this->config['active_modules']['non_autoloaded_widget_options'] = false; // The widget_posts module makes this obsolete.
$this->config['active_modules']['widget_number_incrementing'] = true; // Dependency.
// @todo $this->config['active_modules']['efficient_multidimensional_setting_sanitizing'] = true; // ?
}

add_action( 'wp_default_scripts', array( $this, 'register_scripts' ), 11 );
add_action( 'wp_default_styles', array( $this, 'register_styles' ), 11 );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ) );
Expand All @@ -128,6 +145,9 @@ function init() {
if ( $this->is_module_active( 'https_resource_proxy' ) ) {
$this->https_resource_proxy = new HTTPS_Resource_Proxy( $this );
}
if ( $this->is_module_active( 'widget_posts' ) ) {
$this->widget_posts = new Widget_Posts( $this );
}
if ( $this->is_module_active( 'efficient_multidimensional_setting_sanitizing' ) && ! empty( $wp_customize ) ) {
$this->efficient_multidimensional_setting_sanitizing = new Efficient_Multidimensional_Setting_Sanitizing( $this, $wp_customize );
}
Expand Down Expand Up @@ -325,4 +345,95 @@ function is_normal_multi_widget( $widget_obj ) {
}
return true;
}


/**
* Get the memory limit in bytes.
*
* Uses memory_limit from php.ini, WP_MAX_MEMORY_LIMIT, and memory_limit
* plugin config, whichever is smallest. Note that -1 is considered infinity.
*
* @return int
*/
function get_memory_limit() {
$memory_limit = $this->parse_byte_size( ini_get( 'memory_limit' ) );
if ( $memory_limit <= 0 ) {
$memory_limit = $this->parse_byte_size( \WP_MAX_MEMORY_LIMIT );
}
if ( $memory_limit <= 0 ) {
$memory_limit = \PHP_INT_MAX;
}
$memory_limit = min(
$memory_limit,
$this->parse_byte_size( $this->config['memory_limit'] )
);
return $memory_limit;
}

/**
* Clear all of the caches for memory management
*
* Adapted from WPCOM_VIP_CLI_Command.
*
* @see \WPCOM_VIP_CLI_Command::stop_the_insanity()
*
* @return bool Whether memory was garbage-collected.
*/
function stop_the_insanity() {

$memory_limit = $this->get_memory_limit();
$used_memory = memory_get_usage();
$used_memory_percentage = (float) $used_memory / $memory_limit;

// Do nothing if we haven't reached the memory limit threshold
if ( $used_memory_percentage < $this->config['max_memory_usage_percentage'] ) {
return false;
}

/**
* @var \WP_Object_Cache $wp_object_cache
* @var \wpdb $wpdb
*/
global $wpdb, $wp_object_cache;

$wpdb->queries = array(); // or define( 'WP_IMPORTING', true );

if ( is_object( $wp_object_cache ) ) {
$wp_object_cache->group_ops = array();
$wp_object_cache->stats = array();
$wp_object_cache->memcache_debug = array();
$wp_object_cache->cache = array();

if ( method_exists( $wp_object_cache, '__remoteset' ) ) {
$wp_object_cache->__remoteset(); // important
}
}

return true;
}

/**
* Obtain an integer byte size from a byte string like 1024K, 65M, 1G.
*
* @param int|string $bytes Integer or string like "1024K", "64M" or "1G"
* @return int|null Number of bytes, or null if parse error
*/
public function parse_byte_size( $bytes ) {
if ( is_int( $bytes ) ) {
return $bytes; // already bytes, so no-op
}
if ( ! preg_match( '/^(-?\d+)([BKMG])?$/', strtoupper( $bytes ), $matches ) ) {
return null;
}
$value = intval( $matches[1] );
$unit = empty( $matches[2] ) ? 'B' : $matches[2];
if ( 'K' === $unit ) {
$value *= 1024;
} else if ( 'M' === $unit ) {
$value *= pow( 1024, 2 );
} else if ( 'G' === $unit ) {
$value *= pow( 1024, 3 );
}
return $value;
}
}
20 changes: 17 additions & 3 deletions php/class-widget-number-incrementing.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,35 @@ class Widget_Number_Incrementing {
/**
* @var \WP_Widget[]
*/
public $widget_objs = array();
public $widget_objs;

/**
* @param Plugin $plugin
*/
function __construct( Plugin $plugin ) {
$this->plugin = $plugin;

add_action( 'widgets_init', array( $this, 'store_widget_objects' ), 90 );
add_action( 'wp_ajax_' . self::AJAX_ACTION, array( $this, 'ajax_incr_widget_number' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_filter( 'customize_refresh_nonces', array( $this, 'filter_customize_refresh_nonces' ) );
}

/**
* @action widgets_init, 90
*/
function store_widget_objects() {
$this->widget_objs = array();
foreach ( $this->plugin->widget_factory->widgets as $widget_obj ) {
/** @var \WP_Widget $widget_obj */
if ( "widget_{$widget_obj->id_base}" !== $widget_obj->option_name ) {
continue;
}
$this->widget_objs[ $widget_obj->id_base ] = $widget_obj;
}
}

/**
* @see Widget_Management::gather_registered_widget_types()
* @see Widget_Management::get_widget_number()
Expand All @@ -65,8 +80,7 @@ protected function get_option_key_for_widget_number( $id_base ) {
* @return int
*/
function get_max_existing_widget_number( $id_base ) {
$widget_objs = $this->plugin->get_registered_widget_objects();
$widget_obj = $widget_objs[ $id_base ];
$widget_obj = $this->widget_objs[ $id_base ];
// @todo There should be a pre_existing_widget_numbers, pre_max_existing_widget_number filter, or pre_existing_widget_ids to short circuit the expensive WP_Widget::get_settings()
$widget_numbers = array_keys( $widget_obj->get_settings() );
$widget_numbers[] = 2; // multi-widgets start numbering at 2
Expand Down
Loading