PageRenderTime 17ms CodeModel.GetById 6ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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