def GetDtFromGmtDt(GmtDt): import wp.i.option as WiO from datetime import datetime, timedelta import wpy.time as wTm tz = WiO.get_option('timezone_string') # default = '', can set to ='UTC' if tz: if not GmtDt: GmtDt = wTm.DtMin # = datetime.min +1day, else err convert tz -n hr return wTm.UtcToDt(GmtDt, tz) # LocalDt = GmtDt + gmt_offset # [ add ] !!! return GmtDt + timedelta(hours=int(WiO.get_option('gmt_offset')))
def wp_cookie_constants(self): ''' Defines cookie related WordPress constants Defines constants after multisite is loaded. ''' # Used to guarantee unique hash cookies if not self.defined('COOKIEHASH'): siteurl = WiO.get_site_option('siteurl') if siteurl: self.define('COOKIEHASH', Php.md5(siteurl)) else: self.define('COOKIEHASH', Php.md5(wp_guess_url())) if not self.defined('USER_COOKIE'): self.define('USER_COOKIE', 'wordpressuser_' + self.COOKIEHASH) if not self.defined('PASS_COOKIE'): self.define('PASS_COOKIE', 'wordpresspass_' + self.COOKIEHASH) if not self.defined('AUTH_COOKIE'): self.define('AUTH_COOKIE', 'wordpress_' + self.COOKIEHASH) if not self.defined('SECURE_AUTH_COOKIE'): self.define('SECURE_AUTH_COOKIE', 'wordpress_sec_' + self.COOKIEHASH) if not self.defined('LOGGED_IN_COOKIE'): self.define('LOGGED_IN_COOKIE', 'wordpress_logged_in_' + self.COOKIEHASH) if not self.defined('TEST_COOKIE'): self.define('TEST_COOKIE', 'wordpress_test_cookie') if not self.defined('COOKIEPATH'): self.define( 'COOKIEPATH', Php.preg_replace('|https?://[^/]+|i', '', WiO.get_option('home') + '/')) if not self.defined('SITECOOKIEPATH'): self.define( 'SITECOOKIEPATH', Php.preg_replace('|https?://[^/]+|i', '', WiO.get_option('siteurl') + '/')) if not self.defined('ADMIN_COOKIE_PATH'): self.define('ADMIN_COOKIE_PATH', self.SITECOOKIEPATH + 'wp-admin') if not self.defined('PLUGINS_COOKIE_PATH'): self.define( 'PLUGINS_COOKIE_PATH', Php.preg_replace('|https?://[^/]+|i', '', self.WP_PLUGIN_URL)) if not self.defined('COOKIE_DOMAIN'): self.define('COOKIE_DOMAIN', False)
def get_gmt_from_date(Str, Format='%Y-%m-%d %H:%M:%S'): ''' Returns a date in the GMT equivalent. Requires and returns a date in the Y-m-d H:i:s Format. If there is a timezone_string available, the date is assumed to be in that timezone, otherwise it simply [ subtracts ] the value of the 'gmt_offset' option. Return Format can be overridden using the Format parameter. @param string Str The date to be converted. @param string Format The Format string for the returned date (default is Y-m-d H:i:s) @return string GMT version of the date provided. weston.ruter.net/2013/04/02/do-not-change-the-default-timezone-from-utc-in-wordpress/ [Default tz hardcoded as UTC]wordpress.stackexchange.com/questions/30946/ ''' import wp.i.option as WiO from datetime import datetime import wpy.time as wTm tz = WiO.get_option('timezone_string') # default = '', can set to ='UTC' if tz: #datetime = date_create( Str, new DateTimeZone( tz ) ) Dt = datetime.strptime(Str, Format) if not Dt: #return gmdate( Format, 0 ) Dt = wTm.DtMin # = datetime.min +1day, else err convert tz -n hr #datetime->setTimezone( new DateTimeZone( 'UTC' ) ) UtcDt = wTm.DtToUtc(Dt, tz) #string_gmt = datetime->Format( Format ) else: Result, matches = Php.preg_match_Result( '#([0-9]{1,4})-([0-9]{1,2})-' '([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', Str) if not Result: #datetime = strtotime( Str ) Dt = datetime.strptime(Str, Format) if not Dt: # datetime: #return gmdate( Format, 0 ) Dt = wTm.DtMin # = datetime.min +1day, else err convert tz -n hr #return gmdate( Format, datetime ) #BlogWebHostLocation = WpC.WB.Wj.Bj.WH0.DcNum #BlogWebHostUtc = wTm.DtToUtc(Dt, BlogWebHostLocation ) #return BlogWebHostUtc.strftime(Format) return wTm.SetDtToUtc(Dt) #string_time = gmmktime( matches[4], matches[5], matches[6], # matches[2], matches[3], matches[1] ) #string_gmt = gmdate( Format, string_time - # get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) # UtcDt = Dt - gmt_offset # [ subtract ] !!! UtcDt = datetime(matches[1], matches[2], matches[3], matches[4] - int(WiO.get_option('gmt_offset')), matches[5], matches[6]) string_gmt = UtcDt.strftime(Format) return string_gmt
def GetGmtDtFromDt(Dt): ''' Returns a dt datetime in the GMT equivalent. Requires and returns a py datetime format. ''' import wp.i.option as WiO from datetime import datetime, timedelta import wpy.time as wTm tz = WiO.get_option('timezone_string') # default = '', can set to ='UTC' if tz: if not Dt: Dt = wTm.DtMin # = datetime.min +1day, else err convert tz -n hr return wTm.DtToUtc(Dt, tz) # UtcDt = Dt - gmt_offset # [ subtract ] !!! return Dt - timedelta(hours=int(WiO.get_option('gmt_offset')))
def add_rewrite_rules(self): ''' Adds the necessary rewrite rules for the taxonomy. @access public @global WP wp Current WordPress environment instance. ''' # @var WP wp wp = self.Wj.wp # global wp # Non-publicly queryable taxonomies should not register query vars, # except in the admin. if False is not self.query_var and wp: wp.add_query_var(self.query_var) if False is not self.rewrite and ( self.Wj.is_admin() or '' != WiO.get_option('permalink_structure')): if self.hierarchical and self.rewrite['hierarchical']: tag = '(.+?)' else: tag = '([^/]+)' add_rewrite_tag( "%{}%".format(self.name), tag, self.query_var + "=" if self.query_var else "taxonomy={}&term=".format(self.name)) add_permastruct(self.name, "{}/%{}%".format(self.rewrite['slug'], self.name), self.rewrite)
def get_date_from_gmt(Str, Format='%Y-%m-%d %H:%M:%S'): ''' Converts a GMT date into the correct Format for the blog. Requires and returns a date in the Y-m-d H:i:s Format. If there is a timezone_string available, the returned date is in that timezone, otherwise it simply [ adds ] the value of gmt_offset. Return Format can be overridden using the Format parameter @param string Str The date to be converted. @param string Format The Format string for the returned date (default is Y-m-d H:i:s) @return string Formatted date relative to the timezone / GMT offset. weston.ruter.net/2013/04/02/do-not-change-the-default-timezone-from-utc-in-wordpress/ [Default tz hardcoded as UTC]wordpress.stackexchange.com/questions/30946/ ''' import wp.i.option as WiO from datetime import datetime import wpy.time as wTm tz = WiO.get_option('timezone_string') # default = '', can set to ='UTC' if tz: #Dt = date_create( Str, new DateTimeZone( 'UTC' ) ) GmtDt = wTm.SetDtToUtc(datetime.strptime(Str, Format)) #Gmt = Utc if not GmtDt: #return date( Format, 0 ) GmtDt = wTm.DtMin # = datetime.min +1day, else err convert tz -n hr #Dt->setTimezone( new DateTimeZone( tz ) ) #string_localtime = Dt->Format( Format ) LocalDt = wTm.UtcToDt(GmtDt, tz) string_localtime = LocalDt.strftime(Format) else: Result, matches = Php.preg_match_Result( '#([0-9]{1,4})-([0-9]{1,2})-' '([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', Str) if not Result: #return date( Format, 0 ) #return datetime.min # = datetime.datetime(1, 1, 1, 0, 0) LocalDt = wTm.DtMin # = datetime.min +1day, else err convert tz -n hr else: LocalDt = datetime(matches[1], matches[2], matches[3], matches[4] + int(WiO.get_option('gmt_offset')), matches[5], matches[6]) #string_time = gmmktime( matches[4], matches[5], matches[6], matches[2], # matches[3], matches[1] ) #string_localtime = gmdate( Format, string_time + # int(WiO.get_option( 'gmt_offset' )) * HOUR_IN_SECONDS ) # LocalDt = GmtDt + gmt_offset # [ add ] !!! string_localtime = LocalDt.strftime(Format) return string_localtime
def wp_set_internal_encoding(): ''' Set internal encoding. In most cases the default internal encoding is latin1, which is of no use, since we want to use the `mb_` functions for `utf-8` strings. ''' #if Php.function_exists( 'mb_internal_encoding' ): charset = WiO.get_option('blog_charset') #VT Always return 'UTF-8' if not charset or not Php.mb_internal_encoding(charset): Php.mb_internal_encoding('UTF-8')
def add_rewrite_rules(self): ''' Adds the necessary rewrite rules for the post type. @global WP_Rewrite wp_rewrite WordPress Rewrite Component. @global WP wp Current WordPress environment instance. ''' wp_rewrite = self.Wj.wp_rewrite # global wp_rewrite wp = self.Wj.wp # global wp if False != self.query_var and wp and is_post_type_viewable(self): wp.add_query_var(self.query_var) if (False != self.rewrite and (self.Wj.is_admin() or '' != WiO.get_option('permalink_structure'))): if self.hierarchical: add_rewrite_tag( "%" + self.name + "%", '(.+?)', self.query_var + "=" if self.query_var else "post_type=" + self.name + "&pagename=") else: add_rewrite_tag( "%" + self.name + "%", '([^/]+)', self.query_var + "=" if self.query_var else "post_type=" + self.name + "&name=") if self.has_archive: archive_slug = self.rewrite['slug'] if self.has_archive == True else \ self.has_archive if self.rewrite['with_front']: archive_slug = substr(wp_rewrite.front, 1) + archive_slug else: archive_slug = wp_rewrite.root + archive_slug add_rewrite_rule(archive_slug + "/?$", "index.php?post_type=" + self.name, 'top') if self.rewrite['feeds'] and wp_rewrite.feeds: feeds = '(' + trim(implode('|', wp_rewrite.feeds)) + ')' add_rewrite_rule( archive_slug + "/feed/" + feeds + "/?$", "index.php?post_type=" + self.name + '&feed=$matches[1]', 'top') add_rewrite_rule( archive_slug + "/" + feeds + "/?$", "index.php?post_type=" + self.name + '&feed=$matches[1]', 'top') if self.rewrite['pages']: add_rewrite_rule( archive_slug + "/" + wp_rewrite.pagination_base + "/([0-9]{1,})/?$", "index.php?post_type=" + self.name + '&paged=$matches[1]', 'top') permastruct_args = self.rewrite permastruct_args['feed'] = permastruct_args['feeds'] add_permastruct(self.name, self.rewrite['slug'] + "/%" + self.name + "%", permastruct_args)
def wp_plugin_directory_constants(self): ''' Defines plugin directory WordPress constants Defines must-use plugin directory constants, which may be overridden in the sunrise.php drop-in ''' if not self.defined('WP_CONTENT_URL'): self.define('WP_CONTENT_URL', WiO.get_option('siteurl') + '/wp-content') # full url - WP_CONTENT_DIR is self.defined further up # Allows for the plugins directory to be moved from the default location. if not self.defined('WP_PLUGIN_DIR'): self.define('WP_PLUGIN_DIR', self.WP_CONTENT_DIR + '/plugins') # full path, no trailing slash # Allows for the plugins directory to be moved from the default location. if not self.defined('WP_PLUGIN_URL'): self.define('WP_PLUGIN_URL', self.WP_CONTENT_URL + '/plugins') # full url, no trailing slash # Allows for the plugins directory to be moved from the default location. if not self.defined('PLUGINDIR'): self.define('PLUGINDIR', 'wp-content/plugins') # Relative to ABSPATH+ For back compat. # Allows for the mu-plugins directory to be moved from the default location. if not self.defined('WPMU_PLUGIN_DIR'): self.define('WPMU_PLUGIN_DIR', self.WP_CONTENT_DIR + '/mu-plugins') # full path, no trailing slash # Allows for the mu-plugins directory to be moved from the default location. if not self.defined('WPMU_PLUGIN_URL'): self.define('WPMU_PLUGIN_URL', self.WP_CONTENT_URL + '/mu-plugins') # full url, no trailing slash # Allows for the mu-plugins directory to be moved from the default location. if not self.defined('MUPLUGINDIR'): self.define('MUPLUGINDIR', 'wp-content/mu-plugins')
def register_uninstall_hook(File, callback): ''' Set the uninstallation hook for a plugin. Registers the uninstall hook that will be called when the user clicks on the uninstall link that calls for the plugin to uninstall itself. The link won't be active unless the plugin hooks into the action. The plugin should not run arbitrary code outside of functions, when registering the uninstall hook. In order to run using the hook, the plugin will have to be included, which means that any code laying outside of a function will be run during the uninstall process. The plugin should not hinder the uninstall process. If the plugin can not be written without running code within the plugin, then the plugin should create a file named 'uninstall.php' in the base plugin folder. This file will be called, if it exists, during the uninstall process bypassing the uninstall hook. The plugin, when using the 'uninstall.php' should always check for the 'WP_UNINSTALL_PLUGIN' constant, before executing. @param string File Plugin file. @param callable callback The callback to run when the hook is called. Must be a static method or function. ''' import wp.i.option as WiO if Php.is_array(callback) and Php.is_object(callback[0]): _doing_it_wrong( register_uninstall_hook.__name__, # __FUNCTION__, __('Only a static class method or function can be used in an uninstall hook.' ), '3.1.0') return # The option should not be autoloaded, because it is not needed in most # cases. Emphasis should be put on using the 'uninstall.php' way of # uninstalling the plugin. uninstallable_plugins = Php.Array(WiO.get_option('uninstall_plugins')) uninstallable_plugins[plugin_basename(File)] = callback WiO.update_option('uninstall_plugins', uninstallable_plugins)
def set_props(self, args): ''' Sets post type properties. @param array|str args arguments for registering a post type. ''' import wp.i.post as WpP args = WiFc.wp_parse_args(args) # Filters the arguments for registering a post type. # @param array args Array of arguments for registering a post type. # @param string post_type Post type key. #args = apply_filters( 'register_post_type_args', args, self.name ) #has_edit_link = bool( args['_edit_link']) #err: '_edit_link' not in args! #has_edit_link = bool( args.get('_edit_link', None) ) has_edit_link = not Php.empty(args, '_edit_link') # Args prefixed with an underscore are reserved for internal use. defaults = array( ('labels', array()), ('description', ''), ('public', False), ('hierarchical', False), ('exclude_from_search', None), ('publicly_queryable', None), ('show_ui', None), ('show_in_menu', None), ('show_in_nav_menus', None), ('show_in_admin_bar', None), ('menu_position', None), ('menu_icon', None), ('capability_type', 'post'), ('capabilities', array()), ('map_meta_cap', None), ('supports', array()), ('register_meta_box_cb', None), ('taxonomies', array()), ('has_archive', False), ('rewrite', True), ('query_var', True), ('can_export', True), ('delete_with_user', None), ('_builtin', False), ('_edit_link', 'post.php?post=%d'), ) args = Php.array_merge(defaults, args) args['name'] = self.name # If not set, default to the setting for public. if None == args['publicly_queryable']: args['publicly_queryable'] = args['public'] # If not set, default to the setting for public. if None == args['show_ui']: args['show_ui'] = args['public'] # If not set, default to the setting for show_ui. if None == args['show_in_menu'] or not args['show_ui']: args['show_in_menu'] = args['show_ui'] # If not set, default to the whether the full UI is shown. if None == args['show_in_admin_bar']: args['show_in_admin_bar'] = bool(args['show_in_menu']) # If not set, default to the setting for public. if None == args['show_in_nav_menus']: args['show_in_nav_menus'] = args['public'] # If not set, default to True if public, False if publi: if None == args['exclude_from_search']: args['exclude_from_search'] = not args['public'] # Back compat with quirky handling in version 3.0. #14122. if (Php.empty(args, 'capabilities') and None is args['map_meta_cap'] and Php.in_array(args['capability_type'], array( 'post', 'page'))): args['map_meta_cap'] = True # If not set, default to False. if None == args['map_meta_cap']: args['map_meta_cap'] = False # If there's no specified edit link and no UI, remove the edit link. if not args['show_ui'] and not has_edit_link: args['_edit_link'] = '' #args['cap']=WpP.get_post_type_capabilities(Php.Object(args)) args['cap'] = WpP.get_post_type_capabilities(Php.Object(args), Wj=self.Wj) del args['capabilities'] if Php.is_array(args['capability_type']): args['capability_type'] = args['capability_type'][0] if False != args['query_var']: if True == args['query_var']: args['query_var'] = self.name else: args['query_var'] = WiF.sanitize_title_with_dashes( args['query_var']) if False != args['rewrite'] and ( self.Wj.is_admin() or '' != WiO.get_option('permalink_structure')): if not Php.is_array(args['rewrite']): args['rewrite'] = array() if Php.empty(args['rewrite'], 'slug'): args['rewrite']['slug'] = self.name if not Php.isset(args['rewrite'], 'with_front'): args['rewrite']['with_front'] = True if not Php.isset(args['rewrite'], 'pages'): args['rewrite']['pages'] = True if not Php.isset(args['rewrite'], 'feeds') or not args['has_archive']: args['rewrite']['feeds'] = bool(args['has_archive']) if not Php.isset(args['rewrite'], 'ep_mask'): if Php.isset(args, 'permalink_epmask'): args['rewrite']['ep_mask'] = args['permalink_epmask'] else: args['rewrite']['ep_mask'] = EP_PERMALINK for property_name, property_value in args.items(): setattr(self, property_name, property_value) self.labels = WpP.get_post_type_labels(self) #print('\n VT self.lables =', self.labels, type(self.labels)) self.label = self.labels.name
def set_props(self, object_type, args): ''' Sets taxonomy properties. @access public @param array|str object_type Name of the object type for the taxonomy obj @param array|str args Array or query string of arguments for registering a taxonomy. ''' Wj = self.Wj import wp.i.taxonomy as WiTx args = WiFc.wp_parse_args(args) # Filters the arguments for registering a taxonomy. # @param array args Array of arguments for registering a taxonomy. # @param str taxonomy Taxonomy key. # @param array object_type Array of names of object types for the taxonomy args = WiPg.apply_filters('register_taxonomy_args', args, self.name, Php.Array(object_type), Wj=self.Wj) defaults = array( ('labels', array()), ('description', ''), ('public', True), ('publicly_queryable', None), ('hierarchical', False), ('show_ui', None), ('show_in_menu', None), ('show_in_nav_menus', None), ('show_tagcloud', None), ('show_in_quick_edit', None), ('show_admin_column', False), ('meta_box_cb', None), ('capabilities', array()), ('rewrite', True), ('query_var', self.name), ('update_count_callback', ''), ('_builtin', False), ) args = Php.array_merge(defaults, args) # If not set, default to the setting for public. if None is args['publicly_queryable']: args['publicly_queryable'] = args['public'] if False is not args['query_var'] and ( Wj.is_admin() or False is not args['publicly_queryable']): if True is args['query_var']: args['query_var'] = self.name else: args['query_var'] = WiF.sanitize_title_with_dashes( args['query_var']) else: # Force query_var to False for non-public taxonomies. args['query_var'] = False if False is not args['rewrite'] and ( Wj.is_admin() or '' != WiO.get_option('permalink_structure')): args['rewrite'] = WiFc.wp_parse_args( args['rewrite'], array( ('with_front', True), ('hierarchical', False), ('ep_mask', 'EP_NONE'), )) if Php.empty(args['rewrite'], 'slug'): args['rewrite']['slug'] = WiF.sanitize_title_with_dashes( self.name) # If not set, default to the setting for public. if None is args['show_ui']: args['show_ui'] = args['public'] # If not set, default to the setting for show_ui. if None is args['show_in_menu'] or not args['show_ui']: args['show_in_menu'] = args['show_ui'] # If not set, default to the setting for public. if None is args['show_in_nav_menus']: args['show_in_nav_menus'] = args['public'] # If not set, default to the setting for show_ui. if None is args['show_tagcloud']: args['show_tagcloud'] = args['show_ui'] # If not set, default to the setting for show_ui. if None is args['show_in_quick_edit']: args['show_in_quick_edit'] = args['show_ui'] default_caps = array( ('manage_terms', 'manage_categories'), ('edit_terms', 'manage_categories'), ('delete_terms', 'manage_categories'), ('assign_terms', 'edit_posts'), ) args['cap'] = Php.Object( Php.array_merge(default_caps, args['capabilities'])) Php.unset(args, 'capabilities') args['object_type'] = Php.array_unique(Php.Array(object_type)) # If not set, use the default meta box if None is args['meta_box_cb']: if args['hierarchical']: args['meta_box_cb'] = 'post_categories_meta_box' else: args['meta_box_cb'] = 'post_tags_meta_box' for property_name, property_value in args.items(): setattr(self, property_name, property_value) self.labels = WiTx.get_taxonomy_labels(self) # pass self.Wj self.label = self.labels.name