asd
This commit is contained in:
306
igy8-wp-plugin/admin/class-admin-columns.php
Normal file
306
igy8-wp-plugin/admin/class-admin-columns.php
Normal file
@@ -0,0 +1,306 @@
|
||||
<?php
|
||||
/**
|
||||
* Admin Columns and Row Actions
|
||||
*
|
||||
* Adds custom columns and actions to post/page/product list tables
|
||||
*
|
||||
* @package Igny8Bridge
|
||||
*/
|
||||
|
||||
// Prevent direct access
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Igny8AdminColumns Class
|
||||
*/
|
||||
class Igny8AdminColumns {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
// Add columns for posts, pages, and products
|
||||
add_filter('manage_posts_columns', array($this, 'add_columns'));
|
||||
add_filter('manage_pages_columns', array($this, 'add_columns'));
|
||||
|
||||
// Add column content
|
||||
add_action('manage_posts_custom_column', array($this, 'render_column_content'), 10, 2);
|
||||
add_action('manage_pages_custom_column', array($this, 'render_column_content'), 10, 2);
|
||||
|
||||
// Make columns sortable
|
||||
add_filter('manage_edit-post_sortable_columns', array($this, 'make_columns_sortable'));
|
||||
add_filter('manage_edit-page_sortable_columns', array($this, 'make_columns_sortable'));
|
||||
|
||||
// Add row actions
|
||||
add_filter('post_row_actions', array($this, 'add_row_actions'), 10, 2);
|
||||
add_filter('page_row_actions', array($this, 'add_row_actions'), 10, 2);
|
||||
|
||||
// Handle WooCommerce products
|
||||
if (class_exists('WooCommerce')) {
|
||||
add_filter('manage_product_posts_columns', array($this, 'add_columns'));
|
||||
add_action('manage_product_posts_custom_column', array($this, 'render_column_content'), 10, 2);
|
||||
add_filter('manage_edit-product_sortable_columns', array($this, 'make_columns_sortable'));
|
||||
add_filter('product_row_actions', array($this, 'add_row_actions'), 10, 2);
|
||||
}
|
||||
|
||||
// Handle AJAX actions
|
||||
add_action('wp_ajax_igny8_send_to_igny8', array($this, 'send_to_igny8'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Render taxonomy column
|
||||
*
|
||||
* @param int $post_id Post ID
|
||||
*/
|
||||
private function render_taxonomy_column($post_id) {
|
||||
$taxonomy = get_post_meta($post_id, '_igny8_taxonomy_id', true);
|
||||
|
||||
if ($taxonomy) {
|
||||
echo '<span class="igny8-badge igny8-badge-igny8" title="' . esc_attr__('Synced Taxonomy', 'igny8-bridge') . '">';
|
||||
echo esc_html($taxonomy);
|
||||
echo '</span>';
|
||||
} else {
|
||||
echo '<span class="igny8-empty">—</span>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render attribute column
|
||||
*
|
||||
* @param int $post_id Post ID
|
||||
*/
|
||||
private function render_attribute_column($post_id) {
|
||||
$attribute = get_post_meta($post_id, '_igny8_attribute_id', true);
|
||||
|
||||
if ($attribute) {
|
||||
echo '<span class="igny8-badge igny8-badge-igny8" title="' . esc_attr__('Synced Attribute', 'igny8-bridge') . '">';
|
||||
echo esc_html($attribute);
|
||||
echo '</span>';
|
||||
} else {
|
||||
echo '<span class="igny8-empty">—</span>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add custom columns
|
||||
*
|
||||
* @param array $columns Existing columns
|
||||
* @return array Modified columns
|
||||
*/
|
||||
public function add_columns($columns) {
|
||||
$new_columns = array();
|
||||
|
||||
foreach ($columns as $key => $value) {
|
||||
$new_columns[$key] = $value;
|
||||
|
||||
if ($key === 'title') {
|
||||
$new_columns['igny8_taxonomy'] = __('Taxonomy', 'igny8-bridge');
|
||||
$new_columns['igny8_attribute'] = __('Attribute', 'igny8-bridge');
|
||||
}
|
||||
}
|
||||
|
||||
return $new_columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render column content
|
||||
*
|
||||
* @param string $column_name Column name
|
||||
* @param int $post_id Post ID
|
||||
*/
|
||||
public function render_column_content($column_name, $post_id) {
|
||||
switch ($column_name) {
|
||||
case 'igny8_taxonomy':
|
||||
$this->render_taxonomy_column($post_id);
|
||||
break;
|
||||
|
||||
case 'igny8_attribute':
|
||||
$this->render_attribute_column($post_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make columns sortable
|
||||
*
|
||||
* @param array $columns Sortable columns
|
||||
* @return array Modified columns
|
||||
*/
|
||||
public function make_columns_sortable($columns) {
|
||||
$columns['igny8_source'] = 'igny8_source';
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add row actions
|
||||
*
|
||||
* @param array $actions Existing actions
|
||||
* @param WP_Post $post Post object
|
||||
* @return array Modified actions
|
||||
*/
|
||||
public function add_row_actions($actions, $post) {
|
||||
// Only add for published posts
|
||||
if ($post->post_status !== 'publish') {
|
||||
return $actions;
|
||||
}
|
||||
|
||||
// Check if already synced to IGNY8
|
||||
$task_id = get_post_meta($post->ID, '_igny8_task_id', true);
|
||||
|
||||
if ($task_id) {
|
||||
// Already synced - show update action
|
||||
$actions['igny8_update'] = sprintf(
|
||||
'<a href="%s" class="igny8-action-link" data-post-id="%d" data-action="update">%s</a>',
|
||||
'#',
|
||||
$post->ID,
|
||||
__('Update in IGNY8', 'igny8-bridge')
|
||||
);
|
||||
} else {
|
||||
// Not synced - show send action
|
||||
$actions['igny8_send'] = sprintf(
|
||||
'<a href="%s" class="igny8-action-link" data-post-id="%d" data-action="send">%s</a>',
|
||||
'#',
|
||||
$post->ID,
|
||||
__('Send to IGNY8', 'igny8-bridge')
|
||||
);
|
||||
}
|
||||
|
||||
return $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send post to IGNY8 (AJAX handler)
|
||||
*/
|
||||
public static function send_to_igny8() {
|
||||
check_ajax_referer('igny8_admin_nonce', 'nonce');
|
||||
|
||||
if (!current_user_can('edit_posts')) {
|
||||
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||
}
|
||||
|
||||
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
|
||||
$action = isset($_POST['action_type']) ? sanitize_text_field($_POST['action_type']) : 'send';
|
||||
|
||||
if (!$post_id) {
|
||||
wp_send_json_error(array('message' => 'Invalid post ID'));
|
||||
}
|
||||
|
||||
$post = get_post($post_id);
|
||||
if (!$post) {
|
||||
wp_send_json_error(array('message' => 'Post not found'));
|
||||
}
|
||||
|
||||
if (!igny8_is_connection_enabled()) {
|
||||
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.'));
|
||||
}
|
||||
|
||||
$api = new Igny8API();
|
||||
|
||||
if (!$api->is_authenticated()) {
|
||||
wp_send_json_error(array('message' => 'Not authenticated with IGNY8'));
|
||||
}
|
||||
|
||||
$site_id = get_option('igny8_site_id');
|
||||
if (!$site_id) {
|
||||
wp_send_json_error(array('message' => 'Site ID not set'));
|
||||
}
|
||||
|
||||
// Prepare post data for IGNY8
|
||||
$post_data = array(
|
||||
'title' => $post->post_title,
|
||||
'content' => $post->post_content,
|
||||
'excerpt' => $post->post_excerpt,
|
||||
'status' => $post->post_status === 'publish' ? 'completed' : 'draft',
|
||||
'post_type' => $post->post_type,
|
||||
'url' => get_permalink($post_id),
|
||||
'wordpress_post_id' => $post_id
|
||||
);
|
||||
|
||||
// Get categories
|
||||
$categories = wp_get_post_categories($post_id, array('fields' => 'names'));
|
||||
if (!empty($categories)) {
|
||||
$post_data['categories'] = $categories;
|
||||
}
|
||||
|
||||
// Get tags
|
||||
$tags = wp_get_post_tags($post_id, array('fields' => 'names'));
|
||||
if (!empty($tags)) {
|
||||
$post_data['tags'] = $tags;
|
||||
}
|
||||
|
||||
// Get featured image
|
||||
$featured_image_id = get_post_thumbnail_id($post_id);
|
||||
if ($featured_image_id) {
|
||||
$post_data['featured_image'] = wp_get_attachment_image_url($featured_image_id, 'full');
|
||||
}
|
||||
|
||||
// Get sectors and clusters
|
||||
$sectors = wp_get_post_terms($post_id, 'igny8_sectors', array('fields' => 'ids'));
|
||||
$clusters = wp_get_post_terms($post_id, 'igny8_clusters', array('fields' => 'ids'));
|
||||
|
||||
if (!empty($sectors)) {
|
||||
// Get IGNY8 sector IDs from term meta
|
||||
$igny8_sector_ids = array();
|
||||
foreach ($sectors as $term_id) {
|
||||
$igny8_sector_id = get_term_meta($term_id, '_igny8_sector_id', true);
|
||||
if ($igny8_sector_id) {
|
||||
$igny8_sector_ids[] = $igny8_sector_id;
|
||||
}
|
||||
}
|
||||
if (!empty($igny8_sector_ids)) {
|
||||
$post_data['sector_id'] = $igny8_sector_ids[0]; // Use first sector
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($clusters)) {
|
||||
// Get IGNY8 cluster IDs from term meta
|
||||
$igny8_cluster_ids = array();
|
||||
foreach ($clusters as $term_id) {
|
||||
$igny8_cluster_id = get_term_meta($term_id, '_igny8_cluster_id', true);
|
||||
if ($igny8_cluster_id) {
|
||||
$igny8_cluster_ids[] = $igny8_cluster_id;
|
||||
}
|
||||
}
|
||||
if (!empty($igny8_cluster_ids)) {
|
||||
$post_data['cluster_id'] = $igny8_cluster_ids[0]; // Use first cluster
|
||||
}
|
||||
}
|
||||
|
||||
// Check if post already has task ID
|
||||
$existing_task_id = get_post_meta($post_id, '_igny8_task_id', true);
|
||||
|
||||
if ($existing_task_id && $action === 'update') {
|
||||
// Update existing task
|
||||
$response = $api->put("/writer/tasks/{$existing_task_id}/", $post_data);
|
||||
} else {
|
||||
// Create new task
|
||||
$response = $api->post("/writer/tasks/", $post_data);
|
||||
}
|
||||
|
||||
if ($response['success']) {
|
||||
$task_id = $response['data']['id'] ?? $existing_task_id;
|
||||
|
||||
// Store task ID
|
||||
update_post_meta($post_id, '_igny8_task_id', $task_id);
|
||||
update_post_meta($post_id, '_igny8_last_synced', current_time('mysql'));
|
||||
|
||||
wp_send_json_success(array(
|
||||
'message' => $action === 'update' ? 'Post updated in IGNY8' : 'Post sent to IGNY8',
|
||||
'task_id' => $task_id
|
||||
));
|
||||
} else {
|
||||
wp_send_json_error(array(
|
||||
'message' => 'Failed to send to IGNY8: ' . ($response['error'] ?? 'Unknown error')
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize
|
||||
new Igny8AdminColumns();
|
||||
|
||||
// Register AJAX handler
|
||||
add_action('wp_ajax_igny8_send_to_igny8', array('Igny8AdminColumns', 'send_to_igny8'));
|
||||
|
||||
Reference in New Issue
Block a user