Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active Mar 4, 2022
Embed
What would you like to do?
[Using Ray in WordPress Development] Code Samples
<?php
// ...
// Enable WP_DEBUG mode
define( 'WP_DEBUG', true );
// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );
// Disable display of errors and warnings
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', true );
/* That's all, stop editing! Happy publishing. */
{
"name": "tommcfarlin/ray-for-wordpress",
"description": "An example project for how to use Spatie Ray in WordPress Development.",
"type": "wordpress-plugin",
"license": "GPL-3.0-or-later",
"homepage": "https://github.com/tommcfarlin/ray-for-wordpress",
"authors": [
{
"name": "Tom McFarlin",
"email": "tom@tommcfarlin.com",
"homepage": "https://tommcfarlin.com"
}
],
"support": {
"issues": "https://github.com/tommcfarlin/ray-for-wordpress/issues"
},
"config": {
"preferred-install": "dist",
"platform": {
"php": "7.4.27"
},
"allow-plugins": {
"composer/installers": true
}
},
"repositories": [
{
"type": "composer",
"url": "https://wpackagist.org"
}
],
"require": {
"php": "7.4.27",
"composer/installers": "^1.4"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.5",
"spatie/ray": "^1.29.0"
},
"autoload": {
"psr-4": {
"RayForWP\\": "src"
}
},
"minimum-stability": "stable"
}
<?php
/**
* Ray For WordPress
*
* An example project for how to use Spatie Ray in WordPress Development.
*
* PHP version 7.4.27
*
* @category WordPress_Plugin
* @package RayForWP
* @author Tom McFarlin <tom@tommcfarlin.com>
* @license GPLv3 <https://www.gnu.org/licenses/gpl-3.0.en.html>
* @link https://github.com/tommcfarlin/ray-for-wordpress/
* @since 11 January 2022
*
* @wordpress-plugin
* Plugin Name: Ray For WordPress
* Plugin URI: https://github.com/tommcfarlin/ray-for-wordpress/
* Description: An example project for how to use Spatie Ray in WordPress Development.
* Author: Tom McFarlin <tom@tommcfarlin.com>
* Version: 1.0.0
*/
namespace RayForWP;
defined('WPINC') || die;
require_once __DIR__ . '/vendor/autoload.php';
<?php
/**
* Ray For WordPress
*
* An example project for how to use Spatie Ray in WordPress Development.
*
* PHP version 7.4.27
*
* @category WordPress_Plugin
* @package RayForWP
* @author Tom McFarlin <tom@tommcfarlin.com>
* @license GPLv3 <https://www.gnu.org/licenses/gpl-3.0.en.html>
* @link https://github.com/tommcfarlin/ray-for-wordpress/
* @since 11 January 2022
*
* @wordpress-plugin
* Plugin Name: Ray For WordPress
* Plugin URI: https://github.com/tommcfarlin/ray-for-wordpress/
* Description: An example project for how to use Spatie Ray in WordPress Development.
* Author: Tom McFarlin <tom@tommcfarlin.com>
* Version: 1.0.0
*/
namespace RayForWP;
defined('WPINC') || die;
require_once __DIR__ . '/vendor/autoload.php';
add_filter(
'the_content',
/**
* Renders the content to the browser from the database.
*
* @param string $content The content coming from WordPress.
*
* @return string $content The processed content to be sent to the browser.
*/
function (string $content): string {
if (!is_single()) {
return $content;
}
// TODO: More code to come...
return $content;
}
);
<?php
function (string $content): string {
if (!is_single()) {
return $content;
}
ray(current_user_can('manage_options'));
return $content;
}
<?php
function (string $content): string {
if (!is_single()) {
return $content;
}
$user = wp_get_current_user();
ray($user);
return $content;
}
<?php
function (string $content): string {
if (!is_single()) {
return $content;
}
$user = wp_get_current_user();
ray($user);
ray(
get_user_meta($user->data->ID, 'show_admin_bar_front', true)
);
return $content;
}
<?php
function (string $content): string {
if (!is_single()) {
return $content;
}
$user = wp_get_current_user();
// Removed for brevity.
$capabilities = [];
foreach ($user->allcaps as $key => $value) {
$capabilities[$key] = $value;
if (15 < count($capabilities)) {
break;
}
}
ray()->table($capabilities);
return $content;
}
<?php
$capabilities = [];
foreach ($user->allcaps as $key => $value) {
$capabilities[$key] = $value;
if (15 < count($capabilities)) {
break;
}
}
<?php
/**
* Retrieves the first 15 capabilities from the specified user.
*
* @param WP_User $user The user from which to retrieve the capabilities.
*
* @return array $capabilities The array of the first 15 capabilities from the user.
*/
function getUserCapabilities(WP_User $user): array
{
$capabilities = [];
foreach ($user->allcaps as $key => $value) {
$capabilities[$key] = $value;
if (15 < count($capabilities)) {
break;
}
}
return $capabilities;
}
<?php
add_filter(
'the_content',
/**
* Renders the content to the browser from the database.
*
* @param string $content The content coming from WordPress.
*
* @return string $content The processed content to be sent to the browser.
*/
function (string $content): string {
// ...
ray()->table(getUserCapabilities($user));
// ...
}
);
<?php
/**
* Retrieves the first 15 capabilities from the specified user.
*
* @param WP_User $user The user from which to retrieve the capabilities.
*
* @return array $capabilities The array of the first 15 capabilities from the user.
*/
function getUserCapabilities(WP_User $user): array
{
ray()->caller();
$capabilities = [];
foreach ($user->allcaps as $key => $value) {
$capabilities[$key] = $value;
if (15 < count($capabilities)) {
break;
}
}
return $capabilities;
}
<?php
add_filter(
'the_content',
/**
* Renders the content to the browser from the database.
*
* @param string $content The content coming from WordPress.
*
* @return string $content The processed content to be sent to the browser.
*/
function (string $content): string {
if (!is_single()) {
return $content;
}
$user = wp_get_current_user();
ray($user);
ray()->table(getUserCapabilities($user));
ray()->trace();
return $content;
}
);
<?php
function getUserCapabilities(WP_User $user): array
{
$capabilities = [];
ray()->pause();
foreach ($user->allcaps as $key => $value) {
$capabilities[$key] = $value;
if (15 < count($capabilities)) {
break;
}
}
return $capabilities;
}
<?php
function getUserCapabilities(WP_User $user): array
{
$capabilities = [];
ray('Pausing execution...');
ray()->caller();
ray($user->data->ID);
ray($capabilities);
ray()->pause();
foreach ($user->allcaps as $key => $value) {
$capabilities[$key] = $value;
if (15 < count($capabilities)) {
break;
}
}
return $capabilities;
}
<?php
/**
* Retrieves the first 100 results from the postmeta table.
* This function is used specifically for demonstration purposes
* of measuring performance.
*
* @param int $number The number of records to return.
* @param bool $measure Whether or not to meawsure the performance with Ray.
*
* @return array $results The array of results queried.
*/
function getMetadataRecords(int $number, bool $measure): array
{
global $wpdb;
if ($measure) {
ray()->measure();
}
$results = $wpdb->get_results(
$wpdb->prepare(
"
SELECT *
FROM $wpdb->postmeta
LIMIT %d
",
$number,
),
ARRAY_A
);
if ($measure) {
ray()->measure();
}
return $results;
}
<?php
add_action(
'plugins_loaded', function () {
$number = 1000;
ray("Testing $number records...");
$records = getMetadataRecords($number, true);
}
);
<?php
add_action(
'plugins_loaded', function () {
$number = 10;
ray("Testing $number records...");
$records = getMetadataRecords($number, true);
}
);
<?php
add_action(
'plugins_loaded', function () {
$number = 100;
ray("Testing $number records...");
$records = getMetadataRecords($number, false);
if (true) {
ray()->measure()->green("Start foreach...");
foreach ($records as $record) {
$record;
}
ray()->measure()->green("...Done.");
} else {
// ...
}
}
);
<?php
add_action(
'plugins_loaded', function () {
$number = 1000;
ray("Testing $number records...");
$records = getMetadataRecords($number, false);
if (true) {
ray()->measure()->green("Start foreach...");
foreach ($records as $record) {
$record;
}
ray()->measure()->green("...Done.");
} else {
// ...
}
}
);
<?php
add_action(
'plugins_loaded', function () {
$number = 100;
ray("Testing $number records...");
$records = getMetadataRecords($number, false);
if (true) {
// ...
} else {
ray()->measure()->red("Start array_map...");
array_map(
function ($record) {
$record;
}, $records
);
ray()->measure()->red("...Done.");
}
}
);
<?php
add_action(
'plugins_loaded', function () {
$number = 1000;
ray("Testing $number records...");
$records = getMetadataRecords($number, false);
if (true) {
// ...
} else {
ray()->measure()->red("Start array_map...");
array_map(
function ($record) {
$record;
}, $records
);
ray()->measure()->red("...Done.");
}
}
);
<?php
add_action(
'plugins_loaded',
function () {
// First, we want to see who called this function.
ray()->caller();
ray()->separator();
}
);
<?php
if ('done' === get_option('ray-for-wordpress', true)) {
return;
}
<?php
update_option('ray-for-wordpress', 'done');
<?php
add_action(
'plugins_loaded',
function () {
if ('done' === get_option('ray-for-wordpress', true)) {
return;
}
// First, we want to see who called this function.
ray()->caller();
ray()->separator();
update_option('ray-for-wordpress', 'done');
}
);
<?php
/**
* Generates a set of random numbers up to the specified size.
*
* This uses PHP's `rand()` function to generate the numbers that
* are pushed into the array.
*
* @param mixed $min The lower bound of the random numbers.
* @param mixed $max The upper mount of the random numbers.
* @param mixed $size The total number of random numbers to generate
*
* @return array $numbers The array of randomly generatored numbers.
*/
function generateNumbers(int $min, int $max, int $size): array
{
$numbers = [];
for ($i = 0; $i < $size; $i++) {
$numbers[] = rand($min, $max);
}
return $numbers;
}
<?php
// ...
// Now let's populate an array with 10 elements.
$randomNumbers = generateNumbers(0, 100, 10);
ray($randomNumbers);
ray()->separator();
// ...
<?php
// ...
// Now let's populate an array with 10 elements.
ray("Start with 10 numbers...")->measure();
$randomNumbers = generateNumbers(0, 100, 10);
ray($randomNumbers);
ray("...Done")->measure();
ray()->separator();
// ...
<?php
// ...
ray()->table($randomNumbers);
// ...
<?php
/**
* Measures the time is takes to generate random numbers based on
* an incoming array of predefined size.
*
* This function would be called like measureNumbers([10, 20, 30]).
* If any non-integers are passed, then the function would exit whenever
* that value was found.
*
* @param array $sizes The array of what size arrays to generate.
*
* @return void
*/
function measureNumbers(array $sizes)
{
array_map(function ($size) {
if (!is_int($size)) {
return;
}
ray()->measure(); // Start measuring.
$randomNumbers = generateNumbers(0, 500, $size);
ray()->measure(); // Render the "Time since last call..."
ray()->separator();
}, $sizes);
}
<?php
/**
* Iterates through the specified array of numbers.
*
* @param array $numbers The array of numbers through which we'll iteratoe.
*
* @return void.
*/
function iterateThroughArray(array $numbers)
{
for ($i < 0; $i < count($numbers); $i++) {
$currentNumber = $numbers[$i];
// More to come...
}
}
<?php
/**
* Measures the time is takes to generate random numbers based on
* an incoming array of predefined size.
*
* This function would be called like measureNumbers([10, 20, 30]).
* If any non-integers are passed, then the function would exit whenever
* that value was found.
*
* @param array $sizes The array of what size arrays to generate.
*
* @return void
*/
function measureNumbers(array $sizes)
{
array_map(function ($size) {
if (!is_int($size)) {
return;
}
ray()->measure(); // Start measuring.
iterateThroughArray(
generateNumbers(0, 500, $size)
);
ray()->measure(); // Render the "Time since last call..."
ray()->separator();
}, $sizes);
}
<?php
function iterateThroughArray(array $numbers)
{
ray($numbers);
ray()->pause();
for ($i < 0; $i < count($numbers); $i++) {
$currentNumber = $numbers[$i];
// More to come...
}
}
<?php
// If the index is 10, pause execution.
if (10 === $i) {
ray("Pausing iterating at $i.");
ray()->pause();
// TODO This is where you can set custom code to run!
ray('Now we are resuming...');
ray(wp_get_current_user());
// Set $i to the length of the array then hit continue.
$i = count($numbers);
ray("Now we've set the index to: $i."); // Print this into Ray for good measure
ray()->separator();
}
<?php
add_action(
'shutdown',
function () {
ray('Deleting the ray-for-wordpress option since we are done.');
delete_option('ray-for-wordpress');
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment