def map_deep(value, callback): ''' Maps a function to all non-iterable elements of an array or an object. This is similar to `array_walk_recursive()` but acts upon objects too. @param mixed value The array, object, or scalar. @param callable callback The function to map onto value. @return mixed The value with the callback applied to all non-arrays and non-objects inside it. refer to Php.serialize FlattenObj() ''' #from copy import deepcopy #val = deepcopy(value) if value is None: return None #from datetime import datetime #if isinstance(value, Php.SeqSetTypes): #= (list,tuple,range,set,frozenset,) # value = [ map_deep( item, callback) for item in value ] #isinstance( value, Php.MappingTypes): #= (dict, ODict, array) if Php.is_array(value): for index, item in value.items(): value[index] = map_deep(item, callback) elif Php.is_object(value): #if inspect.isclass(value): # class, not instance of a class # element = Func(value, Func) #else: # by now, must be instance of class object_vars = Php.get_object_vars(value) for property_name, property_value in object_vars.items(): setattr(value, property_name, map_deep(property_value, callback)) else: #elif isinstance( value, Php.ScalarTypes +(datetime,) ): # Php.ScalarTypes = (bool, str, bytes, int, float, complex,) value = callback(value) #else: # raise TypeError("map_deep value={} has wrong type!".format(value)) return value
def __init__(self, term): ''' Constructor. @param WP_Term|object term Term object. Inherited classes no long need to define 'self._obj=array()' in __init__() ''' # Term ID. # @access public @var int #$term_id self.term_id = None # The term's name. # @access public @var string self.name = '' # The term's slug. # @access public @var string self.slug = '' # The term's term_group. # @access public @var string self.term_group = '' # Term Taxonomy ID. # @access public @var int self.term_taxonomy_id = 0 # The term's taxonomy name. # @access public @var string self.taxonomy = '' # The term's description. # @access public @var string self.description = '' # ID of a term's parent term. # @access public @var int self.parent = 0 # Cached object count for this term. # @access public @var int self.count = 0 # Stores the term object's sanitization level. # Does not correspond to a database field. # @access public @var string self.filter = 'raw' if isinstance(term, array): # super().__init__(arg): Php.stdClass._obj = arg self._obj = term # term is already in array so don't need to setattr below else: for key, value in Php.get_object_vars(term).items(): #$this->$key = $value setattr(self, key, value)
def to_array(self): ''' Convert object to array. @return array Object as array. ''' post = Php.get_object_vars(self) for key in ('ancestors', 'page_template', 'post_category', 'tags_input'): if hasattr(self, key): post[key] = self.__get(key) return post
def wp_parse_args(args, defaults=''): '''/fs/web/wp/wp-includes/functions.php Merge user defined arguments into defaults array. This function is used throughout WordPress to allow for both string or array to be merged into another array. @param string|array|object args Value to merge with defaults. @param array defaults Optional. Array that serves as the defaults. Default empty. @return array Merged user defined values with defaults. ''' if Php.is_object(args): r = Php.get_object_vars(args) #same as: #r = array( (attr, getattr(args, attr, None)) for attr in dir(args) # if not attr.startswith(('_',)) ) elif Php.is_array(args): r = args # no need for & args since array is mutable else: #elif isinstance(args, str): #Php# WiF.wp_parse_str( args, r ) r = WiF.wp_parse_str(args) if Php.is_array(defaults): return Php.array_merge(defaults, r) return r
def to_array(self): ''' Converts an object to array. @return array Object as array. ''' return Php.get_object_vars(self)
def __init__(self, post=None): #php allows post=None in __construct ''' Constructor. @param WP_Post|object post Post object. Inherited classes no long need to define 'self._obj=array()' in __init__() ''' #public @var int. Post ID. self.ID = None self.post_author = 0 # self.post_author = '0' '''public @var str. post author ID. A numeric str, for compatibility reasons str! NOT int! According to codex.wordpress.org/Class_Reference/WP_Post ''' #public @var str. post's local publication time. self.post_date = '0000-00-00 00:00:00' # public @var str. The post's GMT publication time. self.post_date_gmt = '0000-00-00 00:00:00' # public @var str. The post's content. self.post_content = '' # public @var str. The post's title. self.post_title = '' # public @var str. The post's excerpt. self.post_excerpt = '' # public @var str. The post's status. self.post_status = 'publish' # public @var str. Whether comments are allowed. self.comment_status = 'open' # public @var str. Whether pings are allowed. self.ping_status = 'open' # public @var str. The post's password in plain text. self.post_password = '' # public @var str. The post's slug. self.post_name = '' # public @var str. URLs queued to be pinged. self.to_ping = '' # public @var str. URLs that have been pinged. self.pinged = '' # public @var str. The post's local modified time. self.post_modified = '0000-00-00 00:00:00' # public @var str. The post's GMT modified time. self.post_modified_gmt = '0000-00-00 00:00:00' # public @var str. A utility DB field for post content. self.post_content_filtered = '' # public @var int * ID of a post's parent post. self.post_parent = 0 # public @var str. The unique identifier for a post, not necessarily a URL, used as the feed GUID. self.guid = '' # public @var int * A field used for ordering posts. self.menu_order = 0 # public @var str. The post's type, like post or page. self.post_type = 'post' # public @var str. An attachment's mime type. self.post_mime_type = '' self.comment_count = 0 # self.comment_count = '0' ''' public @var str. Cached comment count. A numeric string, for compatibility reasons. str! NOT int! According to codex.wordpress.org/Class_Reference/WP_Post ''' # public @var str. Stores the post object's sanitization level. # Does not correspond to a DB field. self.filter = None #for key, value in Php.get_object_vars( post ).items(): # self.key = value for key, value in Php.get_object_vars(post).items(): setattr(self, key, value) #inherite self._obj from Php.stdClass