The free version of Post Pay Counter allows to pay authors for words, visits, comments and images with no further effort than setting up the appropriate payment values in the Options
page. Further addons extend the basic payment criterias, with for example Adsense revenues, Facebook likes/shares/comments, single characters, referral visits, and many more.
However, what if you want to add your own custom payment criteria? In that case, Post Pay Counter has some handy functions that allow you do just that, with ease.
The class which contains payment types functions is called PPC_counting_types
. It contains a method called register_counting_type
, which accepts as input an array of parameters as detailed below. The ones in bold are compulsory.
id
– unique identifier of the payment criteria (payment criteria are usually prefixed by a shortened version of the addon that registered them, for exampleppc
,ppcp
,ppcp_fb
…).label
– text that will be displayed in the stats table to refer to this payment criteria.apply_to
– whether the payment criteria applies to posts or authors (or both). Possible values arepost
,author
,both
.settings_status_index
– (optional) the name of the settings index of the plugin settings to check whether the payment criteria is enabled. (This method can only be used if the setting is stored among the default ones, in the samewp_option
. Otherwise you will have to check the enabled status in another way.)count_callback
– (optional) method to count how many elements there are to be paid (eg. how many words, visits…).
Needs to return an array in the formarray( 'to_count' => int, 'real' => int )
.
Can (and should) be a class method, in which case an array is needed (e.g.array( 'classname', 'static_method' )
orarray( $object, 'method' ) )
. In any case, it will receive the$post
WP_Post_Object as parameter ifapply_to == post
; while it will receive author stats and author id ifapply_to == author
.display
– (optional) whether you want the count, the payment, both or nothing displayed in the stats table. Possible values arecount
,payment
,both
,none
. Default isboth
.payment_callback
– method to compute payment of the result ofcount_callback
. Will receive the counting output array as parameter.other_params
– (optional) extra information you may need to store in the payment criteria definition.not_to_pay
– (bool) exclude counting from payments (only affects PRO).
Note that this will only add a new column to the stats table, as well as in the confirm payment page and in payment history records. It will not, however, add a related settings section in the Options
page: that you have to take care yourself. If you need to do it, look into ppc_meta_boxes_class.php
for the frontend part, into ppc_ajax_functions_class.php
for handling the AJAX communication between frontend and backend.
What follows is an example of registering a payment criteria, taken from the PRO version code. It adds a post
payment criteria to allow awarding a custom bonus.
/** * Registers payment bonus payment criteria. * * Hooks to ppc_registered_built_in_counting_types - PPCP_payment_bonus::register_counting_type_payment_bonus */ static function register_counting_type_payment_bonus() { global $ppc_global_settings; $payment_bonus_counting_type = array( 'id' => 'bonus', 'label' => __( 'Bonus', 'ppcp' ), 'apply_to' => 'post', 'settings_status_index' => 'enable_payment_bonus', 'display' => 'payment', 'payment_only' => true, 'count_callback' => array( 'PPCP_payment_bonus', 'get_post_payment_bonus_count' ), 'payment_callback' => array( 'PPCP_payment_bonus', 'get_post_payment_bonus' ) ); $ppc_global_settings['counting_types_object']->register_counting_type( $payment_bonus_counting_type ); } /** * Assigns payment. * * @access public * @param $countings array countings * @param $post_id int post id * @return object WP post */ static function get_post_payment_bonus( $countings, $post_id ) { global $ppcp_global_settings; $post_bonus = (float) get_post_meta( $post_id, 'ppcp_payment_bonus', true ); return $post_bonus; }