Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Last active January 12, 2018 03:46
Show Gist options
  • Save wpsmith/6128174 to your computer and use it in GitHub Desktop.
Save wpsmith/6128174 to your computer and use it in GitHub Desktop.
PHP: WordPress test page
<?php
date_default_timezone_set( 'America/New_York' );
// Load the WordPress Environment
define( 'WP_DEBUG', true ); /* uncomment for debug mode */
require('./wp-load.php');
// User functions
//require('./wp-admin/includes/user.php');
// Debugging
if(!function_exists('wps_printr')){function wps_printr($args,$name=''){if(apply_filters('wps_debug_off',false))return;if(''!=$name)echo'<strong>'.$name.'</strong><br/>';echo'<pre>',htmlspecialchars(print_r($args,true)),"</pre>\n";}}if(function_exists('wps_printr')&&!function_exists('wps_die')){function wps_die($args,$name=''){if(apply_filters('wps_debug_off',false))return;wp_die(wps_printr($args,$name));}}
<?php
// Set date, just in case
date_default_timezone_set( 'America/New_York' );
// Load the WordPress Environment
define( 'WP_DEBUG', TRUE ); // Set to FALSE to remove DEBUG MODE
require( './wp-load.php' );
// User functions
//require( './wp-admin/includes/user.php' );
//** Admin **//
/**
* Pluggable function, prevent redirect to admin pages
* @author Travis Smith <t@wpsmith.net>
*/
function auth_redirect() {
$loginusername = 'admin';
$user = get_user_by( 'login', $loginusername );
wp_set_current_user( $user->ID, $loginusername );
wp_set_auth_cookie( $user->ID );
}
/**
* Load the WordPress Admin Environment
* @author Travis Smith <t@wpsmith.net>
*/
function wps_load_admin() {
// Select either Network or User Admin
// define( 'WP_NETWORK_ADMIN', TRUE ); // Used in is_network_admin()
define( 'WP_USER_ADMIN', TRUE ); // Used in is_user_admin()
define( 'WP_BLOG_ADMIN', TRUE ); // Used in is_blog_admin()
define( 'WP_ADMIN', TRUE ); // Used in is_admin()
// Required for admin.php & prevent errors
global $wp_db_version, $_wp_submenu_nopriv;
require( './wp-admin/admin.php' );
echo '<h1>Admin Loaded</h1>';
}
// Load Admin
//wps_load_admin();
// WordPress Plugins API
//require( './wp-admin/includes/plugin_install.php' );
// Endpoints
//https://api.wordpress.org/plugins/info/1.0/{slug}
//https://api.wordpress.org/plugins/info/1.0/{slug}.json
//https://api.wordpress.org/plugins/update-check/1.0/
//https://api.wordpress.org/plugins/update-check/1.1/
// Importers
//https://api.wordpress.org/core/importers/1.0/ (serialized)
//https://api.wordpress.org/core/importers/1.1/ (JSON)
//Checksum
//https://api.wordpress.org/core/checksums/1.0/?version={wp-version}&locale=en_US (JSON)
//** Debugging **//
if ( ! function_exists( 'wps_printr' ) ) {
/**
* Pretty prints variable (array) value.
* @author Travis Smith <t@wpsmith.net>
*
* @param $args Variable to be outputted.
* @param string $name Title to output before the variable output.
*/
function wps_printr( $args, $name = '' ) {
if ( apply_filters( 'wps_debug_off', false ) ) {
return;
}
if ( '' !== $name && false !== $name ) {
printf( '<strong>%s</strong><br/>', $name );
}
if ( is_array( $args ) ) {
printf( '<pre>%s</pre>', htmlspecialchars( print_r( $args, true ) ) );
} else {
var_dump( $args );
}
}
}
if ( function_exists( 'wps_printr' ) && ! function_exists( 'wps_die' ) ) {
/**
* Pretty prints variable (array) value & dies.
* @author Travis Smith <t@wpsmith.net>
*
* @param $args Variable to be outputted.
* @param string $name Title to output before the variable output.
*/
function wps_die( $args, $name = '' ) {
if ( apply_filters( 'wps_debug_off', false ) ) {
return;
}
wp_die( wps_printr( $args, $name ) );
}
}
/**
* Plugins API Remote Post Example
*
* @author Travis Smith <t@wpsmith.net>
*/
function wps_plugins_api_remote_post() {
$args = (object) array( 'slug' => 'custom-favicon' );
$request = array( 'action' => 'plugin_information', 'timeout' => 15, 'request' => serialize( $args ) );
$url = 'http://api.wordpress.org/plugins/info/1.0/';
$response = wp_remote_post( $url, array( 'body' => $request ) );
$plugin_info = unserialize( $response['body'] );
wps_printr( $plugin_info, 'plugin_info' );
}
/**
* Helper function to output plugins_api()
*
* @author Travis Smith <t@wpsmith.net>
* @param $result array Array of plugins_api() result.
*/
function wps_output_result( $result ) {
/** Check for Errors & Display the results */
if ( is_wp_error( $result ) ) {
wps_printr( $result->get_error_message() );
} else {
wps_printr( $result );
}
}
/**
* Plugins API using plugins_api(): Get specific plugin information
*
* @author Travis Smith <t@wpsmith.net>
*/
function wps_plugins_api_plugin_information() {
$call_api = plugins_api( 'plugin_information', array( 'slug' => 'custom-favicon' ) );
wps_output_result( $call_api );
}
/**
* Plugins API using plugins_api(): Get plugins with tag
*
* @author Travis Smith <t@wpsmith.net>
*/
function wps_plugins_api_hot_tags() {
/** Prepare our query */
$call_api = plugins_api( 'hot_tags',
array(
'number' => '50',
)
);
wps_output_result( $call_api );
}
/**
* Plugins API using plugins_api(): Search plugins
*
* @author Travis Smith <t@wpsmith.net>
*/
function wps_plugins_api_query_plugins() {
$call_api = plugins_api( 'query_plugins',
array(
'browse' => 'top-rated',
'page' => '1',
'per_page' => '5',
'fields' => array(
'downloaded' => false,
'rating' => false,
'description' => false,
'short_description' => false,
'donate_link' => false,
'tags' => false,
'sections' => false,
'homepage' => false,
'added' => false,
'last_updated' => false,
'compatibility' => false,
'tested' => false,
'requires' => false,
'downloadlink' => true,
)
)
);
wps_output_result( $call_api );
}
// Execute plugins_api() functions
//wps_plugins_api_remote_post();
//wps_plugins_api_hot_tags();
//wps_plugins_api_query_plugins();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment