| 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 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Fire method when plugin assigns |
| 25 |
*/ |
| 26 |
public function assign() { |
| 27 |
$this->assignCapabilities(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Use other plugins hooks |
| 32 |
*/ |
| 33 |
public function onPluginsLoad() { |
| 34 |
// Add depicter custom capabilities to members plugin if it's installed |
| 35 |
if ( function_exists( 'members_get_capabilities' ) ) { |
| 36 |
add_filter( 'members_get_capabilities', [ $this, 'customCapabilities' ] ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Add custom capabilities to members plugin |
| 42 |
* |
| 43 |
* @param array $caps |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public function customCapabilities( array $caps = [] ) { |
| 48 |
$caps[] = 'access_depicter' ; |
| 49 |
$caps[] = 'edit_depicter' ; |
| 50 |
$caps[] = 'edit_others_depicter'; |
| 51 |
$caps[] = 'publish_depicter'; |
| 52 |
$caps[] = 'delete_depicter' ; |
| 53 |
$caps[] = 'delete_others_depicter'; |
| 54 |
$caps[] = 'create_depicter' ; |
| 55 |
$caps[] = 'import_depicter' ; |
| 56 |
$caps[] = 'export_depicter' ; |
| 57 |
$caps[] = 'duplicate_depicter'; |
| 58 |
$caps[] = 'manage_depicter' ; |
| 59 |
|
| 60 |
return $caps; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Assign custom capabilities to main roles |
| 65 |
* |
| 66 |
* @param false $force_update |
| 67 |
*/ |
| 68 |
protected function assignCapabilities( bool $force_update = false ) { |
| 69 |
// check if custom capabilities are added before or not |
| 70 |
$is_added = \Depicter::options()->get( 'capabilities_added', 0 ); |
| 71 |
|
| 72 |
// add caps if they are not already added |
| 73 |
if( ! $is_added || $force_update ) { |
| 74 |
|
| 75 |
// assign depicter capabilities to following roles |
| 76 |
$roles = array( 'administrator', 'editor' ); |
| 77 |
|
| 78 |
foreach ( $roles as $role ) { |
| 79 |
if( ! $role = get_role( $role ) ){ |
| 80 |
continue; |
| 81 |
} |
| 82 |
foreach( $this->customCapabilities() as $capability ){ |
| 83 |
$role->add_cap( $capability ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
\Depicter::options()->set( 'capabilities_added', 1 ); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|