PluginProbe
Gianism / trunk
Gianism vtrunk
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / app / Gianism / Controller / Network.php

Network.php in Gianism trunk, at app/Gianism/Controller/Network.php

131 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Gianism\Controller;
4
5 use Gianism\Bootstrap;
6 use Gianism\Helper\Option;
7 use Gianism\Helper\ServiceManager;
8 use Gianism\Pattern\AbstractController;
9
10 /**
11 * Network controller
12 *
13 * @package gianism
14 * @since 4.3.0
15 */
16 class Network extends AbstractController {
17
18 /**
19 * Network constructor.
20 *
21 * @param array $argument
22 */
23 protected function __construct( array $argument = [] ) {
24 parent::__construct( $argument );
25 // Add network notice
26 add_action( 'admin_notices', [ $this, 'network_notice' ] );
27 // Set role.
28 add_action( 'gianism_before_set_login_cookie', [ $this, 'set_role' ], 1, 2 );
29 // If failed to authenticate, redirect to original site.
30 add_filter( 'gianism_redirect_to', [ $this, 'redirect_to' ], 10, 3 );
31 }
32
33 /**
34 * Detect if Gianism can network available.
35 *
36 * @return bool
37 */
38 public function network_available() {
39 return is_multisite() && ! is_subdomain_install();
40 }
41
42 /**
43 * Detect if this is child site.
44 */
45 public function is_child_site() {
46 return is_multisite() && ( $this->option->get_parent_blog_id() !== get_current_blog_id() );
47 }
48
49 /**
50 * Set user role for child blog.
51 *
52 * @param int $user_id
53 * @param string $service_name
54 */
55 public function set_role( $user_id, $service_name ) {
56 $blog_id = (int) $this->session->get( 'blog_id' );
57 if ( ! $blog_id ) {
58 // Do nothing.
59 return;
60 }
61 $should_assign_default_role = apply_filters( 'gianism_should_assign_default_role', $this->option->is_network_activated() );
62 if ( ! $should_assign_default_role ) {
63 return;
64 }
65 // Get blog role for current user.
66 $user = new \WP_User( $user_id, '', $blog_id );
67 if ( empty( $user->roles ) ) {
68 // This user has no role for child site.
69 $base_role = apply_filters( '', get_blog_option( $blog_id, 'default_role' ) );
70 $user->set_role( $base_role );
71 }
72 }
73
74 /**
75 * Filter redirect URI if this is from child site.
76 *
77 * @param string $url
78 * @param ServiceManager $service
79 * @param string $context
80 * @return string
81 */
82 public function redirect_to( $url, $service, $context ) {
83 if ( ( 'login-failure' !== $context ) || ! $this->option->is_network_activated() ) {
84 return $url;
85 }
86 $blog_id = (int) $this->session->get( 'blog_id' );
87 if ( ! $blog_id ) {
88 // Do nothing.
89 return $url;
90 }
91 // If this is from child site, change url.
92 switch_to_blog( $blog_id );
93 $redirect_to = '';
94 $url_segments = explode( '?', $url );
95 if ( 1 < count( $url_segments ) ) {
96 parse_str( $url_segments[1], $params );
97 foreach ( [ 'redirect_to', 'redirect' ] as $key ) {
98 if ( ! empty( $params[ $key ] ) ) {
99 $redirect_to = $params[ $key ];
100 break;
101 }
102 }
103 }
104 switch_to_blog( $blog_id );
105 $url = wp_login_url( $redirect_to, true );
106 $url = apply_filters( 'gianism_network_login_failed_redirect_to', $url, $service, $context, $blog_id );
107 restore_current_blog();
108 return $url;
109 }
110
111 /**
112 * Display notices
113 */
114 public function network_notice() {
115 if ( ! $this->network_available() ) {
116 // This is not network available install.
117 return;
118 }
119 if ( Admin::get_instance()->no_nag_notice() ) {
120 return;
121 }
122 try {
123 if ( current_user_can( 'manage_network' ) && ! $this->option->is_network_activated() ) {
124 throw new \Exception( __( 'Gianism provides network sites supports. Please consider network activation.', 'wp-gianism' ) );
125 }
126 } catch ( \Exception $e ) {
127 printf( '<div class="notice notice-info is-dismissible"><p>%s</p></div>', wp_kses_post( $e->getMessage() ) );
128 }
129 }
130 }
131