Example #1
0
/**
 * Load the entry type id on a taxonomy.
 *
 * @param  string $entry_type_id
 * @param  string $type
 *
 * @return string
 */
function papi_load_taxonomy_type_id($entry_type_id = '', $type = 'term')
{
    if ($type !== 'term') {
        return $entry_type_id;
    }
    $key = papi_get_page_type_key();
    $term_id = papi_get_term_id();
    $taxonomy = papi_get_taxonomy($term_id);
    // Try to load the entry type id from only taxonomy type filter.
    if (empty($entry_type_id)) {
        $entry_type_id = papi_filter_settings_only_taxonomy_type($taxonomy);
    }
    // If we have a term id we can load the entry type id from the term.
    if (empty($entry_type_id) && $term_id > 0 && papi_supports_term_meta()) {
        $meta_value = get_term_meta($term_id, $key, true);
        $entry_type_id = empty($meta_value) ? '' : $meta_value;
    }
    // Load entry type id from the container if it exists.
    if (empty($entry_type_id)) {
        $key = sprintf('entry_type_id.taxonomy.%s', $taxonomy);
        if (papi()->exists($key)) {
            return papi()->make($key);
        }
    }
    return $entry_type_id;
}
Example #2
0
/**
 * Load the entry type id on a taxonomy.
 *
 * @param  string $entry_type_id
 * @param  string $type
 *
 * @return string
 */
function papi_load_taxonomy_type_id($entry_type_id = '', $type = 'term')
{
    if ($type !== 'term') {
        return $entry_type_id;
    }
    $key = papi_get_page_type_key();
    $term_id = papi_get_term_id();
    $taxonomy = papi_get_taxonomy($term_id);
    // Try to load the entry type id from only taxonomy type filter.
    if (empty($entry_type_id)) {
        $entry_type_id = papi_filter_settings_only_taxonomy_type($taxonomy);
    }
    // If we have a term id we can load the entry type id from the term.
    if (empty($entry_type_id) && $term_id > 0 && papi_supports_term_meta()) {
        $meta_value = get_term_meta($term_id, $key, true);
        $entry_type_id = empty($meta_value) ? '' : $meta_value;
    }
    // Try to load the entry type from all taxonomy types and check
    // if only one exists of that post type.
    //
    // The same as only taxonomy type filter but without the filter.
    if (empty($entry_type_id)) {
        $key = sprintf('entry_type_id.taxonomy.%s', $taxonomy);
        if (papi()->exists($key)) {
            return papi()->make($key);
        }
        $entries = papi_get_all_entry_types(['args' => $taxonomy, 'mode' => 'include', 'types' => ['taxonomy']]);
        if (is_array($entries)) {
            usort($entries, function ($a, $b) {
                return strcmp($a->name, $b->name);
            });
        }
        $entries = papi_sort_order(array_reverse($entries));
        if (count($entries) === 1) {
            $entry_type_id = $entries[0]->get_id();
            papi()->bind($key, $entry_type_id);
        }
    }
    return $entry_type_id;
}
Example #3
0
            ?>
					</tr>
				<?php 
        }
        ?>
				</tr>
			</tbody>
		</table>
		<?php 
    }
}
?>
<div class="wrap">
	<div class="papi-options-logo"></div>
	<h1><?php 
echo papi()->name;
?>
</h1>

	<br/>

	<?php 
$page_type = papi_get_qs('page_type');
$page_type = papi_get_page_type_by_id($page_type);
if (empty($page_type)) {
    ?>
		<h3><?php 
    _e('Cannot find the page type', 'papi');
    ?>
</h3>
		<p>
 /**
  * Create a new instance of the given type or a
  * empty core property if no type is given.
  *
  * @return null|object
  */
 public static function factory()
 {
     if (count(func_get_args()) === 0) {
         return new static();
     } else {
         $type = func_get_arg(0);
     }
     if (is_array($type)) {
         $type = (object) $type;
     }
     if (!is_string($type) && !is_object($type)) {
         return;
     }
     if (is_object($type) && !isset($type->type)) {
         $property = new static();
         $property->set_options($type);
         return $property;
     }
     if (is_subclass_of($type, __CLASS__)) {
         return $type;
     }
     $options = null;
     if (is_object($type)) {
         if (!isset($type->type) || !is_string($type->type)) {
             return;
         }
         $options = $type;
         $type = $type->type;
     }
     // Old types, 'PropertyString' => 'String'.
     $type = preg_replace('/^Property/', '', $type);
     if (empty($type)) {
         return;
     }
     $class_name = papi_get_property_class_name($type);
     if (!class_exists($class_name) || !is_subclass_of($class_name, __CLASS__)) {
         return;
     }
     if (!papi()->exists($class_name)) {
         papi()->bind($class_name, new $class_name());
     }
     $class = papi()->make($class_name);
     // @codeCoverageIgnoreStart
     if (!is_object($class) || $class instanceof Papi_Core_Property === false) {
         $class = new $class_name();
         papi()->bind($class_name, $class);
     }
     // @codeCoverageIgnoreEnd
     $property = clone $class;
     if (is_object($options)) {
         $property->set_options((array) $options);
     }
     return $property;
 }
 /**
  * Create a new instance of the given type.
  *
  * @param  mixed $type
  *
  * @return object
  */
 public static function factory($type)
 {
     if (is_array($type)) {
         $prop = self::create($type);
         $type = $prop->get_options();
     }
     if (!is_string($type) && !is_object($type)) {
         return;
     }
     if (is_subclass_of($type, __CLASS__)) {
         return $type;
     }
     $options = null;
     if (is_object($type)) {
         if (!isset($type->type) || !is_string($type->type)) {
             return;
         }
         $options = $type;
         $type = $type->type;
     }
     $type = preg_replace('/^Property/', '', $type);
     if (empty($type)) {
         return;
     }
     $class_name = papi_get_property_class_name($type);
     if (!class_exists($class_name) || !is_subclass_of($class_name, __CLASS__)) {
         return;
     }
     if (!papi()->exists($class_name)) {
         papi()->bind($class_name, new $class_name());
     }
     $class = papi()->make($class_name);
     if (!is_object($class) || $class instanceof Papi_Core_Property === false) {
         $class = new $class_name();
         papi()->bind($class_name, $class);
     }
     $property = clone $class;
     if (is_object($options)) {
         $property->set_options($options);
     }
     return $property;
 }
Example #6
0
/**
 * Get page type id.
 *
 * @param  int $post_id
 *
 * @return string
 */
function papi_get_page_type_id($post_id = 0)
{
    $post_id = papi_get_post_id($post_id);
    $key = papi_get_page_type_key();
    $page_type = '';
    if ($post_id !== 0) {
        $meta_value = get_post_meta($post_id, $key, true);
        $page_type = empty($meta_value) ? '' : $meta_value;
    }
    if (empty($page_type)) {
        $page_type = str_replace('papi/', '', papi_get_qs('page_type'));
    }
    if (empty($page_type)) {
        $page_type = papi_get_sanitized_post(papi_get_page_type_key());
    }
    // Load right page type from a post query string
    if (empty($page_type)) {
        $meta_value = get_post_meta(papi_get_parent_post_id(), $key, true);
        $page_type = empty($meta_value) ? '' : $meta_value;
    }
    // Load page type id from the container if it exists or
    // load it from `papi_get_all_page_types`.
    if (empty($page_type)) {
        $post_type = papi_get_post_type();
        $load_once = papi_filter_core_load_one_type_on();
        $collection_key = 'core.page_type.' . $post_type;
        if (in_array($post_type, $load_once)) {
            if (papi()->exists($collection_key)) {
                return papi()->make($collection_key);
            }
            if ($page_types = papi_get_all_page_types(false, $post_type)) {
                return $page_types[0]->get_id();
            }
        }
    }
    return $page_type;
}
Example #7
0
/**
 * Get a content type by file path.
 *
 * @param  string $file_path
 *
 * @return null|Papi_Content_Type
 */
function papi_get_content_type($file_path)
{
    if (!is_file($file_path) || !is_string($file_path)) {
        return;
    }
    $class_name = papi_get_class_name($file_path);
    if (empty($class_name)) {
        return;
    }
    // Try to add the page type to the container.
    if (!papi()->exists($class_name)) {
        // @codeCoverageIgnoreStart
        if (!class_exists($class_name)) {
            require_once $file_path;
        }
        // @codeCoverageIgnoreEnd
        $rc = new ReflectionClass($class_name);
        $content_type = $rc->newInstanceArgs([$file_path]);
        // If the page type don't have a name we can't use it.
        if (!$content_type->has_name()) {
            return;
        }
        papi()->singleton($class_name, $content_type);
    }
    return papi()->make($class_name);
}
Example #8
0
/**
 * Load the content type id on a post types.
 *
 * @param  string $content_type_id
 *
 * @return string
 */
function papi_load_page_type_id($content_type_id = '')
{
    $key = papi_get_page_type_key();
    $post_id = papi_get_post_id();
    $post_type = papi_get_post_type($post_id);
    // If we have a post id we can load the content type id
    // from the post.
    if ($post_id > 0) {
        $meta_value = get_post_meta($post_id, $key, true);
        $content_type_id = empty($meta_value) ? '' : $meta_value;
    }
    // Try to fetch the content type id from `page_type`
    // query string.
    if (empty($content_type_id)) {
        $content_type_id = papi_get_qs('page_type');
    }
    // When using `only_page_type` filter we need to fetch the value since it
    // maybe not always saved in the database.
    if (empty($content_type_id)) {
        $content_type_id = papi_filter_settings_only_page_type($post_type);
    }
    // Load right content type from the parent post id.
    if (empty($content_type_id)) {
        $meta_value = get_post_meta(papi_get_parent_post_id(), $key, true);
        $content_type_id = empty($meta_value) ? '' : $meta_value;
    }
    // Load content type id from the container if it exists.
    if (empty($content_type_id)) {
        $key = sprintf('content_type_id.%s', $post_type);
        if (papi()->exists($key)) {
            return papi()->make($key);
        }
    }
    return $content_type_id;
}
Example #9
0
/**
 * Get entry type by identifier.
 *
 * @param  string $id
 *
 * @return Papi_Entry_Type
 */
function papi_get_entry_type_by_id($id)
{
    if (!is_string($id) || empty($id)) {
        return;
    }
    if (papi()->exists($id)) {
        return papi()->make($id);
    }
    $result = null;
    $entry_types = papi_get_all_entry_types();
    foreach ($entry_types as $entry_type) {
        if ($entry_type->match_id($id)) {
            $result = $entry_type;
            break;
        }
    }
    if (is_null($result)) {
        $path = papi_get_file_path($id);
        $result = papi_get_entry_type($path);
    }
    return $result;
}
 /**
  * Register management page.
  */
 public function admin_menu()
 {
     $papi = papi();
     add_management_page($papi->name, $papi->name, 'manage_options', 'papi', [$this, 'render_view']);
 }
Example #11
0
File: io.php Project: wp-papi/papi
/**
 * Get all core type files from the register directories.
 *
 * @return array
 */
function papi_get_all_core_type_files()
{
    return papi()->once(__FUNCTION__, function () {
        $directories = papi_filter_settings_directories();
        $result = [];
        foreach ($directories as $directory) {
            $result = array_merge($result, papi_get_all_files_in_directory($directory));
        }
        // Get the last file path from directories.
        $result = array_map('papi_get_core_type_file_path', $result);
        // Only unique path, no duplicated path is allowed.
        return array_unique($result);
    });
}
 /**
  * Check if the content type is a singleton.
  *
  * @return bool
  */
 public function singleton()
 {
     $key = sprintf('content_type_id.%s', $this->get_post_type());
     if (papi()->exists($key)) {
         return true;
     }
     papi()->singleton($key, $this->get_id());
     return false;
 }
Example #13
0
/**
 * Load the entry type id on a post types.
 *
 * @param  string $entry_type_id
 *
 * @return string
 */
function papi_load_page_type_id($entry_type_id = '')
{
    $key = papi_get_page_type_key();
    $post_id = papi_get_post_id();
    $post_type = papi_get_post_type($post_id);
    // Try to load the entry type id from only page type filter.
    if (empty($entry_type_id)) {
        $entry_type_id = papi_filter_settings_only_page_type($post_type);
    }
    // If we have a post id we can load the entry type id from the post.
    if (empty($entry_type_id) && $post_id > 0) {
        $meta_value = get_post_meta($post_id, $key, true);
        $entry_type_id = empty($meta_value) ? '' : $meta_value;
    }
    // Try to fetch the entry type id from `page_type` query string.
    if (empty($entry_type_id)) {
        $entry_type_id = papi_get_qs('page_type');
    }
    // Load right entry type from the parent post id.
    if (empty($entry_type_id)) {
        $meta_value = get_post_meta(papi_get_parent_post_id(), $key, true);
        $entry_type_id = empty($meta_value) ? '' : $meta_value;
    }
    // Try to load the entry type from all page types and check
    // if only one exists of that post type.
    //
    // The same as only page type filter but without the filter.
    if (empty($entry_type_id)) {
        $key = sprintf('entry_type_id.post_type.%s', $post_type);
        if (papi()->exists($key)) {
            return papi()->make($key);
        }
        $entries = papi_get_all_page_types($post_type);
        if (count($entries) === 1) {
            $entry_type_id = $entries[0]->get_id();
            papi()->bind($key, $entry_type_id);
        }
    }
    return $entry_type_id;
}