Difference between revisions of "JReviews:Developers Filters"

From JReviews Documentation
Jump to: navigation, search
(Using Filter Functions)
(Defining a Filter)
Line 16: Line 16:
 
* Add or remove CSS and Scripts to and from the page.
 
* Add or remove CSS and Scripts to and from the page.
  
= Defining a Filter =
+
= Defining a new Filter Hook =
  
Typically you will not need to do this because JReviews 3 already comes with many filters and we'll add more over time. However, if you need to create a new filter that is not yet available this is the syntax from creating one:
+
JReviews 3 already comes with many filter hooks and we'll add more over time. However, if you need to create a new filter that is not yet available you would use the following syntax:
  
 
<source lang="php">
 
<source lang="php">
$data = \Clickfwd\Hook\Filter::filter('filter_name', $data, ['foo'=>$bar]);
+
$data = \Clickfwd\Hook\Filter::filter('example_filter_name', $data, ['foo'=>$bar]);
 
</source>
 
</source>
  
 
Where:
 
Where:
  
* $data is the variable that can be modified by the filters
+
* $data is the variable that can be modified by filter callback function
* $params is an array of reference variables that can be used inside the filter logic
+
* $params is an array of reference variables that can be used inside the filter's callback function
  
 
= Using Filter Functions =
 
= Using Filter Functions =

Revision as of 11:07, 14 February 2018

JReviews 3 comes with Filters that allow you to make certain types of customizations and extend the functionality of JReviews without having to modify core files.


What is a Filter?

JReviews has many filter hooks strategically placed in the code at points where you may want to change or override the value of a variable at runtime.

Filters can modify data by binding a callback function to a filter hook. When the filter is executed, each callback function is run in order of priority, and can modify the filtered data by returning a new value.

For example, you can use a filter to:

  • Change the page title and metatags of different pages
  • Override the permissions of specific users or user groups for different events (creating listings, reviews, etc.)
  • Add or remove CSS and Scripts to and from the page.

Defining a new Filter Hook

JReviews 3 already comes with many filter hooks and we'll add more over time. However, if you need to create a new filter that is not yet available you would use the following syntax:

$data = \Clickfwd\Hook\Filter::filter('example_filter_name', $data, ['foo'=>$bar]);

Where:

  • $data is the variable that can be modified by filter callback function
  • $params is an array of reference variables that can be used inside the filter's callback function

Using Filter Functions

The following example shows how a callback function is bound to a filter hook:

function example_filter_callback_function($data, $params = [])
{
   // Modify $data in some way and return it
   return $data;
}
 
Clickfwd\Hook\Filter::add('example_filter_name', 'example_filter_callback_function', $priority = 10);

The $data variable can be modified inside the callback function and must always be returned at the end of the function.

To create new filter functions that are not overwritten on upgrades, create a new file in overrides and place your filter functions there:

Joomla

/templates/jreviews_overrides/filters/filter_functions.php

WordPress

/templates/jreviews_overrides/filters/filter_functions.php

Available Filters

Notifications

$payload = \Clickfwd\Hook\Filter::filter('listing_inquiry_email_payload', $payload, ['event'=>$event]);

Asset Manager

 266: 		self::$css = Hook::filter('asset_manager_stylesheets', self::$css);
 312: 		self::$js = Hook::filter('asset_manager_scripts', self::$js);

Metatags

 160: 		$robots = Hook::filter('page_robots_metatag', $robots, $params);
 182: 		$title = Hook::filter('page_title_metatag', $title, $params);
 202: 		$keywords = Hook::filter('page_keywords_metatag', $keywords, $params);
 214: 		$description = Hook::filter('page_description_metatag', $description, $params);
 226: 		$canonical = Hook::filter('page_canonical_metatag', $canonical, $params);
 238: 		$prev = Hook::filter('page_prev_metatag', $prev, $params);
 248: 		$next = Hook::filter('page_next_metatag', $next, $params);

Listing Detail

 541:    $menuExtras = Clickfwd\Hook\Filter::filter('listing_detail_action_buttons', $menuExtras, [['listing'=>$listing]]);

Listing Permissions

  57:    return $this->filteredResponse($trust,'trusted_on_listing_create');
  85:    return $this->filteredResponse($permission,'listing_allows_claims',$listing);
 132:    return $this->filteredResponse($permission,'listing_allows_inquiries',$listing);
 150:    return $this->filteredResponse($permission,'listing_allows_favorites',$listing);
 173:    return $this->filteredResponse($permission,'listing_allows_user_reviews',$listing);
 191:    return $this->filteredResponse($permission,'listing_allows_editor_reviews',$listing);
 207:    return $this->filteredResponse($permission,'listing_user_reviews_opened',$listing);
 223:    return $this->filteredResponse($permission,'listing_editor_reviews_opened',$listing);
 251:    $permission = $this->filteredResponse($permission,'can_create_listing',[$listingTypeId]);
 278:    return $this->filteredResponse($permission,'can_update_listing',$listing);
 296:    return $this->filteredResponse($permission,'can_add_listing_metadata',$listingType);
 312:    return $this->filteredResponse($permission,'can_use_editor_in_listing',[]);
 334:    return $this->filteredResponse($permission,'can_publish_listing',$listing);
 356:    return $this->filteredResponse($permission,'can_delete_listing',$listing);
 374:    return $this->filteredResponse($permission,'can_claim_listing',$listing);
 390:    return $this->filteredResponse($permission,'can_send_listing_inquiry',$listing);
 406:    return $this->filteredResponse($permission,'can_favorite_listing',$listing);
 420:    return $this->filteredResponse($permission,'can_feature_listing',[]);
 496:    return $this->filteredResponse($permission,'can_create_user_review_for_listing',$listing,$this->getMessage());
 546:    return $this->filteredResponse($permission,'can_create_editor_review_for_listing',$listing,$this->getMessage());
 564:    return $this->filteredResponse($permission,'can_add_review_on_listing_create',$listingType);
 586:    return $this->filteredResponse($permission,'can_update_listing_media',[$listing,$listingType]);
 624:    return $this->filteredResponse($permission,'can_upload_listing_media',[$listing,$listingType]);
 644:    return $this->filteredResponse($permission,'can_upload_media_from_url_in_listing',$listing);
 662:    return $this->filteredResponse($permission,'can_upload_listing_photo',[$listing,$listingType]);
 680:    return $this->filteredResponse($permission,'can_upload_listing_video');
 698:    return $this->filteredResponse($permission,'can_upload_listing_audio',[$listing,$listingType]);
 716:    return $this->filteredResponse($permission,'can_upload_listing_attachment',[$listing,$listingType]);

Media Permissions

  30:    return $this->filteredResponse($trust,'trusted_on_create_photo');
  49:    return $this->filteredResponse($trust,'trusted_on_create_video');
  68:    return $this->filteredResponse($trust,'trusted_on_create_audio');
  87:    return $this->filteredResponse($trust,'trusted_on_create_attachment');
 105:    return $this->filteredResponse($permission,'media_allows_voting',$listing);
 121:    return $this->filteredResponse($permission,'media_allows_report',$listing);
 149:    return $this->filteredResponse($permission,'can_update_media_in_listing',[$listing,$media]);
 177:    return $this->filteredResponse($permission,'can_delete_media_in_listing',[$listing,$media]);
 205:    return $this->filteredResponse($permission,'can_publish_media_in_listing',[$listing,$media]);
 237:    return $this->filteredResponse($permission,'can_set_main_media',[$listing,$media]);
 259:    return $this->filteredResponse($permission,'can_update_media_details',[$listing,$media]);
 286:    return $this->filteredResponse($permission,'can_vote_on_media',[$listing,$media]);
 302:    return $this->filteredResponse($permission,'can_report_media',$listing);
 323:    return $this->filteredResponse($permission,'can_download_attachment',$media);

Review Permissions

  28:    return $this->filteredResponse($trust,'trusted_on_user_review_create');
  45:    return $this->filteredResponse($trust,'trusted_on_user_review_update');
  58:    return $this->filteredResponse($trust,'trusted_on_editor_review_create');
  71:    return $this->filteredResponse($trust,'trusted_on_editor_review_update');
  84:    return $this->filteredResponse($trust,'trusted_on_create_review_owner_reply');
 104:    return $this->filteredResponse($permission,'review_allows_report',$review);
 124:    return $this->filteredResponse($permission,'review_allows_voting',$review);
 140:    return $this->filteredResponse($permission,'review_allows_replies',$review);
 156:    return $this->filteredResponse($permission,'review_allows_discussions',$review);
 180:    return $this->filteredResponse($permission,'can_update_review',$review);
 204:    return $this->filteredResponse($permission,'can_reply_to_review',[$listing,$review]);
 220:    return $this->filteredResponse($permission,'can_delete_review_reply',$listing);
 236:    return $this->filteredResponse($permission,'can_report_review',$review);
 261:    return $this->filteredResponse($permission,'can_vote_on_review',$review);
 279:    return $this->filteredResponse($permission,'can_create_review_post',$review);
 319:    return $this->filteredResponse($permission,'can_upload_review_media',$review);
 337:    return $this->filteredResponse($permission,'can_upload_media_from_url_in_review',$review);
 353:    return $this->filteredResponse($permission,'can_upload_review_photo',$review);
 369:    return $this->filteredResponse($permission,'can_upload_review_video',$review);
 385:    return $this->filteredResponse($permission,'can_upload_review_audio',$review);
 401:    return $this->filteredResponse($permission,'can_upload_review_attachment',$review);

Review Discussion Permissions

  26:    return $this->filteredResponse($trust,'trusted_on_create_review_discussion');
  48:    return $this->filteredResponse($permission,'review_discussion_allows_report',$post);
  64:    return $this->filteredResponse($permission,'can_report_review_discussion',$post);
  86:    return $this->filteredResponse($permission,'can_update_review_discussion',[$post,$review]);
 108:    return $this->filteredResponse($permission,'can_delete_review_discussion',[$post,$review]);

Field Permissions

  59:    return $this->filteredResponse($permission,'can_view_field_in_'.$location.'_page',$field);
  87:    return $this->filteredResponse($permission,'can_read_field',[$fname]);
 115:    return $this->filteredResponse($permission,'can_write_field',[$fname]);