Skip to content

Commit 5b12d5d

Browse files
authored
Merge pull request #7 from getshifter/feat/with-passwordless
feat: just add option
2 parents dd67bd0 + aa80681 commit 5b12d5d

File tree

6 files changed

+248
-132
lines changed

6 files changed

+248
-132
lines changed

classes/class.page-content.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
namespace Shifter_Webhook;
3+
4+
class Page_Content {
5+
public function __construct( $options ) {
6+
$this->options = $options;
7+
}
8+
public function get_body_data() {
9+
$content_urls = new RequestContent();
10+
$body = $content_urls->get_body_by_current_user();
11+
return $body;
12+
}
13+
public function get_body( string $webhook_content_type ) {
14+
$body = $this->get_body_data();
15+
16+
if ( 'application/json' === $webhook_content_type ) {
17+
return "-d '" . json_encode( $body ) . "'";
18+
}
19+
$text_content = '';
20+
foreach ( $body as $key => $value ) {
21+
$text_content .= "-d '" . esc_attr( $key . '=' . $value ) . "' ";
22+
}
23+
return $text_content;
24+
}
25+
public function show_example( string $webhook_url, string $webhook_content_type ) {
26+
?>
27+
<h3> Example</h3>
28+
<p>
29+
$ curl <?php echo esc_url( $webhook_url ? $webhook_url : 'https://example.com' ); ?> -XPOST
30+
-H "Content-Type: <?php echo esc_attr( $webhook_content_type ); ?>"
31+
<?php echo $this->get_body( $webhook_content_type ); ?></>
32+
</p>
33+
<?
34+
}
35+
public function get_option( string $key, string $form_type, string $default ) {
36+
$value = get_option( $key );
37+
$value = $value ? $value : $default;
38+
if ( 'url' === $form_type ) {
39+
return esc_url( $value );
40+
} else {
41+
return esc_attr( $value );
42+
}
43+
}
44+
public function get_form_input( $item, $current_value ) {
45+
$name = esc_attr( $item[ 'key' ] );
46+
if ( 'select' === $item[ 'form_type'] ) {
47+
?>
48+
<select name="<?php echo $name; ?>" style="min-width: 300px">
49+
<?php
50+
foreach( $item[ 'options' ] as $option ) {
51+
$option_value = esc_attr( $option );
52+
?>
53+
<option
54+
value="<?php echo $option_value; ?>"
55+
<?php
56+
if ( $current_value === $option_value) {
57+
echo 'selected';
58+
}
59+
?>
60+
>
61+
<?php echo $option_value; ?>
62+
</option>
63+
<?php
64+
}
65+
?>
66+
</select>
67+
<?php
68+
} else if ( 'boolean' === $item['form_type'] ) {
69+
?>
70+
<input
71+
type="checkbox"
72+
name="<?php echo $name ?>"
73+
value="1"
74+
<?php checked( "1", $current_value ); ?>
75+
/>
76+
<?php
77+
} else {
78+
?>
79+
<input
80+
type="<?php echo esc_attr( $item[ 'form_type'] ); ?>"
81+
name="<?php echo $name ?>"
82+
value="<?php echo $current_value; ?>"
83+
style="min-width: 300px"
84+
/>
85+
<?php
86+
}
87+
}
88+
public function get_form( string $webhook_url, string $webhook_content_type ) {
89+
$key = 'shifter_webhook_settings';
90+
$option = $this->options[ $key ];
91+
?>
92+
<form method="post" action="options.php">
93+
<?php settings_fields( $key ); ?>
94+
<?php do_settings_sections( $key ); ?>
95+
<table class="form-table">
96+
<?php
97+
foreach ( $option as $item ) {
98+
$value = $this->get_option( $item[ 'key' ], $item[ 'form_type'], $item[ 'default' ] );
99+
?>
100+
<tr valign="top">
101+
<th scope="row">
102+
<?php echo ucfirst( $item[ 'label' ] ); ?>
103+
</th>
104+
<td>
105+
<?php $this->get_form_input( $item, $value ); ?>
106+
</td>
107+
</tr>
108+
<?php } ?>
109+
</table>
110+
<?php submit_button(); ?>
111+
</form>
112+
<?php
113+
}
114+
public function shifter_webhook_settings_page() {
115+
$webhook_url = get_option( 'shifter_webhook_url' );
116+
$webhook_content_type = get_option( 'shifter_webhook_content_type' );
117+
$webhook_content_type = $webhook_content_type ? $webhook_content_type : 'application/json';
118+
?>
119+
<div class="wrap">
120+
<h1>
121+
<?php _e('Webhook Settings', 'shifter-wp-webhook'); ?>
122+
</h1>
123+
<div class="card" style="width: 100%; max-width: 100%;">
124+
<h2>
125+
<?php _e('Outbound Webhook', 'shifter-wp-webhook'); ?>
126+
</h2>
127+
<p>
128+
The plugin can send a POST request with the container URL.<br />
129+
You can run some build script to use the request body parameter.
130+
</p>
131+
<?php $this->show_example( $webhook_url, $webhook_content_type ); ?>
132+
<?php $this->get_form( $webhook_url, $webhook_content_type ); ?>
133+
</div>
134+
</div>
135+
<?
136+
}
137+
}

classes/class.page-setting.php

Lines changed: 7 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public function __construct() {
2929
"sanitize_callback" => array( $this, 'esc_boolean' ),
3030
'label' => __( 'Send on boot', 'shifter-wp-webhook'),
3131
"default" => "0"
32+
),
33+
array(
34+
"key" => "shifter_webhook_send_with_login_url",
35+
"form_type" => "boolean",
36+
"sanitize_callback" => array( $this, 'esc_boolean' ),
37+
'label' => __( 'Send with passwordless login URL', 'shifter-wp-webhook'),
38+
"default" => "0"
3239
)
3340
]
3441
];
@@ -60,130 +67,3 @@ public function register_pages() {
6067
);
6168
}
6269
}
63-
64-
class Page_Content {
65-
public function __construct( $options ) {
66-
$this->options = $options;
67-
}
68-
public function get_body( string $webhook_content_type ) {
69-
$container_url = esc_url( get_option( 'home' ) );
70-
if ( 'application/json' === $webhook_content_type ) {
71-
return json_encode( array(
72-
"CONTAINER_URL" => $container_url,
73-
) );
74-
}
75-
return esc_attr( 'CONTAINER_URL=' . $container_url );
76-
}
77-
public function show_example( string $webhook_url, string $webhook_content_type ) {
78-
?>
79-
<h3> Example</h3>
80-
<p>
81-
$ curl <?php echo esc_url( $webhook_url ? $webhook_url : 'https://example.com' ); ?> -XPOST
82-
-H "Content-Type: <?php echo esc_attr( $webhook_content_type ); ?>"
83-
-d '<?php echo $this->get_body( $webhook_content_type ); ?>'</>
84-
</p>
85-
<?
86-
}
87-
public function get_option( string $key, string $form_type, string $default ) {
88-
$value = get_option( $key );
89-
$value = $value ? $value : $default;
90-
if ( 'url' === $form_type ) {
91-
return esc_url( $value );
92-
} else {
93-
return esc_attr( $value );
94-
}
95-
}
96-
public function get_form_input( $item, $current_value ) {
97-
$name = esc_attr( $item[ 'key' ] );
98-
if ( 'select' === $item[ 'form_type'] ) {
99-
?>
100-
<select name="<?php echo $name; ?>" style="min-width: 300px">
101-
<?php
102-
foreach( $item[ 'options' ] as $option ) {
103-
$option_value = esc_attr( $option );
104-
?>
105-
<option
106-
value="<?php echo $option_value; ?>"
107-
<?php
108-
if ( $current_value === $option_value) {
109-
echo 'selected';
110-
}
111-
?>
112-
>
113-
<?php echo $option_value; ?>
114-
</option>
115-
<?php
116-
}
117-
?>
118-
</select>
119-
<?php
120-
} else if ( 'boolean' === $item['form_type'] ) {
121-
?>
122-
<input
123-
type="checkbox"
124-
name="<?php echo $name ?>"
125-
value="1"
126-
<?php checked( "1", $current_value ); ?>
127-
/>
128-
<?php
129-
} else {
130-
?>
131-
<input
132-
type="<?php echo esc_attr( $item[ 'form_type'] ); ?>"
133-
name="<?php echo $name ?>"
134-
value="<?php echo $current_value; ?>"
135-
style="min-width: 300px"
136-
/>
137-
<?php
138-
}
139-
}
140-
public function get_form( string $webhook_url, string $webhook_content_type ) {
141-
$key = 'shifter_webhook_settings';
142-
$option = $this->options[ $key ];
143-
?>
144-
<form method="post" action="options.php">
145-
<?php settings_fields( $key ); ?>
146-
<?php do_settings_sections( $key ); ?>
147-
<table class="form-table">
148-
<?php
149-
foreach ( $option as $item ) {
150-
$value = $this->get_option( $item[ 'key' ], $item[ 'form_type'], $item[ 'default' ] );
151-
?>
152-
<tr valign="top">
153-
<th scope="row">
154-
<?php echo ucfirst( $item[ 'label' ] ); ?>
155-
</th>
156-
<td>
157-
<?php $this->get_form_input( $item, $value ); ?>
158-
</td>
159-
</tr>
160-
<?php } ?>
161-
</table>
162-
<?php submit_button(); ?>
163-
</form>
164-
<?php
165-
}
166-
public function shifter_webhook_settings_page() {
167-
$webhook_url = get_option( 'shifter_webhook_url' );
168-
$webhook_content_type = get_option( 'shifter_webhook_content_type' );
169-
$webhook_content_type = $webhook_content_type ? $webhook_content_type : 'application/json';
170-
?>
171-
<div class="wrap">
172-
<h1>
173-
<?php _e('Webhook Settings', 'shifter-wp-webhook'); ?>
174-
</h1>
175-
<div class="card" style="width: 100%; max-width: 100%;">
176-
<h2>
177-
<?php _e('Outbound Webhook', 'shifter-wp-webhook'); ?>
178-
</h2>
179-
<p>
180-
The plugin can send a POST request with the container URL.<br />
181-
You can run some build script to use the request body parameter.
182-
</p>
183-
<?php $this->show_example( $webhook_url, $webhook_content_type ); ?>
184-
<?php $this->get_form( $webhook_url, $webhook_content_type ); ?>
185-
</div>
186-
</div>
187-
<?
188-
}
189-
}

classes/class.passwordless.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace Shifter_Webhook;
3+
4+
class PasswordlessUtils {
5+
public function __construct() {
6+
$onelogin_file_path = WPMU_PLUGIN_DIR . '/shifter-artifact-helper/include/class-shifter-onelogin.php';
7+
if ( ! file_exists( $onelogin_file_path ) ) {
8+
Log::error( '[Shifter Webhook] Can not read the file:' . $onelogin_file_path );
9+
$this->one_login = null;
10+
} else {
11+
require_once( $onelogin_file_path );
12+
$this->one_login = \ShifterOneLogin::get_instance();
13+
}
14+
}
15+
private function get_magic_link( string $username , string $email = null ) {
16+
if ( ! $this->one_login ) return null;
17+
return $this->one_login->magic_link( $username, $email );
18+
}
19+
public function get_url_by_current_user() {
20+
$user = wp_get_current_user();
21+
return $this->get_url_by_user( $user );
22+
}
23+
public function get_url_by_username( string $username ) {
24+
$user = get_user_by( 'login' , $username);
25+
return $this->get_url_by_user( $user );
26+
}
27+
public function get_url_by_email( string $email ) {
28+
$user = get_user_by( 'email' , $email );
29+
return $this->get_url_by_user( $user );
30+
}
31+
public function get_url_by_user( \WP_User $user ) {
32+
return $this->get_magic_link( $user->user_login, $user->user_email );
33+
}
34+
}

classes/class.request.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
namespace Shifter_Webhook;
3+
4+
class RequestContent {
5+
public function __construct() {
6+
$this->passwordless = new PasswordlessUtils();
7+
}
8+
public function get_base_body() {
9+
$container_url = esc_url( get_option( 'home' ) );
10+
$body = array(
11+
"CONTAINER_URL" => $container_url,
12+
);
13+
return $body;
14+
}
15+
public function push_login_magic_link( array $body, string $url = null ) {
16+
if ( ! $url ) return $body;
17+
return array_merge( $body, array(
18+
"LOGIN_MAGIC_LINK" => $url,
19+
) );
20+
}
21+
/**
22+
* Get request body parameters by current user
23+
*/
24+
public function get_body_by_current_user() {
25+
$base = $this->get_base_body();
26+
$should_add_passwordless = get_option( 'shifter_webhook_send_with_login_url' );
27+
if ( ! $should_add_passwordles ) return $base;
28+
$passwordless_url = $this->passwordless->get_url_by_current_user();
29+
return $this->push_login_magic_link( $base, $passwordless_url );
30+
}
31+
public function get_email( string $email = null ) {
32+
if ( $email ) return $email;
33+
return getenv( 'SHIFTER_USER_EMAIL' );
34+
}
35+
/**
36+
* Get request body parameters by specific email user
37+
* By default, the email get from env "SHIFTER_USER_EMAIL".
38+
*/
39+
public function get_body_by_email( string $email = null ) {
40+
$base = $this->get_base_body();
41+
$should_add_passwordless = get_option( 'shifter_webhook_send_with_login_url' );
42+
if ( ! $should_add_passwordles ) return $base;
43+
$email = $this->get_email( $email );
44+
if ( ! $email ) return $base;
45+
$passwordless_url = $this->passwordless->get_url_by_email( $email );
46+
$this->push_login_magic_link( $base );
47+
}
48+
public function get_body_data() {
49+
$container_url = esc_url( get_option( 'home' ) );
50+
51+
$body = array(
52+
"CONTAINER_URL" => $container_url,
53+
);
54+
55+
$utils = new PasswordlessUtils();
56+
$passwordless_url = $utils->get_url_by_current_user();
57+
if ( ! $passwordless_url ) return $body;
58+
return array_merge( $body, array(
59+
"LOGIN_MAGIC_LINK" => $passwordless_url,
60+
) );
61+
}
62+
63+
}

0 commit comments

Comments
 (0)