<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lea Cohen</title>
    <description>The latest articles on DEV Community by Lea Cohen (@leac).</description>
    <link>https://dev.to/leac</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F305838%2F7c172183-9cbc-4d0f-88e2-8a2fe91a993f.jpeg</url>
      <title>DEV Community: Lea Cohen</title>
      <link>https://dev.to/leac</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leac"/>
    <language>en</language>
    <item>
      <title>The Absence of an ‘S’ Can Create a Big Mess</title>
      <dc:creator>Lea Cohen</dc:creator>
      <pubDate>Mon, 25 May 2020 13:10:04 +0000</pubDate>
      <link>https://dev.to/leac/the-absence-of-an-s-can-create-a-big-mess-2p5</link>
      <guid>https://dev.to/leac/the-absence-of-an-s-can-create-a-big-mess-2p5</guid>
      <description>&lt;p&gt;After deploying one of our sites to production, one of my colleagues wrote to me on Slack:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Look at the image at the bottom of the front page. In Test, the image is a rectangle, and in production, it’s square. How can that be? The Production code and images are an exact duplicate of the Test site’s code and images.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z0OGIG7p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ew897mczf5pn3v0a7vm8.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z0OGIG7p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ew897mczf5pn3v0a7vm8.jpeg" alt="Both images were 280 pixels wide, but the image on the Test server was 220 pixels high, while the image on Production was 280 pixels high"&gt;&lt;/a&gt;&lt;/p&gt;
The left side shows the rectangle image on the test site; the right side is the square image on Production. Credit to Rachel Bratt-Tannenbaum for artistically designing the screenshots



&lt;p&gt;Weird. This is a standard WordPress site built with Elementor and using one of the Pojo themes. What on earth could have caused this difference in sizes and proportions?&lt;/p&gt;

&lt;h2&gt;
  
  
  First Step — Dev Tools
&lt;/h2&gt;

&lt;p&gt;I started by checking the Dev Tools and found that not only were the sizes different, but &lt;strong&gt;the image source was different&lt;/strong&gt;: the image in the test site came from &lt;strong&gt;the year and month folder&lt;/strong&gt; in the uploads folder, and the image on production came from &lt;strong&gt;the thumbs folder&lt;/strong&gt; in the uploads folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ocbh2yqb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/a0yp9crcg6n1n91sfl65.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ocbh2yqb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/a0yp9crcg6n1n91sfl65.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
The left side shows the image source on the test site is in the thumbs folder; the right side is the image source on Production, in the year/month folder. Credit to Rachel Bratt-Tannenbaum for artistically designing the screenshots



&lt;p&gt;The colleague who brought this problem to my attention gave me a lead: the CSS class given to the image was &lt;code&gt;small-thumbnail&lt;/code&gt;. A quick search led her to the &lt;code&gt;recent_post.php&lt;/code&gt; file in the theme’s &lt;code&gt;content&lt;/code&gt; folder, and that’s where I started my journey in search of the culprit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--djCptXIX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e08ouy1olamb82wnsp49.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--djCptXIX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e08ouy1olamb82wnsp49.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Down the Rabbit Hole
&lt;/h2&gt;

&lt;p&gt;The code that retrieves the image url in &lt;code&gt;content/recent_post.php&lt;/code&gt; was easy to locate:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$image_url = Pojo_Thumbnails::get_post_thumbnail_url( array( 'width' =&amp;gt; '360', 'height' =&amp;gt; '360', 'crop' =&amp;gt; true, 'placeholder' =&amp;gt; true ) ) )
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This was my starting point, and from there, I started following the clues. The first clue led me to the &lt;code&gt;get_post_thumbnail_url&lt;/code&gt; function, which resides in the &lt;code&gt;\core\helpers\class-pojo-thumbnails.php&lt;/code&gt; theme file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;get_post_thumbnail_url&lt;/code&gt; calls a few WordPress functions. Those aren’t suspects because I know them, and none of them retrieves a url with the &lt;code&gt;thumbs&lt;/code&gt; folder in it. The only Pojo theme-specific function that’s called is &lt;code&gt;get_attachment_image_src&lt;/code&gt;, and that’s where I continued my search.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;get_attachment_image_src&lt;/code&gt;, like its predecessor &lt;code&gt;get_post_thumbnail_url&lt;/code&gt;, mainly calls WP functions. Here, the only theme-specific function that’s called is &lt;code&gt;get_thumb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;get_thumb function&lt;/code&gt; calls the Class’s &lt;code&gt;init()&lt;/code&gt; function which, in turn, includes the &lt;code&gt;BFI_Thumb.php&lt;/code&gt; file which reside in the Pojo theme’s &lt;code&gt;/core/helpers/bfi_thumb&lt;/code&gt; folder. Then the &lt;code&gt;get_thumb&lt;/code&gt; function calls the &lt;code&gt;bfi_thumb&lt;/code&gt; function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here, the function doesn’t directly call another function but uses the &lt;a href="https://www.php.net/manual/en/function.call-user-func.php"&gt;&lt;code&gt;call_user_func&lt;/code&gt;&lt;/a&gt; PHP function to call the next function dynamically. &lt;code&gt;call_user_func&lt;/code&gt;'s first parameter is a callback function. In our case, this parameter is an array, in which the first item is a class name, and the second item is the function within that class. While the second item was explicitly defined — the ‘thumb’ function — the class was determined using the &lt;code&gt;BFI_Class_Factory::getNewestVersion&lt;/code&gt; function, which retrieved the newest version of &lt;code&gt;BFI_Thumb&lt;/code&gt;. I’ll spare you the intricacies of the Newest Version calculation and tell you that in our installation, the class name is &lt;code&gt;BFI_Thumb_1_2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thankfully, the &lt;code&gt;BFI_Thumb_1_2&lt;/code&gt; class resides in the same file, so all that was needed was that I hop over to its &lt;code&gt;thumb&lt;/code&gt; function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nearing the End
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;thumb&lt;/code&gt; function, in contrast to its predecessors, isn’t a short little 10–20 lines function. This one is a lengthy, detailed ~170 line function, with many conditions and exit-points. On the other hand, it’s very well commented.&lt;/p&gt;

&lt;p&gt;Luckily, the line in which the divergence of the Test site from production occurred, was pretty early on:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As I debugged the code (shamefully, I used the &lt;code&gt;echo&lt;/code&gt; function to do the debugging), I got to the point of divergence:&lt;br&gt;
The code in Production entered this condition, and the Test code — didn’t:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;strpos( $url, $upload_url ) !== false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The condition is: Does the image url contain the upload url? In other words — is the image inside this site’s uploads folder?&lt;br&gt;
Now, &lt;em&gt;&lt;strong&gt;I knew&lt;/strong&gt;&lt;/em&gt; that the images on both sites ARE in the upload folder. So why isn’t the condition being met on Test???&lt;/p&gt;

&lt;p&gt;I checked the values of &lt;code&gt;$url&lt;/code&gt; and &lt;code&gt;$upload_url&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$url = https://test-server/site-name/wp-content/uploads/2019/06/בריא1.jpg

$upload_url = http://test-server/site-name/wp-content/uploads
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Do you see the difference in the url?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;$url&lt;/code&gt;’s protocol is HTTPS, and the &lt;code&gt;$upload_url&lt;/code&gt;'s protocol is HTTP!&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Somewhere, somehow, even though the site uses the https protocol, the upload folder’s definition omitted the &lt;code&gt;s&lt;/code&gt;. &lt;em&gt;&lt;strong&gt;That’s&lt;/strong&gt;&lt;/em&gt; the reason that the code didn’t enter the condition!&lt;/p&gt;

&lt;p&gt;Now all that was left was for me to find where the &lt;code&gt;$upload_url&lt;/code&gt; is defined. The code states that it’s defined in the &lt;code&gt;$upload_info[‘baseurl’]&lt;/code&gt; parameter, but where is that parameter set? Hmmm… baseurl. Sounds familiar. Don’t I know that from somewhere? Isn’t that defined in the wp-admin of the site — in Settings -&amp;gt; General?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WTO_I8mB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9lyyem1spycagut1pvq5.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WTO_I8mB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9lyyem1spycagut1pvq5.jpeg" alt="site address and WordPress address both started with http"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is! I fixed the two URLs that set the site address, adding an &lt;code&gt;s&lt;/code&gt; after the HTTP— and voila! The image on the Test site miraculously became identical to the one on the Production site.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why were they a different size and proportion?
&lt;/h3&gt;

&lt;p&gt;Although I didn’t go on to read the rest of the code, I assume that when the image IS in the uploads dir, the code creates a copy and puts it in the thumbs folder and returns that address, and when the image isn’t there — the image’s original address is returned.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Have We Learned?
&lt;/h2&gt;

&lt;p&gt;Not only is it important to cross your T’s and dot your I’s, but mind your S’s too!&lt;/p&gt;

</description>
      <category>php</category>
      <category>wordpress</category>
      <category>debugging</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
