get_constant() статический публичный Метод

Return a class constant for the called class.
static public get_constant ( string $constant_name, string | boolean | object $class_name = false, boolean $try_parent = true ) : mixed | null
$constant_name string
$class_name string | boolean | object
$try_parent boolean
Результат mixed | null
Пример #1
0
 /**
  * Create new Instance of a Post MVI
  *
  * @param WP_Post|int $_post
  * @param array $args {
  *
  *      @type string $instance_class
  *      @type string $list_owner
  *
  *}
  * @return mixed
  *
  * @future Alias this with make_new_post() so it can be called as WPLib::make_new_post( $post_id )
  */
 static function make_new_item($_post, $args = array())
 {
     $args = wp_parse_args($args, array('instance_class' => false, 'list_owner' => 'WPLib_Posts'));
     if (is_numeric($_post)) {
         $_post = WP_Post::get_instance($_post);
     }
     if (!$args['instance_class']) {
         $args['instance_class'] = WPLib::get_constant('INSTANCE_CLASS', $args['list_owner']);
     }
     if (!$args['instance_class']) {
         $args['instance_class'] = self::get_post_type_class($_post->post_type);
     }
     $instance_class = $args['instance_class'];
     return $instance_class ? new $instance_class($_post) : null;
 }
Пример #2
0
 /**
  * User role as declared in the class constant ROLE.
  *
  * @return string|null
  */
 function role_slug()
 {
     return WPLib::get_constant('ROLE', get_class($this->owner));
 }
Пример #3
0
 /**
  * @return bool|string
  */
 static function short_prefix()
 {
     return WPLib::get_constant('SHORT_PREFIX', get_called_class());
 }
Пример #4
0
 /**
  * @param array|string|WPLib_Query $query
  * @param array $args {
  *
  *      @type string $list_class        The specific class for the list, i.e. WPLib_Post_List
  *
  *      @type string $default_list      The default list if no $list_class, i.e. WPLib_Post_List_Default
  *
  *      @type string $items The array of items, or a callable that will return a list of items.
  *
  *      @type string $list_owner        The class "owning" the list, typically "Owner" if Owner::get_list()
  *
  *      @type string $instance_class    The class for items in the list, i.e. WP_Post
  * }
  *
  * @return WPLib_List_Default[]
  *
  */
 static function get_list($query = array(), $args = array())
 {
     if (is_string($query)) {
         $query = wp_parse_args($query);
     } else {
         if (is_null($query)) {
             $query = array();
         }
     }
     if (!isset($args['list_owner'])) {
         $args['list_owner'] = get_called_class();
     }
     if (!isset($args['instance_class'])) {
         $args['instance_class'] = WPLib::get_constant('INSTANCE_CLASS', $args['list_owner']);
     }
     $try_class = $args['instance_class'];
     $args = wp_parse_args($args, array('list_class' => "{$try_class}_List", 'default_list' => 'WPLib_List_Default', 'items' => false));
     if (!class_exists($args['list_class'])) {
         do {
             /**
              * @future Provide a more robust mechanism for discovering 'list_class'
              */
             /*
              * Add '_Default' to last list class checked,
              * i.e. WPLib_Post_List_Default for WPLib_Posts::get_list()
              */
             $args['list_class'] = "{$args['list_class']}_Default";
             if (class_exists($args['list_class'])) {
                 break;
             }
             $args['list_class'] = false;
             $try_class = preg_replace('#^(.+)_Base$#', '$1', get_parent_class($try_class));
             if (!$try_class) {
                 break;
             }
             /*
              * Add '_List' to element class,
              * i.e. WPLib_Post_List for WPLib_Posts::get_list()
              */
             $args['list_class'] = "{$try_class}_List";
             if (class_exists($args['list_class'])) {
                 break;
             }
         } while ($try_class);
     }
     if (!$args['list_class']) {
         /*
          * Give up and use default, i.e. WPLib_List_Default
          */
         $args['list_class'] = $args['default_list'];
     }
     $list_class = $args['list_class'];
     $items = is_callable($args['items']) ? call_user_func($args['items'], $query, $args) : null;
     if (is_null($args['instance_class'])) {
         $message = __('No constant %s::INSTANCE_CLASS defined.', 'wplib');
         WPLib::trigger_error(sprintf($message, $args['list_owner']));
         $list = array();
     } else {
         $list = !is_null($items) ? new $list_class($items, $args) : array();
     }
     unset($args['list_owner'], $args['list_class'], $args['list_default'], $args['default_list'], $args['items']);
     return $list;
 }
Пример #5
0
 /**
  * @param WPLib_Item_Base|WP_Post|WP_Term $item
  * @param array $args
  *
  * @return WPLib_Term_Base|WPLib_Post_Base
  */
 static function make_new_item($item, $args = array())
 {
     $class = get_called_class();
     if (WPLib::get_constant('INSTANCE_CLASS', $class)) {
         if (self::class_declares_method($class, 'make_new_item')) {
             $item = $class::make_new_item($item, $args);
         } else {
             if (WPLib::is_development()) {
                 $err_msg = __('Cannot make new item. Class %s does not have make_new_item method', 'wplib');
                 WPLib::trigger_error(sprintf($err_msg, $class), E_USER_ERROR);
             }
         }
     } else {
         if (WPLib::is_development()) {
             $err_msg = __('Cannot make new item. Class %s does not have INSTANCE_CLASS constant.', 'wplib');
             WPLib::trigger_error(sprintf($err_msg, $class), E_USER_ERROR);
         }
     }
     return $item;
 }
Пример #6
0
 /**
  * Returns the TAXONOMY value for this model's item.
  *
  * @return mixed|null
  */
 static function item_taxonomy()
 {
     return WPLib::get_constant('TAXONOMY', self::item_class());
 }
Пример #7
0
 /**
  * Create new Instance of a Term Item
  *
  * @param WP_Term|int $term
  * @param array $args {
  *
  *      @type string $taxonomy
  *      @type string $instance_class
  *      @type string $list_owner
  *
  *}
  * @return mixed
  *
  * @future Alias this with make_new_term() so it can be called as WPLib::make_new_term( $term_id )
  */
 static function make_new_item($term, $args = array())
 {
     $args = wp_parse_args($args, array('instance_class' => false, 'taxonomy' => false, 'list_owner' => 'WPLib_Terms'));
     if (is_numeric($term)) {
         if ($args['taxonomy']) {
             $term = WP_Term::get_instance($term, $args['taxonomy']);
         } else {
             global $wp_version;
             if (version_compare($wp_version, '4.3', '<')) {
                 /**
                  * This only works in WordPress DBs created since 4.3+.
                  *
                  * @see https://make.wordpress.org/core/2015/06/09/eliminating-shared-taxonomy-terms-in-wordpress-4-3/
                  */
                 $err_msg = __("Cannot call %s() without \$args['taxonomy'] set in WordPress version 4.2 or earlier.", 'wplib');
                 WPLib::trigger_error($err_msg, __METHOD__);
             } else {
                 $term = WP_Term::get_instance($term);
             }
         }
     }
     if (!$args['instance_class']) {
         $args['instance_class'] = WPLib::get_constant('INSTANCE_CLASS', $args['list_owner']);
     }
     if (!$args['instance_class']) {
         $args['instance_class'] = self::get_taxonomy_class($term->taxonomy);
     }
     $instance_class = $args['instance_class'];
     return $instance_class ? new $instance_class($term) : null;
 }
Пример #8
0
 /**
  * @param WPLib_Item_Base|WP_Post|WP_Term $item
  * @param array $args
  *
  * @return WPLib_Term_Base|WPLib_Post_Base
  */
 static function make_new_item($item, $args = array())
 {
     $class = get_called_class();
     if (WPLib::get_constant('POST_TYPE', $class)) {
         $item = WPLib_Posts::make_new_item($item, $args);
     } else {
         if (WPLib::get_constant('TAXONOMY', $class)) {
             $item = WPLib_Terms::make_new_item($item, $args);
         } else {
             $err_msg = __('Cannot make new item. Class %s does not have POST_TYPE or TAXONOMY constant.', 'wplib');
             WPLib::trigger_error(sprintf($err_msg, $class));
         }
     }
     return $item;
 }
Пример #9
0
 /**
  * @return mixed|null
  */
 static function var_name()
 {
     return WPLib::get_constant('VAR_NAME', static::instance_class());
 }