Plugin Directory

Changeset 3130084


Ignore:
Timestamp:
08/02/2024 02:16:21 PM (20 months ago)
Author:
prospecter
Message:

Front page and post export added

Location:
flowheel-integrator/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • flowheel-integrator/trunk/flowheel-integrator.php

    r3116257 r3130084  
    88Plugin Name:  Flowheel Integrator
    99Description:  Flowheel Integrator is a plugin which provides API endpoints for exporting Wordpress pages into HTML.
    10 Version:      1.0.0
     10Version:      1.1.0
    1111Author:       Prospecter Ventures AG
    1212License:      GPL2
     
    4343    register_rest_route(
    4444        'fhint-api/v1',
     45        '/wp-template-info',
     46        array(
     47            'methods' => 'GET',
     48            'callback' => 'fhint\fhint_wp_template_info',
     49        )
     50    );
     51    register_rest_route(
     52        'fhint-api/v1',
     53        '/post-info',
     54        array(
     55            'methods' => 'GET',
     56            'callback' => 'fhint\fhint_post_info',
     57        )
     58    );
     59    register_rest_route(
     60        'fhint-api/v1',
    4561        '/export/(?P<id>\d+)',
    4662        array(
     
    4965        )
    5066    );
     67    register_rest_route(
     68        'fhint-api/v1',
     69        '/export/front-page',
     70        array(
     71            'methods' => 'GET',
     72            'callback' => 'fhint\fhint_export_front_page',
     73        )
     74    );
     75    register_rest_route(
     76        'fhint-api/v1',
     77        '/get-info/(?P<id>\d+)',
     78        array(
     79            'methods' => 'GET',
     80            'callback' => 'fhint\fhint_get_info',
     81        )
     82    );
    5183}
  • flowheel-integrator/trunk/readme.txt

    r3116257 r3130084  
    44Requires at least: 4.7
    55Tested up to: 6.5
    6 Stable tag: 1.0.0
     6Stable tag: 1.1.0
    77License: GPLv2
    88
  • flowheel-integrator/trunk/src/exporter.php

    r3116257 r3130084  
    1010use DOMDocument;
    1111use DOMXPath;
     12use WP_Error;
    1213
    1314function fhint_export_pages()
     
    6061{
    6162
    62     $wpUrl = get_bloginfo('wpurl');
    63 
    6463    $response = wp_remote_get(get_page_link($page->ID));
    6564    if (is_wp_error($response) && wp_remote_retrieve_response_code($response) != 200) {
     
    7069        return $response;
    7170    }
     71
     72    $modifiedHtml = fhint_parse_html($htmlContent);
     73    if (is_wp_error($response)) {
     74        return $response->get_error_message();
     75    }
     76
     77    return array(
     78        'url' => get_page_link($page->ID),
     79        'title' => $page->post_title,
     80        'name' => $page->post_name,
     81        'html' => $modifiedHtml,
     82    );
     83}
     84
     85function fhint_export_front_page()
     86{
     87
     88    $wpUrl = get_bloginfo('wpurl');
     89    $response = wp_remote_get($wpUrl);
     90    if (is_wp_error($response) && wp_remote_retrieve_response_code($response) != 200) {
     91        return $response;
     92    }
     93    $htmlContent = wp_remote_retrieve_body($response);
     94    if (is_wp_error($htmlContent) && wp_remote_retrieve_response_code($response) != 200) {
     95        return $response;
     96    }
     97
     98    $modifiedHtml = fhint_parse_html($htmlContent);
     99    if (is_wp_error($response)) {
     100        return $response->get_error_message();
     101    }
     102
     103    $data = array(
     104        'url' => $wpUrl,
     105        'title' => 'Front Page',
     106        'name' => 'index',
     107        'html' => $modifiedHtml,
     108    );
     109
     110    return rest_ensure_response(
     111        array(
     112            'data' => $data
     113        )
     114    );
     115}
     116
     117function fhint_parse_html($htmlContent)
     118{
     119
     120    $wpUrl = get_bloginfo('wpurl');
    72121
    73122    // Remove header and footer
     
    84133    $dom->loadHTML($htmlContent);
    85134    if (!$dom) {
    86         return $response;
     135        return new WP_Error('invalid_argument', 'dom false');
    87136    }
    88137
     
    105154        if ($srcUrl != null && !isAbsolute($srcUrl)) {
    106155            $srcUrl = $wpUrl + $srcUrl;
    107             $linkElement->setAttribute('src', $srcUrl);
     156            $imgElement->setAttribute('src', $srcUrl);
    108157        }
    109158        $srcSetUrl = $imgElement->getAttribute('srcset');
    110159        if ($srcSetUrl != null && !isAbsolute($srcSetUrl)) {
    111160            $srcSetUrl = $wpUrl + $srcSetUrl;
    112             $linkElement->setAttribute('src', $srcSetUrl);
     161            $imgElement->setAttribute('src', $srcSetUrl);
    113162        }
    114163    }
    115164
    116165    foreach ($scriptElements as $scriptElement) {
    117         $scriptUrl = $scriptElement->getAttribute('src');
    118         if ($scriptUrl != null && !isAbsolute($scriptUrl)) {
    119             $scriptUrl = $wpUrl + $scriptUrl;
    120             $linkElement->setAttribute('src', $scriptUrl);
    121         }
     166        $scriptElement->parentNode->removeChild($scriptElement);
     167        // $scriptUrl = $scriptElement->getAttribute('src');
     168        // if ($scriptUrl != null && !isAbsolute($scriptUrl)) {
     169        //  $scriptUrl = $wpUrl + $scriptUrl;
     170        //  $scriptElement->setAttribute('src', $scriptUrl);
     171        // }
    122172    }
    123173
     
    130180                $aUrl = $post->post_name . '.html';
    131181                $aElement->setAttribute('href', $aUrl);
     182            } else {
     183                $aUrl = 'index.html';
     184                $aElement->setAttribute('href', $aUrl);
    132185            }
    133186        }
     
    137190    $modifiedHtml = str_replace(["\n", "\r", "\r\n", "\t"], '', $modifiedHtml);
    138191
    139     return array(
    140         'url' => get_page_link($page->ID),
    141         'title' => $page->post_title,
    142         'name' => $page->post_name,
    143         'html' => $modifiedHtml,
    144     );
     192    return $modifiedHtml;
    145193}
    146194
  • flowheel-integrator/trunk/src/pages.php

    r3116257 r3130084  
    3333    );
    3434}
     35
     36function fhint_wp_template_info()
     37{
     38    $templates = get_posts(
     39        array(
     40            'post_type' => 'wp_template',
     41            'post_status' => 'publish',
     42            'number' => -1, // Retrieve all pages
     43        )
     44    );
     45
     46    $templates_info = array();
     47
     48    foreach ($templates as $template) {
     49        $templates_info[] = array(
     50            'page_id' => $template->ID,
     51            'url' => get_page_link($template->ID),
     52        );
     53    }
     54
     55    return rest_ensure_response(
     56        array(
     57            'data' => $templates_info
     58        )
     59    );
     60}
     61
     62function fhint_post_info()
     63{
     64    $posts = get_posts(
     65        array(
     66            'post_type' => 'post',
     67            'post_status' => 'publish',
     68            'number' => -1, // Retrieve all pages
     69        )
     70    );
     71
     72    $posts_info = array();
     73
     74    foreach ($posts as $post) {
     75        $posts_info[] = array(
     76            'page_id' => $post->ID,
     77            'url' => get_page_link($post->ID),
     78        );
     79    }
     80
     81    return rest_ensure_response(
     82        array(
     83            'data' => $posts_info
     84        )
     85    );
     86}
     87
     88function fhint_get_info($request)
     89{
     90    $id = $request['id'];
     91    $page = get_post($id);
     92
     93
     94    $pages_info[] = array(
     95        'page_id' => $page->ID,
     96        'url' => get_page_link($page->ID),
     97        'name' => $page->post_name
     98    );
     99
     100    return rest_ensure_response(
     101        array(
     102            'data' => $pages_info
     103        )
     104    );
     105}
Note: See TracChangeset for help on using the changeset viewer.