Skip to content

Instantly share code, notes, and snippets.

@xxsimoxx
Last active April 20, 2023 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xxsimoxx/05101505cdd12e2c00c1d0979f037d7a to your computer and use it in GitHub Desktop.
Save xxsimoxx/05101505cdd12e2c00c1d0979f037d7a to your computer and use it in GitHub Desktop.
Test Application Passwords
<?php
/**
* Plugin Name: Test Application Password
*/
namespace XXSimoXX\testAP;
if (!defined('ABSPATH')){
die('-1');
};
class testApplicationPassword {
public $username = 'USERNAME';
public $application_password = 'XXXX XXXX XXXX XXXX XXXX XXXX';
public $url = 'classicpress-v2:8890';
public function __construct() {
add_action('admin_menu', [$this, 'create_menu'], 100);
add_filter('http_request_args', [$this, 'fix_ssl_issues'], 10, 2);
}
public function render_page() {
$request = wp_remote_post(
'https://'.$this->url.'/wp-json/wp/v2/posts',
array(
'headers' => array(
'Authorization' => "BASIC ". base64_encode($this->username.':'.$this->application_password)
),
'body' => array(
'title' => 'Post using REST API '.rand(),
'content' => 'Post content using REST API to test Application Password.',
'status' => 'publish',
)
)
);
echo '<h1>Response message</h1>';
$response = wp_remote_retrieve_response_message($request);
echo '<pre>';
var_dump( $response );
echo '</pre>';
echo '<h1>Response body</h1>';
$post = json_decode( wp_remote_retrieve_body($request));
echo '<pre>';
var_dump( $post );
echo '</pre>';
}
public function create_menu() {
if (current_user_can('edit_posts')) {
$page = add_menu_page(
'Test Application Password',
'Test App Pass',
'edit_posts',
'testap',
[$this, 'render_page'],
'dashicons-pets'
);
}
}
public function fix_ssl_issues($r, $url) {
$domain = '/^https?:\/\/'.$this->url.'/';
if (preg_match($domain, $url) === 1) {
$r['sslverify'] = false;
return $r;
}
return $r;
}
}
new testApplicationPassword;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment