wordpress-tag-cloud

WordPress Link Preview like FB & LinkedIn

There was quite a few requests on making a plugin for wordpress which would insert excerpts from a link just like how facebook and linkedin does. I had earlier created this as an independent jquery plugin –  Facebook type link preview

But then it looks like wordpress users really needs this as a WP Plugin – https://wordpress.org/ideas/topic/auto-generate-facebook-style-link-preview-when-inserting-url-into-editor

I have developed a basic plugin which does this.  I just submitted this to wordpress, but it looks like it will take sometime for the wordpress juries to verify this and make it available.

However wordpress link preview can be downloaded from here –

Download wordpress link preview

Screenshots:

  • Click Insert Url

 

screenshot-1

 

  • Enter the url

screenshot-2

 

  • Click Ok and wait for 5 seconds. There is no progress bar.

screenshot-3

 

  • Demo Output – This is how it looks like.

screenshot-5

  • Dont like the UI ? Change the css in the options page.screenshot-4

 

dependency injection

Dependency Injection in PHP

Dependency Injection is a software design pattern that allows avoiding hard-coding dependencies and makes possible to change the dependencies both at runtime and compile time. 

Every project will have lots of classes. Few classes will depend on other classes.  For example, we have a Library class which gets the name of the books from the database. So our library class is dependent on the database class.

 In the above example,  the library object will have full access to database which is what we want. But the way, we are providing this access is not great.

The Library class is tightly coupled with the Database class, and while this might not look bad on this particular example. Let’s imagine that we want to reuse the Library class elsewhere, it will fail if the database class is not included.

Now lets look at this code.

That’s a nice way of injecting the Database class inside the Library class.  And the Library class can now be used in any application. We just have to pass the database object to the Library class to the application. But what if the parameters are more in the constructor ?

Like this:

Over the time it gets messy.  I don’t like this much.

So the next good way to take over this situation is by using the setter injection.

This is way too nice and neat. But you will soon realize that over the period of time, this will start smelling bad. If you have to use this class at 5 different places, you will have to initiate this 5 times, writing the same code. That’s crap.

What now ? We need something that is more robust and can control class initialization at one place.  Lets look at the below code.

We can then call this by:

This code is much more flexible and it has lot of benefits. Lets say you want to access books from two different database.

Dependency injection really is the answer to more maintainable, testable, modular code. Its very easy to do unit tests with such modular code. 

Conclusion:

According to me, the constructor should not be overloaded with parameters.  Its better to inject the dependencies as and when required. However few users prefer to pass everything together in the constructor for easy tracing. So this might differ from users to users. Dependency Injection greatly reduces cost in the long run and makes the code highly scalable with less effort.

If you have any points, please do comment.

Webblog of Abhishek Saha