Develop Your First Zend Server Plugin – Part 1

Zend Server 8.5 introduces a brand-new concept called “Plugins”. This concept is centered around the Zend Gallery – a marketplace for plugins that extend Zend Server functionality, and that were developed by, and for, the Zend Server user community.

What are plugins? Put simply, plugins are pieces of code that can extend Zend Server in either, or both, of the following two ways: they can add panels to Z-Ray, thus displaying additional information about your environment that you want shown, or – they can define the routing logic for your application’s requests. This plugin type helps Zend Server aggregate monitoring events, and display improved results for URLs listed in URL Insight.

In this article, the first in a series of articles describing Zend Server plugin development, you’ll be reading how to write a Z-Ray plugin for WordPress. This plugin extracts and displays a list of loaded WordPress plugins, and is based on the official Z-Ray WordPress plugin that’s bundled with Zend Server. While just an example, it does well to articulate the main constructs plugins Z-Ray plugins include.

All the documentation you need to learn how to build a plugin for Z-Ray is detailed on GitHub.

Note: You will be provided with code snippets throughout this tutorial. You can download the entire plugin here.

Prerequisites

To follow the steps in this tutorial, you will need to do the following:

  • Download, install, and launch Zend Server 8.5 and above
  • Move the existing built-in WordPress plugin located at:
    • Mac and Linux: /usr/local/zend/var/plugins/wordpress
    • Windows: C:\Program Files (x86)\Zend\ZendServer\data\plugins\wordpress
  • Install WordPress on Zend Server.

Step 1 : Creating your Plugin Folder and the ‘zray.php’ File

Your first step is to go to the Z-Ray Plugins folder in your Zend Server installation files (see the file path in the Prerequisites above), and create a new folder with the name of your plugin.

Within this folder, create another folder called ‘Z-Ray’ – this folder will hold the Z-Ray plugin files.

In this folder, we’re going  to create a new PHP file called ‘zray.php’. This file is the plugin’s include file, and it’s good practice to have it in the root of the plugin directory.

Here’s a simple diagram to portray the plugin structure we’ve just created:

Step 2 : Declaring the New Plugin

In the newly created ‘zray.php’ file, you’re now going to enter a class for declaring the new plugin:

This class is the main API for defining all the plugin’s trace actions and metadata.

Step 3 : Enabling the Plugin

Next, you’re going to enable the plugin.
To do this, we’re going to enter the following expression:

We’re basically telling Z-Ray to load the plugin after the ‘wp_initial_constants’ method is called. Since this is the first WordPress function that’s called on a page, it’s the perfect trigger to use.

Step 4 : Declaring a Trace

The next part is to declare a trace on a particular function. This is the main functional part of the plugin as it defines the function to be traced and the data to be displayed in Z-Ray.
The declaration includes the name of the function to be traced, and two callables which are fired when entering and exiting the function. Something along the lines of:

In our case, we’re going to use the ‘wp_cache_close’ function since we know this is a good time to retrieve a list of WordPress plugins.
We can leave the entry callable empty, but we’re going to develop the exit callable a bit.
First, we’re going to add two parameters that the traceFunction callable method accepts:

  • $context is an array of information about the function
  • $storage is a reference to the storage array, and is used to store the info that will actually be displayed in Z-Ray.

Next, we’re going to code what we want executed upon leaving the ‘wp_cache_close‘ function.
So what do we want executed upon leaving the ‘wp_cache_close’ function?
We’re going to start by entering the WordPress method for extracting plugin data.

Now, since we don’t really know what this data actually looks like, we’re going to first dump the $plugins object in a new Z-Ray panel and check out what it looks like in Z-Ray. The way to do this is very simple:

Let’s see what it looks like in Z-Ray. Save the file, and open WordPress in your browser.

pluigns_var

You’ll notice that a new top-level panel was added to Z-Ray for your plugin containing the Plugins_var panel. This panel displays a tree table with a multi-level array. The plugin’s storage mechanism automatically generates tables based on the information stored. If an array or associative array is passed, a grid table will be created with columns as the names of the keys. If a multi-level array or an object is passed, a tree table will be created.
Now, we’d like to extract only part of this information, say only the name and version of the plugin.
So we’re going to enter a ‘foreach’ loop with another storage execution, only this time we’re going to specify an array instead of the $plugins object.

Save the file, and refresh WordPress again.

pluigns

You can now see a new Plugins panel with a grid view of the plugins, and with the plugin info we wanted to be displayed.

Step 5 : Adding a Logo

If not specified otherwise, Z-Ray plugins display the Zend logo on Z-Ray and in the plugin panels themselves. However, a nice, and very much recommended, touch to a plugin is to add your own customized logo.
To do this, you’ll first need to add the logo image to the Z-Ray plugin folder containing the ‘zray.php’ file. Please note that the image should be 24X24 px, and should contain both a regular and hover state.
Next, in the ‘zray.php’ file, we’re going to use the setMetadata function. In this case, we’re going to use the unique key ‘logo’ to set the default logo for the plugin. This is done by passing a full path to the logo file:

Save, and refresh WordPress.

logo

How easy was that?

Here’s what your plugin code should look like at the end:

What’s Next?

So, you’ve developed your plugin. Now what? You can install it in Zend Server to be able to manage it more easily. If you’d like to make your plugin available for all to use, you can submit it into the Zend Gallery (check out the Zend plugin contest). Either way, you’ll need to package it up. Part 2 of this series will deal with these aspects.

Hope you enjoyed the read!

Useful Links