playground-tools icon indicating copy to clipboard operation
playground-tools copied to clipboard

interactive-code-block: correct way to add custom PHP functionality

Open jonathanbossenger opened this issue 2 years ago • 2 comments

Background

I am trying to add core WordPress functionality to the interactive code block. As part of the process of adding the block to Learn WordPress, I would like to be able to run WordPress-specific PHP code (eg get_posts(), or get_option()) in the context of the interactive code block on tutorials and courses on Learn WordPress.

I chatted to @adamziel before he went on sabbatical, and he suggested the best way to implement this would be through a custom .phar library, uploaded via the interactive code block admin pages, and configured as an execution script.

Building a test phar.

In order to test this, I have built a custom phar that merely replicates a simple get_posts() function call with no arguments. The code can be found here and the phar itself can be downloaded here.

I can test this phar using the following code in a simple PHP file, located in the same place as the phar:

<?php
require 'pharpress.phar';
$posts = get_posts();
foreach ( $posts as $post ) {
	echo $post['post_title'] . "\n";
}

This works as expected, and the faux posts are fetched and output to the browser:

pharpress-test-phartest-php

Configuring the phar as an execution script

First I uploaded this phar to the Phar libraries admin page of the Interactive Code Block:

Libraries-‹-LearnPress-—-WordPress

Then, I configured this as an execution script on the Execution Scripts page:

Execution-Scripts-‹-LearnPress-—-WordPress

The problem

When I then add the interactive code block to a post or page, and configure the execution script in the blocks's settings to my custom execution script, and attempt to call the custom get_posts() function, the faux posts do not seem to be returned.

Edit-Post-“Hello-world-”-‹-LearnPress-—-WordPress

I can see that the phar is being used because the <h1>Hello from PHP</h1> output is generated, by my PHP code snippet in the interactive code block to call get_posts does not render the output as expected.

What I am not sure of is if this is a bug in my phar, or a bug in the interactive code block, or something that I have configured incorrectly.

I'm at a loss as to how to debug this further, as I have no idea how I could debug whether the PHP code in the snippet is working correctly, or if there's a problem with accessing the data from my phar. So any advice or suggestions would be greatly appreciated.

jonathanbossenger avatar Jul 08 '23 15:07 jonathanbossenger

I can see that the phar is being used because the <h1>Hello from PHP</h1> output is generated, by my PHP code snippet in the interactive code block to call get_posts does not render the output as expected.

@jonathanbossenger, I found out the solution: In the content of your execution script you need to add require $SCRIPT_PATH; to execute the block content and not only your library.

<?php
require "pharpress.phar";
require $SCRIPT_PATH;
254969953-29b78174-dbdc-434a-8572-d382908e43e3

Let me know if that solves your issue.

sejas avatar Jul 20 '23 18:07 sejas

I did some exploration and the WordPress Playground execution script is supposed to load the Playground, but for some reason in my testing it didn't.

The code block there appears to be a JavaScript code area that exposes the client global, where client is the actual Playground Client. Unfortunately the API it exposes doesn't match the docs for the client.

I think the idea is that we can run client.run( '<?php foreach ( get_posts() as $post ) { … }' ) and this returns a Promise representing the HTTP response as if that were running on the server. Unfortunately again I could only get it to return an empty response.

dmsnell avatar Jul 21 '23 02:07 dmsnell