-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #816 from publishpress/release-v2.8.0
Release v2.8.0
- Loading branch information
Showing
35 changed files
with
2,696 additions
and
1,281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace PublishPress\Capabilities\Classes; | ||
|
||
class PP_Capabilities_Installer | ||
{ | ||
/** | ||
* Runs methods when the plugin is running for the first time. | ||
* | ||
* @param string $currentVersion | ||
*/ | ||
public static function runInstallTasks($currentVersion) | ||
{ | ||
self::addPluginCapabilities(); | ||
|
||
/** | ||
* @param string $currentVersion | ||
*/ | ||
do_action('pp_capabilities_installed', $currentVersion); | ||
} | ||
|
||
/** | ||
* Runs methods when the plugin is being upgraded to a most recent version. | ||
* | ||
* @param string $currentVersions | ||
*/ | ||
public static function runUpgradeTasks($currentVersions) | ||
{ | ||
if (version_compare($currentVersions, '2.8.0', '<')) { | ||
self::addPluginCapabilities(); | ||
} | ||
|
||
/** | ||
* @param string $previousVersion | ||
*/ | ||
do_action('pp_capabilities_upgraded', $currentVersions); | ||
} | ||
|
||
private static function addPluginCapabilities() | ||
{ | ||
$eligible_roles = []; | ||
$pp_capabilities = apply_filters('cme_publishpress_capabilities_capabilities', []); | ||
|
||
/** | ||
* We're not saving installation version prior to 2.8.0. | ||
* So, we need another way to know if this is an upgrade or | ||
* new installs to add or upgrade role capabilities. | ||
*/ | ||
foreach ( wp_roles()->roles as $role_name => $role ) { | ||
$role_object = get_role($role_name); | ||
if ($role_object->has_cap('manage_capabilities')) { | ||
$eligible_roles[] = $role_name; | ||
} | ||
} | ||
|
||
/** | ||
* If it's a fresh installation, we're giving 'administrator' and 'editor' | ||
* all capabilities | ||
*/ | ||
if (empty($eligible_roles)) { | ||
$eligible_roles = ['administrator', 'editor']; | ||
} | ||
|
||
/** | ||
* Add capabilities to eligible roles | ||
*/ | ||
foreach ($eligible_roles as $eligible_role) { | ||
$role = get_role($eligible_role); | ||
foreach ($pp_capabilities as $cap) { | ||
if (!$role->has_cap($cap)) { | ||
$role->add_cap($cap); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.