php PHP辅助函数(WP)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php PHP辅助函数(WP)相关的知识,希望对你有一定的参考价值。
<?php
//////////////////////////////////////////////////////////////
///////// CONSTANTS
//////////////////////////////////////////////////////////////
define( 'WOPMIST_COMPONENTS', get_theme_root_uri().'/wopmist/components', true );
define( 'WOPMIST_COMP_PATH', get_stylesheet_directory().'/components', true);
define( 'UPLOADS_URL', get_site_url().'/wp-content/uploads/', true);
//////////////////////////////////////////////////////////////
///////// ALERTS
//////////////////////////////////////////////////////////////
function woptima_alert_var($var) {
if(is_object($var) || is_array($var)) {
echo "<script type='text/javascript'>alert('".json_encode($var)."')</script>";
} else {
echo "<script type='text/javascript'>alert('".$var."')</script>";
}
}
function woptima_console_log($var) {
if(is_object($var) || is_array($var)) {
echo "<script type='text/javascript'>console.log('".json_encode($var)."')</script>";
} else {
echo "<script type='text/javascript'>console.log('".$var."')</script>";
}
}
function woptima_error_log($var,$log = null) {
if($log != null) {
error_log(print_r($var, true), 3, $log);
} else {
error_log(print_r($var, true));
}
}
//////////////////////////////////////////////////////////////
///////// CHECK IF EDIT PAGE
//////////////////////////////////////////////////////////////
function is_edit_page($new_edit = null){
global $pagenow;
//make sure we are on the backend
if (!is_admin()) return false;
if($new_edit == "edit")
return in_array( $pagenow, array( 'post.php', ) );
elseif($new_edit == "new") //check for new post page
return in_array( $pagenow, array( 'post-new.php' ) );
else //check for either new or edit
return in_array( $pagenow, array( 'post.php', 'post-new.php' ) );
}
//////////////////////////////////////////////////////////////
///////// START WITH - END WITH
//////////////////////////////////////////////////////////////
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
return $length === 0 ||
(substr($haystack, -$length) === $needle);
}
//////////////////////////////////////////////////////////////
///////// USER ROLES FUNCS
//////////////////////////////////////////////////////////////
function woptima_get_user_role( $user_id ) {
$user_meta=get_userdata($user_id);
$user_roles=$user_meta->roles;
return $user_roles[0];
}
function woptima_get_user_roles_by_user_id( $user_id ) {
$user = get_userdata( $user_id );
return empty( $user ) ? array() : $user->roles;
}
function woptima_is_user_in_role($role, $id = "") {
$user_id = $id != "" ? $id : get_current_user_id();
$user_role = woptima_get_user_roles_by_user_id( $user_id );
return woptima_match_vars( $role, $user_role );
}
function woptima_get_users_by_role($role) {
$roelUsers = array();
$args = array(
'role' => $role,
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($args);
foreach ($users as $user) {
$roleUsers[$user->ID]['id'] = $user->ID;
$roleUsers[$user->ID]['username'] = $user->user_nicename;
$roleUsers[$user->ID]['name'] = $user->display_name;
}
return $roleUsers;
}
function woptima_get_user_name_from_id($id) {
$userdata = get_userdata( $id );
return $userdata->first_name." ".$userdata->last_name." (".$userdata->user_login.")";
}
//////////////////////////////////////////////////////////////
///////// OS PATH FIXER
//////////////////////////////////////////////////////////////
function woptima_winpath($path) {
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
$path = str_replace('/', '\\', $path);
}
return $path;
}
//////////////////////////////////////////////////////////////
///////// GET CURRENT USER GROUP
//////////////////////////////////////////////////////////////
function woptima_get_user_group( $user_id ) {
return get_user_meta($user_id, 'user_group', true);
}
//////////////////////////////////////////////////////////////
///////// COMPARE VARS / ARRAYS
//////////////////////////////////////////////////////////////
function woptima_match_vars( $var1, $var2 ) {
if(is_array($var1) && is_array($var2)) {
if(array_intersect($var1, $var2) > 0) {
return true;
}
} elseif(!is_array($var1) && is_array($var2)) {
if(in_array($var1, $var2)) {
return true;
}
} elseif(is_array($var1) && !is_array($var2)) {
if(in_array($var2, $var1)) {
return true;
}
} elseif(!is_array($var1) && !is_array($var2)) {
if($var1 == $var2) {
return true;
}
}
return false;
}
//////////////////////////////////////////////////////////////
///////// GET FILES
//////////////////////////////////////////////////////////////
function woptima_find_files_recursive($extensions, $folder="") {
$files = array();
if(file_exists($folder)) {
$dir = new RecursiveDirectoryIterator($folder);
foreach (new RecursiveIteratorIterator($dir) as $filename => $file) {
if ($file->isFile() && in_array($file->getExtension(),$extensions)) {
$files[] = $file->getPathname();
}
}
}
return $files;
}
function woptima_find_files($extensions, $folder="") {
$files = array();
if(file_exists($folder)) {
foreach ($extensions as $extension) {
foreach (glob($folder.'/*.'.$extension) as $file) {
$files[] = $file;
}
}
}
return $files;
}
//////////////////////////////////////////////////////////////
///////// FIND META PARTIAL KEY
//////////////////////////////////////////////////////////////
function woptima_get_post_meta_partial_key($id,$keyPartial) {
$metas = get_post_meta($id);
$missing = false;
if(is_array($metas) && !empty($metas)) {
foreach ($metas as $key => $value) {
if(strpos($key, $keyPartial) !== false) {
if(!isset($value) || $value == "") {
$missing = true;
}
}
}
}
return $missing;
}
//////////////////////////////////////////////////////////////
///////// FIND MATCH USER META VALUE WITH CATEGORIES IDS
//////////////////////////////////////////////////////////////
function woptima_match_usermeta_categories($userID, $metaKey,$postID) {
$metaValue = get_usermeta( $userID, $metaKey , true);
$postCats = wp_get_post_categories( $postID );
if( (is_array($metaValue) && array_intersect($metaValue, $postCats)) || in_array($metaValue, $postCats)) {
return true;
}
return false;
}
//////////////////////////////////////////////////////////////
///////// SEARCH FOR KEY=>VALUE MULTIDIMENSIONAL ARRAY
//////////////////////////////////////////////////////////////
function woptima_find_key_value($array, $key, $val) {
foreach ($array as $item) {
if (is_array($item) && find_key_value($item, $key, $val)) return true;
if (isset($item[$key]) && $item[$key] == $val) return true;
}
return false;
}
//////////////////////////////////////////////////////////////
///////// CHECK IF META EMPTY/NOT EXIST
//////////////////////////////////////////////////////////////
function woptima_check_metas_exist_empty($id,$keys) {
$metas = get_post_meta($id);
$missing = false;
if(is_array($metas) && !empty($metas)) {
foreach ($metas as $name => $value) {
if(!in_array($name, $keys)) {
$missing = true;
} elseif(!isset($value) || $value == "") {
$missing = true;
}
}
}
return $missing;
}
//////////////////////////////////////////////////////////////
///////// ISSET NOT EMPTY
//////////////////////////////////////////////////////////////
function woptima_isset_notempty($array) {
if(is_array($array)) {
foreach ($array as $var) {
if(!isset($var) || empty($var)) {
return false;
}
}
}
return true;
}
//////////////////////////////////////////////////////////////
///////// META FILES ARRAY TO DONLOAD BUTTONS
//////////////////////////////////////////////////////////////
function woptima_meta_files_to_download_buttons($items) {
$buttons = "";
$items = unserialize($items);
if(is_array($items) && !empty($items)) {
foreach ($items as $id => $url) {
$url = wp_get_attachment_url( $id );
$title = get_the_title($id);
$buttons .= '<a href="'.$url.'" traget="_blank" class="btn btn-info btn-xs mar-5">'.$title.'</a>';
}
}
return $buttons;
}
//////////////////////////////////////////////////////////////
///////// COMPRESSION
//////////////////////////////////////////////////////////////
function woptima_compressCSS($code) {
$code = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $code);
$code = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $code);
$code = str_replace('{ ', '{', $code);
$code = str_replace(' }', '}', $code);
$code = str_replace('; ', ';', $code);
return $code;
}
function woptima_minifyJS($code) {
$code = preg_replace("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/", "", $code);
$code = str_replace(["\r\n","\r","\t","\n",' ',' ',' '], '', $code);
$code = preg_replace(['(( )+\))','(\)( )+)'], ')', $code);
return $code;
}
//////////////////////////////////////////////////////////////
///////// REDUX FUNCTIONS
//////////////////////////////////////////////////////////////
/// BG properties to style
function woptima_redux_background_styles($properties, $addStyleTag = false) {
$styles = "";
if(!is_array($properties)) {
return;
}
$styles .= $addStyleTag == true ? 'style=" ' : '';
//color
$bg_color = array_key_exists('background-color', $properties) ? $properties['background-color'] : "";
$styles .= isset($bg_color) && $bg_color != "" ? 'background-color: '.$bg_color.';' : "";
// image
$bg_image = array_key_exists('background-image', $properties) ? $properties['background-image'] : "";
$styles .= isset($bg_image) && $bg_image != "" ? 'background-image: url('.$bg_image.')' : "";
//repeat
$bg_repeat = array_key_exists('background-repeat', $properties) ? $properties['background-repeat'] : "";
$styles .= isset($bg_repeat) && $bg_repeat != "" ? 'background-repeat: url('.$bg_repeat.')' : "";
//position
$bg_position = array_key_exists('background-position', $properties) ? $properties['background-position'] : "";
$styles .= isset($bg_position) && $bg_position != "" ? 'background-position: url('.$bg_position.')' : "";
//size
$bg_size = array_key_exists('background-size', $properties) ? $properties['background-size'] : "";
$styles .= isset($bg_size) && $bg_size != "" ? 'background-size: url('.$bg_size.')' : "";
//attachment
$bg_attachment = array_key_exists('background-attachment', $properties) ? $properties['background-attachment'] : "";
$styles .= isset($bg_attachment) && $bg_attachment != "" ? 'background-attachment: url('.$bg_attachment.')' : "";
$styles .= $addStyleTag == true ? '"' : '';
return $styles;
}
/*
function test_update_meta() {
update_post_meta(1, 'U_S_C_users',array(1));
}
add_action('init', 'test_update_meta');
*/
/*
//////////////////////////////////////////////////////////////
///////// UPDATE POST META
//////////////////////////////////////////////////////////////
function update_meta_all_posts()
{
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'suppress_filters' => true
);
$posts_array = get_posts( $args );
foreach($posts_array as $post_array)
{
update_post_meta($post_array->ID, 'zozo_post_views_count', '0');
}
}
*/
/*
//////////////////////////////////////////////////////////////
///////// UPDATE CATEGORY META
//////////////////////////////////////////////////////////////
add_action('init', 'update_meta_all_tags');
function update_meta_all_tags() {
$terms_array = get_terms('category', array(
'hide_empty' => 0
));
//woptima_console_log($terms_array);
foreach($terms_array as $term_array) {
update_term_meta($term_array->term_id, 'wpta_grid_override', 'yes');
update_term_meta($term_array->term_id, 'wpta_grid_gap', '10');
update_term_meta($term_array->term_id, 'wpta_grid_style', 'title-card');
}
}
*/
/*
//////////////////////////////////////////////////////////////
///////// DISPLAY LOADED SCRIPTS
//////////////////////////////////////////////////////////////
function crunchify_print_scripts_styles() {
// Print all loaded Scripts
global $wp_scripts;
foreach( $wp_scripts->queue as $script ) :
woptima_error_log($script . ' ** ');
endforeach;
// Print all loaded Styles (CSS)
global $wp_styles;
foreach( $wp_styles->queue as $style ) :
woptima_error_log($style . ' || ');
endforeach;
}
add_action( 'wp_print_scripts', 'crunchify_print_scripts_styles' );
*/
以上是关于php PHP辅助函数(WP)的主要内容,如果未能解决你的问题,请参考以下文章
php WP - 使用AJAX从JavaScript调用WP函数
php 示例SVG和Markdown辅助函数#laravel #php