The next part of this web tutorial involves creating a WordPress plugin. This is entirely dependent upon you having a WordPress instance installed on your server.
In essence, you create a folder (let's call it
tt_selfaffirmations) under the
plugins directory of the
wp-content directory, then create a PHP file in there. The starting comments help WordPress identify that this is the plugin file.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.php/**
* Plugin Name: Self-affirmations
* Plugin URI: http://www.teochewthunder.com
* Description: This plugin implements self-affirmations
* Version: 1.0.0
* Author: TeochewThunder
* Author URI: http://www.teochewthunder.com
* License: GPL2
*/
This next function,
wc_admin_menu(), is added to the
admin_menu hook. The final effect is that we will add menu items into the side menu for testing purposes.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.php/**
* Plugin Name: Self-affirmations
* Plugin URI: http://www.teochewthunder.com
* Description: This plugin implements self-affirmations
* Version: 1.0.0
* Author: TeochewThunder
* Author URI: http://www.teochewthunder.com
* License: GPL2
*/
function wc_admin_menu()
{
}
add_action("admin_menu", "wc_admin_menu", 11);
In here, we call the built-in function
add_submenu_page(). We use
index.php as the page to which to execute the code in. The next two arguments are just the label for the link. "manage_options" is the minimum user capability required, and this particular value ensures that only admins can see the link added. "tt_get_readytoreceive" is the name of the function we will call when clicking that link, and we will create that function soon.
Here's some
info on the
add_submenu_page() function.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction wc_admin_menu()
{
add_submenu_page("index.php", "Test Readytoreceive", "Test Readytoreceive", "manage_options", "tt_get_readytoreceive", "tt_get_readytoreceive");
}
add_action("admin_menu", "wc_admin_menu", 11);
And of course, we will need to create the
tt_get_readytoreceive() function.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction wc_admin_menu()
{
add_submenu_page("index.php", "Test Readytoreceive", "Test Readytoreceive", "manage_options", "tt_get_readytoreceive", "tt_get_readytoreceive");
}
add_action("admin_menu", "wc_admin_menu", 11);
function tt_get_readytoreceive()
{
}
If you refresh your WordPress console, you should see the new link at the sidebar.
While we're at it, we may as well create four more links.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction wc_admin_menu()
{
add_submenu_page("index.php", "Test Readytoreceive", "Test Readytoreceive", "manage_options", "tt_get_readytoreceive", "tt_get_readytoreceive");
add_submenu_page("index.php", "Test Terms", "Test Terms", "manage_options", "tt_get_terms", "tt_get_terms");
add_submenu_page("index.php", "Test Lastsent", "Test Lastsent", "manage_options", "tt_set_lastsent", "tt_set_lastsent");
add_submenu_page("index.php", "Test Generate Email", "Test Generate Email", "manage_options", "tt_generate_mail", "tt_generate_mail");
add_submenu_page("index.php", "Test Job", "Test Job", "manage_options", "tt_selfaffirmations", "tt_selfaffirmations");
}
We will, of course, need to create all these functions. Of all these functions, in this part of the web tutorial, we will only concentrate on the first three. These are the tests that will test the REST endpoints we created in the previous part of this web tutorial.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction wc_admin_menu()
{
add_submenu_page("index.php", "Test Readytoreceive", "Test Readytoreceive", "manage_options", "tt_get_readytoreceive", "tt_get_readytoreceive");
add_submenu_page("index.php", "Test Terms", "Test Terms", "manage_options", "tt_get_terms", "tt_get_terms");
add_submenu_page("index.php", "Test Lastsent", "Test Lastsent", "manage_options", "tt_set_lastsent", "tt_set_lastsent");
add_submenu_page("index.php", "Test Generate Email", "Test Generate Email", "manage_options", "tt_generate_mail", "tt_generate_mail");
add_submenu_page("index.php", "Test Job", "Test Job", "manage_options", "tt_selfaffirmations", "tt_selfaffirmations");
}
add_action("admin_menu", "wc_admin_menu", 11);
function tt_get_readytoreceive()
{
}
function tt_get_terms()
{
}
function tt_set_lastsent()
{
}
function tt_generate_mail()
{
}
function tt_selfaffirmations()
{
}
And here's five links instead of one!
We continue with the
tt_get_readytoreceive() function. We're going to use cURL to get data via the REST endpoints. Thus, we begin by calling the
curl_init() function and set it to the variable
cURLConnection.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_readytoreceive()
{
$cURLConnection = curl_init();
}
We follow up by using the
curl_setopt() function to set the variables
CURLOPT_URL and
CURLOPT_RETURNTRANSFER for
cURLConnection. For
CURLOPT_URL, we use the URL that we defined for the endpoint
readytoreceive. And
CURLOPT_RETURNTRANSFER has to be
true.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_readytoreceive()
{
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/readytoreceive");
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
}
We then run the
curl_exec() function using
cURLConnection as an argument. The result is assigned to the variable
json_list. After that, we use the function
curl_close() just as a matter of neatness.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_readytoreceive()
{
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/readytoreceive");
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
}
OK, so over here, we display
json_list on screen.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_readytoreceive()
{
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/readytoreceive");
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
}
Run the code by clicking on the "Test readytoreceive" link. See what happens? Look familiar? If you didn't get anything, then you need to get back to the Oracle
APEX database to change the
LAST_SENT value back to "01/01/1970".
In case the screenshot was unclear, here's the JSON object.
{"items":[{"email":"teochewthunder@gmail.com","first_name":"Teochew","last_name":"Thunder","dob":"1977-11-05T00:00:00Z","gender":"M"}],"hasMore":false,"limit":25,"offset":0,"count":1,"links":[{"rel":"self","href":"https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/readytoreceive"},{"rel":"describedby","href":"https://apex.oracle.com/pls/apex/teochewthunder/metadata-catalog/mailinglist/item"},{"rel":"first","href":"https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/readytoreceive"}]}
Well, you know what the JSON looks like now. So we use
json_decode() to decode
json_list, and assign the result to
list. Then we return the
items array from the
list object.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_readytoreceive()
{
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/readytoreceive");
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
$list = json_decode($json_list);
return $list->items;
}
Now we will work on
tt_get_terms(). This is the function that calls the REST endpoint that returns the user's list of terms. It's largely the same as the last one, so just copy everything over first.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_terms()
{
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/readytoreceive");
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
$list = json_decode($json_list);
return $list->items;
}
For this function, there is a parameter,
id, which is the user's email address. We will then change the URL, and append the value of
id at the end.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_terms($id)
{
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/terms/" . $id);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
$list = json_decode($json_list);
return $list->items;
}
We add this line to ensure that if
id is
NULL, as would be the case in a test, we'll use "teochewthunder@gmail.com".
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_terms($id)
{
$id = ($id ? $id : "teochewthunder@gmail.com");
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/terms/" . $id);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
$list = json_decode($json_list);
return $list->items;
}
Test this. You should get the following output.
This is the JSON output. It could potentially be quite lengthy.
{"items":[{"type":"DESCRIPTIONS","term":"acidic"},{"type":"DESCRIPTIONS","term":"cynical"},{"type":"DESCRIPTIONS","term":"jolly"},{"type":"DESCRIPTIONS","term":"patient"},{"type":"DESCRIPTIONS","term":"pragmatic"},{"type":"INTERESTS","term":"comedy"},{"type":"INTERESTS","term":"programming"},{"type":"INTERESTS","term":"soccer"},{"type":"INTERESTS","term":"superheroes"}],"hasMore":false,"limit":25,"offset":0,"count":9,"links":[{"rel":"self","href":"https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/terms/teochewthunder@gmail.com"},{"rel":"describedby","href":"https://apex.oracle.com/pls/apex/teochewthunder/metadata-catalog/mailinglist/terms/item"},{"rel":"first","href":"https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/terms/teochewthunder@gmail.com"}]}
Now, for this function, we don't want to return the entire list. We want to randomly include terms, so as to vary the output more. We begin by declaring two arrays,
interests and
descriptions. We'll change the returned output to an array containing the arrays interests and descriptions, named the same.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_terms($id)
{
$id = ($id ? $id : "teochewthunder@gmail.com");
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/terms/" . $id);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
$list = json_decode($json_list);
$interests = [];
$descriptions = [];
return ["interests" => $interests, "descriptions" => $descriptions];
}
Then we use a
Foreach loop on the
items array of the
list object.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_terms($id)
{
$id = ($id ? $id : "teochewthunder@gmail.com");
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/terms/" . $id);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
$list = json_decode($json_list);
$interests = [];
$descriptions = [];
foreach($list->items as $i) {
}
return ["interests" => $interests, "descriptions" => $descriptions];
}
Depending on the value of the
type property of the current element, add the
term property to either
interests or
descriptions.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_terms($id)
{
$id = ($id ? $id : "teochewthunder@gmail.com");
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/terms/" . $id);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
$list = json_decode($json_list);
$interests = [];
$descriptions = [];
foreach($list->items as $i) {
if ($i->type == "INTERESTS") $interests[] = $i->term;
if ($i->type == "DESCRIPTIONS") $descriptions[] = $i->term;
}
return ["interests" => $interests, "descriptions" => $descriptions];
}
We then use the
rand() function in each
If block, giving a 50% probability of the term being added to their respective arrays.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_get_terms($id)
{
$id = ($id ? $id : "teochewthunder@gmail.com");
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/terms/" . $id);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
$list = json_decode($json_list);
$interests = [];
$descriptions = [];
foreach($list->items as $i) {
if ($i->type == "INTERESTS" && rand(0, 1) == 1) $interests[] = $i->term;
if ($i->type == "DESCRIPTIONS" && rand(0, 1) == 1) $descriptions[] = $i->term;
}
return ["interests" => $interests, "descriptions" => $descriptions];
}
For the last REST endpoint, we again copy the contents of
tt_get_readytoreceive() into
tt_set_lastsent().
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_set_lastsent()
{
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/readytoreceive");
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
$list = json_decode($json_list);
return $list->items;
}
As with
tt_get_terms(), we add a parameter and change the URL. We'll also add the line to use "teochewthunder@gmail.com" as the default value of
id.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_set_lastsent($id)
{
$id = ($id ? $id : "teochewthunder@gmail.com");
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/setreceived/" . $id);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
echo $json_list;
$list = json_decode($json_list);
return $list->items;
}
We don't want to display anything, or return anything.
wp-content/plugins/tt_selfaffirmations/tt_selfaffirmations.phpfunction tt_set_lastsent($id)
{
$id = ($id ? $id : "teochewthunder@gmail.com");
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, "https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/setreceived/" . $id);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$json_list = curl_exec($cURLConnection);
curl_close($cURLConnection);
//echo $json_list;
//$list = json_decode($json_list);
return; //$list->items;
}
Now test this. You should get nothing in the display.
If you test the first link again, this time you should not see the user in the list, because the value of
LAST_SENT has been set to today's date.
In the JSON object, you see that the
items property is now an empty array.
{"items":[],"hasMore":false,"limit":25,"offset":0,"count":0,"links":[{"rel":"self","href":"https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/readytoreceive"},{"rel":"describedby","href":"https://apex.oracle.com/pls/apex/teochewthunder/metadata-catalog/mailinglist/item"},{"rel":"first","href":"https://apex.oracle.com/pls/apex/teochewthunder/mailinglist/readytoreceive"}]}
This wasn't very hard, was it? We basically built on what we already created in Oracle
APEX. The next part will also deal with REST endpoints, but it will be
ChatGPT's REST endpoint.
Next
Generating email content using ChatGPT.