Creating Simple:Press Plugins: The NEW POST Class

Posted on Jan 08, 2019

Introduction

This is the sixth in our series of articles on constructing plugins/extensions specifically for Simple:Press. Here are links to the prior articles:

In this article we will engage in an extensive discussion of one of the more important CLASSes in Simple:Press – the NEW POST Class. This class is the base object that handles creating new posts and topics.

About The NEW POST Class

The NEW POST class is the recommended medium for creating a new topic or post in Simple:Press. It ensures that all the proper permission and data validation is performed, that the database is correctly updated and that any or all necessary indexing is carried out. Through the use of standard WordPress hooks, it also ensures that any active plugins that may affect a new topic or post are correctly actioned.

The php code for the new post class can be found at: /sp-api/sp-api-class-spcpost.php

The class itself is used by the core code called from /sp-api/forum/library/sp-post.php

It is also used by the following premium plugins:

For most uses, the action and filter hooks described at the bottom of this article should be sufficient. But for the sake of completeness we will start from a high-level and drill down into the gory details.

High Level Overview

Essentially the class builds an array named $newpost which is filled with the necessary data as the different steps in the process are carried out. This array is available to all action and filter hooks. Note that some of the main elements are filled prior to any of the class methods being called (this will be detailed later in this document.) Also note that public methods can only be invoked in a pre-set order – creating the necessary data and saving it requires these steps.

Finally, each public method will set an abort property to true if there is a failure at any stage and this must be checked for in the calling program code.

Public Methods

validateHuman($postVars)

This method is only used in the actual forum UI as it checks the new topic/post forms and any captcha available in an attempt to ensure that the call is being made by an actual user. It is listed here for the sake of completeness.

validatePermission()

The first of the three main methods, this checks all permissions of the user making the call for a new topic or post to ensure they are allowed to do so.

validateData()

This next stage validates the data which by this time should all be compiled into the array.

saveData()

The final stage updates the database with the new topic and/or post plus updates any indexing required. It also performs any post-save tasks  such as, for example, email notifications etc.

show()

This method is for debugging and will display the object data to the screen if possible. This should, of course, never be used in production code.

The spcPost Data Object

The following list describes the data object that is built during the new post call. These properties, where applicable, should all be filled prior to the call to the validatePermission() method.

The $newpost Data Array

The following list describes the $newpost array that is created using the post class. Items marked with an asterisk (*) are those that the calling code needs to provide. The remainder are filled by the class code itself.

Filter and Action Hooks

Assuming a class instantiation of $p = new spcPost the following standard WP style hooks are available for use by plugins and other third party code.

ValidatePermission()

sph_post_permissions_validation

$this->newpost = 
apply_filters('sph_post_permissions_validation', $this->newpost);

This filter hook is available at the very end of the success of the permission validation method. At this point the $newpost array can be additionally manipulated.

validateData()

sph_post_data_validation

$this->newpost = 
apply_filters('sph_post_data_validation', $this->newpost);

This filter hook is available at the end of of the success of the data validation method. At that point the $newpost array can be additionally manipulated.

sph_pre_post_create

do_action('sph_pre_post_create', $this->newpost);

This action hook follows the above filter hook allowing for extra data or events to be triggered at this stage of the proceedings.

sph_new_forum_post

$this->newpost = 
apply_filters('sph_new_forum_post', $this->newpost);

This filter hook follows the above action hook should it be needed.

saveData()

sph_new_post_pre_save

do_action_ref_array('sph_new_post_pre_save', array(&$this));

This action hook is available at the very start of the save process. NOTE: this makes the entire class object available for modification before saving and should be used with care. Especially note the passing by reference.

sph_new_topic_pre_data_saved

$this->newpost = 
apply_filters('sph_new_topic_pre_data_saved', $this->newpost);

A pre-save filter hook available if the ‘action’ is set to ‘topic’.

sph_new_topic_data

$query = 
apply_filters('sph_new_topic_data', $query, $this->newpost);

A filter hook to enable manipulating the INSERT $query for the topic record save. This hook would typically be used by a plugin that has added new DB fields to add the name and data to the $query→fields and $query→properties.

sph_new_topic_data_saved

$this→newpost =
apply_filters('sph_new_topic_data_saved', $this->newpost);

A filter hook called immediately following a successful save of the topic record.

sph_new_post_pre_data_saved

$this->newpost = 
apply_filters('sph_new_post_pre_data_saved', $this->newpost);

A pre-save filter hook available if the ‘action’ is set to ‘post’.

sph_new_post_data

$query = apply_filters('sph_new_post_data', $query, $this->newpost);

A filter hook to enable manipulating the INSERT $query for the post record save. This hook would typically be used by a plugin that has added new DB fields to add the name and data to the $query→fields and $query→properties.

sph_new_post_data_saved

$this->newpost = 
apply_filters('sph_new_post_data_saved', $this->newpost);

A filter hook called immediately following a successful save of the post record.

sph_post_message

$this->newpost['submsg'] = 
apply_filters('sph_post_message', $this->newpost['submsg'], $this->newpost);

A filter hook that allows a plugin to add or change the success message that is returned.

sph_post_new_completed

do_action_ref_array('sph_post_new_completed', array(&$this));

This action hook is available at the very end of the save process. NOTE: this makes the entire class object available for modification before saving and should be used with care. Especially note the passing by reference.

sph_new_post, sph_post_create

do_action('sph_new_post', $this->newpost);
do_action('sph_post_create', $this->newpost);

Two final action hooks at the very end of the whole process – just in case!