/**
 * SYNOPSIS: Used inside theme files, e.g. single.php or single-my_post_type.php
 * where you need to print out the value of a specific custom field. This can also
 * get called via the [custom_field] shortcode.  If identical instances of the same
 * call, the output will be from the request cache.
 * 
 * WordPress allows for multiple rows in wp_postmeta to share the same meta_key for 
 * a single post; the CCTM plugin expects all meta_key's for a given post_id to be 
 * unique.  To deal with the possibility that the user has created multiple custom 
 * fields that share the same name for a single post (e.g. created manually with the 
 * CCTM plugin disabled), this prints the 1st instance of the meta_key identified by 
 * $fieldname associated with the current post. See get_post_meta() for more details.
 *
 * See also 	
 * http://codex.wordpress.org/Function_Reference/get_post_custom_values
 *
 * @param	string the name of the custom field (exists in wp_postmeta).
 * 		Optionally this string can be in the format of 'fieldname:output_filter'
 * @param	mixed	can be used to specify additional arguments
 * @return	mixed	The contents of the custom field, processed through output filters
 */
function get_custom_field($raw_fieldname, $options = null)
{
    global $post;
    // Shortcodes can override which post they're retrieving data for
    if (!empty(CCTM::$post_id)) {
        $post_id = CCTM::$post_id;
    } elseif (is_object($post) && isset($post->ID)) {
        $post_id = $post->ID;
    } else {
        return '$post not defined.';
    }
    $options_array = func_get_args();
    // Request cache: this helps speed up cases where there are identical instances of get_custom_field()
    // Get the cache key
    $cache_key = serialize($options_array) . $post_id;
    if (isset(CCTM::$cache[$cache_key])) {
        return CCTM::$cache[$cache_key];
        // done!  Output comes from cache!
    }
    // Extract any output filters.
    $input_array = explode(':', $raw_fieldname);
    $fieldname = array_shift($input_array);
    // We need the custom field definition for 2 reasons:
    // 1. To find the default Output Filter
    // 2. To find any default value (if the field is not defined)
    if (!isset(CCTM::$data['custom_field_defs'][$fieldname])) {
        // return get_post_meta($post->ID, $fieldname, true); // ???
        return sprintf(__('The %s field is not defined as a custom field.', CCTM_TXTDOMAIN), "<code>{$fieldname}</code>");
    }
    // Get default output filter
    if (empty($input_array)) {
        if (isset(CCTM::$data['custom_field_defs'][$fieldname]['output_filter']) && !empty(CCTM::$data['custom_field_defs'][$fieldname]['output_filter'])) {
            $input_array[] = CCTM::$data['custom_field_defs'][$fieldname]['output_filter'];
        }
    }
    // Raw value from the db
    $value = get_post_meta($post_id, $fieldname, true);
    // Default value? See http://wordpress.org/support/topic/default-value-behaviour?replies=7
    //if ( empty($value) && isset(CCTM::$data['custom_field_defs'][$fieldname]['default_value'])) {
    //	$value = CCTM::$data['custom_field_defs'][$fieldname]['default_value'];
    //}
    // Pass thru Output Filters
    $i = 1;
    // <-- skip 0 b/c that's the $raw_fieldname in the $options_array
    foreach ($input_array as $outputfilter) {
        if (isset($options_array[$i])) {
            $options = $options_array[$i];
        } else {
            $options = null;
        }
        $value = CCTM::filter($value, $outputfilter, $options);
        $i++;
    }
    // Store in the request cache
    CCTM::$cache[$cache_key] = $value;
    return $value;
}
 /**
  * Apply the filter.
  *
  * @param 	mixed 	$input, usually a string representing a JSON-encoded array, but a real PHP array is ok too.
  * @param	string	$options optional arguments, to_array accepts the name of an Output Filter
  * @return mixed
  */
 public function filter($input, $options = null)
 {
     $the_array = $this->to_array($input);
     // Apply secondary optional filter to each item in the array.
     if ($options && is_scalar($options)) {
         foreach ($the_array as &$item) {
             $item = CCTM::filter($item, $options);
         }
     }
     return $the_array;
 }
	<div class="container">
		<div class="row">
			<div class="col-xs-10 col-xs-offset-1">
				<div class="bike-shot">
					<div class="bike-shot-ft">
						<img src="<?php 
        print_custom_field('photoImage');
        ?>
" alt="<?php 
        the_title();
        ?>
" class="top show"/>
						<?php 
        $array_of_images = get_custom_field('uploadsGallery:to_array');
        foreach ($array_of_images as $img_id) {
            print CCTM::filter($img_id, 'to_image_tag');
        }
        ?>
					</div>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-10 col-xs-offset-1 col-md-8 col-md-offset-2 pre-text">
				<?php 
        if ($array_of_images) {
            ?>
				<div class="bike-shot-thumbs"></div>
				<?php 
        } else {
            ?>
 function testFilter112()
 {
     $txt = CCTM::filter('Big Stuff', 'wrapper', '<strong>[+content+]</strong>');
     $this->assertTrue($txt == '<strong>Big Stuff</strong>');
 }
        if ($counter == 2 || $counter == 1) {
            $fluidClasses = 'col-xs-12 col-sm-6 col-md-6 col-lg-6';
        } else {
            $fluidClasses = 'col-xs-12 col-sm-6 col-md-4 col-lg-4';
        }
        ?>
		
				<div class="case <?php 
        echo $fluidClasses;
        ?>
  ">
					<a href="<?php 
        echo get_permalink();
        ?>
"><?php 
        echo CCTM::filter(get_post_meta(get_the_ID(), 'slider_image', true), 'to_image_tag', 'overview-img');
        ?>
</a>
					<a href="<?php 
        echo get_permalink();
        ?>
">
						<p class="related-title-white"><?php 
        print_custom_field('overview_title');
        ?>
<span><?php 
        print_custom_field('overview_subtitle');
        ?>
</span></p>
					</a>
				</div>
Example #6
0
 /**
  * Our parsing function for basic templating, based on the MODX placeholders
  * and output filters
  *
  * @param string  $tpl: a string containing [+placeholders+]
  * @param array   $hash: an associative array('key' => 'value') corresponding to the keys of the hash will be replaced
  * @param boolean $preserve_unused_placeholders (optional) if true, will not remove unused [+placeholders+]
  * @return string parsed text
  */
 public static function parse($tpl, $hash, $preserve_unused_placeholders = false)
 {
     if (is_array($hash)) {
         // Get all placeholders in this tpl
         $all_placeholders = array_keys($hash);
         $hash['help'] = '<ul>';
         foreach ($all_placeholders as $p) {
             $hash['help'] .= "<li>&#91;+{$p}+&#93;</li>";
         }
         $hash['help'] .= '</ul>';
         // Simple Placeholders
         foreach ($hash as $key => $value) {
             if (is_scalar($value)) {
                 $tpl = str_replace('[+' . $key . '+]', $value, $tpl);
             }
         }
         // ADVANCED PLACEHOLDERS, e.g. [+my_field:output_filter==opt1||opt2:output_filter2+]
         // Check for in-line output filters, e.g. some_id:to_image_tag or post_id:get_post:guid
         $pattern = preg_quote('[+') . '(.*)' . preg_quote('+]');
         $placeholders = array();
         preg_match_all('/' . $pattern . '/U', $tpl, $placeholders);
         foreach ($placeholders[1] as $complex_ph) {
             //die(print_r($placeholders[1],true));
             $components = explode(':', $complex_ph);
             // First placeholder would be what the simple placeholder would use
             // and the first value comes from the original $hash
             $first = array_shift($components);
             $value = '';
             if (isset($hash[$first])) {
                 $value = $hash[$first];
             } elseif ($preserve_unused_placeholders) {
                 continue;
             }
             // "Components" are the filter==opt chunks
             foreach ($components as $comp) {
                 // does this value exist?
                 $filter_and_opts = explode('==', $comp);
                 // $filter_and_opts[0] = the filter name
                 // $filter_and_opts[1] = comma-sep options (if any)
                 $options = null;
                 if (isset($filter_and_opts[1])) {
                     // if you used alternate glyphs for nested tags, here's where
                     // you'd convert them...
                     $filter_and_opts[1] = str_replace('{{', '[+', $filter_and_opts[1]);
                     $filter_and_opts[1] = str_replace('}}', '+]', $filter_and_opts[1]);
                     $options = explode('||', $filter_and_opts[1]);
                     // avoid the array if not needed.
                     if ($options[0] == $filter_and_opts[1]) {
                         $options = $filter_and_opts[1];
                     }
                 }
                 $new_value = CCTM::filter($value, $filter_and_opts[0], $options);
                 // if we don't get a scalar, we skip that value.
                 if (is_scalar($new_value)) {
                     $value = $new_value;
                 }
             }
             $tpl = str_replace('[+' . $complex_ph . '+]', $value, $tpl);
         }
     } else {
         CCTM::log(print_r(debug_backtrace(), true), __FILE__, __LINE__);
     }
     // Remove any unparsed [+placeholders+]
     if (!$preserve_unused_placeholders) {
         $tpl = preg_replace('/\\[\\+(.*?)\\+\\]/', '', $tpl);
     }
     return $tpl;
 }