Created
October 30, 2018 15:28
-
-
Save Pebblo/41e82e9665e041f9fba092ab57de4891 to your computer and use it in GitHub Desktop.
Example of how to add a 'LAST_PAYMENT_PAYMENT_TIMESTAMP_*' shortcode to the EE Messages system. This will parse to the full timestamp of the last payment made on a transaction, however you can pass a format attribute to set your own, e.g "LAST_PAYMENT_PAYMENT_TIMESTAMP_* format='d/m/Y']"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( | |
'FHEE__EE_Shortcodes__shortcodes', | |
'register_new_last_payment_timestamp_shortcode', | |
10, | |
2 | |
); | |
function register_new_last_payment_timestamp_shortcode( | |
$shortcodes, | |
EE_Shortcodes $lib | |
) { | |
if ( $lib instanceof EE_Transaction_Shortcodes ) { | |
$shortcodes['[LAST_PAYMENT_PAYMENT_TIMESTAMP_*]'] = _('This will parse to either the full timestamp using your sites date and time format, or if you add a format attribute you can set a specific format to use'); | |
} | |
return $shortcodes; | |
} | |
add_filter( | |
'FHEE__EE_Shortcodes__parser_after', | |
'register_new_last_payment_timestamp_shortcode_parser', | |
10, | |
5 | |
); | |
function register_new_last_payment_timestamp_shortcode_parser( | |
$parsed, | |
$shortcode, | |
$data, | |
$extra_data, | |
EE_Shortcodes $lib | |
) { | |
if ( strpos($shortcode, '[LAST_PAYMENT_PAYMENT_TIMESTAMP_*') !== false ) { | |
if ( $lib instanceof EE_Transaction_Shortcodes ) { | |
// Find the transaction object. | |
$transaction = $data->txn instanceof EE_Transaction ? $data->txn : null; | |
$transaction = ! $transaction instanceof EE_Transaction | |
&& is_array($extra_data) | |
&& isset($extra_data['data']) | |
&& $extra_data['data'] instanceof EE_Messages_Addressee | |
? $extra_data['data']->txn | |
: $transaction; | |
// Sanity check to make sure we have a transaction object. | |
if( $transaction instanceof EE_Transaction ) { | |
$last_payment = $transaction->last_payment(); | |
if ( $last_payment instanceof EE_Payment ) { | |
if (! function_exists('shortcode_parse_atts')) { | |
require_once(ABSPATH . WPINC . '/shortcodes.php'); | |
} | |
// let's get any attributes that may be present and set the defaults. | |
$shortcode_to_parse = str_replace('[', '', str_replace(']', '', $shortcode)); | |
$attrs = shortcode_parse_atts($shortcode_to_parse); | |
// If we have a format set on the shortcode, use it | |
if(! empty( $attrs['format'] ) ) { | |
return date_i18n( | |
$attrs['format'], | |
strtotime($last_payment->timestamp()) | |
); | |
} | |
//Otherwise just return the timestamp as is. | |
return $last_payment->timestamp(); | |
} | |
} | |
} | |
} | |
return $parsed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment