/**
     * Apply the filter.
     *
     * @param 	mixed 	input
     * @param	mixed	optional arguments
     * @return mixed
     */
    public function filter($input, $options = null)
    {
        $output = '
		<style>
			div.cctm_code {
				border: solid 1px blue;
				font-size: 1.3 em; 
			 	color: blue; 
				margin: 10px; 
				padding:10px; 
				background: #FFFFB3		
			}
			#cctm_help {
				border:1px solid black;
				background-color: lightyellow; 
				width: 90%; 
				margin: 10px; 
				padding: 10px;
			}
			h2.cctm_h2 {
				font-size:25px; 
				line-height:32px;
				margin-bottom: 10px;
			}
			h3.cctm_h3 {
				font-size:18px; 
				line-height:25px;
				margin-top: 20px;
			}
		</style>
		<div id="cctm_help">
			<h2 class="cctm_h2">' . __('Output Filter Help', CCTM_TXTDOMAIN) . '</h2>
			<p>' . __('This help page was generated by <a href="http://code.google.com/p/wordpress-custom-content-type-manager/wiki/OutputFilters">Custom Content Type Manager</a> to demonstrate the use of the available Output Filters. For samples of template files, see the CCTM menus in the WordPress manager.', CCTM_TXTDOMAIN) . '</p>';
        $filters = CCTM::get_available_helper_classes('filters', true);
        //		print_r($filters); exit;
        foreach ($filters as $filter => $file) {
            //if( CCTM::include_output_filter_class($filter) ) {
            if ($Obj = CCTM::load_object($filter, 'filters')) {
                /*
                				$filter_name = CCTM::filter_prefix . $filter;
                				$Obj = new $filter_name();
                */
                $output .= sprintf('<h3 class="cctm_h3">%s (%s)</h3>
					<p>%s -- [<a href="%s" target="_new">%s</a>]</p>
					<strong>%s</strong>:<br/>
					<div class="cctm_code">
					%s
					</div>
					', $Obj->get_name(), $filter, $Obj->get_description(), $Obj->get_url(), __('More info', CCTM_TXTDOMAIN), __('Example', CCTM_TXTDOMAIN), highlight_string($Obj->get_example('myfield', $filter), true));
            } else {
                $output .= sprintf(__('File not found for %s output filter: %s', CCTM_TXTDOMAIN), '<strong>' . $filter . '</strong>', $file) . '<br/>';
            }
        }
        return $output . '</div>';
    }
$data['help'] = 'http://code.google.com/p/wordpress-custom-content-type-manager/wiki/CustomFieldDefinitions';
$data['msg'] = '';
$data['menu'] = sprintf('<a href="' . get_admin_url(false, 'admin.php') . '?page=cctm_fields&a=list_custom_fields" title="%s" class="button">%s</a>', __('Cancel'), __('Cancel'));
$data['submit'] = __('Save', CCTM_TXTDOMAIN);
$data['action_name'] = 'custom_content_type_mgr_edit_custom_field';
$data['nonce_name'] = 'custom_content_type_mgr_edit_custom_field_nonce';
$nonce = CCTM::get_value($_GET, '_wpnonce');
if (!wp_verify_nonce($nonce, 'cctm_edit_field')) {
    die(__('Invalid request.', CCTM_TXTDOMAIN));
}
// Get the post-types for listing associations.
$displayable_types = CCTM::get_post_types();
$field_type = self::$data['custom_field_defs'][$field_name]['type'];
$field_data = self::$data['custom_field_defs'][$field_name];
// Data object we will save
if (!($FieldObj = CCTM::load_object($field_type, 'fields'))) {
    die('Field not found.');
}
$field_data['original_name'] = $field_name;
$FieldObj->set_props($field_data);
$data['change_field_type'] = '<br/><a href="?page=cctm_fields&a=change_field_type&field=' . $field_name . '&_wpnonce=' . wp_create_nonce('cctm_change_field_type') . '" class="button">' . __('Change Field Type', CCTM_TXTDOMAIN) . '</a></p>';
// Save if submitted...
if (!empty($_POST) && check_admin_referer($data['action_name'], $data['nonce_name'])) {
    // A little cleanup before we handoff to save_definition_filter
    unset($_POST[$data['nonce_name']]);
    unset($_POST['_wp_http_referer']);
    // Handle editing of the associations
    // All associations were removed
    $post_type_defs = CCTM::get_post_type_defs();
    if (!isset($_POST['post_types'])) {
        // die('All associations removed...');
}
$field_type = self::$data['custom_field_defs'][$field_name]['type'];
$field_data = self::$data['custom_field_defs'][$field_name];
// Data object we will save
$field_type_str = '';
if ($FieldObj = CCTM::load_object($field_type, 'fields')) {
    $field_type_str = $FieldObj->get_name();
    $field_type_url = $FieldObj->get_url();
}
$data['content'] = '<p>' . sprintf(__('Change the %s field from a %s field into the following type of field:', CCTM_TXTDOMAIN), '<code>' . $field_data['name'] . '</code>', sprintf('<a href="%s">%s</a>', $field_type_url, $field_type_str)) . '</p>';
$elements = CCTM::get_available_helper_classes('fields');
$data['content'] .= '<input type="hidden" name="old_field_type" value="' . $field_type . '">';
$data['content'] .= '<select name="new_field_type" id="new_field_type">';
foreach ($elements as $ft => $file) {
    if ($field_type == $ft) {
        continue;
        //  can't  change a field to itself
    }
    if ($FieldObj = CCTM::load_object($ft, 'fields')) {
        $d = array();
        $data['content'] .= sprintf('<option value="%s">%s</option>', $ft, $FieldObj->get_name());
    } else {
        die(sprintf(__('Form element not found: %s', CCTM_TXTDOMAIN), "<code>{$field_type}</code>"));
    }
}
$data['content'] .= '</select>';
$data['content'] .= '<p style="color:red;">' . __('WARNING: different fields have different attributes. The conversion process may cause some attributes in your field definition to be lost.  Export a copy of your field definitions before continuing.', CCTM_TXTDOMAIN) . '</p>';
$data['content'] .= '<p style="color:red;">' . sprintf('<a href="%s">%s</a>', '?page=cctm_tools&a=export_def', __('Export a copy of your field definitions before continuing.', CCTM_TXTDOMAIN)) . '</p>';
$data['content'] = CCTM::load_view('basic_form.php', $data);
print CCTM::load_view('templates/default.php', $data);
/*EOF*/
 function testBogusValidator()
 {
     $V = CCTM::load_object('bogus', 'validators');
     $this->assertFalse($V);
 }
 /**
  * Validate custom fields on a post that's already been saved.
  *
  * @param string $post_type
  * @param array $full_post array of all submitted values
  * @return boolean : true if valid, false if there were errors
  */
 public static function validate_fields($post_type, $full_post)
 {
     $custom_fields = self::_get_custom_fields($post_type);
     $validation_errors = array();
     foreach ($custom_fields as $field_name) {
         if (!isset(CCTM::$data['custom_field_defs'][$field_name]['type'])) {
             continue;
         }
         $field_type = CCTM::$data['custom_field_defs'][$field_name]['type'];
         if ($FieldObj = CCTM::load_object($field_type, 'fields')) {
             $FieldObj->set_props(CCTM::$data['custom_field_defs'][$field_name]);
             $value = '';
             if (isset($full_post[$field_name])) {
                 $value = $full_post[$field_name];
             }
             // Check for empty json arrays, e.g. [""], convert them to empty PHP array()
             $value_copy = '';
             if ($FieldObj->is_repeatable) {
                 $value_copy = $FieldObj->get_value($value, 'to_array');
                 if (is_array($value_copy)) {
                     foreach ($value_copy as $k => $v) {
                         if (empty($v)) {
                             unset($value_copy[$k]);
                         }
                     }
                 }
             } else {
                 $value_copy = $FieldObj->get_value($value, 'to_string');
             }
             // Is this field required?  OR did validation fail?
             if ($FieldObj->required) {
                 if (is_array($value_copy) && empty($value_copy) || !is_array($value_copy) && !strlen(trim($value_copy))) {
                     CCTM::$post_validation_errors[$FieldObj->name] = sprintf(__('The %s field is required.', CCTM_TXTDOMAIN), $FieldObj->label);
                 } elseif (!is_array($value_copy) && !strlen(trim($value_copy))) {
                     CCTM::$post_validation_errors[$FieldObj->name] = sprintf(__('The %s field is required.', CCTM_TXTDOMAIN), $FieldObj->label);
                 }
             } elseif ((!empty($value_copy) || $value_copy == '0') && isset($FieldObj->validator) && !empty($FieldObj->validator)) {
                 $Validator = CCTM::load_object($FieldObj->validator, 'validators');
                 if (isset(CCTM::$data['custom_field_defs'][$field_name]['validator_options'])) {
                     $Validator->set_options(CCTM::$data['custom_field_defs'][$field_name]['validator_options']);
                 }
                 $Validator->set_subject($FieldObj->label);
                 $Validator->set_options($FieldObj->validator_options);
                 if (is_array($value_copy)) {
                     foreach ($value_copy as $i => $val) {
                         $value_copy[$i] = $Validator->validate($val);
                     }
                 } else {
                     $value_copy = $Validator->validate($value_copy);
                 }
                 if (!empty($Validator->error_msg)) {
                     CCTM::$post_validation_errors[$FieldObj->name] = $Validator->get_error_msg();
                 }
             }
         } else {
             // error!  Can't include the field class.  WTF did you do to get here?
         }
     }
     if (empty(CCTM::$post_validation_errors)) {
         return true;
     } else {
         return false;
     }
 }
Example #6
0
 /**
  * Each custom field can optionally do stuff during the admin_init event -- this
  * was designed so custom fields could include their own JS & CSS, but it could
  * be used for other purposes I suppose, e.g. registering other actions/filters.
  *
  * Custom field classes will be included and initialized only in the following
  * two cases:
  *  1. when creating/editing a post that uses one of these fields
  *  2. when creating/editing a field definition of the type indicated.
  * E.g.
  *  post-new.php
  *  post-new.php?post_type=page
  * 	post.php?post=807
  *  admin.php?page=cctm_fields&a=create_custom_field
  *  admin.php?page=cctm_fields&a=edit_custom_field
  */
 public static function initialize_custom_fields()
 {
     // Look around/read variables to get our bearings
     $page = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], '/') + 1);
     $fieldtype = self::get_value($_GET, 'type');
     $fieldname = self::get_value($_GET, 'field');
     $action = self::get_value($_GET, 'a');
     $post_type = 'post';
     // default
     // Bail if we're not on the relevant pages
     if (!in_array($page, array('post.php', 'post-new.php', 'admin.php'))) {
         return;
     }
     if ($page == 'post-new.php') {
         $post_type = self::get_value($_GET, 'post_type', 'post');
         // post_type is only set for NEW posts
     } else {
         // ( $page == 'post.php') {
         $post_id = self::get_value($_POST, 'post_ID');
         // TODO: wouldn't you think the post_type was already defined somewhere?
         if (empty($post_id)) {
             $post_id = self::get_value($_GET, 'post');
         }
         $post = get_post($post_id);
         if (!empty($post)) {
             $post_type = $post->post_type;
         }
     }
     // Here's where we will load up all the field-types that are active on this particular post or page.
     $field_types = array();
     // Create/edit posts
     if ($page == 'post.php' || $page == 'post-new.php') {
         if (isset(self::$data['post_type_defs'][$post_type]['is_active'])) {
             $custom_fields = self::get_value(self::$data['post_type_defs'][$post_type], 'custom_fields', array());
             // We gotta lookup the fieldtype by the name
             foreach ($custom_fields as $cf) {
                 if (!isset(self::$data['custom_field_defs'][$cf])) {
                     // unset this?
                     continue;
                     // we shouldn't get here, but just in case...
                 }
                 // Get an array of field-types for this
                 $fieldtype = self::get_value(self::$data['custom_field_defs'][$cf], 'type');
                 if (!empty($fieldtype)) {
                     $field_types[$fieldtype][] = $cf;
                 }
             }
         }
     } elseif ($page == 'admin.php' && $action == 'create_custom_field') {
         $field_types[$fieldtype] = array();
     } elseif ($page == 'admin.php' && $action == 'edit_custom_field' && isset(self::$data['custom_field_defs'][$fieldname])) {
         $fieldtype = self::get_value(self::$data['custom_field_defs'][$fieldname], 'type');
         $field_types[$fieldtype][] = $fieldname;
     } elseif ($page == 'admin.php' && $action == 'duplicate_custom_field') {
         $fieldtype = CCTM::get_value($_GET, 'type');
         $field_types[$fieldtype] = array();
     }
     // We only get here if we survived the gauntlet above
     foreach ($field_types as $ft => $fieldlist) {
         if ($FieldObj = CCTM::load_object($ft, 'fields')) {
             $FieldObj->admin_init($fieldlist);
         }
     }
     if (!empty(CCTM::$errors)) {
         self::print_notices();
     }
 }
<?php

/*------------------------------------------------------------------------------
Load up the validator and return the validator's options.

@param	string	validator
------------------------------------------------------------------------------*/
$validator = CCTM::get_value($_POST, 'validator');
$V = CCTM::load_object($validator, 'validators');
if ($V) {
    print $V->draw_options();
} else {
    print '<pre>' . __('Error loading validator.', CCTM_TXTDOMAIN) . '</pre>';
}
/*EOF*/
 /**
  *
  *
  * @param mixed   $current_value current value for this field (an integer ID).
  * @return string
  */
 public function get_edit_field_instance($current_value)
 {
     require_once CCTM_PATH . '/includes/GetPostsQuery.php';
     $Q = new GetPostsQuery();
     // Populate the values (i.e. properties) of this field
     $this->id = str_replace(array('[', ']', ' '), '_', $this->name);
     $this->content = '';
     if (empty($this->button_label)) {
         $this->button_label = __('Choose Relation', CCTM_TXTDOMAIN);
     }
     $this->post_id = $this->value;
     $fieldtpl = '';
     $wrappertpl = '';
     $relationmeta_tpl = CCTM::load_tpl(array('fields/options/' . $this->name . '.tpl', 'fields/options/_relationmeta.tpl'));
     // Multi field?
     if ($this->is_repeatable) {
         $fieldtpl = CCTM::load_tpl(array('fields/elements/' . $this->name . '.tpl', 'fields/elements/_relationmeta_multi.tpl'));
         $wrappertpl = CCTM::load_tpl(array('fields/wrappers/' . $this->name . '.tpl', 'fields/wrappers/_relationmeta_multi.tpl', 'fields/wrappers/_relation_multi.tpl'));
     } else {
         $fieldtpl = CCTM::load_tpl(array('fields/elements/' . $this->name . '.tpl', 'fields/elements/_relationmeta.tpl'));
         $wrappertpl = CCTM::load_tpl(array('fields/wrappers/' . $this->name . '.tpl', 'fields/wrappers/_relation.tpl'));
     }
     $data = $this->get_value($current_value, 'ignored');
     if ($data) {
         foreach ($data as $post_id => $metafields) {
             // Look up all the data on those foriegn keys
             // We gotta watch out: what if the related post has custom fields like "description" or
             // anything that would conflict with the definition?
             // I'm using $post as my hash for $fieldtpl... I think $wrappertpl is what I need to watch out for.
             $post = $Q->get_post($post_id);
             $post['thumbnail_url'] = CCTM::get_thumbnail($post_id);
             if (empty($post)) {
                 $this->content = '<div class="cctm_error"><p>' . sprintf(__('Post %s not found.', CCTM_TXTDOMAIN), $post_id) . '</p></div>';
             } else {
                 // Warning: $metafields that is set on this post may not containt fields
                 // that were newly added to the def, so we flesh it out from the $this->metafields def.
                 $content = '';
                 if (is_array($this->metafields)) {
                     foreach ($this->metafields as $mf) {
                         if (!isset($metafields[$mf])) {
                             $metafields[$mf] = '';
                         }
                     }
                 }
                 // Look up data for each of the metafields
                 foreach ($metafields as $mf => $v) {
                     if (!isset(CCTM::$data['custom_field_defs'][$mf])) {
                         continue;
                     }
                     $d = CCTM::$data['custom_field_defs'][$mf];
                     if (!($FieldObj = CCTM::load_object($d['type'], 'fields'))) {
                         continue;
                     }
                     $d['name'] = $this->name . '[' . $post_id . '][' . $d['name'] . ']';
                     $d['value'] = $v;
                     $d['is_repeatable'] = false;
                     // override
                     $FieldObj->set_props($d);
                     $output_this_field = $FieldObj->get_edit_field_instance($v);
                     $content .= CCTM::parse($relationmeta_tpl, array('content' => $output_this_field));
                 }
                 $post['metafields'] = $content;
                 $this->content .= CCTM::parse($fieldtpl, $post);
             }
         }
         // endforeach
     }
     // end $data
     if (empty($this->button_label)) {
         $this->button_label = __('Choose Relation', CCTM_TXTDOMAIN);
     }
     return CCTM::parse($wrappertpl, $this->get_props());
 }
}
if (isset($metaboxes['advanced'])) {
    foreach ($metaboxes['advanced'] as $m => $items) {
        $data['advanced_boxes'] .= CCTM_Metabox::get_metabox_holder($m, $items);
    }
}
if (isset($metaboxes['side'])) {
    foreach ($metaboxes['side'] as $m => $items) {
        $data['side_boxes'] .= CCTM_Metabox::get_metabox_holder($m, $items);
    }
}
//print_r($data); exit;
// List the unused custom fields
$remaining_custom_fields = array_diff($all_custom_fields, $active_custom_fields);
foreach ($remaining_custom_fields as $cf) {
    $d = self::$data['custom_field_defs'][$cf];
    if (!($FieldObj = CCTM::load_object($d['type'], 'fields'))) {
        continue;
    }
    $d['icon'] = $FieldObj->get_icon();
    if (!CCTM::is_valid_img($d['icon'])) {
        $d['icon'] = self::get_custom_icons_src_dir() . 'default.png';
    }
    $d['class'] = 'ui-state-default';
    $d['metabox'] = '';
    $d['edit_field_link'] = sprintf('<a href="%s/wp-admin/admin.php?page=cctm_fields&a=edit_custom_field&field=%s&_wpnonce=%s" title="%s" class="linklike">%s</a>', get_site_url(), $d['name'], wp_create_nonce('cctm_edit_field'), __('Edit this custom field', CCTM_TXTDOMAIN), __('Edit', CCTM_TXTDOMAIN));
    $data['unused'] .= CCTM::load_view('li_field.php', $d);
}
$data['content'] = CCTM::load_view('metaboxes.php', $data);
print CCTM::load_view('templates/default.php', $data);
/*EOF*/
    return;
}
$fieldname = preg_replace('/^' . CCTM_FormElement::css_id_prefix . '/', '', $raw_fieldname);
$def = CCTM::get_value(CCTM::$data['custom_field_defs'], $fieldname);
if (empty($def)) {
    print '<p>' . sprintf(__('Invalid fieldname: %s', CCTM_TXTDOMAIN), '<em>' . htmlspecialchars($fieldname) . '</em>') . '</p>';
    return;
}
$instance = CCTM::get_value($_POST, 'instance');
// Will be either the single or the multi, depending.
$tpl = '';
// Use multi - tpls
if (CCTM::get_value($def, 'is_repeatable')) {
    $tpl = CCTM::load_tpl(array('fields/elements/' . $def['name'] . '.tpl', 'fields/elements/_' . $def['type'] . '_multi.tpl'));
} else {
    $tpl = CCTM::load_tpl(array('fields/elements/' . $def['name'] . '.tpl', 'fields/elements/_' . $def['type'] . '.tpl'));
}
// Just in case...
if (empty($tpl)) {
    print '<p>' . __('Formatting template not found!', CCTM_TXTDOMAIN) . '</p>';
    return;
}
$FieldObj = CCTM::load_object($def['type'], 'fields');
if (!$FieldObj) {
    return;
}
$def['id'] = $fieldname;
$def['i'] = $instance;
$FieldObj->set_props($def);
print CCTM::parse($tpl, $FieldObj->get_props());
/*EOF*/
 /**
  * Show a bit about validation rules if we can
  * @return string
  */
 public function get_options_desc()
 {
     $out = '';
     if (!empty($this->props['validator'])) {
         $Vobj = CCTM::load_object($this->props['validator'], 'validators');
         if ($Vobj) {
             $out .= $Vobj->get_name() . '<br/>';
         }
     }
     if ($this->props['required']) {
         $out .= sprintf('<span style="color:red;">%s</span><br/>', __('Required', CCTM_TXTDOMAIN));
     }
     if (!empty($this->props['default_value'])) {
         $out .= ' ' . $this->props['default_value'] . '<em>(' . __('default', CCTM_TXTDOMAIN) . ')</em>';
     }
     return $out;
 }
    /**
     * Get the list of available validators.
     *
     * @param	array	current def
     * @return	strin	HTML
     */
    public function format_validators($def, $show_validators = true)
    {
        $req_is_checked = '';
        if (isset($def['required']) && $def['required'] == 1) {
            $req_is_checked = 'checked="checked"';
        }
        // Is Required?
        // Get available Validators
        $select_options = '';
        $validation_select = '';
        // containing select element
        $validator_options = '';
        // options for the active validator (if any)
        if ($show_validators) {
            $validators = CCTM::get_available_helper_classes('validators');
            foreach ($validators as $shortname => $path) {
                $Vobj = CCTM::load_object($shortname, 'validators');
                if (!$Vobj) {
                    continue;
                    // skip  bogus validators
                }
                $is_selected = '';
                if ($this->validator == $shortname) {
                    $is_selected = ' selected="selected"';
                    $Vobj->set_options($this->validator_options);
                    $validator_options = $Vobj->draw_options();
                }
                $select_options .= sprintf('<option value="%s"%s>%s</option>', $shortname, $is_selected, $Vobj->get_name());
                $validation_select = '
				<div class="' . self::wrapper_css_class . '" id="validator_wrapper">
					<label for="validator" class="cctm_label cctm_dropdown_label" id="validator_label">' . __('Validation Rule', CCTM_TXTDOMAIN) . '</label>
					<span class="cctm_description">' . __('A validation rule can ensure that any data entered into this field meets a specific criteria.', CCTM_TXTDOMAIN) . '</span>
					<br />
					<select id="validator" name="validator" onchange="javascript:get_validator_options();">
						<option value="">-- ' . __('None', CCTM_TXTDOMAIN) . '--</option>				
						' . $select_options . '
					</select>
			 	</div>';
            }
        }
        $out = '
		<div class="postbox">
			<div class="handlediv" title="Click to toggle"><br /></div>
			<h3 class="hndle"><span>' . __('Validation', CCTM_TXTDOMAIN) . '</span></h3>
			<div class="inside">

				<table>
					<tr>
						<td style="vertical-align:top">
						
				<div class="' . self::wrapper_css_class . '" id="required_wrapper">
					<label for="required" class="cctm_label" id="required_label">' . __('Required?', CCTM_TXTDOMAIN) . '</label>
					<br />
					<input type="checkbox" name="required" class="cctm_checkbox" id="required" value="1" ' . $req_is_checked . '/> <span class="cctm_checkbox_label">' . $this->descriptions['required'] . '</span>
			 	</div>' . $validation_select . '
						
						</td>
						<td width="100"></td>
						<td style="vertical-align:top">
							<div id="validator_options">
								' . $validator_options . '
							</div>
			 			</td>
			 		</tr>
			 	</table>
			 	
		 	</div><!-- /inside -->
		</div><!-- /postbox -->
		';
        return $out;
    }