/** * Inspect the RECENT_COMMIT for both WPLib and WPLib::app_class() * and if changed call 'wplib_commit_revised' hook and update * option in database. */ static function _wp_loaded() { $commit_revised = false; foreach (array('WPLib', WPLib::app_class()) as $class_name) { $recent_commit = self::get_recent_commit($class_name); if (WPLib::is_development()) { /** * During development look at file RECENT_COMMIT * that a git commit-hook will hopefully have added */ self::_maybe_update_class($class_name); $loaded_commit = self::load_recent_commit($class_name); if ($loaded_commit !== $recent_commit) { $recent_commit = $loaded_commit; } } $prefix = strtolower($class_name); $previous_commit = get_option($option_name = "{$prefix}_recent_commit"); if ($recent_commit !== $previous_commit) { $commit_revised = true; break; } } if ($commit_revised) { update_option($option_name, $recent_commit); do_action('wplib_commit_revised', $recent_commit, $previous_commit); } }
/** * Inspect the RECENT_COMMIT for both WPLib and WPLib::app_class() * and if changed call 'wplib_commit_revised' hook and update * option in database. */ static function _wp_loaded() { $option_name = $recent_commit = $previous_commit = null; $commit_revised = false; foreach (array('WPLib', WPLib::app_class()) as $class_name) { $recent_commit = self::get_recent_commit($class_name); if (WPLib::is_development()) { $loaded_commit = self::_maybe_update_class($class_name); if ($loaded_commit !== $recent_commit) { $recent_commit = $loaded_commit; } } $prefix = strtolower($class_name); $previous_commit = get_option($option_name = "{$prefix}_recent_commit"); if ($recent_commit !== $previous_commit) { $commit_revised = true; break; } } if ($commit_revised) { update_option($option_name, $recent_commit); do_action('wplib_commit_revised', $recent_commit, $previous_commit); } }
/** * Runs through all the registered roles and ensures that all roles and get_capabilities * are set as defined in the classes. * * @param string $recent_commit * @param string $previous_commit */ private static function _initialize_roles($recent_commit, $previous_commit) { WPLib::autoload_all_classes(); $app_slug = strtolower(WPLib::app_class()); $option = get_option($option_name = "{$app_slug}_roles", array()); $wp_roles = new WP_Roles(); $dirty = false; foreach (self::$_roles as $role_slug => $role) { if (preg_match('#^WPLib_(Administrators|Editors|Contributors|Subscribers|Authors)$#', $role['class_name'])) { /* * Easier just to punt on these for right now. * WordPress does some weird things with built-in roles, * especially with Administrator related to Multisite */ continue; } if (empty($role_slug)) { /* * Somehow we got an empty role slug?!?. Carry on. */ continue; } $capabilities = call_user_func(array($role['class_name'], 'capabilities')); if (!isset($option[$role_slug])) { $option[$role_slug] = array('prior_capabilities' => $capabilities, 'recent_commit' => $recent_commit, 'previous_commit' => $previous_commit); $dirty = true; } $display_name = self::get_role_display_name($role_slug); $prior_capabilities = $option[$role_slug]['prior_capabilities']; /* * Get the capabilities */ if (is_null($current_capabilities = $wp_roles->role_objects[$role_slug]->capabilities)) { $current_capabilities = array(); } else { /** * Remove all the legacy level_0 through level_10 capabilities. */ for ($i = 0; $i <= 10; $i++) { unset($current_capabilities["level_{$i}"]); } } if (empty($prior_capabilities)) { /** * First time in, let's assume previous are same as current. */ $prior_capabilities = $current_capabilities; } /** * Filter the capabilities that should be applied for the role. * * @since 0.11.0 * * @param string[] $capabilities * @param string $role_slug { * @param array $role { * An array of information about the role as assigned in self::register_role(). * * @type string $display_name Title used to display the role to users. * @type string[] $capabilities Array of capabilities that should be assigned to the role. * @type string $class_name Name of class defining the role that inherits from WPLib_Role_Module_Base. * } */ $capabilities = apply_filters('wplib_role_capabilities', $capabilities, $role_slug, $role); $capabilities = array_fill_keys($capabilities, true); if (defined('WPLIB_UPDATE_ROLES') && WPLIB_UPDATE_ROLES) { $change_role = true; } else { if (!isset($wp_roles->roles[$role_slug])) { /* * Whelp, the role does not exists, so let's add it. */ $change_role = true; } else { if (isset($merged) || !self::_arrays_are_equivalent($capabilities, $current_capabilities)) { /* * The new capabilities are different than the current ones, AND * nobody changed the capabilities since we last updated them. * * This stops manually changed capabilities from being overwritten * at the expense of not containing new capabilities defined in the * code. Better to respect the user's efforts and add a burden on * them then to ignore the user's efforts and simply reset. */ $change_role = self::_arrays_are_equivalent($current_capabilities, $prior_capabilities); } else { if ($display_name !== $wp_roles->role_names[$role_slug]) { /* * The display name has changed so let's update the role. */ $change_role = true; } else { if ($display_name !== $wp_roles->role_names[$role_slug]) { /* * Does not seem there is a reason to change. */ $change_role = false; } else { /* * Bah. Don't change it. No evidence we need to. */ $change_role = false; } } } } } if ($change_role) { /** * @note: Just FYI, this will remove the legacy get_capabilities of level_0..level_10. * @note: Should not be an issue for a modern WP app. If it becomes an issue we can test for them too. */ remove_role($role_slug); call_user_func($role_slug, $display_name, $capabilities); $option[$role_slug] = array('prior_capabilities' => $capabilities, 'recent_commit' => $recent_commit, 'previous_commit' => $previous_commit); $dirty = true; } } self::$_roles = array(); if ($dirty) { update_option($option_name, $option, 'no'); /** * @future Change this to redirect to the same URL they were on * Which means adding something like WPLib::current_url(). * Maybe even a WPLib::redirect_to_self(). * But I want to sleep on that a few days first. */ wp_safe_redirect(home_url('/')); exit; } }
/** * @return null|string */ function app_class() { return WPLib::app_class($this); }