Plugin Directory

Changeset 2526622


Ignore:
Timestamp:
05/05/2021 12:00:26 PM (5 years ago)
Author:
pressmate
Message:

Added Republish option and also improved the listing page functionality

Location:
makestories-helper/trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • makestories-helper/trunk/api/publish.php

    r2521956 r2526622  
    5959        foreach ($parsed['media'] as $media) {
    6060            $imageurl = $media['url'];
    61             if(strpos($imageurl, "ss.makestories.io/get") > 0){
    62                 continue;
    63             }
    6461            $name = $imageurl;
    6562            $nameExploded = explode("?",$imageurl);
     
    7673            $attach_id = false;
    7774            if( post_exists($atta_title) && !$forceUploadMedia ) {
    78                 $getAttachment = wp_get_attachment_by_post_name( $atta_title );
     75                if(wp_validate_boolean(apply_filters("ms_check_for_duplicate_media", true))){
     76                    $getAttachment = wp_get_attachment_by_post_name( $atta_title );
     77                }
    7978
    8079                if($getAttachment) {
     
    8786                        "into_else" => true,
    8887                        "exists" => post_exists($atta_title),
    89                         "i" => wp_get_attachment_by_post_name( $atta_title ),
    9088                    ];
    9189                }
     
    107105        }
    108106
    109         if(ms_is_categories_enabled()){
     107        if(ms_is_categories_enabled() && !isset($_REQUEST['is_republish'])){
    110108            $category = ms_get_default_category();
    111109            if(isset($_REQUEST['category']) && !empty($_REQUEST['category'])){
     
    195193        $attach_id = false;
    196194        $forceUploadMedia = ms_get_options()['forceUploadMedia'];
    197         $getAttachment = wp_get_attachment_by_post_name( $atta_title );
     195        if(wp_validate_boolean(apply_filters("ms_check_for_duplicate_media", true))){
     196            $getAttachment = wp_get_attachment_by_post_name( $atta_title );
     197        }
     198        if(isset($_REQUEST['is_republish']) && wp_validate_boolean($_REQUEST['is_republish'])){
     199            $forceUploadMedia = true;
     200        }
    198201        if( post_exists($atta_title) && !$forceUploadMedia && $getAttachment) {
    199202            $attach_id = $getAttachment->ID;
    200203        }else{
     204
     205            $wp_filetype = wp_check_filetype(basename($filename), null );
     206            if(empty($wp_filetype['type'])){
     207                $wp_filetype = ms_find_filetype($imageurl);
     208            }
     209
     210            if(isset($wp_filetype['ext']) && !empty($wp_filetype['ext']) && !msEndsWith($filename, $wp_filetype['ext'])){
     211                $filename = $filename.".".$wp_filetype['ext'];
     212            }
     213
    201214            $uploaddir = wp_upload_dir();
    202215            $uploadfile = $uploaddir['path'] . '/' . $filename;
     
    206219            fclose($savefile);
    207220
    208             $wp_filetype = wp_check_filetype(basename($filename), null );
    209221            $attachment = array(
    210222                'post_mime_type' => $wp_filetype['type'],
     
    214226            );
    215227            $attach_id = wp_insert_attachment( $attachment, $uploadfile );
     228            if($attach_id){
     229                $attach_data = wp_generate_attachment_metadata($attach_id, $uploadfile);
     230                wp_update_attachment_metadata($attach_id, $attach_data);
     231            }
    216232        }
    217233
     
    552568            foreach (MS_DOMAINS as $domain){
    553569                if(strpos($imageUrl, $domain) !== false){
    554                     $media[] = $imageUrl;
     570                    $media[] = ms_replace_domains($imageUrl);
    555571                    break;
    556572                }
     
    559575    }
    560576    header("Content-Type: application/json");
    561     print_r(json_encode($media));
     577    print_r(json_encode(array_unique($media)));
    562578    die();
     579}
     580
     581function ms_replace_domains($url){
     582    $url = str_replace("storage.googleapis.com/makestories-202705.appspot.com",MS_CDN_LINK, $url);
     583    $url = str_replace("storage.googleapis.com/cdn-storyasset-link",MS_CDN_LINK, $url);
     584    return $url;
    563585}
    564586
  • makestories-helper/trunk/assets/css/ms-style.css

    r2481763 r2526622  
    162162
    163163.default-stories h3 {
    164     margin-bottom: 40px;
     164    margin-bottom: 10px;
     165    margin-top: 15px;
     166    font-size: 2rem;
     167    text-overflow: unset;
     168    white-space: normal;
     169    -webkit-box-orient: vertical;
     170    display: -webkit-box;
     171    -webkit-line-clamp: 2;
     172    max-height: 3em;
     173    overflow: hidden;
     174}
     175.default-stories p{
     176    margin-bottom: 0;
     177    margin-top: 0;
     178    font-size: 1.2rem;
     179    text-decoration: none;
    165180}
    166181
    167182.story-thumb {
    168     margin-bottom: 3%;
    169     margin-right: 0;
     183    margin: 0 15px;
     184    margin-bottom: 15px;
    170185    transition: transform 0.5s;
    171     flex-basis: 17%;
     186    flex-basis: 250px;
    172187    border-radius: 6px;
    173     box-shadow: 0 0 8px 0 rgb(53 53 53 / 18%);
     188    box-shadow: 0 0 8px 0 rgba(53,53,53,0.18);
     189    padding: 12px;
     190}
     191
     192.story-thumb a{
     193    text-decoration: none;
    174194}
    175195
    176196.story-thumb figure {
    177     padding: 12px;
     197    position: relative;
     198    padding-bottom: 133%;
    178199}
    179200
     
    185206.story-thumb img {
    186207    width: 100%;
    187     border-radius: 6px;
     208    border-radius: 4px;
     209    position: absolute;
     210    top: 0;
     211    left: 0;
     212    height: 100%;
     213    object-fit: cover;
    188214}
    189215
     
    211237@media screen and (max-width: 995px) {
    212238    .story-thumb {
    213         flex-basis: 22%;
    214         margin-right: 3.5%;
    215         margin-bottom: 2rem;
    216     }
    217 
    218     .story-thumb:nth-child(6n) {
    219         margin-right: 3.5%;
    220     }
    221 
    222     .story-thumb:nth-child(4n) {
    223         margin-right: 0;
    224     }
    225 
     239        flex-basis: 225px;
     240    }
     241    .default-stories{
     242        width: 90%;
     243    }
    226244}
    227245
    228246@media screen and (max-width: 550px) {
    229247    .story-thumb {
    230         flex-basis: 45%;
    231         margin-right: 8%;
    232         margin-bottom: 2.5rem;
    233     }
    234     .story-thumb:nth-child(2n) {
    235         margin-right: 0;
     248        flex-basis: 40vw;
     249    }
     250    .default-stories{
     251        width: 94%;
    236252    }
    237253}
  • makestories-helper/trunk/config.php

    r2521947 r2526622  
    1010define("MS_CDN_URL", "https://api.makestories.io/wp/".MS_PLUGIN_VERSION."/");
    1111
    12 
     12/**
     13 * Base server url
     14 */
     15define("MS_BASE_SERVER_URL", "https://server.makestories.io/");
    1316
    1417/**
    1518 * Url for viewing live preview of story being edited.
    1619 */
    17 define("MS_PREVIEW_URL", "https://server.makestories.io/preview/");
     20define("MS_PREVIEW_URL", MS_BASE_SERVER_URL."preview/");
    1821
    1922/**
     
    7275]);
    7376
     77define("MS_CDN_LINK", "cdn.storyasset.link");
     78
    7479define("MS_DOMAINS", [
    7580    "storage.googleapis.com/makestories",
     81    "storage.googleapis.com/cdn-storyasset-link",
    7682    "makestories.io",
    7783    "images.unsplash.com",
  • makestories-helper/trunk/helpers.php

    r2521947 r2526622  
    3030}
    3131
     32/**
     33 * Function to get MIMETYPE of URL
     34 * @param $imageurl string
     35 * @return bool|string
     36 */
     37function ms_find_filetype($imageurl){
     38    $c = curl_init(MS_BASE_SERVER_URL.'get-image-meta?url='.$imageurl);
     39    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
     40
     41    $details = curl_exec($c);
     42
     43    if (curl_error($c)){
     44        die(json_encode(["success" => false, "error" => "Error occurred while fetching image type."]));
     45    }
     46    // Get the status code
     47    curl_close($c);
     48    return json_decode($details, true);
     49}
     50
     51function msEndsWith( $haystack, $needle ) {
     52    $length = strlen( $needle );
     53    if( !$length ) {
     54        return true;
     55    }
     56    return substr( $haystack, -$length ) === $needle;
     57}
    3258function ms_get_options(){
    3359    $options = get_option('mscpt_makestories_settings');
  • makestories-helper/trunk/pages/category-structure.php

    r2521956 r2526622  
    8888            </tr>
    8989            <tr>
    90                 <th scope="row"><label>Update Story Media</label></th>
     90                <th scope="row"><label></label></th>
    9191                <td>
    9292                    <p class="submit">
     
    112112                    <p class="submit">
    113113                        <input type="submit" id="run-media-updates" name="submit" class="button button-primary" value="Run Updates"/>
     114                    </p>
     115                </td>
     116            </tr>
     117        </table>
     118        <table class="form-table" role="presentation">
     119            <tbody>
     120            <tr>
     121                <td colspan="2">
     122                    <hr/>
     123                </td>
     124            </tr>
     125            <tr>
     126                <th scope="row"><label>Republish all stories</label></th>
     127                <td>
     128                    <p class="description">This button re-publishes all the stories on your wordpress site. Helpful for bulk changes and in case you have not yet migrated from your old image urls.</p>
     129                    <p id="summary-republish"></p>
     130                    <p class="submit">
     131                        <input type="submit" id="run-story-updates" name="submit" class="button button-primary" value="Republish All"/>
    114132                    </p>
    115133                </td>
     
    213231
    214232            });
     233
     234            $("#run-story-updates").on("click", function(){
     235                if(alreadyRunning){
     236                    return false;
     237                }
     238                alreadyRunning = true;
     239                let button = $(this);
     240                let summary = $("#summary-republish");
     241                $.ajax({
     242                    url: baseApiUrl,
     243                    data: {
     244                        _wpnonce: nonce,
     245                        action: "ms_get_published_posts_all",
     246                    },
     247                    success: function(data){
     248                        if(data && data.posts){
     249                            let numOfPosts = Object.keys(data.posts).length;
     250                            let summaryPrepend = `Found ${numOfPosts} posts. Now fetching details and updating each post.`;
     251                            summary.html(summaryPrepend);
     252                            let chain = Promise.resolve();
     253                            let index = 1;
     254                            Object.values(data.posts).map(({ post_id, story_id, title }) => {
     255                                chain = chain.then(() => republishStory(post_id, story_id, title, summaryPrepend+`<br>Publishing story ${index++} out of ${numOfPosts}`));
     256                            });
     257                            chain.then(() => {
     258                                summary.html("All stories updated. Thank you!")
     259                            });
     260                        }
     261                    },
     262                    error: function(){
     263                        button.html("Run Updates");
     264                        summary.html("Some error occurred while fetching story details. Please try again in some time!");
     265                    }
     266                });
     267
     268                button.html("Running Updates...");
     269                summary.html("Fetching story details. Please do not leave the page");
     270
     271
     272                function republishStory(post_id, story, slug, summaryPrepend){
     273                    return new Promise(resolve => {
     274                        $.ajax({
     275                            url: baseApiUrl,
     276                            data: {
     277                                _wpnonce: nonce,
     278                                post_id,
     279                                story,
     280                                slug,
     281                                is_republish: true,
     282                                action: "ms_publish_post",
     283                            },
     284                            success: function(response){
     285                                const { media } = response || {};
     286                                if(media && Array.isArray(media) && media.length){
     287                                    let chain = Promise.resolve();
     288                                    let index = 1;
     289                                    media.map((url) => {
     290                                        chain = chain.then(() => {
     291                                            summary.html(`${summaryPrepend}.<br/>Downloading media: ${index++} out of ${media.length}`);
     292                                            return replaceMediaInStory(url.imageurl, post_id)
     293                                        });
     294                                    });
     295                                    chain.then(resolve);
     296                                }else{
     297                                    resolve();
     298                                }
     299                            },
     300                            error: (err) => {
     301                                resolve();
     302                            },
     303                        });
     304                    });
     305                }
     306
     307                function replaceMediaInStory(imageurl, post_id){
     308                    return new Promise(resolve => {
     309                        $.ajax({
     310                            url: baseApiUrl,
     311                            data: {
     312                                _wpnonce: nonce,
     313                                action: "ms_upload_image_to_media_library",
     314                                post_id,
     315                                imageurl,
     316                                is_republish: true,
     317                            },
     318                            success: resolve,
     319                            error: (err) => {
     320                                resolve();
     321                            }
     322                        });
     323                    })
     324                }
     325
     326            });
    215327        })
    216328    </script>
  • makestories-helper/trunk/templates/prepare-story-vars.php

    r2516559 r2526622  
    1010$logo = '';
    1111$title = get_the_title();
     12$publishDate = get_the_date();
    1213try{
    1314    if($publisherDetails && $parsedDetails = json_decode($publisherDetails, true)){
    14         $posterPortrait = $parsedDetails['poster-portrait-src'];
    15         $posterLandscape = $parsedDetails['poster-landscape-src'];
    16         $posterSquare = $parsedDetails['poster-square-src'];
    17         $logo = $parsedDetails['poster-square-src'];
    1815        if($posterPortrait && !empty($posterPortrait)){
    1916            $posterImage = $posterPortrait;
    2017        }
     18        $posterPortrait = isset($parsedDetails['poster-portrait-src']) ? $parsedDetails['poster-portrait-src'] : $posterImage;
     19        $posterLandscape = isset($parsedDetails['poster-landscape-src']) ? $parsedDetails['poster-landscape-src'] : $posterImage;
     20        $posterSquare = isset($parsedDetails['poster-square-src']) ? $parsedDetails['poster-square-src'] : $posterImage;
     21        $logo = isset($parsedDetails['poster-square-src']) ? $parsedDetails['poster-square-src'] : '';
    2122    }
    2223}catch (ErrorException $e){}
  • makestories-helper/trunk/templates/single-story.php

    r2516559 r2526622  
    44            <img src="<?php echo $posterImage ?>" />
    55        </figure>
     6        <div class="bottom-text">
     7            <h3><?php echo $title; ?></h3>
     8            <p><?php echo $publishDate; ?></p>
     9        </div>
    610    </a>
    711</div>
Note: See TracChangeset for help on using the changeset viewer.