query_vars[KIRKI_CONTENT_MANAGER_PREFIX . '_child_post'])) { return $GLOBALS['wp']->query_vars[KIRKI_CONTENT_MANAGER_PREFIX . '_child_post']; } else if (isset($GLOBALS['wp']->query_vars[KIRKI_CONTENT_MANAGER_PREFIX . '_parent_post']) && false) {//disable content manager archive page logic return $GLOBALS['wp']->query_vars[KIRKI_CONTENT_MANAGER_PREFIX . '_parent_post']; } $post_id = get_the_ID(); $post_id = self::sanitize_text(isset($_GET['post_id']) ? $_GET['post_id'] : $post_id); $post_id = self::sanitize_text(isset($_POST['post_id']) ? $_POST['post_id'] : $post_id); $post_id = self::sanitize_text(isset($_GET['p']) ? $_GET['p'] : $post_id); return (int) $post_id; } /** * Get author/user id if possible from url */ public static function get_user_id_if_possible_from_url() { if (isset($GLOBALS['wp']->query_vars[KIRKI_CONTENT_MANAGER_PREFIX . '_user'])) { return $GLOBALS['wp']->query_vars[KIRKI_CONTENT_MANAGER_PREFIX . '_user']; } $user_id = ''; if (isset($GLOBALS['wp']->query_vars['author_name'])) { $user = get_user_by('slug', $GLOBALS['wp']->query_vars['author_name']); if ($user instanceof WP_User) { $user_id = $user->ID; } } else { $user_id = get_current_user_id(); } return (int) $user_id; } /** * Get all user roles by access levels * @param array $access_levels access levels => array(KIRKI_ACCESS_LEVELS['FULL_ACCESS'], KIRKI_ACCESS_LEVELS['CONTENT_ACCESS']); * * @return array roles => array ('editor', 'author'); */ public static function get_all_user_roles_by_access_levels($access_levels = array()) { $all_roles = RBAC::get_all_roles(); $roles_with_access = RBAC::get_access_level($all_roles); $filtered_roles = []; $filtered_roles = array_filter($roles_with_access, function ($level) use ($access_levels) { return in_array($level, $access_levels); }); return array_keys($filtered_roles); } /** * Get term or tag id if possible from url */ public static function get_term_id_if_possible_from_url() { if (isset($GLOBALS['wp']->query_vars[KIRKI_CONTENT_MANAGER_PREFIX . '_term'])) { return $GLOBALS['wp']->query_vars[KIRKI_CONTENT_MANAGER_PREFIX . '_term']; } // http://kirki.test/tag/wp-tag-2/ $term_id = get_queried_object_id(); $term = null; if ($term_id) { $term = get_term($term_id); } else if (isset($GLOBALS['wp']->query_vars['tag'])) { $term = get_term_by('slug', $GLOBALS['wp']->query_vars['tag'], 'post_tag'); } if ($term instanceof WP_Term) { $term_id = $term->term_id; } if (!$term_id) { // get all terms and return the first one $term = get_terms(array( 'taxonomy' => 'category', 'hide_empty' => false, 'number' => 1, )); $term_id = isset($term[0], $term[0]->term_id) ? $term[0]->term_id : 1; } return (int) $term_id; } /** * @deprecated * @see Kirki\App\Services\PageService::save_page_data() * @see \Kirki\App\Services\GlobalDataService::save_styles() */ public static function save_kirki_data_to_db($post_id, $page_data, $is_staging = false) { $version_where_saved = false; if ($is_staging) { $data = Staging::save_page_staging_data_to_db($post_id, $page_data); $page_data = $data['page_data']; $version_where_saved = $data['version']; } if (isset($page_data['styles'])) { $global_style_blocks_for_collaboration = []; foreach ($page_data['styles'] as $key => $sb) { if ((isset($sb['isDefault']) && $sb['isDefault'] === true) || (isset($sb['isGlobal']) && $sb['isGlobal'] === true)) { if (isset($sb['fromStage'])) { unset($page_data['styles'][$key]['fromStage']); $global_style_blocks_for_collaboration[$key] = $page_data['styles'][$key]; } else unset($page_data['styles'][$key]); } } if (count($global_style_blocks_for_collaboration) > 0) { $session_id = self::sanitize_text(isset($_REQUEST['session_id']) ? $_REQUEST['session_id'] : ''); if ($session_id && $is_staging === false) { // cause template import also called this method without session_id $data = array( 'type' => 'COLLABORATION_UPDATE_GLOBAL_STYLE', 'payload' => array('styleBlock' => $global_style_blocks_for_collaboration), ); Collaboration::save_action_to_db('global', 0, $data, 1, $session_id); } } self::update_page_styleblocks($post_id, $page_data['styles']); unset($page_data['styles']); } if (isset($page_data['usedStyles'])) { update_post_meta($post_id, KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS, $page_data['usedStyles']); unset($page_data['usedStyles']); } if (isset($page_data['usedStyleIdsRandom'])) { update_post_meta($post_id, KIRKI_META_NAME_FOR_USED_STYLE_BLOCK_IDS . '_random', $page_data['usedStyleIdsRandom']); unset($page_data['usedStyleIdsRandom']); } if (isset($page_data['usedFonts'])) { update_post_meta($post_id, KIRKI_META_NAME_FOR_USED_FONT_LIST, $page_data['usedFonts']); unset($page_data['usedFonts']); } if (isset($page_data['customFonts'])) { //save others data if isset. this is for template import $custom_fonts = self::get_global_data_using_key(KIRKI_USER_CUSTOM_FONTS_META_KEY); foreach ($page_data['customFonts'] as $key => $cf) { $custom_fonts[$key] = $cf; } self::update_global_data_using_key(KIRKI_USER_CUSTOM_FONTS_META_KEY, $custom_fonts); unset($page_data['customFonts']); } if (isset($page_data['viewportList'])) { //save others data if isset. this is for template import $controller_data = self::get_global_data_using_key(KIRKI_USER_CONTROLLER_META_KEY); if (!$controller_data) { $init = array( 'active' => 'md', 'defaults' => ["md", "tablet", "mobileLandscape", "mobile"], 'list' => $page_data['viewportList'], 'mdWidth' => 1200, "scale" => 1, "width" => 2484, "zoom" => 1 ); $controller_data = array('viewport' => $init); } else if (isset($controller_data['viewport'], $controller_data['viewport']['list'])) { $controller_data['viewport']['list'] = $page_data['viewportList']; } HelperFunctions::update_global_data_using_key(KIRKI_USER_CONTROLLER_META_KEY, $controller_data); unset($page_data['viewportList']); } if (isset($page_data['blocks'])) { update_post_meta($post_id, 'kirki', array('blocks' => $page_data['blocks'])); // $data = array( // 'type' => 'COLLABORATION_PAGE_DATA', // 'payload' => array( 'data' => $page_data['blocks'] ), // ); //Collaboration::save_action_to_db( 'post', $post_id, $data, 1 ); } update_post_meta($post_id, KIRKI_META_NAME_FOR_POST_EDITOR_MODE, 'kirki'); return $version_where_saved; } /** * */ function abc() { } /** * This function will return page style blocks from option meta and post meta * post meta for migration and option meta for global style block * * @param int $post_id post id. * @return object * * @todo: * * @deprecated * @see Kirki\App\Managers\PageManager::get_page_styleblocks() */ public static function get_page_styleblocks($post_id, $stage_version = false) { return FacadesPage::get_page_styleblocks($post_id, $stage_version); // $random_style_blocks = get_post_meta($post_id, KIRKI_GLOBAL_STYLE_BLOCK_META_KEY . '_random', true); // $global_style_blocks = self::get_global_data_using_key(KIRKI_GLOBAL_STYLE_BLOCK_META_KEY); // $random_style_blocks = self::fix_duplicate_class_name_from_random_sbs($random_style_blocks, $global_style_blocks); // $merged_style_blocks = array(); // if ($random_style_blocks) { // $merged_style_blocks = array_merge($merged_style_blocks, $random_style_blocks); // } // if ($global_style_blocks) { // $merged_style_blocks = array_merge($merged_style_blocks, $global_style_blocks); // } // $published_version = Staging::get_published_stage_version($post_id); // if ($published_version && $stage_version !== $published_version) { // $staging_style_blocks = array(); // $meta_key = Staging::get_staged_meta_name(KIRKI_GLOBAL_STYLE_BLOCK_META_KEY, $post_id, $stage_version); // $stage_style = get_post_meta($post_id, $meta_key, true); // if ($stage_style) // $staging_style_blocks = array_merge($staging_style_blocks, $stage_style); // $meta_key = $meta_key . '_random'; // $stage_style = get_post_meta($post_id, $meta_key, true); // if ($stage_style) // $staging_style_blocks = array_merge($staging_style_blocks, $stage_style); // if ($stage_version) // $merged_style_blocks = self::merge_style_blocks($merged_style_blocks, $staging_style_blocks); // else // $merged_style_blocks = self::merge_style_blocks($staging_style_blocks, $merged_style_blocks); // } // return $merged_style_blocks; } /** * @deprecated * @see Kirki\App\Managers\PageManager::merge_style_blocks() */ public static function merge_style_blocks($a, $b) { $names_in_a = []; foreach ($a as $val) { if (!empty($val['name']) && is_string($val['name'])) { $names_in_a[strtolower($val['name'])] = true; } } $names_in_b = []; foreach ($b as $val) { if (!empty($val['name']) && is_string($val['name'])) { $names_in_b[strtolower($val['name'])] = true; } } foreach ($b as $id_b => &$value_b) { if (empty($value_b['name']) || !is_string($value_b['name'])) { continue; } $b_name = strtolower($value_b['name']); // If same ID exists in A, remove it first (old behavior) if (isset($a[$id_b])) { unset($a[$id_b]); if (isset($names_in_a[$b_name])) { unset($names_in_a[$b_name]); } } // If name already exists in A, make it unique if (isset($names_in_a[$b_name])) { $i = 1; while (isset($names_in_a[$b_name . '_' . $i]) || isset($names_in_b[$b_name . '_' . $i])) { $i++; } $new_name = $value_b['name'] . '_' . $i; foreach ($b as &$v) { if (isset($v['name']) && is_array($v['name'])) { $v['name'] = array_map(fn($item) => $item === $value_b['name'] ? $new_name : $item, $v['name']); } } $value_b['name'] = $new_name; unset($names_in_b[$b_name]); $names_in_b[strtolower($new_name)] = true; } } unset($value_b); // Use array_merge to keep old semantics return array_merge($a, $b); } /** * @deprecated * @see Kirki\App\Managers\PageManager::resolve_duplicate_current_style_block_names() */ private static function fix_duplicate_class_name_from_random_sbs($random_style_blocks, $global_style_blocks) { $global_class_names = []; $random_class_names = []; if ($global_style_blocks) { foreach ($global_style_blocks as $key => $value) { if (isset($value['name']) && is_string($value['name'])) { $global_class_names[self::get_class_name_from_string($value['name'])] = true; } } } if ($random_style_blocks) { foreach ($random_style_blocks as $key => $value) { if (isset($value['name']) && is_string($value['name'])) { $random_class_names[self::get_class_name_from_string($value['name'])] = true; } } } $class_match = []; foreach ($random_class_names as $key => $value) { if (isset($global_class_names[$key])) { $class_match[$key] = true; } } $class_match = self::check_or_generate_new_class_names($class_match, $global_class_names, $random_class_names); if (count($class_match) > 0) { foreach ($random_style_blocks as $key => $value) { if (isset($value['name']) && is_string($value['name'])) { $class_name = self::get_class_name_from_string($value['name']); if (isset($class_match[$class_name])) { $random_style_blocks[$key]['name'] = $class_match[$class_name]; } } else if (isset($value['name']) && is_array($value['name'])) { //array foreach ($value['name'] as $key2 => $v2) { $class_name = self::get_class_name_from_string($v2); if (isset($class_match[$class_name])) { $random_style_blocks[$key]['name'][$key2] = $class_match[$class_name]; } } } } } return $random_style_blocks; } /** * @deprecated * @see Kirki\App\Managers\PageManager::make_duplicate_classes_to_unique() */ private static function check_or_generate_new_class_names($class_match, $global_class_names, $random_class_names) { foreach ($class_match as $key => $value) { $temp_class = $key; $found = true; while ($found) { if (isset($global_class_names[$temp_class]) || isset($random_class_names[$temp_class])) { $temp_class = $temp_class . '-copy'; } else { $found = false; } } $class_match[$key] = $temp_class; } return $class_match; } /** * @deprecated * @see Kirki\App\Managers\PageManager::normalize_style_block_name() */ public static function get_class_name_from_string($s) { $s = strtolower(str_replace(' ', '-', $s)); return $s; } public static function get_selector_from_sb_name($name) { if (!$name) return ''; $class_name = ''; if (is_string($name)) { $class_name = '.' . self::get_class_name_from_string($name); } else { foreach ($name as $key2 => $cn) { $class_name .= '.' . self::get_class_name_from_string($cn); } } return $class_name; } /** * This function will update page style blocks into option meta and post meta * post meta for migration and option meta for global style block * * @param int $post_id post id. * @param object $style_blocks styleblocks. * * @deprecated the method is not used anymore. handle separately in GlobalDataManager and PageManager */ public static function update_page_styleblocks($post_id, $style_blocks) { $prev_style_blocks = self::get_page_styleblocks($post_id); $style_blocks = array_merge($prev_style_blocks, $style_blocks); $global_style_blocks = array(); foreach ($style_blocks as $key => $sb) { if ((isset($sb['isDefault']) && $sb['isDefault'] === true) || (isset($sb['isGlobal']) && $sb['isGlobal'] === true)) { $global_style_blocks[$sb['id']] = $sb; unset($style_blocks[$key]); } } self::save_global_style_blocks($global_style_blocks); self::save_random_style_blocks($post_id, $style_blocks); } /** * Save global style blocks in option table. Also save collaboration data. * * @param array $style //take styleblocs if isDefault and isGlobal key is true. * @return void * @deprecated * @see Kirki\App\Managers\GlobalDataManager::update_deprecated_global_style_blocks() */ public static function save_global_style_blocks($style) { // $session_id = self::sanitize_text(isset($_REQUEST['session_id']) ? $_REQUEST['session_id'] : ''); self::update_global_data_using_key(KIRKI_GLOBAL_STYLE_BLOCK_META_KEY, $style); // if($session_id){ // // cause template import also called this method without session_id // $data = array( // 'type' => 'COLLABORATION_UPDATE_GLOBAL_STYLE', // 'payload' => array( 'styleBlock' => $style ), // ); // Collaboration::save_action_to_db( 'global', 0, $data, 1, $session_id); // } } /** * Save post related random style blocks in option table. Also save collaboration data. * * @param array $post_id //current post id. * @param array $style //take styleblocs if not isDefault and isGlobal key is true. * @return void * @deprecated * @see Kirki\App\Managers\PageManager::save_style_blocks() */ public static function save_random_style_blocks($post_id, $style) { update_post_meta($post_id, KIRKI_GLOBAL_STYLE_BLOCK_META_KEY . '_random', $style); // $data = array( // 'type' => 'COLLABORATION_UPDATE_GLOBAL_STYLE', // 'payload' => array( 'styleBlock' => $style ), // ); //Collaboration::save_action_to_db( 'post', $post_id, $data, 1 ); } /** * Save staged style blocks for a specific post and staged meta key. * * @param int $post_id Post ID. * @param string $meta_key Staged meta key (can be normal or random). * @param array $styles Styles array to save. * * @deprecated * @see \Kirki\App\Managers\PageManager::save_deprecated_global_style_blocks() * @see \Kirki\App\Managers\PageManager::save_style_blocks() */ public static function save_staged_style_blocks($post_id, $meta_key, $styles) { update_post_meta($post_id, $meta_key, $styles); // $data = array( // 'type' => 'COLLABORATION_UPDATE_GLOBAL_STYLE', // 'payload' => array('styleBlock' => $styles), // ); // Collaboration::save_action_to_db('post', $post_id, $data, 1); } /** * No module import this method right now * * @return array */ public static function get_custom_code_block_element() { return array( 'name' => 'custom-code', 'title' => 'Code', 'visibility' => true, 'properties' => array( 'tag' => 'div', 'content' => '', 'data-type' => 'code', ), 'styleIds' => array(), 'className' => '', 'id' => 'kirki' . uniqid(), 'parentId' => 'body', ); } /** * Get current editor mode is kirki or others * * @param int $post_id post id. * @return bool true if kirki. * @deprecated * @see Kirki\App\Managers\PageManager::is_kirki_editor_mode() */ public static function is_editor_mode_is_kirki($post_id) { $editor_mode = get_post_meta($post_id, KIRKI_META_NAME_FOR_POST_EDITOR_MODE, true); if ('kirki' === $editor_mode) { return true; } return false; } /** * Get post url arr from post id * preview_url, iframe_url, post_url * * @param int $post_id post id. * @return array * * @deprecated * @see \Kirki\App\Supports\PageUrl::class */ public static function get_post_url_arr_from_post_id($post_id, $options = array()) { $post_perma_link = self::get_page_perma_url($post_id); $obj = ['post_url' => $post_perma_link, 'post_id' => $post_id]; if (isset($options['ajax_url']) && $options['ajax_url']) { $protocol = strpos(home_url(), 'https://') !== false ? 'https' : 'http'; $obj['ajax_url'] = admin_url('admin-ajax.php', $protocol); } if (isset($options['preview_url']) && $options['preview_url']) { $preview_url = self::get_page_preview_url($post_id); $obj['preview_url'] = $preview_url; } if (isset($options['editor_url']) && $options['editor_url']) { $obj['editor_url'] = add_query_arg( array( 'action' => KIRKI_EDITOR_ACTION, ), $post_perma_link ); if (HelperFunctions::is_api_call_from_editor_preview() && HelperFunctions::is_api_header_post_editor_preview_token_valid()) { $headers = self::getallheaders(); $obj['editor_url'] = add_query_arg( array( 'editor-preview-token' => isset($headers['Editor-Preview-Token']) ? $headers['Editor-Preview-Token'] : null, ), $obj['editor_url'] ); } } if (isset($options['iframe_url']) && $options['iframe_url']) { $iframe_url_params = array( 'action' => KIRKI_EDITOR_ACTION, 'load_for' => 'kirki-iframe', 'post_id' => $post_id, ); if (isset($_GET['editor-preview-token'])) { $iframe_url_params['editor-preview-token'] = self::sanitize_text($_GET['editor-preview-token']); } $obj['iframe_url'] = add_query_arg( $iframe_url_params, $post_perma_link ); } if (isset($options['nonce']) && $options['nonce']) { $obj['nonce'] = wp_create_nonce('wp_rest'); } if (isset($options['editor_preview_token']) && $options['editor_preview_token']) { $obj['editor_preview_token'] = isset($_GET['editor-preview-token']) ? self::sanitize_text($_GET['editor-preview-token']) : false; } if (isset($options['site_url']) && $options['site_url']) { $obj['site_url'] = get_site_url(); } if (isset($options['admin_url']) && $options['admin_url']) { $obj['admin_url'] = get_admin_url(); } if (isset($options['core_plugin_url']) && $options['core_plugin_url']) { $obj['core_plugin_url'] = KIRKI_CORE_PLUGIN_URL; } if (isset($options['rest_url']) && $options['rest_url']) { try { $rest_url = rest_url(); $obj['rest_url'] = $rest_url; } catch (\Throwable $th) { $obj['rest_url'] = ''; } } return $obj; } /** * @deprecated * @see \Kirki\App\Supports\PageUrl::get_preview_url() */ private static function get_page_preview_url($post_id) { $post = get_post($post_id); if ($post && $post->post_type === 'kirki_template') { $conditions = get_post_meta($post->ID, 'kirki_template_conditions', true); $d = self::get_collection_items_from_conditions($conditions); if ($d['type'] === 'post' && count($d['data']) > 0) { return self::get_page_perma_url($d['data'][0]['ID']); } if ($d['type'] === 'user' && count($d['data']) > 0) { return get_author_posts_url($d['data'][0]['ID']); } if ($d['type'] === 'term' && count($d['data']) > 0) { return get_term_link($d['data'][0]['ID']); } } return self::get_page_perma_url($post_id); } /** * @deprecated * @see \Kirki\App\Supports\PageUrl::get_page_permalink() */ private static function get_page_perma_url($post_id) { $post_perma_link = get_permalink($post_id); $protocol = strpos(home_url(), 'https://') !== false ? 'https' : 'http'; if ($protocol === 'https') { $post_perma_link = str_replace('http://', 'https://', $post_perma_link); } else { $post_perma_link = str_replace('https://', 'http://', $post_perma_link); } return $post_perma_link; } /** * @deprecated * @see \Kirki\App\Supports\CollectionItem::get_items_from_condition() */ public static function get_collection_items_from_conditions($conditions, $query = '') { $data = []; $type = 'post'; $post_type = 'post'; $role = ''; if (is_array($conditions) && count($conditions) > 0) { if (isset($conditions[0]['type'])) { $type = $conditions[0]['type']; if ($type === 'post') { $post_type = $conditions[0]['post_type']; // if the conditions has from key then it is term. then it will be term type. if (isset($conditions[0]['from']) && $conditions[0]['from'] === 'term') { $type = 'term'; } } if ($type === 'user') { $role = $conditions[0]['to']; } } else { //legacy support $post_type = $conditions[0]['category']; } } $numberposts = 20; $post_status = array('publish', 'draft', 'future'); if ($type === 'post' && HelperFunctions::user_has_post_edit_access()) { // type will be wordpress post type $arg = array( 'post_type' => $post_type, 'post_status' => $post_status, 'numberposts' => $numberposts, 'orderby' => 'ID', 'order' => 'DESC', ); if ($query) { $arg['s'] = $query; } $posts = get_posts($arg); foreach ($posts as $key => $post) { $data[] = array( 'ID' => $post->ID, 'title' => $post->post_title, ); } } if ($type === 'user' && HelperFunctions::user_has_post_edit_access()) { // get all users $arg = array( 'role' => $role === '*' ? '' : $role, 'number' => $numberposts, 'orderby' => 'ID', 'order' => 'DESC', ); if ($query) { $arg['search'] = '*' . $query . '*'; } $users = get_users($arg); foreach ($users as $key => $user) { $data[] = array( 'ID' => $user->ID, 'title' => $user->display_name, ); } } if ($type === 'term' && HelperFunctions::user_has_post_edit_access()) { // conditions $taxonomy = []; foreach ($conditions as $key => $condition) { if ($condition['from'] === 'term') { $taxonomy[] = $condition['where']; } } $arg = array( 'taxonomy' => $taxonomy, 'number' => $numberposts, 'orderby' => 'ID', 'order' => 'DESC', ); if ($query) { $arg['search'] = $query; } $terms = get_terms($arg); if (!is_wp_error($terms) && is_array($terms)) { foreach ($terms as $term) { // Handle both object and array cases safely $term_id = is_object($term) ? $term->term_id : ($term['term_id'] ?? null); $term_name = is_object($term) ? $term->name : ($term['name'] ?? null); $data[] = [ 'ID' => $term_id, 'title' => $term_name, ]; } } } return ['data' => $data, 'type' => $type]; } /** * Get post content from content value. like => id, title, description, content * * @param string $content_value for post dynamic content. * @param object $post post object. * * @return string */ public static function get_post_dynamic_content($content_value, $post = null, $meta_name = '', $dynamic_options = []) { if (isset($post) && !empty($post)) { $post_id = $post->ID; } if (empty($post_id)) { $post_id = self::get_post_id_if_possible_from_url(); } $content = null; switch ($content_value) { case 'post_id': { $content = $post_id; break; } case 'post_title': { $content = get_the_title($post_id); break; } case 'post_excerpt': { // Excerpt length global variable is only for kirki editor but not for the frontend. if (isset($GLOBALS['kirki_post_excerpt_length']) && $GLOBALS['kirki_post_excerpt_length'] > 0) { $content = wp_trim_words(get_the_excerpt($post_id), $GLOBALS['kirki_post_excerpt_length'], '[...]'); } else { $content = get_the_excerpt($post_id); } break; } case 'post_author': { $post = get_post($post_id); if (!$post) return ""; $content = get_the_author_meta('display_name', $post->post_author); break; } case 'post_date': { $content = get_the_date('', $post_id); if (isset($dynamic_options['format'])) { $content = HelperFunctions::format_date($content, $dynamic_options['format']); } break; } case 'post_time': { $content = get_the_time('', $post_id); if (isset($dynamic_options['timeFormat'])) { $content = HelperFunctions::convert_time_format($content, $dynamic_options['timeFormat']); } break; } case 'post_content': { $content = self::retrieve_post_content($post_id); break; } case 'post_status': { $content = get_post_status($post_id); break; } case 'featured_image': { $content = array( 'wp_attachment_id' => get_post_thumbnail_id($post_id), 'src' => get_the_post_thumbnail_url($post_id) ); break; } case 'site_name': { $content = get_bloginfo('name'); break; } case 'site_description': { $content = get_bloginfo('description'); break; } case 'site_url': { $content = get_site_url(); break; } case 'site_logo': { $content = ''; // Try site icon first $site_icon_id = get_option('site_icon'); if ($site_icon_id) { $content = wp_get_attachment_url($site_icon_id); } // If no site icon, try get_site_icon_url() if (!$content) { $content = get_site_icon_url(512); } // If still nothing, try custom logo if (!$content) { $custom_logo_id = get_theme_mod('custom_logo'); if ($custom_logo_id) { $imgs = wp_get_attachment_image_src($custom_logo_id, 'full'); if ($imgs && isset($imgs[0])) { $content = $imgs[0]; } } } break; } case 'author_profile_picture': { $post = get_post($post_id); if (!empty($post)) { $post_author = $post->post_author; $content = get_avatar_url($post_author); } else { $content = 'Something wrong!'; } break; } case 'user_profile_picture': { $content = get_avatar_url(get_current_user_id()); break; } case 'post_page_link': { $url = \get_permalink($post_id); $content = !empty($url) ? $url : ''; break; } case 'author_posts_page_link': { $post = \get_post($post_id); $url = \get_author_posts_url($post->post_author); $content = !empty($url) ? $url : ''; break; } case 'post_meta': { if (!empty($meta_name)) { $meta = get_post_meta($post_id, $meta_name, true); // For now, only string is supported! if (is_string($meta)) { // Post meta is arbitrary, low-privilege-authored data: always escape. $content = self::escape_post_meta_value($meta); } } break; } default: { $post = get_post($post_id); if (isset($post) && 0 !== strpos(KIRKI_CONTENT_MANAGER_PREFIX, $post->post_type)) { $meta_key = ContentManagerHelper::get_child_post_meta_key_using_field_id($post->post_parent, $content_value); $content = get_post_meta($post->ID, $meta_key, true); $fields = ContentManagerHelper::get_post_type_custom_field_keys($post->post_parent); if (!$content && isset($fields[$content_value], $fields[$content_value]['default_value'])) { $content = $fields[$content_value]['default_value']; } if (isset($fields[$content_value]) && $fields[$content_value]['type'] === 'date') { $content = self::format_date($content, $fields[$content_value]['default_format']); //TODO: need to check with editor format } if (isset($fields[$content_value]['type']) && $fields[$content_value]['type'] === 'image') { $content = array( 'wp_attachment_id' => $content['id'] ?? '', 'src' => $content['url'] ?? '', ); } if (isset($fields[$content_value]['type']) && $fields[$content_value]['type'] === 'time') { $time = ""; if (isset($fields[$content_value], $fields[$content_value]['default_value']) && $fields[$content_value]['default_value']) { $default_time = $fields[$content_value]['default_value']; $value = isset($default_time['value']) ? $default_time['value'] : '00:00'; $unit = isset($default_time['unit']) ? strtolower($default_time['unit']) : 'am'; $time = $value . ' ' . $unit; } $content = $content ? $content['value'] . ' ' . $content['unit'] : $time; if (isset($dynamic_options['timeFormat']) && $dynamic_options['timeFormat']) { $content = HelperFunctions::convert_time_format($content, $dynamic_options['timeFormat']); } } } else { $content = 'Not Implemented'; } break; } } return $content ? $content : ''; } /** * The function `get_user_dynamic_content` retrieves specific dynamic content related to a user based * on the provided content value. */ public static function get_user_dynamic_content($content_value, $user_id = null, $meta_name = '', $dynamic_options = []) { $content = ''; // Get the user by ID $user = get_user_by('id', $user_id); // fall back to the current user if (empty($user)) { $user = get_user_by('id', get_current_user_id()); } if (empty($user)) { return $content; } // Switch statement to handle dynamic content based on the content_value switch ($content_value) { case 'display_name': return $user->display_name; case 'user_email': return $user->user_email; case 'user_nicename': return $user->user_nicename; case 'registered_date': { $date = $user->user_registered; if (isset($dynamic_options['format'])) { return HelperFunctions::format_date($date, $dynamic_options['format']); } $date = new DateTime($date); return $date->format('F j, Y'); } case 'registered_time': { $date = new DateTime($user->user_registered); return $date->format('H:i:a'); } case 'user_url': return get_author_posts_url($user->ID); case 'profile_image': return get_avatar_url($user->ID); case 'user_meta': { if (!empty($meta_name)) { $meta = get_user_meta($user->ID, $meta_name, true); if (is_string($meta)) { return $meta; } return ''; } } case 'initials': { $first_name = isset($user->first_name) ? $user->first_name : ''; $last_name = isset($user->last_name) ? $user->last_name : ''; $initials = ''; if ($first_name) { $initials .= strtoupper(substr($first_name, 0, 1)); } if ($last_name) { $initials .= strtoupper(substr($last_name, 0, 1)); } // if initials are lenght 1 or empty, then the first two letters of the username if (empty($initials) || strlen($initials) < 2) { $username = isset($user->user_login) ? $user->user_login : ''; $initials = strtoupper(substr($username, 0, 2)); } return $initials; } default: return ''; } } private static function get_value($data, $key) { if (is_array($data)) { return $data[$key] ?? ''; } if (is_object($data)) { return $data->$key ?? ''; } return ''; } public static function get_term_dynamic_content($content_value, $term_id = null, $meta_name = '') { $term = get_term($term_id); if (empty($term)) { return ''; } switch ($content_value) { case 'name': return self::get_value($term, 'name'); case 'description': return self::get_value($term, 'description'); case 'slug': return self::get_value($term, 'slug'); case 'count': return self::get_value($term, 'count'); case 'meta': if (!empty($meta_name)) { $term_id = self::get_value($term, 'term_id'); $meta = get_term_meta($term_id, $meta_name, true); return is_scalar($meta) ? (string) $meta : ''; } return ''; default: return ''; } } public static function retrieve_post_content($post_id) { $content = apply_filters('the_content', get_the_content(null, false, $post_id)); $load_for = HelperFunctions::sanitize_text(isset($_GET['load_for']) ? $_GET['load_for'] : ''); if ($load_for !== 'kirki-iframe' && $kirki_data = HelperFunctions::is_kirki_type_data($post_id)) { $params = array( 'blocks' => $kirki_data['blocks'], 'style_blocks' => $kirki_data['styles'], 'root' => 'root', 'post_id' => $post_id, ); $content = apply_filters('the_content', HelperFunctions::get_html_using_preview_script($params)); } return $content; } /** * This methos is for if Theme enque some style and css then it will remove those styel and css codes. * * @return void */ public static function remove_theme_style() { $theme = wp_get_theme(); $parent_style = $theme->stylesheet . '-style'; wp_dequeue_style($parent_style); wp_deregister_style($parent_style); wp_dequeue_style($parent_style . '-css'); wp_deregister_style($parent_style . '-css'); } public static function dequeue_all_except_my_plugin() { global $wp_scripts, $wp_styles, $kirki_editor_assets; foreach ($wp_scripts->queue as $handle) { // Keep WordPress media scripts if ( str_starts_with($handle, 'media') || str_starts_with($handle, 'wp-media') || in_array($handle, ['underscore', 'backbone', 'jquery', 'wp-util']) ) { continue; } wp_dequeue_script($handle); } foreach ($wp_styles->queue as $handle) { // Keep media related styles if ( str_starts_with($handle, 'media') || in_array($handle, ['buttons', 'dashicons', 'imgareaselect']) ) { continue; } wp_dequeue_style($handle); } if (!empty($kirki_editor_assets['scripts'])) { foreach ($kirki_editor_assets['scripts'] as $script_handle) { wp_enqueue_script($script_handle); } } if (!empty($kirki_editor_assets['styles'])) { foreach ($kirki_editor_assets['styles'] as $style_handle) { wp_enqueue_style($style_handle); } } } /** * Generate html using Preview script. * * @param array $data blocks. * @param array $style_blocks style_blocks. * @param string $root data root. * @param int $id if need prefix like symbol or popup post_id. * @return string the html string. */ public static function get_html_using_preview_script(array $params = []) { $blocks = $params['blocks'] ?? null; $style_blocks = $params['style_blocks'] ?? null; $root = $params['root'] ?? null; $options = $params['options'] ?? []; $post_id = $params['post_id'] ?? null; $get_style = $params['get_style'] ?? true; $get_variable = $params['get_variable'] ?? true; $get_fonts = $params['get_fonts'] ?? true; $should_take_app_script = $params['should_take_app_script'] ?? true; $prefix = $params['prefix'] ?? false; $get_all_style_forcefully_if_get_style_true = $params['get_all_style_forcefully_if_get_style_true'] ?? false; if ($blocks) { $options['search_related_collection_ids'] = isset($params['search_related_collection_ids']) ? $params['search_related_collection_ids'] : self::collect_search_related_collection_ids($blocks); } //set initial context data start if (!isset($options['post'])) { $post = get_post(get_the_ID()); if ($post) { $options['post'] = $post; $options['itemType'] = 'post'; } } if (!isset($options['user'])) { $user = get_user_by('id', get_current_user_id()); if ($user) { $options['user'] = Users::get_format_single_user_data($user); } } //set initial context data end $preview = new Preview($blocks, $style_blocks, $root, $post_id, $prefix); $html = $preview->getHtml($options);// this method need to call first cause after that only used style block array construct. $only_used_style_blocks = $get_all_style_forcefully_if_get_style_true ? $style_blocks : $preview->get_only_used_style_blocks(); $s = ''; if ($get_fonts) { $s .= $preview->getCustomFontsLinks(); } if ($get_style) { //style will be false when it calls from collection single item. only first item will generate style. others item will be same. $s .= $preview->getStyleTag($only_used_style_blocks); } $s .= $preview->get_interaction_set_as_initial_css(); $s .= $html; $s .= $preview->getScriptTag($should_take_app_script); $s = self::decode_entities_without_creating_markup($s); return $s; } /** * Decode HTML entities in already-rendered page markup without ever turning * escaped text back into live tags. * * The rendered document mixes trusted markup (elements the builder emitted, * including admin authored custom code) with escaped text nodes. A blanket * html_entity_decode() over that mix undoes the escaping applied while * rendering, so `<script>` stored in any text value — a visitor's * comment, for instance — becomes an executable `'; return $s; } /** * Check kirki and kirki pro is active or not * * @param string $plugin_main_file plugin main PHP file name. * @return boolean */ public static function is_plugin_activate($plugin_main_file) { if (in_array($plugin_main_file, apply_filters('active_plugins', get_option('active_plugins')), true)) { // plugin is activated. return true; } return false; } /** * This function will verify nonce * ACT like API calls auth middleware * * @param string $action ajax action name. * * @return void */ public static function verify_nonce($action = -1) { //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $headers = static::getallheaders(); $header_nonce = isset($headers['X-Wp-Nonce']) ? static::sanitize_text($headers['X-Wp-Nonce']) : ''; $nonce = $header_nonce ? $header_nonce : static::sanitize_text(isset($_GET['_wpnonce']) ? $_GET['_wpnonce'] : ''); if (!wp_verify_nonce($nonce, $action)) { wp_send_json_error('Not authorized'); exit; } } /** * Unslash and sanitize text * * @param string $v text. * @return string sanitized text. */ public static function sanitize_text($v) { return sanitize_text_field(wp_unslash($v)); } /** * Get current WordPress session ID. * This method generates a unique session ID if none exists. * * @deprecated * @see \Kirki\App\Supports\Session::get_session_id() * * @return string Session ID. */ public static function get_session_id() { // First, check if a session ID is already stored in the static variable. if (self::$global_session_id) { return self::$global_session_id; } // Check if a session ID exists in a cookie. if (isset($_COOKIE['kirki_session_id'])) { self::$global_session_id = sanitize_text_field($_COOKIE['kirki_session_id']); return self::$global_session_id; } // Generate a new session ID. self::$global_session_id = wp_generate_uuid4(); // Set the session ID in a cookie. setcookie('kirki_session_id', self::$global_session_id, time() + (DAY_IN_SECONDS * 7), COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true); return self::$global_session_id; } /** * Get session data by key using WordPress transients. * * @deprecated * @see \Kirki\App\Supports\Session::get() * * @param string $key The key of the session data to retrieve. * @return mixed|null The session data if found, null otherwise. */ public static function get_session_data($key) { // Get the current session ID. $session_id = self::get_session_id(); // Retrieve the session data. $session_data = get_transient('kirki_session_' . $session_id); if (isset($session_data[$key])) { return $session_data[$key]; } return null; } /** * Add or update session data using WordPress transients. * * @deprecated * @see \Kirki\App\Supports\Session::put() * * @param string $key The key of the session data. * @param mixed $value The value of the session data. * @return void */ public static function set_session_data($key, $value) { // Get the current session ID. $session_id = self::get_session_id(); // Retrieve existing session data. $session_data = get_transient('kirki_session_' . $session_id) ?: array(); // Update the session data. $session_data[$key] = $value; // Save the updated session data with a 24-hour expiration time. set_transient('kirki_session_' . $session_id, $session_data, DAY_IN_SECONDS); } /** * Delete session data by key using WordPress transients. * * @deprecated * @see \Kirki\App\Supports\Session::forget() * * @param string $key The key of the session data to delete. * @return void */ public static function delete_session_data($key) { // Get the current session ID. $session_id = self::get_session_id(); // Retrieve existing session data. $session_data = get_transient('kirki_session_' . $session_id); if (isset($session_data[$key])) { unset($session_data[$key]); // Save the updated session data or delete the transient if empty. if (!empty($session_data)) { set_transient('kirki_session_' . $session_id, $session_data, DAY_IN_SECONDS); } else { delete_transient('kirki_session_' . $session_id); } } } /** * Escape a post meta value for safe output. * * The front-end content pipeline (TheFrontend::replace_content) applies a * single html_entity_decode() pass after core has processed shortcodes, * which would undo a plain esc_html(). Re-encoding the ampersands (with * double_encode enabled) yields a single-escaped value in the final output * while still neutralizing markup authored by low-privileged users. * * @param mixed $value The raw post meta value. * @return string Escaped value. */ public static function escape_post_meta_value($value) { if (!is_scalar($value)) { return ''; } return htmlspecialchars(esc_html((string) $value), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', true); } /** * Is Pro user checking function. * * @return bool */ public static function is_pro_user() { $common_data = WpAdmin::get_common_data(true); $bool = isset($common_data['license_key']['valid']) && boolval($common_data['license_key']['valid']) === true; return $bool; } /** * Get all view port lists * * @return string viewports list variable in script markup. */ public static function get_view_port_lists() { $s = ''; $list = UserData::get_view_port_list(); if ($list) { $s .= "'; } return $s; } /** * Get all css variables * * @return string variables in script markup. */ public static function get_kirki_css_variables_data() { $s = ''; $variableData = UserData::get_kirki_variable_data(); if ($variableData) { $s .= "'; } return $s; } /** * Get smooth scroll script * * @return string script markup. */ public static function get_smooth_scroll_script() { $common_data = WpAdmin::get_common_data(true); $smooth_scroll_enabled = isset($common_data['smooth_scroll'], $common_data['smooth_scroll']['enabled']) ? $common_data['smooth_scroll']['enabled'] : false; $smooth_scroll_value = isset($common_data['smooth_scroll'], $common_data['smooth_scroll']['value']) ? $common_data['smooth_scroll']['value'] : 1; /** * User value 1 to 200 * * Min duration 1s * Max duration 12s * */ $duration = ceil(($smooth_scroll_value / 200) * 12); $s = ''; if ($smooth_scroll_enabled) { $s .= "'; } return $s; } /** * Format the date with date format * * @return string */ public static function format_date($date, $format) { if ($date && $format) { $date_formats_arr = [ 'DD/MM/YYYY' => 'd/m/Y', 'DD-MM-YYYY' => 'd-m-Y', 'DD.MM.YYYY' => 'd.m.Y', 'MM/DD/YYYY' => 'm/d/Y', 'MM-DD-YYYY' => 'm-d-Y', 'MM.DD.YYYY' => 'm.d.Y', 'MMMM DD, YYYY' => 'F j, Y', 'MMM DD, YYYY' => 'M j, Y', 'YYYY-MM-DD' => 'Y-m-d', 'YYYY/MM/DD' => 'Y/m/d', 'YY.MM.DD' => 'y.m.d', 'YY/MM/DD' => 'y/m/d', 'YY-MM-DD' => 'y-m-d', ]; $timestamp = strtotime($date); if ($timestamp === false) { return $date; // fallback (avoid fatal) } $datetime = (new \DateTime())->setTimestamp($timestamp); return $datetime->format($date_formats_arr[$format] ?? $format); } return $date; } /** * Format the time with time format * * @return string */ public static function convert_time_format($timeString, $format = 'h:i a') { $dateTime = null; try { $dateTime = new \DateTime($timeString); if ($dateTime && $timeString) { return $dateTime->format($format); } } catch (\Exception $e) { // if timeString is an invalid time format $parts = explode(' ', $timeString); if (count($parts) > 1) { // Remove the last part (am/pm) $time_value = $parts[0]; try { $dateTime = new \DateTime($time_value); if ($dateTime && $timeString) { return $dateTime->format($format); } } catch (\Exception $e2) { return $timeString; } } return $timeString; } return $timeString; } /** * Get single post if has a kirki type post * * @return object|bool */ public static function get_last_edited_kirki_editor_type_page() { $args = array( 'post_type' => 'page', // Change to 'post' if you want to search for posts 'post_status' => ['publish', 'draft'], 'numberposts' => 1, // Number of results to retrieve (change as needed) 'meta_key' => 'kirki_editor_mode', 'meta_value' => 'kirki', 'orderby' => 'modified', // Order by post date 'order' => 'DESC', // Sort in descending order ); $pages = get_posts($args); if (count($pages) > 0) { return $pages[0]; } return false; } public static function get_kirki_version_from_db() { $version = wp_cache_get('kirki_version', 'kirki'); if (false === $version) { $version = get_option('kirki_version', ''); if (!empty($version)) { wp_cache_set('kirki_version', $version, 'kirki'); } } return $version; } public static function set_kirki_version_in_db() { $version = self::get_kirki_version_from_db(); if ($version && version_compare($version, KIRKI_VERSION, '==')) { // No need to update the version if it's already equal to the current version. return; } update_option('kirki_version', KIRKI_VERSION, false); wp_cache_set('kirki_version', KIRKI_VERSION, 'kirki'); } public static function accepted_file_types_by_plugin($accepted_media_types = KIRKI_SUPPORTED_MEDIA_TYPES) { $result = array(); foreach ($accepted_media_types as $value) { if (is_array($value)) { $result = array_merge($result, self::accepted_file_types_by_plugin($value)); } else { $result[] = $value; } } return $result; } public static function content_manager_link_filter($dynamic_content = array(), $href = "#") { $current_post = get_post(self::get_post_id_if_possible_from_url()); if ($current_post->post_type === KIRKI_CONTENT_MANAGER_PREFIX) { $fields = ContentManagerHelper::get_post_type_custom_field_keys($current_post->post_parent); if (isset($fields[$dynamic_content['value']]) && is_array($fields[$dynamic_content['value']])) { if ('email' === $fields[$dynamic_content['value']]['type']) { $href = "mailto:$href"; } else if ('phone' === $fields[$dynamic_content['value']]['type']) { $href = "tel:$href"; } } } return $href; } public static function check_string_has_this_tags($string, $tag) { // Check if the string contains either a
tag or an