$action, 'placement' => $placement, 'size' => $size, 'generation_size' => $size, 'display_size' => sanitize_key($raw['display_size'] ?? 'large'), 'custom_width' => absint($raw['custom_width'] ?? 800), 'custom_height' => absint($raw['custom_height'] ?? 0), 'alignment' => sanitize_key($raw['alignment'] ?? 'none'), 'mobile_behavior' => sanitize_key($raw['mobile_behavior'] ?? 'full_width'), 'replace_existing' => !empty($raw['replace_existing']) ? 'yes' : 'no', 'attachment_id' => absint($raw['attachment_id'] ?? 0), 'prompt' => sanitize_textarea_field($raw['prompt'] ?? ''), ]; } function aiwest_content_post_action_links($post_id) { $post_id = absint($post_id); if (!$post_id) return ''; $edit_link = get_edit_post_link($post_id); $preview_link = get_preview_post_link($post_id); $permalink = get_permalink($post_id); $post_status = get_post_status($post_id); $links = []; if ($edit_link) { $links[] = '' . esc_html(__('Edit post', 'ai-webmaster-studio')) . ''; } if ($post_status === 'publish' && $permalink) { $links[] = '' . esc_html(__('View post', 'ai-webmaster-studio')) . ''; } elseif ($preview_link) { $links[] = '' . esc_html(__('Preview post', 'ai-webmaster-studio')) . ''; } if (!$links) return ''; return '

' . implode(' ', $links) . '

'; } function aiwest_content_media_label($type, $value) { $value = sanitize_key($value); $labels = [ 'source' => [ 'library' => __('Media Library', 'ai-webmaster-studio'), 'ai' => __('AI', 'ai-webmaster-studio'), 'generated' => __('AI', 'ai-webmaster-studio'), 'none' => __('None', 'ai-webmaster-studio'), ], 'placement' => [ 'featured' => __('Featured image / SEO', 'ai-webmaster-studio'), 'content_top' => __('Top of content', 'ai-webmaster-studio'), 'featured_and_content_top' => __('Featured image + top of content', 'ai-webmaster-studio'), 'after_first_paragraph' => __('After first paragraph', 'ai-webmaster-studio'), 'content_end' => __('End of content', 'ai-webmaster-studio'), ], 'display_size' => [ 'thumbnail' => __('Thumbnail', 'ai-webmaster-studio'), 'medium' => __('Medium', 'ai-webmaster-studio'), 'large' => __('Large', 'ai-webmaster-studio'), 'full' => __('Full', 'ai-webmaster-studio'), 'custom' => __('Custom width', 'ai-webmaster-studio'), ], ]; return $labels[$type][$value] ?? $value; } function aiwest_content_media_success_notice($post_id, $result) { $post_id = absint($post_id); $result = is_array($result) ? $result : []; $attachment_id = absint($result['attachment_id'] ?? 0); $source = sanitize_key($result['source'] ?? (($result['status'] ?? '') === 'generated' ? 'ai' : 'library')); $placement = sanitize_key($result['placement'] ?? ''); $display_size = sanitize_key($result['display_size'] ?? ''); $message = $result['message'] ?? __('Image has been updated.', 'ai-webmaster-studio'); $notice = '
'; $notice .= '

' . esc_html(__('Image has been applied.', 'ai-webmaster-studio')) . ' ' . esc_html($message) . '

'; $details = []; if ($source) { $details[] = '' . esc_html(__('Source:', 'ai-webmaster-studio')) . ' ' . esc_html(aiwest_content_media_label('source', $source)); } if ($placement) { $details[] = '' . esc_html(__('Placement:', 'ai-webmaster-studio')) . ' ' . esc_html(aiwest_content_media_label('placement', $placement)); } if ($display_size) { $details[] = '' . esc_html(__('Display size:', 'ai-webmaster-studio')) . ' ' . esc_html(aiwest_content_media_label('display_size', $display_size)); } if ($attachment_id) { $details[] = '' . esc_html(__('Image ID:', 'ai-webmaster-studio')) . ' ' . intval($attachment_id); } if ($details) { $notice .= '

' . implode('  |  ', $details) . '

'; } if ($attachment_id) { $image = wp_get_attachment_image($attachment_id, [120, 120], false, ['class' => 'aiwest-notice-media-image', 'loading' => 'lazy']); if ($image) { $notice .= '
' . esc_html(__('Image preview:', 'ai-webmaster-studio')) . '
' . $image . '
'; } } $notice .= aiwest_content_post_action_links($post_id); $notice .= '
'; return $notice; } function aiwest_content_media_fields($prefix = 'aiwest_media', $include_site_default = true, $compact = false) { static $aiwest_media_field_counter = 0; $aiwest_media_field_counter++; $attachment_field_id = 'aiwest-media-attachment-' . $aiwest_media_field_counter; $preview_id = 'aiwest-media-preview-' . $aiwest_media_field_counter; $rules = class_exists('AIWEST_Graphics_Rules') ? AIWEST_Graphics_Rules::get() : []; $source_default = $include_site_default ? 'site_default' : ($rules['source'] ?? 'none'); $placements = [ 'featured' => __('Featured image / SEO only', 'ai-webmaster-studio'), 'content_top' => __('Top of content', 'ai-webmaster-studio'), 'featured_and_content_top' => __('Featured image + top of content', 'ai-webmaster-studio'), 'after_first_paragraph' => __('After first paragraph', 'ai-webmaster-studio'), 'content_end' => __('End of content', 'ai-webmaster-studio'), ]; $display_sizes = [ 'thumbnail' => __('Thumbnail', 'ai-webmaster-studio'), 'medium' => __('Medium', 'ai-webmaster-studio'), 'large' => __('Large', 'ai-webmaster-studio'), 'full' => __('Full', 'ai-webmaster-studio'), 'custom' => __('Custom width', 'ai-webmaster-studio'), ]; $alignments = [ 'none' => __('Default', 'ai-webmaster-studio'), 'left' => __('Left of text', 'ai-webmaster-studio'), 'right' => __('Right of text', 'ai-webmaster-studio'), 'center' => __('Centered', 'ai-webmaster-studio'), 'random' => __('Random left/right', 'ai-webmaster-studio'), ]; ?>

' . esc_html(__('Content history item has been deleted. The WordPress post was not deleted.', 'ai-webmaster-studio')) . '

'; } if ($action === 'apply_media') { $post_id = AIWEST_Security::post_int('post_id'); $media_options = aiwest_content_sanitize_media_options(AIWEST_Security::post_array('aiwest_media')); if ($post_id && class_exists('AIWEST_Content_Engine') && method_exists('AIWEST_Content_Engine', 'handle_media_for_existing_post')) { $result = AIWEST_Content_Engine::handle_media_for_existing_post($post_id, $media_options); if (is_wp_error($result)) { $notice = '

' . esc_html(__('Could not update image:', 'ai-webmaster-studio')) . ' ' . esc_html($result->get_error_message()) . '

'; } elseif (($result['status'] ?? '') === 'error') { $notice = '

' . esc_html($result['message'] ?? __('Could not update image.', 'ai-webmaster-studio')) . '

'; } else { $notice = aiwest_content_media_success_notice($post_id, $result); } } else { $notice = '

' . esc_html(__('Missing post or graphics module.', 'ai-webmaster-studio')) . '

'; } } if ($action === 'generate') { $topic = AIWEST_Security::post_text('topic'); $keyword = AIWEST_Security::post_text('keyword'); $status = AIWEST_Security::allowed_post_status(AIWEST_Security::post_key('status', 'draft'), 'draft', true); $length = AIWEST_Security::post_text('length', 'medium'); $media_options = aiwest_content_sanitize_media_options(AIWEST_Security::post_array('aiwest_media')); $post_id = AIWEST_Content_Engine::generate_post($topic, $keyword, $status, $length, 'manual', $media_options); if (is_wp_error($post_id)) { $notice = '

' . esc_html(__('Content could not be generated:', 'ai-webmaster-studio')) . ' ' . esc_html($post_id->get_error_message()) . '

'; } else { $post_status = get_post_status($post_id); $media_result = get_post_meta($post_id, '_aiwest_media_result', true); $media_result = is_array($media_result) ? $media_result : []; $generated_attachment_id = absint($media_result['attachment_id'] ?? 0); $notice = '

' . esc_html(__('Content has been created.', 'ai-webmaster-studio')) . ' ' . esc_html(__('Post ID:', 'ai-webmaster-studio')) . ' ' . intval($post_id) . ', ' . esc_html(__('status:', 'ai-webmaster-studio')) . ' ' . esc_html($post_status) . '.

'; if ($generated_attachment_id) { $media_details = []; if (!empty($media_result['source'])) $media_details[] = '' . esc_html(__('Image source:', 'ai-webmaster-studio')) . ' ' . esc_html(aiwest_content_media_label('source', $media_result['source'])); if (!empty($media_result['placement'])) $media_details[] = '' . esc_html(__('Image placement:', 'ai-webmaster-studio')) . ' ' . esc_html(aiwest_content_media_label('placement', $media_result['placement'])); if ($media_details) $notice .= '

' . implode('  |  ', $media_details) . '

'; $image = wp_get_attachment_image($generated_attachment_id, [120, 120], false, ['class' => 'aiwest-notice-media-image', 'loading' => 'lazy']); if ($image) $notice .= '
' . esc_html(__('Image preview:', 'ai-webmaster-studio')) . '
' . $image . '
'; } $notice .= aiwest_content_post_action_links($post_id); $notice .= '
'; } } } $history = AIWEST_History::recent_content(50); ?>

post_id); $edit_link = $post_id ? get_edit_post_link($post_id) : ''; $preview_link = $post_id ? get_preview_post_link($post_id) : ''; $permalink = $post_id ? get_permalink($post_id) : ''; $live_status = $post_id ? get_post_status($post_id) : ''; $cats = $post_id ? wp_get_post_categories($post_id, ['fields' => 'names']) : []; $tags = $post_id ? wp_get_post_tags($post_id, ['fields' => 'names']) : []; $thumb_id = $post_id ? get_post_thumbnail_id($post_id) : 0; ?>
id); ?> created_at); ?> source); ?> topic); ?>
message); ?>
keyword); ?> status)); ?> ID:
post_status)); ?>
'max-width:80px;height:auto;border-radius:6px;'])); ?>
#