Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.3 kB
9
Indexable
Never
add_action('mainwp_posts_table_action', 'custom_mainwp_posts_table_action', 10, 2);

function custom_mainwp_posts_table_action($post, $website) {
    if ($post['post_type'] === 'post') {
        $child_post_id = $post['id'];
        $website_id = $website->id;

        // Add a custom action link with the post ID and website ID as data attributes
        echo '<div>';
        echo '<a class="item" href="#" onclick="fetchPostContent(' . esc_attr($child_post_id) . ', ' . esc_attr($website_id) . '); return false;">Publish to new site</a>';
        echo '<div id="post-content-' . esc_attr($child_post_id) . '"></div>';
        echo '</div>';
    }
}

add_action('admin_footer', 'custom_mainwp_script');
function custom_mainwp_script() {
    ?>
    <script type="text/javascript">
        function fetchPostContent(postId, websiteId) {
            var data = {
                action: 'fetch_post_content',
                post_id: postId,
                website_id: websiteId
            };

            jQuery.post(ajaxurl, data, function(response) {
                if (response.success) {
                    jQuery('#post-content-' + postId).html(response.data);
                } else {
                    jQuery('#post-content-' + postId).html('Error: ' + response.data);
                }
            });
        }
    </script>
    <?php
}

add_action('wp_ajax_fetch_post_content', 'fetch_post_content_callback');

function fetch_post_content_callback() {
    if (!isset($_POST['post_id']) || !isset($_POST['website_id'])) {
        wp_send_json_error('Invalid parameters.');
        return;
    }

    $post_id = intval($_POST['post_id']);
    $website_id = intval($_POST['website_id']);

    // Get the website object from the website ID
    $website = MainWP_DB::instance()->get_website_by_id($website_id);

    if (!$website || !MainWP_System_Utility::can_edit_website($website)) {
        wp_send_json_error('Invalid website or you do not have permission to edit this website.');
        return;
    }

    // Fetch the post content
    $post_content = get_child_post_content($website, $post_id);

    if ($post_content) {
        wp_send_json_success($post_content);
    } else {
        wp_send_json_error('Could not fetch post content.');
    }
}

function get_child_post_content($website, $post_id) {
    // Load the MainWP_Post class
    if (!class_exists('MainWP_Post')) {
        include_once ABSPATH . 'wp-content/plugins/mainwp/class/class-mainwp-post.php';
    }

    // Prepare the data to retrieve the post
    $post_data = array(
        'postId' => $post_id,
        'websiteId' => $website->id
    );

    try {
        $information = MainWP_Connect::fetch_url_authed(
            $website,
            'post_action',
            array(
                'action' => 'get_edit',
                'id' => $post_id,
                'post_type' => 'post',
            )
        );

        if (is_array($information) && isset($information['post']['post_content'])) {
            return $information['post']['post_content'];
        }
    } catch (MainWP_Exception $e) {
        return 'Error fetching post content: ' . $e->getMessage();
    }

    return 'No content found.';
}
Leave a Comment