/** * The constructor. * * @param Papi_Core_Box $box */ public function __construct(Papi_Core_Box $box) { // Check if the current user is allowed to view this box. if (!papi_current_user_is_allowed($box->capabilities)) { return; } $this->box = $box; $this->setup_actions(); }
/** * The constructor. * * @param array $options * @param array $properties */ public function __construct($options = [], $properties = []) { if (empty($options)) { return; } $this->setup_options($options); // Check if the current user is allowed to view this box. if (!papi_current_user_is_allowed($this->options->capabilities)) { return; } $this->populate_properties($properties); $this->setup_actions(); }
/** * Setup tabs. * * @param array $tabs * * @return array */ function papi_tabs_setup(array $tabs) { $_tabs = []; foreach ($tabs as $tab) { if ($tab instanceof Papi_Core_Tab === false) { continue; } if (papi_current_user_is_allowed($tab->capabilities)) { $_tabs[] = $tab; } } $tabs = papi_sort_order($_tabs); // Generate unique names for all tabs. $len = count($tabs); for ($i = 0; $i < $len; $i++) { $tabs[$i]->id = papi_html_name($tabs[$i]->title) . '_' . $i; } return $tabs; }
/** * Setup tabs. * * @param array $tabs * * @return array */ function papi_setup_tabs(array $tabs) { $_tabs = []; foreach ($tabs as $tab) { $tab = (object) $tab; if (!isset($tab->options)) { continue; } $tab->options = papi_get_tab_options($tab->options); if (papi_current_user_is_allowed($tab->options->capabilities)) { $_tabs[] = $tab; } } $tabs = papi_sort_order($_tabs); // Generate unique names for all tabs. $len = count($tabs); for ($i = 0; $i < $len; $i++) { $tabs[$i]->options->_name = papi_html_name($tabs[$i]->options->title) . '_' . $i; } return $tabs; }
/** * Render the property. */ public function render() { // Check so the property has a type and capabilities on the property. if (!papi_current_user_is_allowed($this->get_option('capabilities'))) { return; } // A disabled property should not be rendered. if ($this->disabled()) { return; } // Only render if it's the right language if the definition exist. if ($this->get_option('lang') === strtolower(papi_get_qs('lang'))) { $render = true; } else { $render = $this->get_option('lang') === false && papi_is_empty(papi_get_qs('lang')); } if ($this->display()) { $this->display = $this->render_is_allowed_by_rules(); } $this->render_row_html(); $this->render_hidden_html(); $this->render_rules_json(); }
/** * Determine if the current user has capabilities rights. * * @return bool */ public function current_user_can() { return papi_current_user_is_allowed($this->capabilities); }
/** * Determine if the page type is allowed * by capabilities and post type. * * @return bool */ public function allowed() { $args = func_get_args(); return empty($args) ? parent::allowed() : papi_current_user_is_allowed($this->capabilities) && isset($args[0]) && in_array($args[0], $this->post_type, true); }
/** * Get all page types that exists. * * @param bool $all * @param string $post_type * @param bool $fake_post_types * * @return array */ function papi_get_all_page_types($all = false, $post_type = null, $fake_post_types = false) { if (empty($post_type)) { $post_type = papi_get_post_type(); } $cache_key = papi_cache_key(sprintf('%s_%s', $all, $post_type), $fake_post_types); $page_types = wp_cache_get($cache_key); $load_once = papi_filter_core_load_one_type_on(); if (empty($page_types)) { $files = papi_get_all_page_type_files(); foreach ($files as $file) { $page_type = papi_get_page_type($file); if (is_null($page_type)) { continue; } if ($page_type instanceof Papi_Page_Type === false) { continue; } if (papi()->exists('core.page_type.' . $page_type->post_type[0])) { if (!empty($page_types)) { continue; } } else { if (in_array($page_type->post_type[0], $load_once)) { papi()->singleton('core.page_type.' . $page_type->post_type[0], $page_type->get_id()); } } if ($fake_post_types) { if (isset($page_type->post_type[0]) && !post_type_exists($page_type->post_type[0])) { // Boot page type. $page_type->boot(); // Add it to the page types array. $page_types[] = $page_type; } continue; } else { if ($page_type instanceof Papi_Option_Type) { continue; } } // Add the page type if the post types is allowed. if (!is_null($page_type) && papi_current_user_is_allowed($page_type->capabilities) && ($all || in_array($post_type, $page_type->post_type))) { // Boot page type. $page_type->boot(); // Add it to the page types array. $page_types[] = $page_type; } } if (is_array($page_types)) { usort($page_types, function ($a, $b) { return strcmp($a->name, $b->name); }); wp_cache_set($cache_key, $page_types); } } if (!is_array($page_types)) { return []; } return papi_sort_order(array_reverse($page_types)); }
/** * Switch page type if all checks pass. * * @param int $post_id * @param WP_post $post */ public function save_post($post_id, $post) { // Check if post id and post object is empty or not. if (empty($post_id) || empty($post)) { return false; } // Check if our nonce is vailed. if (!wp_verify_nonce(papi_get_sanitized_post('papi_meta_nonce'), 'papi_save_data')) { return false; } // Check if so both page type keys exists. if (empty($_POST[papi_get_page_type_key()]) || empty($_POST[papi_get_page_type_key('switch')])) { return false; } // Page type information. $page_type_id = sanitize_text_field($_POST[papi_get_page_type_key()]); $page_type_switch_id = sanitize_text_field($_POST[papi_get_page_type_key('switch')]); // Don't update if the same ids. if ($page_type_id === $page_type_switch_id) { return false; } $page_type = papi_get_entry_type_by_id($page_type_id); $page_type_switch = papi_get_entry_type_by_id($page_type_switch_id); $post_type_object = get_post_type_object(papi_get_post_type()); // Check if page type and post type is not empty. if (empty($page_type_switch) || empty($post_type_object)) { return false; } // Check if autosave. if (wp_is_post_autosave($post_id)) { return false; } // Check if revision. if (wp_is_post_revision($post_id)) { return false; } // Check if revision post type. if (in_array($post->post_type, ['revision', 'nav_menu_item'], true)) { return false; } // Check so page type has the post type. if (!$page_type->has_post_type($post->post_type) || !$page_type_switch->has_post_type($post->post_type)) { return false; } // Check page type capabilities. if (!papi_current_user_is_allowed($page_type_switch->capabilities)) { return false; } // Check so user can edit posts and that the user can publish posts on the post type. if (!current_user_can('edit_post', $post_id) || !current_user_can($post_type_object->cap->publish_posts)) { return false; } // Get properties. $properties = $page_type->get_properties(); $properties_switch = $page_type_switch->get_properties(); // Delete only properties that don't have the same type and slug. foreach ($properties as $property) { $delete = true; // Check if the properties are the same or not. foreach ($properties_switch as $property_switch) { if ($property_switch->type === $property->type && $property_switch->match_slug($property->get_slug())) { $delete = false; break; } } if (!$delete) { continue; } // Delete property values. $property->delete_value($property->get_slug(true), $post_id, papi_get_meta_type()); } // Update page type id. return papi_set_page_type_id($post_id, $page_type_switch_id); }