Example #1
0
 /**
  * Sets post
  * 
  * @global type $post
  * @param type $post_id
  * @return null 
  */
 function set_post($post_id = null)
 {
     // Set post
     if (!is_null($post_id)) {
         $_post = get_post($post_id);
     } else {
         $post_id = wpcf_get_post_id();
         $_post = get_post($post_id);
     }
     if (empty($_post->ID)) {
         $this->post = null;
         return null;
     }
     $this->post = $_post;
     return $_post;
 }
Example #2
0
/**
 * Fix due to a bug saving date as array.
 * 
 * BUGS
 * 'timestamp' is saved without Hour and Minute appended.
 * 
 * @param type $value
 * @param type $field
 */
function __wpcf_fields_date_check_leftover($value, $field, $use_cache = true)
{
    if (empty($value)) {
        return $value;
    }
    if (!is_object($field)) {
        $post_id = wpcf_get_post_id();
        $field_id = isset($field['id']) ? $field['id'] : false;
        $meta_id = isset($field['__meta_id']) ? $field['__meta_id'] : false;
    } else {
        $post_id = isset($field->meta_object->post_id) ? $field->meta_object->post_id : false;
        $field_id = isset($field->cf['id']) ? $field->cf['id'] : false;
        $meta_id = isset($field->meta_object->meta_id) ? $field->meta_object->meta_id : false;
    }
    if (empty($post_id) || empty($meta_id) || empty($field_id)) {
        return $value;
    }
    $field_slug = wpcf_types_get_meta_prefix() . $field_id;
    // Check if cached
    static $cache = array();
    $cache_key = $meta_id;
    if (isset($cache[$cache_key]) && $use_cache) {
        return $cache[$cache_key];
    }
    $_meta = wpcf_get_post_meta($post_id, '_wpcf_' . $field_id . '_hour_and_minute', true);
    /*
     * If meta exists - it's outdated value
     * and Hour and Minute should be appended and removed.
     */
    if (!empty($_meta) && is_array($_meta) && isset($_meta[$meta_id])) {
        $meta = $_meta[$meta_id];
        // Return empty date if can not be calculated
        if (!empty($meta['timestamp']) || !empty($meta['datepicker'])) {
            $meta['timestamp'] = wpcf_fields_date_value_get_filter($meta, $field, 'timestamp', 'check_leftover');
            // Check if calculation needed
            if (isset($meta['hour']) && $meta['hour'] != adodb_date('H', $meta['timestamp']) || isset($meta['minute']) && $meta['minute'] != adodb_date('i', $meta['timestamp'])) {
                $value = wpcf_fields_date_calculate_time($meta);
            }
        }
    }
    // Cache it
    if ($use_cache) {
        $cache[$cache_key] = $value;
    }
    return $value;
}
/**
 * Check if checkbox is submitted.
 *
 * Currently used on Relationship saving. May be expanded to general code.
 *
 * @param type $post_id
 */
function wpcf_fields_checkbox_save_check($post_id)
{
    $meta_to_unset = array();
    $meta_to_unset[$post_id] = array();
    $cf = new WPCF_Field();
    /*
     *
     * We hve several calls on this:
     * 1. Saving post with Update
     * 2. Saving all children
     * 3. Saving child
     */
    $mode = 'save_main';
    if (defined('DOING_AJAX') && isset($_GET['wpcf_action'])) {
        switch ($_GET['wpcf_action']) {
            case 'pr_save_all':
                $mode = 'save_all';
                break;
            case 'pr_save_child_post':
                $mode = 'save_child';
                break;
        }
    }
    /**
     * update edited post chechboxes
     */
    switch ($mode) {
        case 'save_main':
            if (isset($_POST['_wptoolset_checkbox'])) {
                foreach (array_keys($_POST['_wptoolset_checkbox']) as $slug) {
                    if (array_key_exists('wpcf', $_POST)) {
                        wpcf_fields_checkbox_update_one($post_id, $slug, $_POST['wpcf']);
                    } else {
                        $slug_without_form = preg_replace('/cred_form_\\d+_\\d+_/', '', $slug);
                        wpcf_fields_checkbox_update_one($post_id, $slug_without_form, $_POST);
                    }
                }
            }
            return;
        case 'save_child':
        case 'save_all':
            if (!array_key_exists('_wptoolset_checkbox', $_POST)) {
                break;
            }
            foreach (array_keys($_POST['wpcf_post_relationship']) as $post_id) {
                /**
                 * sanitize and check variable
                 */
                $post_id = intval($post_id);
                if (0 == $post_id) {
                    continue;
                }
                /**
                 * stop if do not exist arary key
                 */
                if (!array_key_exists($post_id, $_POST['wpcf_post_relationship'])) {
                    continue;
                }
                /**
                 * stop if array is empty
                 */
                if (!count($_POST['wpcf_post_relationship'][$post_id])) {
                    continue;
                }
                /**
                 * prepare children id
                 */
                $children = array();
                foreach (array_keys($_POST['wpcf_post_relationship'][$post_id]) as $child_id) {
                    $children[] = $child_id;
                }
                $re = sprintf('/\\-(%s)$/', implode('|', $children));
                $checkboxes = array();
                foreach (array_keys($_POST['_wptoolset_checkbox']) as $key) {
                    $checkboxes[] = preg_replace($re, '', $key);
                }
                foreach ($children as $child_id) {
                    foreach (array_unique($checkboxes) as $slug) {
                        wpcf_fields_checkbox_update_one($child_id, $slug, $_POST['wpcf_post_relationship'][$post_id][$child_id]);
                    }
                }
            }
            break;
    }
    // See if any marked for checking
    if (isset($_POST['_wpcf_check_checkbox'])) {
        // Loop and search in $_POST
        foreach ($_POST['_wpcf_check_checkbox'] as $child_id => $slugs) {
            foreach ($slugs as $slug => $true) {
                $cf->set($child_id, $cf->__get_slug_no_prefix($slug));
                // First check main post
                if ($mode == 'save_main' && intval($child_id) == wpcf_get_post_id()) {
                    if (!isset($_POST['wpcf'][$cf->cf['slug']])) {
                        $meta_to_unset[intval($child_id)][$cf->slug] = true;
                    }
                    continue;
                }
                // If new post
                if ($mode == 'save_main' && $child_id == 0) {
                    if (!isset($_POST['wpcf'][$cf->cf['slug']])) {
                        $meta_to_unset[$post_id][$cf->slug] = true;
                    }
                    continue;
                }
                /**
                 * Relationship check
                 */
                if ($mode == 'save_main') {
                    if (!isset($_POST['wpcf'][$cf->cf['slug']])) {
                        $meta_to_unset[$post_id][$cf->slug] = true;
                    }
                    continue;
                } elseif (!empty($_POST['wpcf_post_relationship'])) {
                    foreach ($_POST['wpcf_post_relationship'] as $_parent => $_children) {
                        foreach ($_children as $_child_id => $_slugs) {
                            if (!isset($_slugs[$slug])) {
                                $meta_to_unset[$_child_id][$cf->slug] = true;
                            }
                        }
                    }
                }
            }
        }
    }
    // After collected - delete them
    foreach ($meta_to_unset as $child_id => $slugs) {
        foreach ($slugs as $slug => $true) {
            $cf->set($child_id, $cf->__get_slug_no_prefix($slug));
            if ($cf->cf['data']['save_empty'] != 'no') {
                update_post_meta($child_id, $slug, 0);
            } else {
                delete_post_meta($child_id, $slug);
            }
        }
    }
}
Example #4
0
/**
 * Check if checkbox is submitted.
 * 
 * Currently used on Relationship saving. May be expanded to general code.
 * 
 * @param type $value
 * @param type $field
 * @param type $cf
 */
function wpcf_fields_checkbox_save_check()
{
    $meta_to_unset = array();
    $cf = new WPCF_Field();
    /*
     * 
     * We hve several calls on this:
     * 1. Saving post with Update
     * 2. Saving all children
     * 3. Saving child
     */
    $mode = 'save_main';
    if (defined('DOING_AJAX')) {
        if (isset($_GET['wpcf_action']) && $_GET['wpcf_action'] == 'pr_save_all') {
            $mode = 'save_all';
        } else {
            if (isset($_GET['wpcf_action']) && $_GET['wpcf_action'] == 'pr_save_child_post') {
                $mode = 'save_child';
            }
        }
    }
    // See if any marked for checking
    if (isset($_POST['_wpcf_check_checkbox'])) {
        // Loop and search in $_POST
        foreach ($_POST['_wpcf_check_checkbox'] as $child_id => $slugs) {
            foreach ($slugs as $slug => $true) {
                $cf->set($child_id, $cf->__get_slug_no_prefix($slug));
                // First check main post
                if ($mode == 'save_main' && intval($child_id) == wpcf_get_post_id()) {
                    if (!isset($_POST['wpcf'][$cf->cf['slug']])) {
                        $meta_to_unset[intval($child_id)][$cf->slug] = true;
                    }
                    continue;
                }
                /*
                 * 
                 * Relationship check
                 */
                if (!isset($_POST['wpcf_post_relationship'])) {
                    $meta_to_unset[$child_id][$cf->slug] = true;
                } else {
                    foreach ($_POST['wpcf_post_relationship'] as $_parent => $_children) {
                        foreach ($_children as $_child_id => $_slugs) {
                            if (!isset($_slugs[$slug])) {
                                $meta_to_unset[$_child_id][$cf->slug] = true;
                            }
                        }
                    }
                }
            }
        }
    }
    // After collected - delete them
    foreach ($meta_to_unset as $child_id => $slugs) {
        foreach ($slugs as $slug => $true) {
            $cf->set($child_id, $cf->__get_slug_no_prefix($slug));
            if ($cf->cf['data']['save_empty'] != 'no') {
                update_post_meta($child_id, $slug, 0);
            } else {
                delete_post_meta($child_id, $slug);
            }
        }
    }
}
Example #5
0
/**
 * Fix due to a bug saving date as array.
 * 
 * BUGS
 * 'timestamp' is saved without Hour and Minute appended.
 * 
 * @param type $value
 * @param type $field
 */
function __wpcf_fields_date_check_leftover($value, $field, $use_cache = true)
{
    if (empty($value)) {
        return $value;
    }
    if (!is_object($field)) {
        $post_id = wpcf_get_post_id();
        $field_id = isset($field['id']) ? $field['id'] : false;
        $meta_id = isset($field['__meta_id']) ? $field['__meta_id'] : false;
    } else {
        $post_id = isset($field->meta_object->post_id) ? $field->meta_object->post_id : false;
        $field_id = isset($field->cf['id']) ? $field->cf['id'] : false;
        $meta_id = isset($field->meta_object->meta_id) ? $field->meta_object->meta_id : false;
    }
    if (empty($post_id) || empty($meta_id) || empty($field_id)) {
        return $value;
    }
    $field_slug = wpcf_types_get_meta_prefix() . $field_id;
    // Check if cached
    static $cache = array();
    $cache_key = $meta_id;
    if (isset($cache[$cache_key]) && $use_cache) {
        return $cache[$cache_key];
    }
    $_meta = wpcf_get_post_meta($post_id, '_wpcf_' . $field_id . '_hour_and_minute', true);
    /*
     * If meta exists - it's outdated value
     * and Hour and Minute should be appended and removed.
     */
    if (!empty($_meta) && is_array($_meta) && isset($_meta[$meta_id])) {
        $meta = $_meta[$meta_id];
        // Return empty date if can not be calculated
        if (!empty($meta['timestamp']) || !empty($meta['datepicker'])) {
            $meta['timestamp'] = wpcf_fields_date_value_get_filter($meta, $field, 'timestamp', 'check_leftover');
            // Check if calculation needed
            if (isset($meta['hour']) && $meta['hour'] != adodb_date('H', $meta['timestamp']) || isset($meta['minute']) && $meta['minute'] != adodb_date('i', $meta['timestamp'])) {
                $value = wpcf_fields_date_calculate_time($meta);
                /*
                 * 
                 * If enabling clearing old values here,
                 * pay attention if field is repetitive.
                 * 
                 * For now - old data is cleared on date save.
                 * wpcf_fields_date_value_save_filter()
                 */
                // Update meta
                //                    $success = update_post_meta( $post_id, $field_slug, $value );
                // Remove additional meta
                //                    if ( $success ) {
                //                        delete_post_meta( $post_id,
                //                                '_wpcf_' . $field_id . '_hour_and_minute' );
                //                    }
            }
        }
    }
    // Cache it
    if ($use_cache) {
        $cache[$cache_key] = $value;
    }
    return $value;
}
Example #6
0
/**
 * Parses date meta.
 * 
 * @param type $value
 * @param type $field Field data
 * @return type 
 */
function wpcf_fields_date_value_get_filter($value, $field)
{
    global $wpcf;
    if (is_array($value)) {
        /*
         * Since Types 1.2
         */
        $value = wp_parse_args($value, array('datepicker' => null, 'hour' => 8, 'minute' => 0));
    } else {
        /*
         * We assume it is timestamp from earlier versions
         */
        $value = array('datepicker' => $value, 'hour' => 8, 'minute' => 0);
    }
    /*
     * Since Types 1.2 we require $cf field object
     */
    if ($field instanceof WPCF_Field) {
        $post = $field->post;
    } else {
        // Remove for moment
        remove_filter('wpcf_fields_type_date_value_get', 'wpcf_fields_date_value_get_filter', 10, 4);
        // Hide on frontpage where things will go fine because of loop
        if (is_admin()) {
            _deprecated_argument('date_obsolete_parameter', '1.2', '<br /><br /><div class="wpcf-error">' . 'Since Types 1.2 $cf field object is required' . '</div><br /><br />');
        }
        /*
         * Set default objects
         */
        $_field = $field;
        $field = new WPCF_Field();
        $field->context = is_admin() ? 'frontend' : 'group';
        $post_id = wpcf_get_post_id($field->context);
        $post = get_post($post_id);
        if (empty($post)) {
            return $value;
        }
        $field->set($post, $_field);
        // Back to filter
        add_filter('wpcf_fields_type_date_value_get', 'wpcf_fields_date_value_get_filter', 10, 4);
    }
    /*
     * 
     * Get hour and minute
     * CHECKPOINT
     * 
     * We need meta_id here.
     */
    if (!empty($post->ID)) {
        $_meta_id = isset($_field['__meta_id']) ? $_field['__meta_id'] : $field->meta_object->meta_id;
        $_hm = get_post_meta($post->ID, '_wpcf_' . $field->cf['id'] . '_hour_and_minute', true);
        $hm = isset($_hm[$_meta_id]) ? $_hm[$_meta_id] : array();
    } else {
        /*
         * If $post is not set.
         * We need to record this
         */
        $wpcf->errors['missing_post'][] = func_get_args();
    }
    /*
     * Setup hour and minute.
     */
    if (!empty($hm) && is_array($hm) && (isset($hm['hour']) && isset($hm['minute']))) {
        $value['hour'] = $hm['hour'];
        $value['minute'] = $hm['minute'];
    }
    // Calculate time
    $value['timestamp'] = wpcf_fields_date_calculate_time($value);
    // Set datepicker to use formatted date
    if (!empty($value['datepicker'])) {
        // Test if already formatted
        if (strtotime($value['datepicker']) === false) {
            $value['datepicker'] = date(wpcf_get_date_format(), intval($value['datepicker']));
        }
    }
    return $value;
}
Example #7
0
/**
 * Parses date meta.
 * 
 * Use this as main function.
 * 
 * @uses wpcf_fields_date_calculate_time()
 * 
 * @param type $value
 * @param type $field Field data
 * $param string $return Specify to return array or specific element of same array
 *          ( timestamp, datepicker, hour, minute )
 * @return mixed array | custom parameter
 */
function wpcf_fields_date_value_get_filter($value, $field, $return = 'array', $context = 'get')
{
    global $wpcf;
    $value_cloned = $value;
    if (is_array($value)) {
        /*
         * See if missing timestamp and datepicker present
         */
        if ((!isset($value['timestamp']) || !is_int($value['timestamp'])) && isset($value['datepicker'])) {
            $_check = strtotime(strval($value['datepicker']));
            if ($_check !== false) {
                $value['timestamp'] = $_check;
            }
        }
        $value = wp_parse_args($value, array('timestamp' => null, 'hour' => 8, 'minute' => 0, 'datepicker' => ''));
    } else {
        if (empty($value)) {
            return array('timestamp' => null, 'hour' => 8, 'minute' => 0, 'datepicker' => '');
        } else {
            /*
             * strtotime() returns negative numbers like -49537390513
             * 
             * https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/160422568/comments
             * http://www.php.net/manual/en/function.strtotime.php
             * 
             * QUOTE
             * Returns a timestamp on success, FALSE otherwise.
             * Previous to PHP 5.1.0, this function would return -1 on failure. 
             * 
             * BUT on some hosts it returns negative numbers ( our test sites too )
             */
            if (!is_numeric($value)) {
                $_check = strtotime($value);
                if ($_check !== false && $_check > 1) {
                    $value = $_check;
                }
            }
            $value = array('timestamp' => intval($value), 'hour' => 8, 'minute' => 0, 'datepicker' => '');
        }
    }
    $value['datepicker'] = trim($value['datepicker']);
    /*
     * Since Types 1.2 we require $cf field object
     */
    if ($field instanceof WPCF_Field) {
        $post = $field->post;
    } else {
        // Remove for moment
        remove_filter('wpcf_fields_type_date_value_get', 'wpcf_fields_date_value_get_filter', 10, 4);
        // Hide on frontpage where things will go fine because of loop
        if (is_admin()) {
            _deprecated_argument('date_obsolete_parameter', '1.2', '<br /><br /><div class="wpcf-error">' . 'Since Types 1.2 $cf field object is required' . '</div><br /><br />');
        }
        /*
         * Set default objects
         */
        $_field = $field;
        $field = new WPCF_Field();
        $field->context = is_admin() ? 'frontend' : 'group';
        $post_id = wpcf_get_post_id($field->context);
        $post = get_post($post_id);
        if (empty($post)) {
            return $value;
        }
        $field->set($post, $_field);
        // Back to filter
        add_filter('wpcf_fields_type_date_value_get', 'wpcf_fields_date_value_get_filter', 10, 4);
    }
    /*
     * Get hour and minute
     * We need meta_id here.
     * 
     * NOT Used for 'save' context.
     * We already have submitted data in $value
     */
    if (!in_array($context, array('save', 'skip_hour_and_minute'))) {
        if (!empty($post->ID)) {
            $_meta_id = isset($_field['__meta_id']) ? $_field['__meta_id'] : $field->meta_object->meta_id;
            $_hm = get_post_meta($post->ID, '_wpcf_' . $field->cf['id'] . '_hour_and_minute', true);
            $hm = isset($_hm[$_meta_id]) ? $_hm[$_meta_id] : array();
        } else {
            /*
             * If $post is not set.
             * We need to record this
             */
            $wpcf->errors['missing_post'][] = func_get_args();
        }
        /*
         * Setup hour and minute.
         */
        if (!empty($hm) && is_array($hm) && (isset($hm['hour']) && isset($hm['minute']))) {
            $value['hour'] = $hm['hour'];
            $value['minute'] = $hm['minute'];
        }
    }
    // Calculate time IF NOT SET ( otherwise it's same as main meta value )
    // Always when using 'get' context on frontend
    if (!is_admin() && $context == 'get' || (empty($value['timestamp']) || !is_int($value['timestamp']))) {
        $value['timestamp'] = wpcf_fields_date_calculate_time($value);
    }
    /*
     * Set datepicker to use formatted date IF DO NOT EXISTS
     * (otherwise it keeps Datepicker string like 'August 9, 2012'.
     * OR is not time string
     */
    if (!empty($value['timestamp']) && (empty($value['datepicker']) || strtotime(strval($value['datepicker'])) === false)) {
        $value['datepicker'] = date(wpcf_get_date_format(), intval($value['timestamp']));
    }
    $_return = $value;
    if ($return != 'array') {
        if (isset($value[strval($return)])) {
            $_return = $value[strval($return)];
        }
    }
    // Debug
    $wpcf->debug->dates[] = array('original_value' => $value_cloned, 'value' => $value, 'return' => $_return, 'field' => $field->cf, 'context' => $context);
    return $_return;
}