Skip to content

Commit

Permalink
Merge pull request #1125 from greenpeace/planet-5099-avoid-eval-usage
Browse files Browse the repository at this point in the history
PLANET-5099 Add a command as alternative for eval
  • Loading branch information
Inwerpsel authored Jun 19, 2020
2 parents 6c81bf2 + feb9484 commit d70a663
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 105 deletions.
21 changes: 3 additions & 18 deletions classes/class-p4-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,13 @@ public function __construct() {
* Hooks the activator functions.
*/
protected function hooks() {
add_action( 'after_switch_theme', [ $this, 'run' ] );
add_action( 'after_switch_theme', [ self::class, 'run' ] );
}

/**
* Run activation functions.
*/
public function run() {
$this->add_custom_roles_and_capabilities();
}

/**
* Add campaigner role and its capabilities.
*/
public function add_custom_roles_and_capabilities() {

$campaigner = new P4_Campaigner();
$campaigner->register_role_and_add_capabilities();

// Needed to allow the editor rule to change the author of a post in the document sidebar. The users data for that
// control is fetched using the REST API, where WordPress by default doesn't perform a permissions check, however
// the Wordfence plugin adds this check in `\wordfence::jsonAPIAuthorFilter`.
$roles = wp_roles();
$roles->add_cap( 'editor', 'list_users' );
public static function run(): void {
P4_Campaigner::register_role_and_add_capabilities();
}
}
148 changes: 61 additions & 87 deletions classes/class-p4-campaigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,93 +12,62 @@
class P4_Campaigner {

/**
* P4_Campaigner constructor.
* @var [[string]] List of capabilities for each standard WordPress role.
*/
public function __construct() {
}

/**
* Register campaigner role and add custom capabilities.
*/
public function register_role_and_add_capabilities() {
$this->add_campaigner_role();
$this->add_campaign_caps_admin();
$this->add_campaign_caps_editor();
$this->add_campaign_caps_author();
$this->add_campaign_caps_contributor();
$this->add_campaigner_caps_import();
}

/**
* Add Campaign capabilities to Administrator User.
*/
public function add_campaign_caps_admin() {
$role = get_role( 'administrator' );

$role->add_cap( 'edit_campaign' );
$role->add_cap( 'read_campaign' );
$role->add_cap( 'delete_campaign' );
$role->add_cap( 'edit_campaigns' );
$role->add_cap( 'edit_others_campaigns' );
$role->add_cap( 'publish_campaigns' );
$role->add_cap( 'read_private_campaigns' );
$role->add_cap( 'delete_campaigns' );
$role->add_cap( 'delete_private_campaigns' );
$role->add_cap( 'delete_published_campaigns' );
$role->add_cap( 'delete_others_campaigns' );
$role->add_cap( 'edit_private_campaigns' );
$role->add_cap( 'edit_published_campaigns' );
}

/**
* Add Campaign capabilities to Editor User.
*/
public function add_campaign_caps_editor() {
$role = get_role( 'editor' );

$role->add_cap( 'edit_campaign' );
$role->add_cap( 'read_campaign' );
$role->add_cap( 'delete_campaign' );
$role->add_cap( 'edit_campaigns' );
$role->add_cap( 'edit_others_campaigns' );
$role->add_cap( 'publish_campaigns' );
$role->add_cap( 'delete_campaigns' );
$role->add_cap( 'delete_published_campaigns' );
$role->add_cap( 'delete_others_campaigns' );
$role->add_cap( 'edit_published_campaigns' );
}

/**
* Add Campaign capabilities to Author User.
*/
public function add_campaign_caps_author() {
$role = get_role( 'author' );

$role->add_cap( 'edit_campaign' );
$role->add_cap( 'read_campaign' );
$role->add_cap( 'delete_campaign' );
$role->add_cap( 'edit_campaigns' );
$role->add_cap( 'publish_campaigns' );
$role->add_cap( 'delete_published_campaigns' );
$role->add_cap( 'edit_published_campaigns' );
}

/**
* Add Campaign capabilities to Author User.
*/
public function add_campaign_caps_contributor() {
$role = get_role( 'contributor' );

$role->add_cap( 'edit_campaign' );
$role->add_cap( 'read_campaign' );
$role->add_cap( 'edit_campaigns' );
$role->add_cap( 'edit_published_campaigns' );
}
private const CAPABILITIES_MAP = [
'administrator' => [
'edit_campaign',
'read_campaign',
'delete_campaign',
'edit_campaigns',
'edit_others_campaigns',
'publish_campaigns',
'read_private_campaigns',
'delete_campaigns',
'delete_private_campaigns',
'delete_published_campaigns',
'delete_others_campaigns',
'edit_private_campaigns',
'edit_published_campaigns',
],
'editor' => [
'edit_campaign',
'read_campaign',
'delete_campaign',
'edit_campaigns',
'edit_others_campaigns',
'publish_campaigns',
'delete_campaigns',
'delete_published_campaigns',
'delete_others_campaigns',
'edit_published_campaigns',

// Needed to allow the editor rule to change the author of a post in the document sidebar. The users data for that
// control is fetched using the REST API, where WordPress by default doesn't perform a permissions check, however
// the Wordfence plugin adds this check in `\wordfence::jsonAPIAuthorFilter`.
'list_users',
],
'author' => [
'edit_campaign',
'read_campaign',
'delete_campaign',
'edit_campaigns',
'publish_campaigns',
'delete_published_campaigns',
'edit_published_campaigns',
],
'contributor' => [
'edit_campaign',
'read_campaign',
'edit_campaigns',
'edit_published_campaigns',
],
];

/**
* Add Campaigner role.
*/
public function add_campaigner_role() {
private static function add_campaigner_role() {
add_role(
'campaigner',
__( 'Campaigner', 'planet4-master-theme-backend' ),
Expand Down Expand Up @@ -129,16 +98,21 @@ public function add_campaigner_role() {
'delete_others_campaigns' => true,
'edit_private_campaigns' => true,
'edit_published_campaigns' => true,
'import' => true,
]
);
}

/**
* Add Campaign import capabilities to Campaigner User.
* Register campaigner role and add custom capabilities.
*/
public function add_campaigner_caps_import() {
$role = get_role( 'campaigner' );

$role->add_cap( 'import' );
public static function register_role_and_add_capabilities() {
foreach ( self::CAPABILITIES_MAP as $role_name => $capabilities ) {
$role = get_role( $role_name );
foreach ( $capabilities as $capability ) {
$role->add_cap( $capability );
}
}
self::add_campaigner_role();
}
}
16 changes: 16 additions & 0 deletions classes/class-p4-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private function __construct( $services ) {
$this->load_files();
$this->load_services( $services );
$this->add_filters();
$this->load_commands();
}

/**
Expand Down Expand Up @@ -158,6 +159,21 @@ private function add_filters(): void {
add_filter( 'pre_delete_post', [ $this, 'do_not_delete_autosave' ], 1, 3 );
}

/**
* Registers WP_CLI commands.
*/
public function load_commands() {
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
return;
}

$command = static function ( $args, $assoc_args ) {
P4_Activator::run();
};

WP_CLI::add_command( 'p4-run-activator', $command );
}

/**
* Due to a bug in WordPress core the "autosave revision" of a post is created and deleted all of the time.
* This is pretty pointless and makes it impractical to add any post meta to that revision.
Expand Down

0 comments on commit d70a663

Please sign in to comment.