Updates from Jay Versluis Toggle Comment Threads | Keyboard Shortcuts

  • Jay Versluis 9:26 am on May 25, 2018 Permalink | Reply  
    Categories: PHP ( 29 )   

    How to declare Classes in PHP 

    Since PHP 5 we can use object oriented features in PHP. This allows us to not only use functions and variables, but also write classes and create instances of them in our code.

    Here’s quick rundown on how to do it.

    Writing and instantiating a Class

    Creating classes is very similar to writing functions. Here’s an example of a class with one method and one variable:

    // creating a basic class with one method
    
    class Test {
      var $testValue = 47;
      function sampleFunction() {
      echo "Hello from the Sample Function!\n";
      }
    }

    Once declared, we can instantiate our class like this:

    // instantiating the class
    
    $myTest = new Test();

    Accessing variables and methods

    Now we can access any variables in our instance using the -> operator like this:

    // accessing a class variable
    
    echo $myTest->testValue;

    The same principle is true for calling methods in our instance.

    // calling a method
    
    $myTest->sampleFunction();

    Constructor Methods

    A method is nothing other than a function really, but when a function is part of a class, we call it method. I guess that’s done to differentiate it from a “classic” function that’s declared outside of a class.

    There’s a special method we can declare inside our classes called a constructor method. It works just like a regular method, with the only difference that the constructor is called automatically when the class is instantiated. This is useful if we want something to happen as soon as our class is used the first time.

    Constructors have a special name (__construct); here’s an example:

    // creating a class with a constructor method
    
      class Test {
        function __construct() {
          echo "Hello from the Test Constuctor!\n";
      }
    }

    Extending a Class

    Classes can inherit functionality from other classes. When we do that we create an exact duplicate of an existing class. This is useful if we want to change the behaviour of a class, for example by overwriting existing methods or values.

    Here’s how we can extend a class:

    // extending a class
    
    class MegaTest extends Test {
      function __construct() {
      parent::__construct();
      echo "And Hello again from the extended class constructor.\n";
      }
    }

    Overwriting methods and variables is as easy as simply re-declaring them using their original names. Extended classes can be extended again, but a class can only ever inherit from another single class.

    Notice the use of the parent:: keyword in our constructor method. When our extended class is instantiated, our constructor method is called. If our parent class also has a constructor method (which is optional), we must call this before doing anything else to make sure that any functionality that is setup is being kicked off.

    In essence, anything prefixed with the parent:: keyword will call the related function “one level up”.

    Now we can create an instance of our extended class as described above:

    // instantiate the extended class
    
    $myMegaTest = new MegaTest();




     
  • Jay Versluis 9:24 am on May 24, 2018 Permalink | Reply
    Tags:   

    Categories: Linux ( 101 ), Plesk ( 76 )   

    How to switch off emails from Anacron in Plesk Onyx 

    In 2011 I wrote an article about how to avoid emails from Dr. Web. In it I was discussing how to switch off these notifications, which are generated when the Dr. Web service updates itself.

    Here’s an example:

    /etc/cron.daily/drweb-update:
    
    Dr.Web update details:
    Update server: http://update.msk5.drweb.com/plesk/700/unix
    Update has begun at Fri May 18 03:53:47 2018
    Update has finished at Fri May 18 03:53:54 2018
    
    Following files have been updated:
     /var/drweb/bases/drwdaily.vdb
     /var/drweb/bases/drwtoday.vdb
     /var/drweb/bases/dwmtoday.vdb
     /var/drweb/bases/dwntoday.vdb
     /var/drweb/bases/dwrtoday.vdb
     /var/drweb/bases/timestamp
     /var/drweb/updates/timestamp

    Sadly, as of 2018, there is still no way to switch these emails off via a tick box from inside Plesk. It was relatively simple though to redirect the output to /dev/null in Linux, thereby avoiding yet another email in our already overflowing inbox.

    In the latest version of Plesk, the earlier approach is no longer working.

    Instead, we can tweak the Dr. Web configuration file at /etc/drweb/drweb32.ini. In the Updater section, find the following block of code:

    # CronSummary = {Boolean}
    # Enables output of update session log to stdout.
    CronSummary = yes

    Now change the CronSummary value to no and restart the Dr. Web service. On CentOS it’s called drwebd:

    systemctl restart drwebd

    This will ask Dr. Web to not send us an email when he updates himself. The great thing about this solution is that we can still get command line output if we want to run the service manually.

    However, if the service is updated in the future, those emails may re-appear because it’s likely that our configuration file may be overwritten. Only time will tell I guess!





     
  • Jay Versluis 9:46 am on May 23, 2018 Permalink | Reply
    Tags: ,   

    Categories: How To ( 34 )   

    Fan Maintenance on my Samsung NC10 

    My Samsung NC10 Netbook has been in constant operation since 2013, for 24 hours a day, 7 days a week. It’s doing a great job as our internal office server, purring along quietly running CentOS 6.

    When I put it in operation 5 years ago, I made sure no mechanical parts we being used anymore to avoid wear and tear: I’ve added an SSD, and once a day valuable data is backed up on a permanently mounted SD card.

    The only mechanical thing still in use is the internal fan. I knew the day would come on which the poor thing would either give up and need replacing, or at least require some maintenance to make it go a few extra miles. Well, that day has come at the beginning on this month, when I noticed a bit of rattling noise that started happening behind my big monitor. That’s where I keep the little guy.

    Turns out that the fan can be easily whipped back into shape with a drop of bicycle oil. It’s really easy to disassemble too – let me show you how I did it.

    (More …)





     
  • Jay Versluis 9:45 am on May 22, 2018 Permalink | Reply
    Tags: ,   

    Categories: Linux ( 101 )   

    How to host multiple websites with Apache 

    The Apache web server has a convenient feature called Name-based Virtual Hosting. This function allows us to have a single LAMP Stack server configured on one IP address, but serve a different set of files depending on which domain is being requested.

    This sounds more complicated than it is. Say we had example1.com and example2.com, both of which are to be separate websites, but both domains point to the same IP address. Apache’s Name-based Virtual hosting makes this possible. In fact, this feature forms the basis of 90% of this planet’s shared hosting business.

    Let’s see how to do this in CentOS 6 and 7. (More …)





     
  • Jay Versluis 8:51 am on May 20, 2018 Permalink | Reply
    Tags:   

    Categories: Themes ( 28 ), WordPress ( 144 )   

    How to increase the font size in Automattic's P2 Post Box 

    Front page posting is what the P2 theme is all about – but I personally find the default font size on the front page a bit too small. Perhaps it’s my raging tired eyes. In every child theme I write for P2, I usually increase this – both for immediate posting, as well as for text editing (which also happens inline on the front page).

    To do that, add the following to your style.css file:

    /* larger text for post box and editing */
      #postbox textarea #posttext, textarea.posttext {
      font-size: 1.3em !important;
    }

    This will address the font size for both inline editing as well as posting. I’ve chosen 1.3em because it integrates well into my other settings, but feel free to choose something larger or smaller (1.2em or 1.4em respectively). You can also choose a defined point size like 16px if you like.

    To also address the text box for default comments, we can add this:

    #respond textarea, .textedit textarea {
    	font-size: 1.3em !important;
    	color: #555;
    }
    

    By default the font colour is black, so #555 tweaks it to be the dark grey that the rest of the P2 text has.

    Happy hacking!





     
  • Jay Versluis 9:44 am on May 19, 2018 Permalink | Reply
    Tags:   

    Categories: PHP ( 29 )   

    How to update legacy constructor methods in PHP 7 

    When I was fiddling with my P2 Categories theme last week, debug mode generated several warnings when run in PHP 7.2.1. That’s because class methods are no longer allowed to have the same name as the class itself.

    This was allowed in PHP 5 and earlier, but from what I gather it’s no longer the way to do things. Back then such methods were used as constructors, or in other words, methods that would be run automatically when the class is instantiated.

    Let’s take an example from the P2 theme. Here’s the beginning of the P2 class as of version 1.5.8:

    class P2 {
    	// ...
    	function P2() {
                // ...
    

    This will work just fine in PHP 5, but will generate a warning in PHP 7 (even though the code will execute). To update this, all we need to do is change our function name to __construct (notice the two underscores at the beginning of the name):

    class P2 {
    	// ...
    	function __construct() {
                // ...
    

    Anything inside the __construct() function is executed as soon as an instance of the class is created.

    When updating legacy code, there is the risk that the previous function name is called during instantiation. Consider this:

    class P2_Post_List_Creator extends P2_List_Creator {
    	var $form_action_name = 'p2-post-task-list';
    
    	function P2_Post_List_Creator() {
    		parent::P2_List_Creator();
                    // ...
    

    Here a class extends another class and calls a constructor method of the parent class by name. If we had just patched the parent class with _construct(), the child class would throw an “undefined function” error.

    To avoid this we’ll also have to update any calls made to the original constructor method, like so:

    class P2_Post_List_Creator extends P2_List_Creator {
    	var $form_action_name = 'p2-post-task-list';
    
    	function P2_Post_List_Creator() {
    		parent::__construct();
                    // ...
    

    And that’s really all there’s to it.





     
  • Jay Versluis 9:52 am on May 18, 2018 Permalink | Reply
    Tags: , Releases   

    Categories: Themes ( 28 ), WordPress ( 144 )   

    P2 Categories – Version 1.6 released 

     

    Last week I found some time to update my fork of Automattic’s P2 theme, aptly titled P2 Categories. In addition to all the greatness of P2, it’s been adding front-page category posting since 2013 (and hasn’t seen an update since then either).

    Here’s what’s new in the latest version:

    • fixed the drop down menu, which was no longer working since Safari 10
    • rewrote the whole theme from scratch, based on P2 v1.5.8 (2016)
    • fixed a bug that would not show the correct number of posts in a category
    • added new p2-categories-functions.php file
    • updated a call to a deprecated WordPress function with wp_get_current_user()
    • fixed several PHP 7 deprecation warnings
    • updated class constructors to use __construct() methods
    • hunted down undocumented features and documented them
    • added a changelog file

    P2 is no longer maintained by Automattic, except for dire security patches. I’ve noticed several deprecation notices, both for WordPesss 4.9 as well as PHP 7.2.1 and fixed those as well.

    Get the latest version on GitHub

    You can download the latest version of the theme over on the official GitHub Repository. Feel free to examine the code, tinker with amendmends and share with everyone who wants to use P2 with categories:

    Why is this version not on WordPress?

    I had submitted previous versions of P2 Categories to the WordPress repos too, and you can still download version 1.5 there. However, since 2013 web standards have become more strict, and there’s no way my theme would pass modern tests the theme team are using for new submissions. Sadly I’m not a web developer and it’ll be very difficult for me to update aspects of the theme I don’t fully understand.

    Having said that, if you know how to pass modern web standard tests, feel free to send me a pull request on GitHub.





     
  • Jay Versluis 10:04 am on May 17, 2018 Permalink | Reply
    Tags:   

    Categories: Themes ( 28 ), WordPress ( 144 )   

    How to display categories in Automattic’s P2 Theme 

    By default, Automattic’s phenomenal P2 theme does not support posting into categories from the front page, it only supports tags. My fork of the theme called P2 Categories does that though and lets you conveniently select a category from a drop down menu right there on the front page.

    I wrote an update to it last week, and in so doing my article from 2013 came in handy that explains how to add this functionality to P2. Nothing much has changed in the source code, so it’s still relevant and accurate.

    What the above article did not explain however was how to show which category a post belongs to. And because it’s still fresh in my  memory how to do this, I thought I’d better write it down for next time (and anyone who’s interested in how to do it).

    (More …)





     
  • Jay Versluis 9:56 am on May 16, 2018 Permalink | Reply  
    Categories: Plesk ( 76 )   

    How to hide the Promo Box in Plesk Customer View 

    There’s a small Promo Box on the right hand side in Plesk’s Customer View. This box can be a little confusion for users – especially when it shows products and extensions with highly cryptic names. Here’s an example:

    Lucky for us, there’s an easy way to remove it. All we need to do is create a vile called /usr/local/psa/admin/conf/panel.ini and add the following content to it:

    [promos]
    ; Disable other products promotions
    
    enabled = off

     

    This file is read by Plesk every time the panel loads, telling Plesk what to display in the admin interface. With the above command, the Promo Box is suppressed.

    There’s no need to restart anything, simply reload the page in your web browser and the box will be gone.





     
  • Jay Versluis 9:24 am on May 15, 2018 Permalink | Reply
    Tags:   

    Categories: Linux ( 101 ), PHP ( 29 )   

    How to install PHP 7.x on a CentOS LAMP Stack 

    By default, CentOS 7 comes with support for PHP 5.4. Sadly that version has reached the end of its life in 2015and is no longer updated by the developers. If we want to stay up to date with the latest software, we may want to upgrade (if our applications are working with newer versions of PHP).

    For CentOS users this either means to compile cutting edge versions from source and tweaking lots of scary system configurations – or dipping into the power of Software Collections. These are official pre-compiled packages by the software vendor, designed to run newer versions of software alongside those that are provided by default.

    At the time of writing, PHP 7.2 is available but it’s not part of the software collections yet, so we’ll use  PHP 7.1 with FPM support under Apache (as it’s the recommended way to do so).

    Let’s begin! (More …)





     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel