Example #1
0
/**
 * Load a template array file and merge values with it.
 *
 * @param  string $file
 * @param  array $values
 * @param  bool $convert_to_object
 *
 * @return array
 */
function papi_template($file, $values = [], $convert_to_object = false)
{
    if (!is_string($file) || empty($file)) {
        return [];
    }
    $filepath = papi_get_file_path($file);
    if (empty($filepath) && is_file($file)) {
        $filepath = $file;
    }
    if (empty($filepath) || !file_exists($filepath) || is_dir($filepath)) {
        return [];
    }
    $template = (require $filepath);
    if (papi_is_property($template)) {
        foreach ($values as $key => $value) {
            $template->set_option($key, $value);
        }
        $result = $template;
    } else {
        $result = array_merge((array) $template, $values);
    }
    if ($convert_to_object) {
        return (object) $result;
    }
    return $result;
}
Example #2
0
 /**
  * Convert property value with the property type converter.
  *
  * @param  string $slug
  * @param  mixed  $value
  *
  * @return mixed
  */
 protected function convert($slug, $value)
 {
     $property = $this->get_property($slug);
     // If no property type is found, just return null.
     if (!papi_is_property($property)) {
         return;
     }
     if (papi_is_empty($value)) {
         if (!papi_is_empty($property->get_option('value'))) {
             return $property->get_option('value');
         }
         return;
     }
     // A property need to know about the page.
     $property->set_page($this);
     // Run load value method right after the value has been loaded from the database.
     $value = $property->load_value($value, $slug, $this->id);
     $value = papi_filter_load_value($property->type, $value, $slug, $this->id);
     // Format the value from the property class.
     $value = $property->format_value($value, $slug, $this->id);
     // Only fired when not in admin.
     if (!is_admin()) {
         $value = papi_filter_format_value($property->type, $value, $slug, $this->id);
     }
     if (is_array($value)) {
         $value = array_filter($value);
     }
     return $value;
 }
 /**
  * Prepare rules.
  *
  * @param  array $rules
  * @param  Papi_Core_Property $property
  *
  * @return array
  */
 public function prepare_rules(array $rules, $property = null)
 {
     if (!isset($rules['relation'])) {
         $rules['relation'] = 'OR';
     } else {
         $rules['relation'] = strtoupper($rules['relation']);
     }
     foreach ($rules as $index => $value) {
         if (is_string($index)) {
             continue;
         }
         if (is_array($value)) {
             $rules[$index] = new Papi_Core_Conditional_Rule($value);
             if (strpos($rules[$index]->slug, '.') === false && papi_is_property($property)) {
                 $rules[$index]->slug = $this->get_rule_slug($rules[$index], $property);
             }
         }
     }
     return $rules;
 }
 /**
  * Prepare properties data for saving.
  *
  * @param  array $data
  * @param  int   $post_id
  *
  * @return array
  */
 protected function prepare_properties_data(array $data = [], $post_id = 0)
 {
     // Since we are storing witch property it is in the $data array
     // we need to remove that and set the property type to the property
     // and make a array of the property type and the value.
     foreach ($data as $key => $value) {
         $property_type_key = papi_get_property_type_key();
         if (strpos($key, $property_type_key) === false) {
             continue;
         }
         $property_key = str_replace($property_type_key, '', $key);
         // Check if value exists.
         if (isset($data[$property_key])) {
             $data[$property_key] = ['type' => $value, 'value' => $data[$property_key]];
         }
         unset($data[$key]);
     }
     foreach ($data as $key => $item) {
         $property = papi_get_property_type($item['type']);
         unset($data[$key]);
         if (papi_is_property($property)) {
             // Run `update_value` method on the property class.
             $data[$key] = $property->update_value($item['value'], papi_remove_papi($key), $post_id);
             // Apply `update_value` filter so this can be changed from the theme for specified property type.
             $data[$key] = papi_filter_update_value($item['type']->type, $data[$key], papi_remove_papi($key), $post_id);
             if ($item['type']->overwrite) {
                 $slug = papi_remove_papi($key);
                 $this->overwrite[$slug] = $data[$key];
             }
         }
     }
     return $data;
 }
Example #5
0
 /**
  * Get properties via POST.
  *
  * POST /papi-ajax/?action=get_properties
  */
 public function get_properties()
 {
     if (!papi_get_sanitized_post('properties')) {
         $this->render_error('No properties found');
         return;
     }
     $items = json_decode(stripslashes($_POST['properties']), true);
     if (empty($items) || !is_array($items)) {
         $this->render_error('No properties found');
         return;
     }
     foreach ($items as $key => $item) {
         $property = papi_property((array) $item);
         if (!papi_is_property($property)) {
             unset($items[$key]);
             continue;
         }
         ob_start();
         $property->render_ajax_request();
         $items[$key] = trim(ob_get_clean());
     }
     $items = array_filter($items);
     if (empty($items)) {
         $this->render_error('No properties found');
     } else {
         wp_send_json(['html' => $items]);
     }
 }
Example #6
0
/**
 * Get require tag for property.
 *
 * @param  stdClass $property
 * @param  bool     $text
 *
 * @return string
 */
function papi_required_html($property, $text = false)
{
    if (!papi_is_property($property) || !$property->required) {
        return '';
    }
    return ' <span class="papi-rq" data-property-name="' . $property->title . '" data-property-id="' . $property->slug . '">' . ($text ? papi_require_text($property) : '*') . '</span>';
}
 /**
  * Prepare property for JSON.
  *
  * @param  Papi_Property $property
  *
  * @return bool|object
  */
 protected function prepare_property_for_json($property)
 {
     // Only real property objects and not properties that are disabled.
     if (!papi_is_property($property) || $property->disabled()) {
         return false;
     }
     $options = clone $property->get_options();
     if (isset($options->settings->items)) {
         foreach ($options->settings->items as $index => $property) {
             if (is_array($property) && isset($property['items'])) {
                 foreach ($property['items'] as $child_index => $child_property) {
                     $options->settings->items[$index]['items'][$child_index] = $this->prepare_property_for_json($child_property);
                 }
             }
             if ($property = $this->prepare_property_for_json($property)) {
                 $options->settings->items[$index] = $property;
             }
         }
     }
     return $options;
 }
 /**
  * Get property option or default value.
  *
  * @param  string $slug
  * @param  string $option
  * @param  mixed  $default
  *
  * @return bool
  */
 public function get_property_option($slug, $option, $default = null)
 {
     $slug = unpapify($slug);
     $property = $this->property($slug);
     // If no property type is found, return default
     // value since we don't have a property.
     if (!papi_is_property($property)) {
         return $default;
     }
     $value = $property->get_option($option);
     if (papi_is_empty($value)) {
         return $default;
     }
     return $value;
 }
Example #9
0
 /**
  * Get property from entry type.
  *
  * @param  string $slug
  * @param  string $child_slug
  *
  * @return Papi_Property
  */
 public function get_property($slug, $child_slug = '')
 {
     $boxes = $this->get_boxes();
     $parts = preg_split('/\\[\\d+\\]/', $slug);
     $parts = array_map(function ($part) {
         return preg_replace('/(\\[|\\])/', '', $part);
     }, $parts);
     if (count($parts) > 1) {
         $property = null;
         for ($i = 0, $l = count($parts); $i < $l; $i++) {
             $child = isset($parts[$i + 1]) ? $parts[$i + 1] : '';
             $property = $this->get_property($parts[$i], $child);
             if (isset($parts[$i + 1])) {
                 $i++;
             }
         }
         /**
          * Modify property.
          *
          * @param  Papi_Core_Property $property
          */
         return apply_filters('papi/get_property', $property);
     }
     foreach ($boxes as $box) {
         foreach ($box->properties as $property) {
             $property = papi_get_property_type($property);
             if (papi_is_property($property) && $property->match_slug($slug)) {
                 if (empty($child_slug)) {
                     /**
                      * Modify property.
                      *
                      * @param  Papi_Core_Property $property
                      */
                     return apply_filters('papi/get_property', $property);
                 }
                 $property = $property->get_child_property($child_slug);
                 if (papi_is_property($property)) {
                     /**
                      * Modify property.
                      *
                      * @param  Papi_Core_Property $property
                      */
                     return apply_filters('papi/get_property', $property);
                 }
             }
         }
     }
 }
 /**
  * Prepare properties data for saving.
  *
  * @param  array $data
  * @param  int   $post_id
  *
  * @return array
  */
 protected function prepare_properties_data(array $data = [], $post_id = 0)
 {
     // Since we are storing witch property it is in the `$data` array
     // we need to remove that and set the property type to the property
     // and make a array of the property type and the value.
     foreach ($data as $key => $value) {
         if (papi_is_property_type_key($key)) {
             continue;
         }
         $property_type_key = papify(papi_get_property_type_key($key));
         // Check if value exists.
         if (!isset($data[$key]) && !isset($data[$property_type_key])) {
             continue;
         }
         // Pair property value with property type object.
         $data[$key] = ['type' => $data[$property_type_key], 'value' => $value];
         // Remove property type object since it's not needed anymore.
         unset($data[$property_type_key]);
     }
     foreach ($data as $key => $item) {
         if (papi_is_property_type_key($key)) {
             continue;
         }
         $property = papi_get_property_type($item['type']);
         unset($data[$key]);
         if (papi_is_property($property)) {
             // Run `update_value` method on the property class.
             $data[$key] = $property->update_value($item['value'], unpapify($key), $post_id);
             // Apply `update_value` filter so this can be changed from
             // the theme for specified property type.
             $data[$key] = papi_filter_update_value($item['type']->type, $data[$key], unpapify($key), $post_id, papi_get_meta_type());
             if ($item['type']->overwrite) {
                 $slug = unpapify($key);
                 $this->overwrite[$slug] = $data[$key];
                 unset($data[$key]);
             }
         }
     }
     return $data;
 }
Example #11
0
 /**
  * Get property from page type.
  *
  * @param  string $slug
  * @param  string $child_slug
  *
  * @return null|Papi_Property
  */
 public function get_property($slug, $child_slug = '')
 {
     $boxes = $this->get_boxes();
     $parts = preg_split('/\\[\\d+\\]/', $slug);
     $parts = array_map(function ($part) {
         return preg_replace('/(\\[|\\])/', '', $part);
     }, $parts);
     if (count($parts) > 1) {
         $property = null;
         for ($i = 0, $l = count($parts); $i < $l; $i++) {
             $child = isset($parts[$i + 1]) ? $parts[$i + 1] : '';
             $property = $this->get_property($parts[$i], $child);
             if (isset($parts[$i + 1])) {
                 $i++;
             }
         }
         return $property;
     }
     if (empty($boxes)) {
         return;
     }
     foreach ($boxes as $box) {
         $properties = isset($box[1][0]->properties) ? $box[1][0]->properties : $box[1];
         foreach ($properties as $property) {
             $property = papi_get_property_type($property);
             if (papi_is_property($property) && $property->match_slug($slug)) {
                 if (empty($child_slug)) {
                     return $property;
                 }
                 $result = $this->get_child_property($property->get_child_properties(), $child_slug);
                 if (is_object($result)) {
                     return papi_get_property_type($result);
                 }
             }
         }
     }
 }
 /**
  * Render property JSON template.
  *
  * @param string $slug
  */
 protected function render_json_template($slug)
 {
     $options = $this->get_options();
     $options->settings->items = papi_to_array($options->settings->items);
     foreach ($options->settings->items as $key => $value) {
         if (!papi_is_property($value)) {
             unset($options->settings->items[$key]);
             continue;
         }
         $options->settings->items[$key] = clone $value->get_options();
     }
     papi_render_html_tag('script', ['data-papi-json' => sprintf('%s_repeater_json', $slug), 'type' => 'application/json', json_encode([$options])]);
 }
Example #13
0
/**
 * Update field with new value. The old value will be deleted.
 *
 * @param  int    $id
 * @param  string $slug
 * @param  mixed  $value
 * @param  string $type
 *
 * @return bool
 */
function papi_update_field($id = null, $slug = null, $value = null, $type = 'post')
{
    if (!is_numeric($id) && is_string($id)) {
        $type = empty($value) ? $value : $type;
        $value = $slug;
        $slug = $id;
        $id = null;
    }
    if (!is_string($slug) || empty($slug)) {
        return false;
    }
    if (papi_is_empty($value)) {
        return papi_delete_field($id, $slug, $type);
    }
    $id = papi_get_meta_id($type, $id);
    $store = papi_get_meta_store($id, $type);
    if (is_null($store)) {
        return false;
    }
    $property = $store->get_property($slug);
    if (!papi_is_property($property)) {
        return false;
    }
    papi_delete_field($id, $slug, $type);
    $value = $property->update_value($value, $slug, $id);
    $value = papi_filter_update_value($property->get_option('type'), $value, $slug, $id, $type);
    return papi_update_property_meta_value(['type' => $type, 'id' => $id, 'slug' => $slug, 'value' => $value]);
}
 /**
  * Get property value.
  *
  * @param  Papi_Core_Conditional_Rule $rule
  *
  * @return mixed
  */
 private function get_value(Papi_Core_Conditional_Rule $rule)
 {
     if (papi_doing_ajax()) {
         $source = $rule->get_source();
         $meta_id = papi_get_meta_id();
         $entry_type = papi_get_entry_type_by_meta_id($meta_id);
         if (!papi_is_empty($source) && $entry_type instanceof Papi_Entry_Type !== false) {
             if (papi_is_property($entry_type->get_property($rule->slug))) {
                 return $this->get_deep_value($rule->slug, $source);
             }
         }
     }
     if (!papi_is_empty($rule->get_source())) {
         return $this->get_deep_value($rule->slug, $rule->get_source());
     }
     $slug = $rule->get_field_slug();
     $type = papi_get_meta_type();
     $value = papi_get_field($slug, null, $type);
     return $this->get_deep_value($slug, $value);
 }
 /**
  * Setup options settings.
  *
  * @param  stdClass $options
  *
  * @return stdClass
  */
 private function setup_options_settings($options)
 {
     $property_class = self::factory($options->type);
     if (papi_is_property($property_class)) {
         $options->settings = array_merge((array) $property_class->get_default_settings(), (array) $options->settings);
     }
     return (object) $options->settings;
 }
Example #16
0
 /**
  * Import data to Papi.
  *
  * @param  mixed $options
  * @param  array $fields
  *
  * @return bool
  */
 public function import($options, array $fields = [])
 {
     $options = $this->get_import_options($options);
     $meta_id = empty($options['meta_id']) ? $options['post_id'] : $options['meta_id'];
     $meta_type = $options['meta_type'];
     $entry_type = $options['page_type'];
     if (isset($options['update_arrays'])) {
         $this->driver->set_options(['update_array' => $options['update_arrays']]);
     }
     if (empty($meta_id) || empty($fields)) {
         return false;
     }
     if (empty($entry_type)) {
         $entry_type = papi_get_entry_type_by_meta_id($meta_id, $meta_type);
     }
     if (is_string($entry_type)) {
         $entry_type = papi_get_entry_type_by_id($entry_type);
     }
     if ($entry_type instanceof Papi_Entry_Type === false) {
         return false;
     }
     update_metadata($meta_type, $meta_id, papi_get_page_type_key(), $entry_type->get_id());
     $result = true;
     foreach ($fields as $slug => $value) {
         if (!is_string($slug) || papi_is_empty($value)) {
             continue;
         }
         $property = $entry_type->get_property($slug);
         if (!papi_is_property($property)) {
             $result = false;
             continue;
         }
         $value = $this->fire_filter(['filter' => 'driver:value', 'type' => 'before', 'value' => [$value, $slug]]);
         $value = $this->get_value(['post_id' => $meta_id, 'property' => $property, 'slug' => $slug, 'value' => $value]);
         $value = $this->fire_filter(['filter' => 'driver:value', 'type' => 'after', 'value' => [$value, $slug]]);
         $out = papi_update_property_meta_value(['id' => $meta_id, 'slug' => $slug, 'type' => $meta_type, 'value' => $value]);
         $result = $out ? $result : $out;
     }
     return $result;
 }
Example #17
0
/**
 * Update field with new value. The old value will be deleted.
 *
 * @param  int    $post_id
 * @param  string $slug
 * @param  mixed  $value
 * @param  string $type
 *
 * @return bool
 */
function papi_update_field($post_id = null, $slug = null, $value = null, $type = 'page')
{
    if (!is_numeric($post_id) && is_string($post_id)) {
        $value = $slug;
        $slug = $post_id;
        $post_id = null;
    }
    if (!is_string($slug) || empty($slug)) {
        return false;
    }
    if (papi_is_empty($value)) {
        return papi_delete_field($post_id, $slug, $type);
    }
    $post_id = papi_get_post_id($post_id);
    if ($post_id === 0 && $type === Papi_Post_Page::TYPE) {
        return false;
    }
    $page = papi_get_page($post_id, $type);
    if (is_null($page)) {
        return false;
    }
    $property = $page->get_property($slug);
    if (!papi_is_property($property)) {
        return false;
    }
    papi_delete_field($post_id, $slug, $type);
    $value = $property->update_value($value, $slug, $post_id);
    $value = papi_filter_update_value($property->get_option('type'), $value, $slug, $post_id);
    return papi_update_property_meta_value(['type' => $type, 'post_id' => $post_id, 'slug' => $slug, 'value' => $value]);
}
 /**
  * Get property value.
  *
  * @param  Papi_Core_Conditional_Rule $rule
  *
  * @return mixed
  */
 private function get_value(Papi_Core_Conditional_Rule $rule)
 {
     if (papi_doing_ajax()) {
         $source = $rule->get_source();
         $post_id = papi_get_post_id();
         $page_type = papi_get_page_type_by_post_id($post_id);
         if (!papi_is_empty($source) && $page_type instanceof Papi_Page_Type !== false) {
             if (papi_is_property($page_type->get_property($rule->slug))) {
                 return $this->get_deep_value($rule->slug, $source);
             }
         }
     }
     if (!papi_is_empty($rule->get_source())) {
         return $this->get_deep_value($rule->slug, $rule->get_source());
     }
     $slug = $rule->get_field_slug();
     if (papi_is_option_page()) {
         $value = papi_get_option($slug);
     } else {
         $value = papi_get_field($slug);
     }
     return $this->get_deep_value($slug, $value);
 }