get_results($query, 'ARRAY_A');
if(empty($array)){
return current($result);
}else{
return $result;
}
}
// Check if an IP is valid
function pagelayer_valid_ip($ip){
// IPv6
if(pagelayer_valid_ipv6($ip)){
return true;
}
// IPv4
if(!ip2long($ip)){
return false;
}
return true;
}
function pagelayer_valid_ipv6($ip){
$pattern = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/';
if(!preg_match($pattern, $ip)){
return false;
}
return true;
}
// Check if a field is posted via POST else return default value
function pagelayer_optpost($name, $default = ''){
if(!empty($_POST[$name])){
return pagelayer_inputsec(pagelayer_htmlizer(trim($_POST[$name])));
}
return $default;
}
// Check if a field is posted via GET else return default value
function pagelayer_optget($name, $default = ''){
if(!empty($_GET[$name])){
return pagelayer_inputsec(pagelayer_htmlizer(trim($_GET[$name])));
}
return $default;
}
// Check if a field is posted via GET or POST else return default value
function pagelayer_optreq($name, $default = ''){
if(!empty($_REQUEST[$name])){
return pagelayer_inputsec(pagelayer_htmlizer(trim($_REQUEST[$name])));
}
return $default;
}
// For filling in posted values
function pagelayer_POSTval($name, $default = ''){
return (!empty($_POST) ? (!isset($_POST[$name]) ? '' : $_POST[$name]) : $default);
}
function pagelayer_POSTchecked($name, $default = false){
return (!empty($_POST) ? (isset($_POST[$name]) ? 'checked="checked"' : '') : (!empty($default) ? 'checked="checked"' : ''));
}
function pagelayer_POSTselect($name, $value, $default = false){
if(empty($_POST)){
if(!empty($default)){
return 'selected="selected"';
}
}else{
if(isset($_POST[$name])){
if(trim($_POST[$name]) == $value){
return 'selected="selected"';
}
}
}
}
function pagelayer_inputsec($string){
if(!get_magic_quotes_gpc()){
$string = addslashes($string);
}else{
$string = stripslashes($string);
$string = addslashes($string);
}
// This is to replace ` which can cause the command to be executed in exec()
$string = str_replace('`', '\`', $string);
return $string;
}
function pagelayer_htmlizer($string){
$string = htmlentities($string, ENT_QUOTES, 'UTF-8');
preg_match_all('/(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)/', $string, $matches);//r_print($matches);
foreach($matches[1] as $mk => $mv){
$tmp_m = pagelayer_entity_check($matches[2][$mk]);
$string = str_replace($matches[1][$mk], $tmp_m, $string);
}
return $string;
}
function pagelayer_entity_check($string){
//Convert Hexadecimal to Decimal
$num = ((substr($string, 0, 1) === 'x') ? hexdec(substr($string, 1)) : (int) $string);
//Squares and Spaces - return nothing
$string = (($num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num < 0x20) ? '' : ''.$num.';');
return $string;
}
// Check if a checkbox is selected
function pagelayer_is_checked($post){
if(!empty($_POST[$post])){
return true;
}
return false;
}
// Reoort an error
function pagelayer_report_error($error = array()){
if(empty($error)){
return true;
}
$error_string = 'Please fix the below error(s) : ';
foreach($error as $ek => $ev){
$error_string .= '* '.$ev.' ';
}
echo '
'
. __pl($error_string)
. '
';
}
// Report a notice
function pagelayer_report_notice($notice = array()){
global $wp_version;
if(empty($notice)){
return true;
}
// Which class do we have to use ?
if(version_compare($wp_version, '3.8', '<')){
$notice_class = 'updated';
}else{
$notice_class = 'updated';
}
$notice_string = 'Please check the below notice(s) : ';
foreach($notice as $ek => $ev){
$notice_string .= '* '.$ev.' ';
}
echo '
';
endwhile;
}
return $data;
}
// Gets the site logo URLs
function pagelayer_site_logo(){
if(get_theme_mod('custom_logo')){
$logo_id = get_theme_mod('custom_logo');
return pagelayer_image($logo_id);
}
return NULL;
}
// Create select options
function pagelayer_create_sel_options( $opt_array , $selected = ''){
$options = '';
foreach($opt_array as $x => $val){
// Single item
if(is_string($opt_array[$x])){
$options .= pagelayer_sel_option($x, $val, $selected);
// Groups
}else{
// If Label is there, then its a normal option
if(array_key_exists('label', $opt_array[$x])){
$options .= pagelayer_sel_option($x, $opt_array[$x]['label'], $selected);
// Optgroups
} else{
$options .= '';
}
}
}
return $options;
}
// Create option HTML
function pagelayer_sel_option($val, $lang, $selected){
return '';
}
// Get values from multi-dimensional array by key
function pagelayer_multi_array_search(&$array, $key){
if(!is_array($array)){
return false;
}
foreach ($array as $k => $v) {
if($k == $key){
return $v;
}
if (is_array($v)) {
$found = pagelayer_multi_array_search($v, $key);
if(!empty($found)){
return $found;
}
}
}
return false;
}
function pagelayer_get_post_term(){
$args = [
'taxonomy' => array('category','post_tag'),
'hide_empty' => false,
];
$terms = get_terms( $args );
$ret = array();
foreach ( $terms as $term ) {
$ret[$term->taxonomy.':'.$term->slug.':'.$term->term_taxonomy_id] = $term->taxonomy .': '. $term->name;
}
//pagelayer_print($terms);die();
return $ret;
}
function pagelayer_get_post_author(){
$args = [
'who' => 'authors',
'fields' => [
'ID',
'display_name',
'user_nicename',
]
];
$authors = new \WP_User_Query( $args );
$ret = array();
foreach ( $authors->get_results() as $author ) {
$ret[$author->user_nicename.':'.$author->ID] = $author->display_name;
}
//pagelayer_print($authors->get_results());die();
return $ret;
}
// Gets the registered post types
function pagelayer_get_public_post_types( $args = [] ) {
global $pagelayer;
$post_type_args = [
'public' => true,
];
$post_type_args = wp_parse_args( $post_type_args, $args );
$_post_types = get_post_types( $post_type_args, 'objects' );
$post_types = array();
foreach ( $_post_types as $post_type => $object ) {
if($post_type == $pagelayer->builder['name']){
continue;
}
$post_types[ $post_type ] = $object->label;
}
//print_r($post_types);
return $post_types;
}
// Simply echo and dir
function pagelayer_json_output(&$done){
echo json_encode($done);
wp_die();
}
// Get the current query for render the product
function pagelayer_shortcode_current_query($query_args, $atts, $type){
global $wp_query;
if($type == 'pagelayer_current_query'){
if ( ! is_page( wc_get_page_id( 'shop' ) ) ) {
$query_args = $wp_query->query_vars;
}
add_action( "woocommerce_shortcode_before_{$type}_loop", function () {
wc_set_loop_prop( 'is_shortcode', false );
} );
if(!empty($atts['paginate'])){
$page = get_query_var( 'paged', 1 );
if( 1 < $page ) {
$query_args['paged'] = $page;
}
}
// Always query only IDs
$query_args['fields'] = 'ids';
}
return $query_args;
}
function pagelayer_export_content($content){
// Just call do_shortcode so we can get list of media files to export
//do_shortcode($content);
$theme_url = preg_replace('/http(s?):\/\//is', '', get_stylesheet_directory_uri());
$content = preg_replace('/http(s?):\/\/'.preg_quote($theme_url, '/').'/is', '{{theme_url}}', $content);
// Apply a filter
$content = apply_filters('pagelayer_export_content', $content);
return $content;
}
// Insert a post which is a Pagelayer Post
function pagelayer_insert_content($post, &$ret){
// Replace Vars
$template_vars = pagelayer_template_vars();
foreach($template_vars as $key => $val){
$post['post_content'] = str_replace($key, $val, $post['post_content']);
}
//pagelayer_print($post);die();
// Add slashes for safe insert
$post['post_content'] = wp_slash($post['post_content']);
// Now insert / update the post
$ret = wp_insert_post($post);
// Did we save the post ?
if(empty($ret) || is_wp_error($ret)){
return false;
}
// Convert to pagelayer accessed post
if(!add_post_meta($ret, 'pagelayer-data', time(), true)){
update_post_meta($ret, 'pagelayer-data', time());
}
return $ret;
}
// Gets the list of enabled fonts
function pagelayer_enabled_icons(){
$stored_icons = get_option('pagelayer_icons_set');
if(empty($stored_icons)){
update_option('pagelayer_icons_set', ['font-awesome5']);
$stored_icons = get_option('pagelayer_icons_set');
}
return $stored_icons;
}
// Install the Pro version
function pagelayer_install_pro(){
global $pagelayer;
// Include the necessary stuff
include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
// Includes necessary for Plugin_Upgrader and Plugin_Installer_Skin
include_once( ABSPATH . 'wp-admin/includes/file.php' );
include_once( ABSPATH . 'wp-admin/includes/misc.php' );
include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
// Filter to prevent the activate text
add_filter('install_plugin_complete_actions', 'pagelayer_install_plugin_complete_actions', 10, 3);
$upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( ) );
$installed = $upgrader->install(PAGELAYER_API.'download.php?version=latest&license='.$pagelayer->license['license']);
if ( !is_wp_error( $installed ) && $installed ) {
echo 'Activating Pagelayer Pro !';
$activate = activate_plugin(PAGELAYER_PRO_BASE);
if ( is_null($activate) ) {
echo '
'. __('Done! Pagelayer Pro is now installed and activated.', 'pagelayer'). '
';
echo '
Done! Pagelayer Pro is now installed and activated.';
}
}
return $installed;
}
// Prevent pro activate text for installer
function pagelayer_install_plugin_complete_actions($install_actions, $api, $plugin_file){
if($plugin_file == PAGELAYER_PRO_BASE){
return array();
}
return $install_actions;
}
// Load license data
function pagelayer_load_license(){
global $pagelayer;
// Load license
$pagelayer->license = get_option('pagelayer_license');
// Update license details as well
if(!empty($pagelayer->license) && (time() - @$pagelayer->license['last_update']) >= 86400){
$resp = wp_remote_get(PAGELAYER_API.'license.php?license='.$pagelayer->license['license']);
// Did we get a response ?
if(is_array($resp)){
$tosave = json_decode($resp['body'], true);
// Is it the license ?
if(!empty($tosave['license'])){
$tosave['last_update'] = time();
update_option('pagelayer_license', $tosave);
}
}
}
}