| 1 |
<?php |
| 2 |
namespace Depicter\WordPress; |
| 3 |
|
| 4 |
|
| 5 |
use WPEmerge\ServiceProviders\ServiceProviderInterface; |
| 6 |
|
| 7 |
class PermissionsServiceProvider implements ServiceProviderInterface { |
| 8 |
|
| 9 |
/** |
| 10 |
* {@inheritDoc} |
| 11 |
*/ |
| 12 |
public function register( $container ){} |
| 13 |
|
| 14 |
/** |
| 15 |
* {@inheritDoc} |
| 16 |
*/ |
| 17 |
public function bootstrap( $container ){ |
| 18 |
register_activation_hook( DEPICTER_PLUGIN_FILE, [ $this, 'assign' ] ); |
| 19 |
add_action( 'admin_init', [ $this, 'assign' ] ); |
| 20 |
add_action( 'plugins_loaded', [ $this, 'onPluginsLoad' ] ); |
| 21 |
add_action( 'user_register', [ $this, 'addCapabilityToNewUsers' ] ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Fire method when plugin assigns |
| 26 |
*/ |
| 27 |
public function assign() { |
| 28 |
$this->assignCapabilities(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Add capability to new registered user |
| 33 |
* |
| 34 |
* @param int $userID |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function addCapabilityToNewUsers( $userID ) { |
| 38 |
$roles = array( 'administrator', 'editor' ); |
| 39 |
$userData = get_userdata( $userID ); |
| 40 |
$userRole = $userData->roles; |
| 41 |
if ( in_array( $userRole, $roles ) ) { |
| 42 |
$this->assignCapabilities( true ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Use other plugins hooks |
| 48 |
*/ |
| 49 |
public function onPluginsLoad() { |
| 50 |
// Add depicter custom capabilities to members plugin if it's installed |
| 51 |
if ( function_exists( 'members_get_capabilities' ) ) { |
| 52 |
add_filter( 'members_get_capabilities', [ $this, 'customCapabilities' ] ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Add custom capabilities to members plugin |
| 58 |
* |
| 59 |
* @param array $caps |
| 60 |
* |
| 61 |
* @return array |
| 62 |
*/ |
| 63 |
public function customCapabilities( array $caps = [] ) { |
| 64 |
$caps[] = 'access_depicter' ; |
| 65 |
$caps[] = 'edit_depicter' ; |
| 66 |
$caps[] = 'edit_others_depicter'; |
| 67 |
$caps[] = 'publish_depicter'; |
| 68 |
$caps[] = 'delete_depicter' ; |
| 69 |
$caps[] = 'delete_others_depicter'; |
| 70 |
$caps[] = 'create_depicter' ; |
| 71 |
$caps[] = 'import_depicter' ; |
| 72 |
$caps[] = 'export_depicter' ; |
| 73 |
$caps[] = 'duplicate_depicter'; |
| 74 |
$caps[] = 'manage_depicter' ; |
| 75 |
|
| 76 |
return $caps; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Assign custom capabilities to main roles |
| 81 |
* |
| 82 |
* @param false $force_update |
| 83 |
*/ |
| 84 |
protected function assignCapabilities( bool $force_update = false ) { |
| 85 |
// check if custom capabilities are added before or not |
| 86 |
$is_added = \Depicter::options()->get( 'capabilities_added', 0 ); |
| 87 |
|
| 88 |
// add caps if they are not already added |
| 89 |
if( ! $is_added || $force_update ) { |
| 90 |
|
| 91 |
// assign depicter capabilities to following roles |
| 92 |
$roles = array( 'administrator', 'editor' ); |
| 93 |
|
| 94 |
foreach ( $roles as $role ) { |
| 95 |
if( ! $role = get_role( $role ) ){ |
| 96 |
continue; |
| 97 |
} |
| 98 |
foreach( $this->customCapabilities() as $capability ){ |
| 99 |
$role->add_cap( $capability ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
\Depicter::options()->set( 'capabilities_added', 1 ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|