wp-content/pluginsWordPress subdirectory. The directory name will be used as the extension identifier, so make sure it's unique enough to avoid conflicts with other HivePress extensions (use lowercase letters, numbers, and hyphens only).
hivepress/v1/extensionshook to allow HivePress to detect and load all the other extension files automatically:
class-followers.phpfile in the
includes/componentsextension subdirectory. Notice that it has the
class-prefix, and its name matches the extension name. It must be unique enough to avoid conflicts with other HivePress components (use lowercase letters, numbers, and hyphens only).
Foo_Barclass for the
Followmodel for storing the follower ID along with the followed vendor ID. Create a
class-follow.phpfile in the
includes/modelsextension subdirectory. The file and PHP class naming conventions are the same as for components.
Commentclass. This means that the model objects will be stored as WordPress comments of a custom type. We implement the model this way because comments are linked to both users and posts in the WordPress database schema. Since vendors are implemented as posts of a custom type, we can use this model to store both the follower (user) and the followed vendor IDs.
Followmodel contains 2 fields:
user_iddatabase field, used for storing the follower ID;
comment_post_IDdatabase field, used for storing the followed vendor ID.
Commentclass, WordPress will show the model objects in the comment feeds. To keep the model objects hidden, create a
comment-types.phpfile in the
followcomment type registered by the model we created private.
class-listings-feed-page.phpfile in the
includes/templatesextension subdirectory. The file and PHP class naming conventions are the same as for components.
User_Account_Pageclass. This means that the template inherits the user account page layout and adds custom blocks to it.
class-followers.phpfile in the
includes/controllersextension subdirectory. The file and PHP class naming conventions are the same as for components.
listings_feed_pageroute has the
/account/feedURL, renders a page with the “Feed” title and supports pagination.
falseif all checks are passed.
/account/feedURL, you will be redirected because you haven’t followed any vendors yet.
vendor_follow_idsvalue in the request context, but there’s no function that sets this value in context yet. Add this code to the component constructor:
Followmodel objects by user ID and fills an array of vendor IDs, then caches this array. Finally, it sets an array of vendor IDs in the
vendor_follow_idscontext, this allows us to get it anywhere in the code this way:
listings_feed_pageroute we created previously. You can adjust the
_orderparameter value to change the menu item position. The menu item will appear only if the current user follows any vendors.
paginatedparameters, but other parameters are needed instead:
POSTmethod. The first route will follow or unfollow a vendor on every subsequent request, while the second one will unfollow all vendors at once. Next, implement the action functions for these routes:
follow_vendorfunction checks if the current user is logged in, gets a
Vendorobject by ID, and queries the
Followobjects by the user and vendor IDs.
Followobjects are found, they are deleted. If not, a new
Followobject is created and saved in the database. This way, every subsequent call of this function will follow or unfollow a vendor, creating or deleting a
unfollow_vendorsfunction checks if the current user is logged in, then queries the
Followobjects by the user ID and deletes them, thus unfollowing all vendors.
vendor_follow_actionon click and add it somewhere on the vendor page. We need to create a new block type for this.
class-follow-toggle.phpfile in the
includes/blocksextension subdirectory. The file and PHP class naming conventions are the same as for components.
Toggleclass – this is an existing block type available in HivePress, so all the properties and methods are inherited from it.
Vendorobject from the current template context and sets the toggle
vendor_follow_actionroute we created previously. It also enables the
activeflag if the vendor ID is among the followed vendor IDs.
vendor_follow_actionroute URL. Also, it will show the “Unfollow” label by default if the vendor is already followed.
class-vendors-unfollow.phpfile in the
includes/formsextension subdirectory. The file and PHP class naming conventions are the same as for components.
Listings_Feed_Pagetemplate we created earlier:
partblock that loads a specific HTML file and a
modalblock that contains the form we’ve just created. The part block points to a non-existing file, so we need to create a
vendors-unfollow-link.phpfile in the
vendors_unfollow_actionroute URL, thus unfollowing all vendors at once.
class-listing-feed.phpfile in the
includes/emailsextension subdirectory. The file and PHP class naming conventions are the same as for components.
Followobjects by vendor ID and sends an email to each follower, providing the email address and tokens to be replaced in the email text.