/shared/classes/class.templating-engine.php

https://github.com/deltafactory/landing-pages · PHP · 98 lines · 60 code · 27 blank · 11 comment · 8 complexity · 3437fab1145c8ef1e9b483b95954b4e5 MD5 · raw file

  1. <?php
  2. if ( !class_exists('Inbound_Templating_Engine') ) {
  3. function Inbound_Templating_Engine() {
  4. return Inbound_Templating_Engine::instance();
  5. }
  6. class Inbound_Templating_Engine {
  7. public static $instance;
  8. private $defaults;
  9. /* Load Singleton Instance */
  10. public static function instance() {
  11. if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Inbound_Automation_Load_Extensions ) )
  12. {
  13. self::$instance = new Inbound_Templating_Engine;
  14. self::define_default_tokens();
  15. }
  16. return self::$instance;
  17. }
  18. /* Set Default Token Values */
  19. public static function define_default_tokens() {
  20. self::$instance->defaults = array (
  21. 'admin-email-address' => get_option( 'admin_email' , '' ),
  22. 'admin-url' => admin_url(),
  23. 'site-name' => get_option( 'blogname' , '' ),
  24. 'site-tagline' => get_option( 'blogdescription' , '' ),
  25. 'site-url' => get_option( 'siteurl' , '' ) ,
  26. 'date-time' => date( 'Y-m-d H:i:s A', current_time( 'timestamp', 1 ) )
  27. );
  28. /* Plugin specific constants */
  29. if ( defined( 'LANDINGPAGES_URLPATH' ) ) {
  30. self::$instance->defaults['landingpages-urlpath'] = LANDINGPAGES_URLPATH;
  31. }
  32. if ( defined( 'WPL_URLPATH' ) ) {
  33. self::$instance->defaults['leads-urlpath'] = WPL_URLPATH;
  34. }
  35. if ( defined( 'WP_CTA_URLPATH' ) ) {
  36. self::$instance->defaults['callstoaction-urlpath'] = WP_CTA_URLPATH;
  37. }
  38. }
  39. /* Replace Tokens */
  40. public static function replace_tokens( $template , $args ) {
  41. /* Add default key/value pairs to front of $args array */
  42. array_unshift( $args , self::$instance->defaults );
  43. /* Loop through arguments in $args and replacec template tokens with values found in arguments */
  44. foreach ($args as $arg) {
  45. /* Lets look for certain nested arrays and pull their content into the main $arg array */
  46. if ( isset($arg['Mapped_Data']) ) {
  47. $arg_json = json_decode( stripslashes($arg['Mapped_Data']) , true);
  48. foreach ($arg_json as $k=>$v) {
  49. $arg[$k] = $v;
  50. }
  51. }
  52. foreach ($arg as $key => $value ) {
  53. /* ignore child elements that are arrays */
  54. if ( is_array($value) ) {
  55. continue;
  56. }
  57. /* prepare/re-map keys */
  58. $key = str_replace( 'inbound_current_page_url' , 'source' , $key );
  59. $key = str_replace( 'inbound_' , '' , $key );
  60. $key = str_replace( 'wpleads_' , 'lead_' , $key );
  61. $key = str_replace( '_' , '-' , $key );
  62. /* replace tokens in template */
  63. $template = str_replace( '{{'.$key.'}}' , $value , $template );
  64. }
  65. }
  66. /* Replace All Leftover Tokens */
  67. $template = preg_replace( '/{{(.*?)}}/si' , '', $template , -1 );
  68. return do_shortcode($template);
  69. }
  70. }
  71. }