Plugin Directory

Changeset 2636205


Ignore:
Timestamp:
11/27/2021 06:37:54 AM (4 years ago)
Author:
traveledmap
Message:

Refactoring to use SDK

Location:
traveledmap-trip-itinerary-embedded-map
Files:
31 added
6 edited

Legend:

Unmodified
Added
Removed
  • traveledmap-trip-itinerary-embedded-map

    • Property svn:ignore set to
      trunk/.git
  • traveledmap-trip-itinerary-embedded-map/trunk/.gitignore

    r2156545 r2636205  
    1 node_modules
     1src/blocks/node_modules
    22
    33## Uncomment line below if you prefer to
  • traveledmap-trip-itinerary-embedded-map/trunk/plugin.php

    r2543856 r2636205  
    66 * Author: TraveledMap
    77 * Author URI: https://www.traveledmap.com
    8  * Version: 1.0.11
     8 * Version: 1.1.0
    99 * License: GPL2+
    1010 * License URI: https://www.gnu.org/licenses/gpl-2.0.txt
  • traveledmap-trip-itinerary-embedded-map/trunk/readme.txt

    r2543856 r2636205  
    44Donate link: https://donorbox.org/help-traveledmap-to-stay-free
    55Requires at least: 3.0.1
    6 Tested up to: 5.6.0
     6Tested up to: 5.8.2
    77Requires PHP: 5.6
    8 Stable tag: 1.0.11
     8Stable tag: 1.1.0
    99License: GPLv2
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    187187* Update to latest WordPress version
    188188
     189= 1.0.10 =
     190* Update to latest WordPress version
     191
     192= 1.0.11 =
     193* Update to latest WordPress version
     194
     195= 1.1.0 =
     196* Technical rework of the library to be compatible with the TripEmbedderJS SDK.
     197* Improvements to handle more use cases and options
     198* Made compatible with the latest WordPress version
     199
    189200== Upgrade Notice ==
    190 No upgrade to do for the moment
     201= 1.1.0 =
     202When upgrading to 1.1.0, you will need to recover content from the trip-steps' blocks because the content
     203changed. To do this, you just have to visit the editor page of the post where the trip is displayed, and click on the
     204button to recover the content.
     205
  • traveledmap-trip-itinerary-embedded-map/trunk/src/init.php

    r2337263 r2636205  
    11<?php
    2 /**
    3  * Blocks Initializer
    4  *
    5  * Enqueue CSS/JS of all the blocks.
    6  *
    7  * @since   1.0.0
    8  * @package CGB
    9  */
    102
    113// Exit if accessed directly.
     
    146}
    157
    16 require_once('metabox/metabox.php');
    17 require_once('iframe/render.php');
    18 require_once('iframe/iframe-widget.php');
    19 require_once('iframe/shortcode.php');
    20 require_once('categories/traveledmap.php');
     8require_once('metabox/metabox.php'); // the metabox is the right column on the backoffice
     9require_once('shared/utils/utils.php'); // utils
     10require_once('shared/front-renderer.php'); // utils that helps rendering the map in an iframe
     11require_once('shared/constants/env.php'); // constants for the environment
     12require_once('widget/widget.php'); // All the functions that allows the widget to work (save data and so on)
     13require_once('widget/shortcode.php'); // The shortcode to display the map without block
     14require_once('categories/traveledmap.php'); // The category that contains all the blocks
    2115
    2216function traveledmap_trip_block_assets()
     
    2519    wp_register_style(
    2620        'traveledmap-trip-style-css', // Handle.
    27         plugins_url('dist/blocks.style.build.css', dirname(__FILE__)),
     21        plugins_url('src/blocks/dist/blocks.style.build.css', dirname(__FILE__)),
    2822        array('wp-editor'),
    2923        null
     
    3327    wp_register_script(
    3428        'traveledmap-trip-block-js', // Handle.
    35         plugins_url('/dist/blocks.build.js', dirname(__FILE__)), // Block.build.js: We register the block here. Built with Webpack.
     29        plugins_url('/src/blocks/dist/blocks.build.js', dirname(__FILE__)), // Block.build.js: We register the block here. Built with Webpack.
    3630        array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor'), // Dependencies, defined above.
    3731        null, // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time.
     
    4236    wp_register_style(
    4337        'traveledmap-trip-block-editor-css', // Handle.
    44         plugins_url('dist/blocks.editor.build.css', dirname(__FILE__)), // Block editor CSS.
     38        plugins_url('src/blocks/dist/blocks.editor.build.css', dirname(__FILE__)), // Block editor CSS.
    4539        array('wp-edit-blocks'), // Dependency to include the CSS after it.
    4640        null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time.
     
    5650    );
    5751
     52    // TODO: Introduced in 5.8.0: add_filter('block_categories_all', 'add_new_traveledmap_category', 10, 2);
     53    // but wait to introduce it for older versions
    5854    add_filter('block_categories', 'add_new_traveledmap_category', 10, 2);
    5955
     
    6965            'render_callback' => 'traveledmap_render_callback',
    7066            'attributes' => array(
    71                 'mapUrl' => array('type' => 'string'),
     67                'tripId' => array('type' => 'string'),
    7268                'traveledmap_user_id' => array('type' => 'string'),
    73                 'traveledmap_trip_id' => array('type' => 'string'),
    7469                'traveledmap_trip_base_url' => array('type' => 'string'),
     70                'baseUrl' => array('type' => 'string'),
    7571                'traveledmap_trip_steps' => array('type' => 'string'),
    7672                'traveledmap_disable_widget' => array('type' => 'boolean', 'default' => false),
     
    9086        )
    9187    );
    92 
    93     // Custom scripts
    94     wp_enqueue_script('traveledmap_map-sticky-js');
    9588}
    9689
  • traveledmap-trip-itinerary-embedded-map/trunk/src/metabox/metabox.php

    r2337263 r2636205  
    2222
    2323    $html = '
     24
    2425        <div class="components-panel__row">
    2526            <div>
     
    6768
    6869        <div class="components-panel__row">
    69             <button id="traveledmap-check-trip-button" onclick="handleData()" class="components-button is-button is-primary is-large">
     70            <button id="traveledmap-check-trip-button" onclick="traveledMapMetaboxGetSteps()" class="components-button is-button is-primary is-large">
    7071                <span class="not-loading">Validate</span>
    7172                <span class="loading">Loading ...</span>
     
    7778            const loadedTripId = "' . esc_js($tripId) . '";
    7879
    79             const handleData = () => {
    80               const buttonEl = document.getElementById("traveledmap-check-trip-button");
    81               buttonEl.classList.add("is-loading");
    82               const userId = document.getElementById("traveledmap-user-id").value;
    83               const tripId = document.getElementById("traveledmap-trip-id").value;
    84               const link = `https://www.traveledmap.com/trip/${userId}/${tripId}?isWPMetaboxTest=true`;
    85               const iframeEl = document.getElementById("traveledmap-steps-wrapper").appendChild(document.createElement("iframe"));
    86               iframeEl.setAttribute("src", link);
    87               iframeEl.classList.add("traveledmap-validation-iframe");
    88               buttonEl.disabled = true;
    89               buttonEl.classList.add("is-disabled");
    90 
    91               window.addEventListener("message", (event) => {
    92                 if(!event || !event.data || !event.data.type) {
    93                   buttonEl.classList.remove("is-loading");
    94                   return;
     80            const traveledMapMetaboxStopLoading = () => {
     81                const buttonEl = document.getElementById("traveledmap-check-trip-button");
     82                buttonEl.classList.remove("is-loading");
     83                buttonEl.disabled = false;
     84                buttonEl.classList.remove("is-disabled");
     85            }
     86
     87            traveledMapMetaboxGetSteps = () => {
     88                const buttonEl = document.getElementById("traveledmap-check-trip-button");
     89                buttonEl.classList.add("is-loading");
     90                buttonEl.disabled = true;
     91                buttonEl.classList.add("is-disabled");
     92
     93                const userId = document.getElementById("traveledmap-user-id").value;
     94                const tripId = document.getElementById("traveledmap-trip-id").value;
     95
     96                const endpoint = `' . API_URL . '/httpTripGetSteps`;
     97                fetch(endpoint, {
     98                    method: "POST",
     99                    body: JSON.stringify({ data: { userNickname: userId, tripNickname: tripId }}),
     100                    headers: { "Content-Type": "application/json" },
     101                })
     102                    .then((res) => res.json())
     103                    .then((stepsRes) => traveledMapMetaboxHandleSteps(stepsRes.result, tripId, userId  ))
     104                    .catch(err => {
     105                        console.error("Couldnt fetch steps", err);
     106                        traveledMapMetaboxStopLoading()
     107                    })
     108            }
     109
     110            const traveledMapMetaboxHandleSteps = (steps, tripId, userId) => {
     111                if (!steps) {
     112                    traveledMapMetaboxStopLoading();
     113                    stateEl.classList.add("error");
     114                    console.error("No steps loaded", steps);
    95115                }
    96 
    97                 buttonEl.disabled = false;
    98                 buttonEl.classList.remove("is-disabled");
     116                const stepsHashWithName = steps.reduce((acc, step) => ({
     117                    ...acc,
     118                    [step.hash]: step.name,
     119                }), {});
     120
    99121                const stateEl = document.getElementById("traveledmap-metabox-state");
    100                 switch (event.data.type) {
    101                   case "sendTripStepsHash":
    102                     const tripSteps = event.data.detail.steps;
    103                     stateEl.classList.remove("success");
    104                     stateEl.classList.remove("warning");
    105                     stateEl.classList.remove("error");
    106                     if(loadedTripId === tripId && loadedUserId === userId) {
    107                       stateEl.classList.add("success");
    108                     } else {
    109                       stateEl.classList.add("warning");
    110                     }
    111                     document.getElementById("traveledmap-trip-steps").value = JSON.stringify(tripSteps).replace(/\'/g, "");
    112                     break;
    113                   case "tripDoesNotExist":
    114                     stateEl.classList.remove("success");
    115                     stateEl.classList.remove("warning");
    116                     stateEl.classList.add("error");
    117                     break;
    118                 }
    119                 buttonEl.classList.remove("is-loading");
    120               });
    121             };
     122                stateEl.classList.remove("success");
     123                stateEl.classList.remove("warning");
     124                stateEl.classList.remove("error");
     125                if(loadedTripId === tripId && loadedUserId === userId) {
     126                  stateEl.classList.add("success");
     127                } else {
     128                  stateEl.classList.add("warning");
     129                }
     130                console.log({ stepsHashWithName })
     131                document.getElementById("traveledmap-trip-steps").value = JSON.stringify(stepsHashWithName).replace(/\'/g, "");
     132                traveledMapMetaboxStopLoading();
     133            }
    122134        </script>
    123135    ';
     
    133145function traveledmap_save_meta_box($post_id)
    134146{
    135     $baseUrl = "https://www.traveledmap.com/trip";
     147    $baseUrl = ENV_URL . "/trip";
    136148    if (isset($_POST['traveledmap-user-id'])) {
    137149        $userInput = $_POST['traveledmap-user-id'];
Note: See TracChangeset for help on using the changeset viewer.