Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active Jan 25, 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment