<?php
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
use JVBase\utility\Features;
|
|
function jvbGlobalDirectories():array
|
{
|
$directories = get_option(BASE.'_global_directories');
|
if (JVB_TESTING) {
|
$directories = false;
|
}
|
if ($directories === false) {
|
$directories = array_merge(
|
array_fill_keys(array_keys(array_filter(JVB_CONTENT, function ($content) {
|
return jvbCheck('show_directory', $content);
|
})), 'content'),
|
array_fill_keys(array_keys(array_filter(JVB_TAXONOMY, function ($taxonomy) {
|
return jvbCheck('show_directory', $taxonomy);
|
})), 'tax')
|
);
|
// update_option(BASE.'_global_directories', $directories);
|
}
|
return $directories;
|
}
|
|
function jvbGlobalKarma():array
|
{
|
$karma = get_option(BASE.'_global_karma');
|
if (JVB_TESTING) {
|
$karma = false;
|
}
|
if ($karma === false) {
|
$karma = [
|
'content' => array_keys(array_filter(JVB_CONTENT, function ($content) {
|
return isset($content['karma']) && $content['karma'] === true;
|
})),
|
'taxonomy' => array_keys(array_filter(JVB_TAXONOMY, function ($taxonomy) {
|
return isset($taxonomy['karma']) && $taxonomy['karma'] === true;
|
}))
|
];
|
// update_option(BASE.'_global_karma');
|
}
|
return $karma;
|
}
|
|
function jvbGlobalTaxonomyFor():array
|
{
|
$taxonomy_for = get_option(BASE.'_global_taxonomy_for');
|
if (JVB_TESTING) {
|
$taxonomy_for = false;
|
}
|
if ($taxonomy_for === false) {
|
$taxonomy_for = [];
|
foreach (JVB_TAXONOMY as $tax => $config) {
|
$taxonomy_for[$tax] = $config['for_content'];
|
}
|
// update_option(BASE.'_global_taxonomy_for', $taxonomy_for);
|
}
|
return $taxonomy_for;
|
}
|
|
function jvbGlobalFeedContent():array {
|
$content = get_option(BASE.'_global_feed_content');
|
if (JVB_TESTING) {
|
$content = false;
|
}
|
if (!$content) {
|
$taxonomy = array_fill_keys(array_keys(array_filter(JVB_TAXONOMY, function ($tax) {
|
return jvbCheck('is_content', $tax);
|
})), 'tax');
|
$content = array_fill_keys(array_keys(array_filter(JVB_CONTENT, function ($c) {
|
return jvbCheck('show_feed', $c);
|
})), 'content');
|
$content = array_merge($content, $taxonomy);
|
update_option(BASE.'_global_feed_content', $content);
|
}
|
return $content;
|
}
|
|
function jvbBasedFeedContent():array
|
{
|
$content = get_option(BASE.'_based_feed_content');
|
if (!$content) {
|
$content = array_map(function ($c) {
|
return jvbCheckBase($c);
|
}, jvbGlobalFeedContent());
|
update_option(BASE.'based_feed_content', $content);
|
}
|
return $content;
|
}
|
|
function jvbGlobalResponses():array
|
{
|
$responses = get_option(BASE.'_global_responses');
|
if (JVB_TESTING) {
|
$responses = false;
|
}
|
if ($responses === false) {
|
$responses = [
|
'content' => array_keys(array_filter(JVB_CONTENT, function ($content) {
|
return isset($content['responses']) && $content['responses'] === true;
|
})),
|
'taxonomy' => array_keys(array_filter(JVB_TAXONOMY, function ($taxonomy) {
|
return isset($taxonomy['responses']) && $taxonomy['responses'] === true;
|
}))
|
];
|
// update_option(BASE.'_global_responses', $responses);
|
}
|
return $responses;
|
}
|
|
function jvbGetFields(string $type, ?string $object_type = null): array
|
{
|
return JVBase\registry\FieldRegistry::getInstance()->getFields($type, $object_type);
|
}
|
|
|
function jvbGetValidType(string $type) {
|
return str_replace('-', '_', (jvbNoBase($type)));
|
}
|
function jvbGetSections(string $type, ?string $object_type = null):array
|
{
|
$type = jvbGetValidType($type);
|
|
if (!jvbIsValidType($type)) {
|
return [];
|
}
|
if (!$object_type) {
|
$object_type = jvbGetObjectType($type);
|
}
|
|
$optionKey = BASE.$type.'_'.$object_type.'_sections';
|
$sections = get_option($optionKey, false);
|
$sections = false;
|
if (JVB_TESTING) {
|
$sections = false;
|
}
|
if (!$sections) {
|
$fields = jvbGetFields($type, $object_type);
|
$sections = ['basic' => 'Basic'];
|
|
foreach ($fields as $field) {
|
if (array_key_exists('section', $field)) {
|
$sections[sanitize_title($field['section'])] = jvbSectionTitle($field['section']);
|
}
|
}
|
|
update_option($optionKey, $sections);
|
}
|
|
return $sections;
|
}
|
|
function jvbSectionTitle(string $section):string
|
{
|
switch ($section) {
|
case 'gmb':
|
return 'Google My Business';
|
case 'square-config':
|
return 'Square Options';
|
case 'sync':
|
return 'Sync Settings';
|
default:
|
return ucwords(str_replace('-', ' ', $section));
|
}
|
}
|
function jvbSectionIcon(string $section):string
|
{
|
switch ($section) {
|
case 'gmb':
|
return 'storefront';
|
case 'square-config':
|
return 'square-logo';
|
case 'sync':
|
return 'plugs-connected';
|
default:
|
return '';
|
}
|
}
|
|
function jvbSectionDescription(string $section): string
|
{
|
switch ($section) {
|
case 'gmb':
|
return 'These optional fields help optimize your Google Business listing.';
|
case 'variations':
|
return 'If you have different purchase options for this item, list them here. Example: small, medium, large';
|
default:
|
return '';
|
}
|
}
|
function jvbRegisterCommonFields(array $common)
|
{
|
$fields = [];
|
foreach ($common??[] as $field => $c) {
|
if (!is_numeric($field)) {
|
$f = JVBase\registry\MetaFieldRegistrar::getCommonMeta($field, $c);
|
} else {
|
$f = JVBase\registry\MetaFieldRegistrar::getCommonMeta($c);
|
}
|
$fields = array_merge($fields, $f);
|
}
|
return $fields;
|
}
|
|
function jvbGetAllDashboardPages()
|
{
|
$manageableContent = get_option(BASE.'all_dashboard_pages');
|
if (JVB_TESTING) {
|
$manageableContent = false;
|
}
|
if ($manageableContent === false) {
|
|
$manageableContent = [];
|
$bios = [];
|
foreach (JVB_USER as $role => $config) {
|
$manageableContent = array_merge($manageableContent, jvbRolePages($role));
|
}
|
|
if (Features::forSite()->has('referrals')) {
|
$manageableContent[] = 'referrals';
|
}
|
foreach (JVB_TAXONOMY as $tax => $config) {
|
if (jvbCheck('is_content', $config)) {
|
$manageableContent[] = strtolower($config['plural']);
|
}
|
}
|
|
if (jvbCheck('can_invite', JVB_MEMBERSHIP)) {
|
$manageableContent[] = 'invites';
|
}
|
|
if (jvbCheck('term_approval', JVB_MEMBERSHIP)) {
|
$manageableContent[] = 'approvals';
|
}
|
|
if (jvbSiteHasForum()) {
|
$manageableContent[] = 'news';
|
}
|
|
if (jvbCheck('member_content', JVB_MEMBERSHIP)) {
|
$manageableContent[] = 'metrics';
|
}
|
|
if (!empty($bios)) {
|
$manageableContent[] = 'bio';
|
}
|
|
if (jvbCheck('favourites', JVB_SITE)) {
|
$manageableContent[] = 'favourites';
|
}
|
|
if (jvbSiteHasKarma()) {
|
$manageableContent[] = 'karmic-score';
|
}
|
|
if (jvbSiteHasNotifications()) {
|
$manageableContent[] = 'notifications';
|
}
|
|
if (jvbSiteHasSupport()) {
|
$manageableContent[] = 'support';
|
}
|
|
if (jvbSiteHasIntegrations()) {
|
$manageableContent[] = 'integrations';
|
}
|
|
$manageableContent[] = 'admin';
|
$manageableContent = apply_filters('jvbDashboardPages', $manageableContent);
|
$manageableContent = array_unique($manageableContent);
|
sort($manageableContent);
|
$manageableContent = array_map(function ($content) {
|
return str_replace('_', '-', $content);
|
}, $manageableContent);
|
update_option(BASE.'all_dashboard_pages', $manageableContent);
|
}
|
|
|
return $manageableContent;
|
}
|
|
function jvbRolePages($role)
|
{
|
$manageableContent = get_option(BASE.$role.'_pages');
|
if (JVB_TESTING) {
|
$manageableContent = false;
|
}
|
if ($manageableContent === false) {
|
$manageableContent = [];
|
$config = JVB_USER[$role];
|
$content = $config['can_create'];
|
$settings = $bio = false;
|
if (array_key_exists('profile', $config)) {
|
$manageableContent[] = $config['profile'];
|
}
|
$manageableContent = array_merge($manageableContent, jvbExtractUserContent($config['can_create']));
|
|
|
if (array_key_exists('has_dashboard', $config)) {
|
$manageableContent[] = 'settings';
|
}
|
|
update_option(BASE.$role.'_pages', $manageableContent);
|
}
|
|
return $manageableContent;
|
}
|
|
function jvbExtractUserContent(array $content):array
|
{
|
// Deprecated: Use Features::forUser($role)->getCreatableContent() instead
|
_deprecated_function(__FUNCTION__, '2.0.0', 'Features::forUser($role)->getCreatableContent()');
|
|
$out = [];
|
foreach ($content as $c) {
|
if (is_array($c)) {
|
foreach ($c as $type => $contents) {
|
$out = array_merge($out, $contents);
|
}
|
} else {
|
$out = array_merge($out, [$c]);
|
}
|
}
|
return $out;
|
}
|
|
function jvbGlobalDirectoryInfo():array
|
{
|
return get_option(BASE.'directory_list',[]);
|
}
|
function jvbGlobalDirectoryIds():array
|
{
|
return get_option(BASE.'directory_ids',[]);
|
}
|
|
function jvbContentTaxContent(string $type):array
|
{
|
$content = get_option(BASE.$type.'_content');
|
if (JVB_TESTING) {
|
$content = false;
|
}
|
if ($content === false) {
|
global $jvb_taxonomy_for;
|
$content = $jvb_taxonomy_for[$type] ?? [];
|
|
if (jvbCheck('associate_user_content', JVB_TAXONOMY[$type])) {
|
$userTypes = jvbUserTypes();
|
foreach ($content as $c) {
|
$c = jvbCheckBase($c);
|
if (in_array($c, $userTypes)) {
|
$user = array_search($c, $userTypes);
|
$content = array_merge($content, jvbExtractUserContent(JVB_USER[$user]['can_create']??[]));
|
}
|
}
|
$content = array_unique($content);
|
$content = array_filter($content, function ($c) {
|
return array_key_exists($c, jvbGlobalFeedContent());
|
});
|
}
|
|
update_option(BASE.$type.'_content', $content);
|
}
|
return $content;
|
}
|
|
|
function jvbContentTaxonomies():array
|
{
|
$types = get_option(BASE.'content_taxonomies');
|
if (JVB_TESTING) {
|
$types = false;
|
}
|
if ($types === false) {
|
$types = array_map(function ($content) { return BASE.$content; },
|
array_keys(array_filter(JVB_TAXONOMY, function ($tax) {
|
return jvbCheck('is_content', $tax);
|
})));
|
update_option(BASE.'content_taxonomies', $types);
|
}
|
return $types;
|
}
|
|
function jvbContentTaxonomiesTableFields(string $type):array
|
{
|
$fields = get_option(BASE.$type.'_content_fields');
|
if (JVB_TESTING) {
|
$fields = false;
|
}
|
|
if (!$fields) {
|
$all = JVB_TAXONOMY[$type]['content_table']??[];
|
|
$fields = $all['fields']??[];
|
|
$temp = [];
|
if (!empty($fields)) {
|
$index = jvbGetFields($type);
|
foreach ($fields as $field) {
|
$temp[$field] = $index[$field];
|
}
|
$all['fields'] = $temp;
|
}
|
$fields = $all;
|
update_option(BASE.$type.'_content_fields', $fields);
|
}
|
return $fields;
|
}
|
|
function jvbApprovalTypes():array
|
{
|
return get_option(BASE.'approval_types', []);
|
}
|
|
//Gets the user roles of users that can create
|
function jvbAuthorUsers():array
|
{
|
return array_keys(array_filter(JVB_USER, function ($value) {
|
return !empty($value['can_create']??[]);
|
}));
|
}
|
|
function jvbNoSingleContentTypes():array
|
{
|
return array_keys(array_filter(JVB_CONTENT, function ($value) {
|
return jvbCheck('hide_single', $value);
|
}));
|
}
|
|
function jvbTaxonomiesForContent(string $content):array
|
{
|
$taxonomies = get_option(BASE.$content.'_taxonomies');
|
if (JVB_TESTING) {
|
$taxonomies = false;
|
}
|
if (!$taxonomies) {
|
global $jvb_taxonomy_for;
|
$taxonomies = [];
|
foreach ($jvb_taxonomy_for as $tax => $contentTypes) {
|
if (in_array($content, $contentTypes)) {
|
$taxonomies[] = BASE.$tax;
|
}
|
}
|
update_option(BASE.$content.'_taxonomies', $taxonomies);
|
}
|
return $taxonomies;
|
}
|
|
function jvbGetRandomSeed():string
|
{
|
return floor(time() / 1800);
|
}
|
|
/**
|
* The following return a slug => $config pair array of any registered types.
|
*/
|
function jvbCalendarTableTypes():array
|
{
|
//Set on activation through CheckCustomTables.php
|
return get_option(BASE.'calendar_tables');
|
}
|
|
function jvbCalenderTables():array
|
{
|
$tables = [];
|
foreach (jvbCalendarTableTypes() as $type => $config) {
|
$tables[$type] = BASE.'calendar_'.$type;
|
}
|
return $tables;
|
}
|
|
function jvbKarmaTableTypes():array
|
{
|
//Set on activation through CheckCustomTables.php
|
return get_option(BASE.'karma_tables');
|
}
|
function jvbKarmaTables():array
|
{
|
$tables = [];
|
foreach (jvbKarmaTableTypes() as $type => $config) {
|
$tables[$type] = BASE.'karma_'.$type;
|
}
|
return $tables;
|
}
|
|
function jvbStatsTableTypes():array
|
{
|
//Set on activation through CheckCustomTables.php
|
return get_option(BASE.'stats_tables');
|
}
|
|
function jvbStatsTables():array
|
{
|
$tables = [];
|
foreach (jvbStatsTableTypes() as $type => $config) {
|
$tables[$type] = BASE.'stats_'.$type;
|
}
|
return $tables;
|
}
|
|
function jvbInviteTableTypes():array
|
{
|
//Set on activation through CheckCustomTables.php
|
return get_option(BASE.'invite_tables');
|
}
|
|
function jvbInviteTables():array
|
{
|
$tables = [];
|
foreach (jvbInviteTableTypes() as $type => $config) {
|
$tables[$type] = BASE.'invitations_'.$type;
|
}
|
return $tables;
|
}
|
|
function jvbVerifyEntryTableTypes():array
|
{
|
//Set on activation through CheckCustomTables.php
|
return get_option(BASE.'verify_entry_tables');
|
}
|
|
|
function jvbVerifyEntryTables():array
|
{
|
$tables = [];
|
foreach (jvbVerifyEntryTableTypes() as $type => $config) {
|
$contents = $config['for_content'];
|
foreach($contents as $content) {
|
$tables[$type][$content] = BASE.$content.'_'.$type.'_requests';
|
}
|
}
|
return $tables;
|
}
|
|
function jvbApprovalTableTypes():array
|
{
|
//Set on activation through CheckCustomTables.php
|
return get_option(BASE.'approval_tables');
|
}
|
|
function jvbApprovalTables():array
|
{
|
$tables = [];
|
foreach (jvbApprovalTableTypes() as $type => $config) {
|
$tables['requests'][$type] = BASE.'approval_'.$type.'_requests';
|
$tables['votes'][$type] = BASE.'approval_'.$type.'_votes';
|
}
|
return $tables;
|
}
|
|
function jvbHistoryTableTypes():array
|
{
|
//Set on activation through CheckCustomTables.php
|
return get_option(BASE.'history_tables');
|
}
|
|
function jvbHistoryTables():array
|
{
|
$tables = [];
|
foreach (jvbHistoryTableTypes() as $type => $config) {
|
$contents = $config['for_content'];
|
foreach($contents as $content) {
|
$tables[$type][$content] = BASE.'history_'.$content.'_'.$type;
|
}
|
}
|
return $tables;
|
}
|
|
|
function jvbSquarePOSTypes():array {
|
$enabled_types = [];
|
|
foreach (JVB_CONTENT as $post_type => $config) {
|
if (!empty($config['integrations']['square']['enabled'])) {
|
$enabled_types[] = $post_type;
|
}
|
}
|
|
return $enabled_types;
|
}
|
|
|
function jvbGetLabels():array {
|
$labels = get_option(BASE.'content_labels');
|
$labels = false;
|
if (!$labels) {
|
$labels = [];
|
$all = array_merge(JVB_CONTENT, JVB_TAXONOMY);
|
foreach ($all as $type => $config) {
|
$labels[$type] = [
|
'single' => $config['singular'],
|
'plural' => $config['plural']
|
];
|
}
|
update_option(BASE.'content_labels', $labels);
|
}
|
return $labels;
|
}
|