| | |
| | | exit; |
| | | } |
| | | |
| | | use JVBase\managers\Cache; |
| | | use JVBase\managers\RoleManager; |
| | | use JVBase\meta\Form; |
| | | use JVBase\registrar\Registrar; |
| | | |
| | | /** |
| | | * For whatever reason, after much testing, it seems that |
| | | * good ol' WP resets post_parent if you call wp_update_post |
| | | * without explicitly setting the post_parent |
| | | * This is a wrapper that grabs old data and merges it with |
| | | * what we're trying to update - reducing repetition |
| | | * @param array $postArr as in wp_update_post |
| | | * @param array $allowOverride an array of keys that are allowed to be overridden |
| | | * @return int|WP_Error |
| | | */ |
| | | function jvb_update_post(array $postArr, array $allowOverride = []) { |
| | | if (empty($postArr['ID'])) { |
| | | return new WP_Error('missing_id', 'Post ID is required'); |
| | | } |
| | | |
| | | $old = get_post($postArr['ID'], ARRAY_A); |
| | | if (!$old) { |
| | | return new WP_Error('invalid_id', 'Post not found'); |
| | | } |
| | | /** |
| | | * WARNING: You won't want to override fields like: |
| | | * guid |
| | | * filter |
| | | * ancestors |
| | | * post_category |
| | | * tags_input |
| | | * to_ping |
| | | * pinged |
| | | **/ |
| | | $preserveFields = [ |
| | | 'post_parent', 'menu_order', |
| | | 'post_status', 'post_password', 'comment_status', 'ping_status', |
| | | 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt', |
| | | 'post_name', 'post_title', 'post_excerpt', 'post_content', |
| | | 'post_author' |
| | | ]; |
| | | // Remove fields we explicitly want to override |
| | | $preserveFields = array_diff($preserveFields, $allowOverride); |
| | | |
| | | // Keep only preserved fields from old post |
| | | $old = array_intersect_key($old, array_flip($preserveFields)); |
| | | |
| | | // Merge old → new (new wins) |
| | | $merged = array_merge($old, $postArr); |
| | | $merged['ID'] = (int)$postArr['ID']; |
| | | |
| | | return wp_update_post($merged, true); |
| | | } |
| | | |
| | | /** |
| | | * @deprecated use CRUDManager.php or CRUDSkeleton.php |
| | | * Outputs the blocks of a CRUD management in backend |
| | | * Mainly used in news.php so far |
| | | * @param string $content |
| | |
| | | 'label' => 'Scrapped', |
| | | ] |
| | | ]; |
| | | $permission = ($content === 'news') ? 'update' : $content; |
| | | $permission = JVB_CONTENT[$content]['plural']??$content.'s'; |
| | | |
| | | $canPublish = current_user_can("publish_{$permission}"); |
| | | $permission = JVB()->roles()->getContentPlural($content); |
| | | $canPublish = current_user_can("publish_{$permission}") && current_user_can('skip_moderation'); |
| | | $out = '<div class="filters">'; |
| | | if (!empty($statusFilters)) { |
| | | $out .= '<div class="status">'; |
| | |
| | | </button> |
| | | </div> |
| | | </div>', |
| | | jvbIcon('close', ['title'=>'Cancel']) |
| | | jvbIcon('x', ['title'=>'Cancel']) |
| | | ); |
| | | } |
| | | $out .= '</div>'; |
| | |
| | | */ |
| | | function jvbGetBulkActions(string $content):array |
| | | { |
| | | |
| | | $permission = ($content === 'news') ? 'update' : $content; |
| | | $permission = JVB_CONTENT[$content]['plural']??$content.'s'; |
| | | $bulkActions = [ |
| | | 'publish' => [ |
| | | 'icon' => 'show', |
| | |
| | | 'disabled' => true, |
| | | ] |
| | | ]; |
| | | if (current_user_can("publish_{$permission}")) { |
| | | $permission = JVB()->roles()->getContentPlural($content); |
| | | if (current_user_can('skip_moderation') && current_user_can("publish_{$permission}")) { |
| | | $bulkActions['publish']['disabled'] = false; |
| | | } |
| | | if (current_user_can("edit_{$permission}")) { |
| | |
| | | */ |
| | | function jvbRenderDateFilter(string $content):string |
| | | { |
| | | $cache = new JVBase\Managers\CacheManager('date_filter'); |
| | | $cache = Cache::for('date_filter')->connect('post', true); |
| | | $check = $cache->get($content); |
| | | if ($check) { |
| | | return $check; |
| | |
| | | string $postType = '', |
| | | bool $prefix = false |
| | | ):void { |
| | | switch ($contentType) { |
| | | case 'post': |
| | | $settings = JVB_CONTENT; |
| | | break; |
| | | case 'term': |
| | | $settings = JVB_TAXONOMY; |
| | | break; |
| | | case 'user': |
| | | $settings = JVB_USER; |
| | | break; |
| | | default: |
| | | return; |
| | | } |
| | | $sections = $settings[$postType]['sections']??[]; |
| | | if (empty($sections)) { |
| | | $registrar = Registrar::getInstance($postType); |
| | | if (!$registrar || empty($registrar->getSections())) { |
| | | return; |
| | | } |
| | | |
| | | echo '<div class="container">'; |
| | | $nav = '<nav class="tabs row start" role="tablist">'; |
| | | $i = 1; |
| | | foreach ($sections as $slug => $section) { |
| | | foreach ($registrar->getSections() as $slug => $section) { |
| | | $nav .= '<button type="button" class="tab'; |
| | | |
| | | $ariaActive = 'false'; |
| | |
| | | $nav .= '</nav>'; |
| | | echo $nav; |
| | | |
| | | $fields = jvbGetFields($postType); |
| | | |
| | | $meta = new JVBase\meta\MetaManager($ID, $contentType); |
| | | $fields = Registrar::getFieldsFor($postType); |
| | | ?> |
| | | <form class="jvb-form" id="bio" data-form-id="bio-<?=$ID?>" data-save="bio" |
| | | data-object-id="<?=$ID?>" data-content-type="<?=$postType?>"> |
| | | <?php |
| | | $i = 0; |
| | | foreach ($sections as $slug => $section) { |
| | | foreach ($registrar->getSections() as $slug => $section) { |
| | | $tabName = ($prefix) ? $ID.'-'.$slug : $slug; |
| | | |
| | | $class = ($i == 0) ? ' active' : ''; |
| | |
| | | if (array_key_exists('role', $config)) { |
| | | $user = get_userdata($ID); |
| | | if (in_array($config['role'], $user->roles)) { |
| | | $meta->render('form', $field, $config); |
| | | echo Form::render($field, null, $config); |
| | | } |
| | | } else { |
| | | $meta->render('form', $field, $config); |
| | | echo Form::render($field, null, $config); |
| | | } |
| | | } |
| | | ?> |