/** * Add fields, based on options to REST read/write requests * * @since 2.5.6 * * @access protected */ protected function add_fields() { $fields = $this->pod->fields(); foreach ($fields as $field_name => $field) { $read = self::field_allowed_to_extend($field_name, $this->pod, 'read'); $write = self::field_allowed_to_extend($field_name, $this->pod, 'write'); $this->register($field_name, $read, $write); } }
/** * Get Pod object * * @since 2.5.6 * * @param $pod_name * @param $id * * @return bool|Pods */ protected static function get_pod($pod_name, $id) { if (!self::$pod || self::$pod->pod != $pod_name) { self::$pod = pods($pod_name, $id, true); } if (self::$pod && self::$pod->id != $id) { self::$pod->fetch($id); } return self::$pod; }
/** * Include and Init the Pods class * * @see Pods * * @param string $type The pod name * @param mixed $id (optional) The ID or slug, to load a single record; Provide array of $params to run 'find' * @param bool $strict (optional) If set to true, return false instead of an object if the Pod doesn't exist * * @return bool|\Pods * @since 2.0 * @link http://pods.io/docs/pods/ */ function pods($type = null, $id = null, $strict = null) { require_once PODS_DIR . 'classes/Pods.php'; $pod = new Pods($type, $id); if (null === $strict) { $strict = pods_strict(); } if (true === $strict && null !== $type && !$pod->valid()) { return false; } return $pod; }
/** * @param $object_type * @param null $_null * @param int $object_id * @param string $meta_key * @param string $meta_value * @param bool $delete_all * * @return null */ public function delete_meta($object_type, $_null = null, $object_id = 0, $meta_key = '', $meta_value = '', $delete_all = false) { if (pods_tableless()) { return $_null; } $object = $this->get_object($object_type, $object_id); if (empty($object_id) || empty($object) || !isset($object['fields'][$meta_key])) { return $_null; } // @todo handle $delete_all (delete the field values from all pod items) if (!empty($meta_value) && in_array($object['fields'][$meta_key]['type'], PodsForm::tableless_field_types())) { if (!is_object(self::$current_field_pod) || self::$current_field_pod->pod != $object['name']) { self::$current_field_pod = pods($object['name'], $object_id); } elseif (self::$current_field_pod->id() != $object_id) { self::$current_field_pod->fetch($object_id); } $pod = self::$current_field_pod; $pod->remove_from($meta_key, $meta_value); } else { if (!is_object(self::$current_field_pod) || self::$current_field_pod->pod != $object['name']) { self::$current_field_pod = pods($object['name']); } $pod = self::$current_field_pod; $pod->save(array($meta_key => null), null, $object_id); } return $_null; }
/** * Get an offset if already set or traverse Pod and then set plus reutrn. * * @since 0.1.0 * * @param mixed $offset Offset to get * * @return mixed */ public function offsetGet($offset) { if ($this->offsetExists($offset)) { return parent::offsetGet($offset); } else { if ('id' == $offset || 'ID' == $offset) { $_value = $this->pod->id(); } else { $_value = $this->pod->field($offset); } if ($_value) { parent::offsetSet($offset, $_value); return $_value; } } }
/** * Export a pod item by depth level * * @param Pods $pod Pods object * @param array $fields Fields to export * @param int $depth Depth limit * @param boolean $flatten Whether to flatten arrays for display * @param int $current_depth Current depth level * * @return array Data array * * @since 2.3 */ private function export_pod_item_level($pod, $fields, $depth, $flatten = false, $current_depth = 1) { $tableless_field_types = PodsForm::tableless_field_types(); $simple_tableless_objects = PodsForm::simple_tableless_objects(); $object_fields = (array) pods_var_raw('object_fields', $pod->pod_data, array(), null, true); $export_fields = array(); foreach ($fields as $k => $field) { if (!is_array($field)) { $field = array('id' => 0, 'name' => $field); } if (isset($pod->fields[$field['name']])) { $field = $pod->fields[$field['name']]; $field['lookup_name'] = $field['name']; if (in_array($field['type'], $tableless_field_types) && !in_array(pods_var('pick_object', $field), $simple_tableless_objects)) { if ('pick' == $field['type']) { if (empty($field['table_info'])) { $field['table_info'] = $this->get_table_info(pods_var_raw('pick_object', $field), pods_var_raw('pick_val', $field), null, null, $field); } if (!empty($field['table_info'])) { $field['lookup_name'] .= '.' . $field['table_info']['field_id']; } } elseif (in_array($field['type'], PodsForm::file_field_types())) { $field['lookup_name'] .= '.guid'; } } $export_fields[$field['name']] = $field; } elseif (isset($object_fields[$field['name']])) { $field = $object_fields[$field['name']]; $field['lookup_name'] = $field['name']; $export_fields[$field['name']] = $field; } elseif ($field['name'] == $pod->pod_data['field_id']) { $field['type'] = 'number'; $field['lookup_name'] = $field['name']; $export_fields[$field['name']] = $field; } } $data = array(); foreach ($export_fields as $field) { // Return IDs (or guid for files) if only one level deep if (1 == $depth) { $data[$field['name']] = $pod->field(array('name' => $field['lookup_name'], 'output' => 'arrays')); } elseif ((-1 == $depth || $current_depth < $depth) && 'pick' == $field['type'] && !in_array(pods_var('pick_object', $field), $simple_tableless_objects)) { $related_data = array(); $related_ids = $pod->field(array('name' => $field['name'], 'output' => 'ids')); if (!empty($related_ids)) { $related_ids = (array) $related_ids; $pick_object = pods_var_raw('pick_object', $field); $related_pod = pods(pods_var_raw('pick_val', $field), null, false); // If this isn't a Pod, return data exactly as Pods does normally if (empty($related_pod) || 'pod' != $pick_object && $pick_object != $related_pod->pod_data['type'] || $related_pod->pod == $pod->pod) { $related_data = $pod->field(array('name' => $field['name'], 'output' => 'arrays')); } else { $related_object_fields = (array) pods_var_raw('object_fields', $related_pod->pod_data, array(), null, true); $related_fields = array_merge($related_pod->fields, $related_object_fields); foreach ($related_ids as $related_id) { if ($related_pod->fetch($related_id)) { $related_item = $this->export_pod_item_level($related_pod, $related_fields, $depth, $flatten, $current_depth + 1); $related_data[$related_id] = $this->do_hook('export_pod_item_level', $related_item, $related_pod->pod, $related_pod->id(), $related_pod, $related_fields, $depth, $flatten, $current_depth + 1); } } if ($flatten && !empty($related_data)) { $related_data = pods_serial_comma(array_values($related_data), array('and' => '', 'field_index' => $related_pod->pod_data['field_index'])); } } } $data[$field['name']] = $related_data; } else { $data[$field['name']] = $pod->field(array('name' => $field['name'], 'output' => 'arrays')); } if ($flatten && is_array($data[$field['name']])) { $data[$field['name']] = pods_serial_comma($data[$field['name']], array('field' => $field['name'], 'fields' => $export_fields, 'and' => '')); } } $data['id'] = (int) $pod->id(); return $data; }
public function restricted($action = 'edit', $row = null) { $restricted = false; $restrict = array(); if (isset($this->restrict[$action])) { $restrict = (array) $this->restrict[$action]; } // @todo Build 'edit', 'duplicate', 'delete' action support for 'where' which runs another find() query /*if ( !in_array( $action, array( 'manage', 'reorder' ) ) ) { $where = pods_var_raw( $action, $this->where, null, null, true ); if ( !empty( $where ) ) { $restricted = true; $old_where = $this->where[ $action ]; $id = $this->row[ $this->sql[ 'field_id' ] ]; if ( is_array( $where ) ) { if ( 'OR' == pods_var( 'relation', $where ) ) $where = array( $where ); $where[] = "`t`.`" . $this->sql[ 'field_id' ] . "` = " . (int) $id; } else $where = "( {$where} ) AND `t`.`" . $this->sql[ 'field_id' ] . "` = " . (int) $id; $this->where[ $action ] = $where; $data = false; //$data = $this->get_data(); $this->where[ $action ] = $old_where; if ( empty( $data ) ) $restricted = true; } }*/ $author_restrict = false; if (!empty($this->restrict['author_restrict']) && $restrict === $this->restrict['author_restrict']) { $restricted = false; $author_restrict = true; if (is_object($this->pod)) { $restricted = true; if ('settings' == $this->pod->pod_data['type'] && 'add' == $action) { $action = 'edit'; } if (pods_is_admin(array('pods', 'pods_content'))) { $restricted = false; } elseif ('manage' == $action) { if (!in_array('edit', $this->actions_disabled) && current_user_can('pods_edit_' . $this->pod->pod) && current_user_can('pods_edit_others_' . $this->pod->pod)) { $restricted = false; } elseif (!in_array('delete', $this->actions_disabled) && current_user_can('pods_delete_' . $this->pod->pod) && current_user_can('pods_delete_others_' . $this->pod->pod)) { $restricted = false; } elseif (current_user_can('pods_' . $action . '_' . $this->pod->pod) && current_user_can('pods_' . $action . '_others_' . $this->pod->pod)) { $restricted = false; } } elseif (current_user_can('pods_' . $action . '_' . $this->pod->pod) && current_user_can('pods_' . $action . '_others_' . $this->pod->pod)) { $restricted = false; } } /* @todo determine proper logic for non-pods capabilities else { $restricted = true; if ( pods_is_admin( array( 'pods', 'pods_content' ) ) ) $restricted = false; elseif ( current_user_can( 'pods_' . $action . '_others_' . $_tbd ) ) $restricted = false; }*/ } if ($restricted && !empty($restrict)) { $relation = strtoupper(trim(pods_var('relation', $restrict, 'AND', null, true))); if ('AND' != $relation) { $relation = 'OR'; } $okay = true; foreach ($restrict as $field => $match) { if ('relation' == $field) { continue; } if (is_array($match)) { $match_okay = true; $match_relation = strtoupper(trim(pods_var('relation', $match, 'OR', null, true))); if ('AND' != $match_relation) { $match_relation = 'OR'; } foreach ($match as $the_field => $the_match) { if ('relation' == $the_field) { continue; } $value = null; if (is_object($this->pod)) { $value = $this->pod->field($the_match, true); } else { if (empty($row)) { $row = $this->row; } if (isset($row[$the_match])) { if (is_array($row[$the_match])) { if (false !== strpos($the_match, '.')) { $the_matches = explode('.', $the_match); $value = $row[$the_match]; foreach ($the_matches as $m) { if (is_array($value) && isset($value[$m])) { $value = $value[$m]; } else { $value = null; break; } } } } else { $value = $row[$the_match]; } } } if (is_array($value)) { if (!in_array($the_match, $value)) { $match_okay = false; } elseif ('OR' == $match_relation) { $match_okay = true; break; } } elseif ($value == $the_match) { $match_okay = false; } elseif ('OR' == $match_relation) { $match_okay = true; break; } } if (!$match_okay) { $okay = false; } if ('OR' == $relation) { $okay = true; break; } } else { $value = null; if (is_object($this->pod)) { $value = $this->pod->field($match, true); } else { if (empty($row)) { $row = $this->row; } if (isset($row[$match])) { if (is_array($row[$match])) { if (false !== strpos($match, '.')) { $matches = explode('.', $match); $value = $row[$match]; foreach ($matches as $m) { if (is_array($value) && isset($value[$m])) { $value = $value[$m]; } else { $value = null; break; } } } } else { $value = $row[$match]; } } } if (is_array($value)) { if (!in_array($match, $value)) { $okay = false; } elseif ('OR' == $relation) { $okay = true; break; } } elseif ($value != $match) { $okay = false; } elseif ('OR' == $relation) { $okay = true; break; } } } if (!empty($author_restrict)) { if (is_object($this->pod) && 'manage' == $action) { if (!in_array('edit', $this->actions_disabled) && !current_user_can('pods_edit_' . $this->pod->pod) && !in_array('delete', $this->actions_disabled) && !current_user_can('pods_delete_' . $this->pod->pod)) { $okay = false; } } if (is_object($this->pod) && !current_user_can('pods_' . $action . '_' . $this->pod->pod)) { $okay = false; } /* @todo determine proper logic for non-pods capabilities elseif ( !current_user_can( 'pods_' . $action . '_' . $_tbd ) ) $okay = false;*/ if (!$okay && !empty($row)) { foreach ($this->restrict['author_restrict'] as $key => $val) { $author_restricted = $this->get_field($key); if (!empty($author_restricted)) { if (!is_array($author_restricted)) { $author_restricted = (array) $author_restricted; } if (is_array($val)) { foreach ($val as $v) { if (in_array($v, $author_restricted)) { $okay = true; } } } elseif (in_array($val, $author_restricted)) { $okay = true; } } } } } if ($okay) { $restricted = false; } } if (isset($this->actions_custom[$action]) && is_array($this->actions_custom[$action]) && isset($this->actions_custom[$action]['restrict_callback']) && is_callable($this->actions_custom[$action]['restrict_callback'])) { $restricted = call_user_func($this->actions_custom[$action]['restrict_callback'], $restricted, $restrict, $action, $row, $this); } $restricted = $this->do_hook('restricted_' . $action, $restricted, $restrict, $action, $row); return $restricted; }
/** * Save */ protected function save() { $this->ID = $this->pod->save($this->data); }
/** * Attach Pods Template to $content * * @param string $template_name The name of a Pods Template to load. * @param string $content Post content * @param Pods $pods Current Pods object. * @param bool|string $append Optional. Whether to append, prepend or replace content. Defaults to true, which appends, if false, content is replaced, if 'prepend' content is prepended. * * @return string $content with Pods Template appended if template exists * * @since 2.4.5 */ function load_template($template_name, $content, $pods, $append = true) { //prevent infinite loops caused by this method acting on post_content remove_filter('the_content', array($this, 'front')); $template = $pods->template($template_name); add_filter('the_content', array($this, 'front')); //check if we have a valid template if (!is_null($template)) { //if so append it to content or replace content. if ($append === 'replace') { $content = $template; } elseif ($append === 'prepend') { $content = $template . $content; } elseif ($append || $append === 'append') { $content = $content . $template; } else { $content = $template; } } return $content; }
public function restricted($action = 'edit', $row = null) { $restricted = false; $restrict = array(); if (isset($this->restrict[$action])) { $restrict = (array) $this->restrict[$action]; } $author_restrict = false; if (!empty($this->restrict['author_restrict']) && $restrict == $this->restrict['author_restrict']) { $author_restrict = true; if (is_object($this->pod)) { $restricted = true; if (pods_is_admin(array('pods', 'pods_content'))) { $restricted = false; } elseif ('manage' == $action) { if (!in_array('edit', $this->actions_disabled) && current_user_can('pods_edit_' . $this->pod->pod) && current_user_can('pods_edit_others_' . $this->pod->pod)) { $restricted = false; } elseif (!in_array('delete', $this->actions_disabled) && current_user_can('pods_delete_' . $this->pod->pod) && current_user_can('pods_delete_others_' . $this->pod->pod)) { $restricted = false; } elseif (current_user_can('pods_' . $action . '_' . $this->pod->pod) && current_user_can('pods_' . $action . '_others_' . $this->pod->pod)) { $restricted = false; } } elseif (current_user_can('pods_' . $action . '_' . $this->pod->pod) && current_user_can('pods_' . $action . '_others_' . $this->pod->pod)) { $restricted = false; } } /* @todo determine proper logic for non-pods capabilities else { $restricted = true; if ( pods_is_admin( array( 'pods', 'pods_content' ) ) ) $restricted = false; elseif ( current_user_can( 'pods_' . $action . '_others_' . $_tbd ) ) $restricted = false; }*/ } if ($restricted && !empty($restrict)) { $relation = strtoupper(trim(pods_var('relation', $restrict, 'AND', null, true))); if ('AND' != $relation) { $relation = 'OR'; } $okay = true; foreach ($restrict as $field => $match) { if ('relation' == $field) { continue; } if (is_array($match)) { $match_okay = true; $match_relation = strtoupper(trim(pods_var('relation', $match, 'OR', null, true))); if ('AND' != $match_relation) { $match_relation = 'OR'; } foreach ($match as $the_field => $the_match) { if ('relation' == $the_field) { continue; } $value = null; if (is_object($this->pod)) { $value = $this->pod->field($the_match, true); } else { if (empty($row)) { $row = $this->row; } if (isset($row[$the_match])) { if (is_array($row[$the_match])) { if (false !== strpos($the_match, '.')) { $the_matches = explode('.', $the_match); $value = $row[$the_match]; foreach ($the_matches as $m) { if (is_array($value) && isset($value[$m])) { $value = $value[$m]; } else { $value = null; break; } } } } else { $value = $row[$the_match]; } } } if (is_array($value)) { if (!in_array($the_match, $value)) { $match_okay = false; } elseif ('OR' == $match_relation) { $match_okay = true; break; } } elseif ($value == $the_match) { $match_okay = false; } elseif ('OR' == $match_relation) { $match_okay = true; break; } } if (!$match_okay) { $okay = false; } if ('OR' == $relation) { $okay = true; break; } } else { $value = null; if (is_object($this->pod)) { $value = $this->pod->field($match, true); } else { if (empty($row)) { $row = $this->row; } if (isset($row[$match])) { if (is_array($row[$match])) { if (false !== strpos($match, '.')) { $matches = explode('.', $match); $value = $row[$match]; foreach ($matches as $m) { if (is_array($value) && isset($value[$m])) { $value = $value[$m]; } else { $value = null; break; } } } } else { $value = $row[$match]; } } } if (is_array($value)) { if (!in_array($match, $value)) { $okay = false; } elseif ('OR' == $relation) { $okay = true; break; } } elseif ($value != $match) { $okay = false; } elseif ('OR' == $relation) { $okay = true; break; } } } if (!empty($author_restrict)) { if (is_object($this->pod) && 'manage' == $action) { if (!in_array('edit', $this->actions_disabled) && !current_user_can('pods_edit_' . $this->pod->pod) && !in_array('delete', $this->actions_disabled) && !current_user_can('pods_delete_' . $this->pod->pod)) { $okay = false; } } if (is_object($this->pod) && !current_user_can('pods_' . $action . '_' . $this->pod->pod)) { $okay = false; } /* @todo determine proper logic for non-pods capabilities elseif ( !current_user_can( 'pods_' . $action . '_' . $_tbd ) ) $okay = false;*/ if (!$okay && !empty($row)) { foreach ($this->restrict['author_restrict'] as $key => $val) { $author_restricted = $this->get_field($key); if (!empty($author_restricted)) { if (!is_array($author_restricted)) { $author_restricted = (array) $author_restricted; } if (is_array($val)) { foreach ($val as $v) { if (in_array($v, $author_restricted)) { $okay = true; } } } elseif (in_array($val, $author_restricted)) { $okay = true; } } } } } if ($okay) { $restricted = false; } } $restricted = $this->do_hook('restricted_' . $action, $restricted, $restrict, $action, $row); return $restricted; }