/**
  * Apply the filter.
  *
  * @param 	mixed 	input: an integer, an array of integers, or a JSON string representing an array of integers.
  * @param	string	optional formatting tpl
  * @return mixed
  */
 public function filter($input, $options = null)
 {
     $required_functions = array('getimagesize', 'imagecreatefromjpeg', 'imagecreatefromgif', 'imagecreatefrompng');
     foreach ($required_functions as $f) {
         if (!function_exists($f)) {
             return sprintf(__('Missing required function %s'), '<code>' . $f . '</code>');
         }
     }
     require_once CCTM_PATH . '/includes/SummarizePosts.php';
     require_once CCTM_PATH . '/includes/GetPostsQuery.php';
     $tpl = '<div class="cctm_gallery" id="cctm_gallery_[+i+]"><img height="[+height+]" width="[+width+]" src="[+guid+]" title="[+post_title+]" alt="[+alt+]" class="cctm_image" id="cctm_image_[+i+]"/></div>';
     if (!empty($options)) {
         $tpl = $options;
     }
     if (empty($input)) {
         return '';
     }
     $inputs = $this->to_array($input);
     $Q = new GetPostsQuery();
     $Q->set_include_hidden_fields(true);
     // We can't use $Q->get_posts() because MySQL will return results in an arbitrary order.  boo.
     $output = '';
     $i = 1;
     foreach ($inputs as $image_id) {
         $r = $Q->get_post($image_id);
         // Translate
         $r['post_title'] = __($r['post_title']);
         $r['post_content'] = __($r['post_content']);
         $r['post_excerpt'] = __($r['post_excerpt']);
         $image_info = getimagesize($r['guid']);
         $image_type = $image_info[2];
         if ($image_type == IMAGETYPE_JPEG) {
             $this_image = imagecreatefromjpeg($r['guid']);
         } elseif ($image_type == IMAGETYPE_GIF) {
             $this_image = imagecreatefromgif($r['guid']);
         } elseif ($image_type == IMAGETYPE_PNG) {
             $this_image = imagecreatefrompng($r['guid']);
         }
         if (isset($r['_wp_attachment_image_alt'])) {
             $r['alt'] = $r['_wp_attachment_image_alt'];
         }
         $r['i'] = $i;
         $r['width'] = imagesx($this_image);
         $r['height'] = imagesy($this_image);
         $output .= CCTM::parse($tpl, $r);
         $i++;
     }
     return $output;
 }
if (isset($GLOBALS['wp_post_types'][$post_type]->cap->edit_posts)) {
    $cap = $GLOBALS['wp_post_types'][$post_type]->cap->edit_posts;
}
if (!current_user_can($cap)) {
    die('<pre>You do not have permission to do that.</pre>');
}
require_once CCTM_PATH . '/includes/GetPostsQuery.php';
//print '<div>'. print_r($_POST, true).'</div>';
$post_id = CCTM::get_value($_POST, 'post_id');
$target_id = CCTM::get_value($_POST, 'target_id');
// Will be either the single or the multi, depending.
$tpl = '';
$tpl = CCTM::load_tpl('widgets/post_item.tpl');
// Just in case...
if (empty($tpl)) {
    print '<p>' . __('Formatting template not found!', CCTM_TXTDOMAIN) . '</p>';
    return;
}
$Q = new GetPostsQuery();
$post = $Q->get_post($post_id);
$post['edit_selected_post_label'] = __('Edit Selected Post', CCTM_TXTDOMAIN);
$post_type = $post['post_type'];
$post['post_icon'] = CCTM::get_thumbnail($post_id);
if ($post_type == 'attachment') {
    $post['edit_url'] = get_admin_url('', 'media.php') . "?attachment_id={$post_id}&action=edit";
} else {
    $post['edit_url'] = get_admin_url('', 'post.php') . "?post={$post_id}&action=edit";
}
$post['target_id'] = $target_id;
print CCTM::parse($tpl, $post);
/*EOF*/
    /**
     * We use this to print out the large icon at the top of the page when editing a post/page
     * and to enforce field validation (including required fields).
     * http://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=188
     * https://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=544
     */
    public static function print_admin_header()
    {
        $file = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], '/') + 1);
        if (!in_array($file, array('post.php', 'post-new.php', 'edit.php'))) {
            return;
        }
        $post_type = CCTM::get_value($_GET, 'post_type');
        if (empty($post_type)) {
            $post_id = (int) CCTM::get_value($_GET, 'post');
            if (empty($post_id)) {
                return;
            }
            $post = get_post($post_id);
            $post_type = $post->post_type;
        }
        // Only do this stuff for active post-types (is_active can be 1 for built-in or 2 for foreign)
        if (!isset(CCTM::$data['post_type_defs'][$post_type]['is_active']) || !CCTM::$data['post_type_defs'][$post_type]['is_active']) {
            return;
        }
        // Show the big icon: http://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=136
        if (isset(CCTM::$data['post_type_defs'][$post_type]['use_default_menu_icon']) && CCTM::$data['post_type_defs'][$post_type]['use_default_menu_icon'] == 0) {
            $baseimg = basename(CCTM::$data['post_type_defs'][$post_type]['menu_icon']);
            // die($baseimg);
            if (file_exists(CCTM_PATH . '/images/icons/32x32/' . $baseimg)) {
                printf('
				<style>
					#icon-edit, #icon-post {
					  background-image:url(%s);
					  background-position: 0px 0px;
					}
				</style>', CCTM_URL . '/images/icons/32x32/' . $baseimg);
            }
        }
        // Validate the custom fields when a page is first printed (e.g. the validation rule was
        // created or updated, then the post is edited)
        $file = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], '/') + 1);
        if (in_array($file, array('post.php'))) {
            global $post;
            $Q = new GetPostsQuery();
            $full_post = $Q->get_post($post->ID);
            if (!self::validate_fields($post_type, $full_post)) {
                // Print any validation errors.
                $output = '<div class="error"><img src="' . CCTM_URL . '/images/warning-icon.png" width="50" height="44" style="float:left; padding:10px; vertical-align:middle;"/><p style=""><strong>' . __('This post has validation errors.  The post will remain in draft status until they are corrected.', CCTM_TXTDOMAIN) . '</strong></p>
						<ul style="clear:both;">';
                foreach (CCTM::$post_validation_errors as $fieldname => $e) {
                    $output .= '<li>' . $e . '</li>';
                }
                $output .= '</ul></div>';
                // You have to print the style because WP overrides styles after the cctm manager.css is included.
                // This isn't helpful during the admin head event because you'd have to also check validation at the time when
                // the fields are printed in print_custom_fields(), which fires later on.
                // We can use this variable to pass data to a point later in the page request.
                // global $cctm_validation;
                // CCTM::$errors
                // CCTM::$errors['my_field'] = 'This is the validation error with that field';
                $output .= '<style>';
                $output .= file_get_contents(CCTM_PATH . '/css/validation.css');
                $output .= '</style>';
                print $output;
                // Override!! set post to draft status if there were validation errors.
                // See https://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=544
                global $wpdb;
                $post_id = (int) CCTM::get_value($_GET, 'post');
                $wpdb->update($wpdb->posts, array('post_status' => 'draft'), array('ID' => $post_id), array('%s'), array('%d'));
            }
        }
    }
 /**
  * Process the $args to something GetPostsQuery. 
  */
 function widget($args, $instance)
 {
     // Avoid placing empty widgets
     if (!isset($instance['post_id']) || empty($instance['post_id'])) {
         return;
     }
     require_once CCTM_PATH . '/includes/GetPostsQuery.php';
     $post_id = (int) $instance['post_id'];
     $Q = new GetPostsQuery();
     $post = $Q->get_post($post_id);
     $post['post_content'] = do_shortcode(wpautop($post['post_content']));
     $output = $args['before_widget'];
     if (isset($instance['override_title']) && $instance['override_title'] == 1) {
         $title = $instance['title'];
     } else {
         $title = $post['post_title'];
         // default is to use the post's title
     }
     if (!empty($title)) {
         $output .= $args['before_title'] . $title . $args['after_title'];
     }
     if (!empty($instance['formatting_string'])) {
         $output .= CCTM::parse($instance['formatting_string'], $post);
     } else {
         $output .= $post['post_content'];
     }
     $output .= $args['after_widget'];
     print $output;
 }
 /**
  *
  *
  * @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 = '';
     // Multi field?
     if ($this->is_repeatable) {
         $this->remove_label = __('Remove All');
         $fieldtpl = CCTM::load_tpl(array('fields/elements/' . $this->name . '.tpl', 'fields/elements/_relation_multi.tpl'));
         $wrappertpl = CCTM::load_tpl(array('fields/wrappers/' . $this->name . '.tpl', 'fields/wrappers/_relation_multi.tpl'));
         $values = $this->get_value($current_value, 'to_array');
         foreach ($values as $v) {
             $hash = $this->get_props();
             $hash['post_id'] = (int) $v;
             $hash['thumbnail_url'] = CCTM::get_thumbnail($hash['post_id']);
             // Look up all the data on that foreign key
             // We gotta watch out: what if the related post has custom fields like "description" or
             // anything that would conflict with the definition?
             $post = $Q->get_post($hash['post_id']);
             if (empty($post)) {
                 $this->content .= '<div class="cctm_error"><p>' . sprintf(__('Post %s not found.', CCTM_TXTDOMAIN), $v) . '</p></div>';
             } else {
                 foreach ($post as $k => $v) {
                     // Don't override the def's attributes!
                     if (!isset($hash[$k])) {
                         $hash[$k] = $v;
                     }
                 }
                 $this->content .= CCTM::parse($fieldtpl, $hash);
             }
         }
     } else {
         $this->remove_label = __('Remove');
         $this->post_id = $this->get_value($current_value, 'to_string');
         $this->thumbnail_url = CCTM::get_thumbnail($this->post_id);
         $fieldtpl = CCTM::load_tpl(array('fields/elements/' . $this->name . '.tpl', 'fields/elements/_relation.tpl'));
         $wrappertpl = CCTM::load_tpl(array('fields/wrappers/' . $this->name . '.tpl', 'fields/wrappers/_relation.tpl'));
         if ($this->post_id) {
             // Look up all the data on that foriegn key
             // We gotta watch out: what if the related post has custom fields like "description" or
             // anything that would conflict with the definition?
             $post = $Q->get_post($this->post_id);
             if (empty($post)) {
                 $this->content = '<div class="cctm_error"><p>' . sprintf(__('Post %s not found.', CCTM_TXTDOMAIN), $this->post_id) . '</p></div>';
             } else {
                 foreach ($post as $k => $v) {
                     // Don't override the def's attributes!
                     if (!isset($this->{$k})) {
                         $this->{$k} = $v;
                     }
                 }
                 $this->content = CCTM::parse($fieldtpl, $this->get_props());
             }
         }
     }
     if (empty($this->button_label)) {
         $this->button_label = __('Choose Relation', CCTM_TXTDOMAIN);
     }
     return CCTM::parse($wrappertpl, $this->get_props());
 }
예제 #6
0
 /**
  * Callback function used by the cctm_post_form() function.  This is what gets 
  * called 
  *
  * @param	array $args: parameters from the shortcode and posted data
  * @return string (printed)	 
  */
 public static function post_form_handler($args)
 {
     //return print_r($args,true);
     // Strip out the control stuff (keys begin with underscore)
     $vals = array();
     foreach ($args as $k => $v) {
         if ($k[0] == '_') {
             continue;
         }
         $vals[$k] = $v;
     }
     // Insert into Database
     $email_only = CCTM::get_value($args, '_email_only');
     if (!$email_only) {
         require_once CCTM_PATH . '/includes/SP_Post.php';
         $P = new SP_Post();
         CCTM::$post_id = $P->insert($vals);
     }
     // Email stuff
     if (isset($args['_email_to']) && !empty($args['_email_to']) && isset($args['_email_tpl']) && !empty($args['_email_tpl'])) {
         $Q = new GetPostsQuery();
         $P = $Q->get_post($args['_email_tpl']);
         //return print_r($P, true);
         $subject = $P['post_title'];
         $message_tpl = wpautop($P['post_content']);
         // If the 'My User' <*****@*****.**> format is used, we have to manipulate the string
         // to keep WP from tripping over itself
         $from = CCTM::get_value($args, '_email_from', get_bloginfo('admin_email'));
         $from = str_replace(array('&#039', '&#034;', '&quot;', '&lt;', '&gt;'), array("'", '"', '"', '<', '>'), $from);
         // die(print_r($args,true));
         $subject = CCTM::get_value($args, '_email_subject', $subject);
         $headers = 'From: ' . $from . "\r\n";
         $headers .= 'content-type: text/html' . "\r\n";
         $message = CCTM::parse($message_tpl, $vals);
         if (!wp_mail($args['_email_to'], $subject, $message, $headers)) {
             return "There was a problem sending the email.";
         }
     }
     // Redirect or show a simple message.
     $redirect = CCTM::get_value($args, '_redirect');
     if ($redirect) {
         $url = get_permalink($redirect);
         CCTM::redirect($url, true);
     }
     // Else, return message:
     return CCTM::get_value($args, '_msg', "Thanks for submitting the form!");
 }
 /**
  *
  *
  * @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());
 }
/**
 * Retrieves a complete post object, including all meta fields. It avoids the 
 * standard WP functions get_post() and get_post_custom() because they encountered
 * some weird issues with conflicting global variables (?):
 * http://codex.wordpress.org/Function_Reference/get_post
 * http://codex.wordpress.org/Function_Reference/get_post_custom
 *
 * Returned is a post array that contains a key for each field and custom field, e.g.
 * 
 * print $post['post_title'];
 * print $post['my_custom_field']; // not $post['my_custom_fields'][0];
 * 
 * and if the custom field *is* a list of items, then attach it as such.
 * 
 * @param	integer	$id is valid ID of a post (regardless of post_type).
 * @return	array	associative array of post with all attributes, including custom fields.
 */
function get_post_complete($id)
{
    $Q = new GetPostsQuery();
    return $Q->get_post($id);
}