Advertisement:

Author Topic: [TUTORIAL] Howto: Create a plugin myself  (Read 5855 times)

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2519
[TUTORIAL] Howto: Create a plugin myself
« on: December 28, 2014, 06:47:24 pm »
Hi there,

since I have a wish for a plugin and I can't ask somebody to create it for me I will create one myself  8)

HOWEVER, I have no clue where to start!

WHERE TO START?
I figured it will be a good idea to just start one based on an existing Plugin of which I think comes closest to what I want.
In this case that will be the 'Watchlist' plugin and the 'Print news' plugin, both freely available and a good starting point for my needs.

WHAT?
The plugin I want to create will read a specific dutch RSS feed from our biggest national classifieds site called 'Marktplaats'.
It will read the rss feed based on a marktplaats user_id, save it as cached file and display the marktplaats ads on the user's public profile page.
The user will be able to edit settings and enable/disable this feature for their public profile.
Ofcourse only available to registered users.

It will use a cache mechanism like the print news plugin and it will use installation, sql struct, user config, display and help as used in Watchlist plugin. In addition I will think of a way how to keep settings osclass way and create a method for viewing Marktplaats ads the way it is done with current ads in my current theme.
So maximum ease and minimum extra work I hope.

COPY vs NEW
OKAY, so I found a WordPress plugin that actually enables this 'feed' in WordPress and so I will use this WP plugin for my needs but ofcourse I will have to change some logic to make it work inside osclass the way I want. This WP plugin is open-source (GPL) and so I will do the same with my creation for which there will be no guarantees of success ofcourse!  ;D

I will start working on the plugin and as I go I will place updates in this thread as of HOW I exactly start and WHAT I change to get the results I want.
If I get stuck I will ask for help in the forums but hopefully I'll be able to just do this on my own with what already is available thanks to others!

Hopefully this thread will inspire others and give it all the extra information needed for others to start creating their own (free) plugins.  ;)

PREREQUISITES
You have a brain, know how to read php code, you are able to cut & paste and to copy php logic to suit your needs.
Also important, you are not too shy or too 'thick' to ask for help :D

Sorry for those not living in the Netherlands however, with this thread you should be able to figure a suitable competitor RSS feed out that you can use if you wanted to.
Moreover you definitely get a better understanding of Osclass plugins and maybe be able to create your own thoughts into nice new plugins for all to use...



Regards,
Eric
« Last Edit: December 28, 2014, 06:49:48 pm by SmaRTeY »

s51

  • Sr. Member
  • ****
  • Posts: 309
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #1 on: December 28, 2014, 08:53:36 pm »
.
Hi Eric ...

I like you, and therefore this warning:
http://www.marktplaats.nl/ & e.g. http://www.dba.dk/
are both owned and operated by fa. Ebay Europe./.Nordic.
- -

A relatively new Inet auto corporation up here (you can get the name by PM)
did almost the same as you chop on, and was convicted of RSS-Deep-Linking
and now Hold on to your keyboard:
Was in high court ordered to pay a fine (euro 100.000,00.-) 750.000, oo - D.kr
- -

Take care - with Best Regards: s51
.

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2519
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #2 on: December 29, 2014, 03:24:11 am »
Hi s51,

thanks for the warning, I don't know how serious to take this but for now I will continue creating the plugin as it also is a good learning experience and I might ask permission for it's use though in this case the plugin is actually an extra channel for their users to show Marktplaats classifieds and they will link back to the original Marktplaats ad so users will be redirected to Marktplaats when clicking an ad of my user. So not my site central but the user central and THEIR ads. Another big difference here is that I offer the option while it will be my users to decide to use it or not. They will enable the feature and use their own marktplaats id for this. I'll just be offering the possibility for them to keep their ads in one 'shop'.

Besides that, this is a website in the netherlands "http://www.rss-widget.nl/rss-marktplaats-zoeken.php" where I got the idea from, then I stumbled upon this plugin for Wordpress: "https://github.com/wp-plugins/wp-marktplaats"

Further more....
Marktplaats: http://wordpress.org/extend/plugins/your-classified-ads/
Ebay: http://wordpress.org/extend/plugins/prospress/

EDIT: these last two are old and no longer available (ahum) :)

Creating it is not the same as using it as a site admin and besides if they decide to convict me to pay that much money I'll be bankrupt so not much worse than I am now.  :-X

Another interesting point here, if I do a Google search on marktplaats dwergpapagaai I get all links to marktplaats ads. isn't that similar to what this plugin will be offering on an informational site like mine where people do similar searches? I guess it is how information is intended vs. how people try to 'steal' from others.

P.s. have you also read the thread to 'get started' in tips & tricks? That is where actual stealing of ads from other classifieds sites is promoted as well as use email addresses based on fake ads for contacting (read spamming) those users. In my opinion I am adding (free!) value for marktplaats, classifieds owners as well as my site.....and if necesarry I'll let the judge decide especially since I do not earn money with this merely enable my users to sell their ads faster.
« Last Edit: December 29, 2014, 01:28:52 pm by SmaRTeY »

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2519
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #3 on: December 29, 2014, 03:48:38 am »
Okay, back to the plugin and how to get a couple of steps further....

starting point for now is the watchlist plugin, this plugin has a total of 6 files (I will skip language files for now) to analyse and change to fit my needs. Of these 6 files I have the following three files modified:
- index.php
- watchlist.js
- struct.sql

This is my starting point, hopefully this enables me to install and uninstall the plugin as well as offer my users a config button in their user profile.

Here's the code   all comment I have in my 'new' index.php
Code: [Select]
<?php
/*
  Plugin Name: Dutch Marktplaats RSS
  Plugin URI: 
  Description: This plugin enables option for user to add Marktplaats items to public profile through RSS feed.
  Version: 1.0
  Author: SmaRTeY
  Author URI: http://www.spulleboel.nl
  Author Email: smartey@xxxxx.nl
  Short Name: Marktplaats
  Plugin update URI: 
 */

    //Set your Plugin version, we will rename it to marktplaats and set it to 1.0
define('MARKTPLAATS_VERSION''1.0');

    
// This is needed in order to be able to activate the plugin
// So this is the plugin function needed to register the plugin and we let the system call our function called
// 'marktplaats_call_after_install'
    
osc_register_plugin(osc_plugin_path(__FILE__), 'marktplaats_call_after_install');

    
// This is a hack to show a Uninstall link at plugins table (you could also use some other hook to show a custom option panel)
// So here is a function that is called / hooked upon a default plugin uninstall option of plugins in Osclass
// The _uninstall is the 'hooked' function and our function that will be called is now called 'marktplaats_call_after_uninstall'
    
osc_add_hook(osc_plugin_path(__FILE__) . '_uninstall''marktplaats_call_after_uninstall');

    
// This is a hack to show a Configure link at plugins table (you could also use some other hook to show a custom option panel)
    // The user will do his/her own config so we show the help file here as watchlist also does, no change needed I guess
osc_add_hook(osc_plugin_path(__FILE__) . '_configure''marktplaats_help');

    
// ORIGINAL: Add link in user menu page
// The user sees a text-button / link in his user-profile-sidebar-menu thanks to this 'hook'
// We change the name to a more convenient name, the hook 'user_menu' we leave in tact since
// we want to see this option for the user in his/her user profile. We rename the function name to
// 'marktplaats_user_menu' and ofcourse also the real function name.
    
osc_add_hook('user_menu''marktplaats_user_menu');

// ORIGINAL: Delete related items from marktplaatss in case item is being deleted
// I rename delete_item to delete_user based on documentation "Hooks"
// delete_user : When an user is deleted. $id is passed as argument.
// This is what I want to remove possible marktplaats setting belonging to a user being deleted from Osclass.
// I rename the original function to 'marktplaats_delete_user_settings' as well as the real function name.
    
osc_add_hook('delete_user''marktplaats_delete_user_settings');

    
// add javascript, only in use for version osc > 3.11 so saving on extra code
// scripts loaded is osclass existing function, hook into this to load marktplaats script
// we keep this setting so we can use it for our own plugin, the marktplaats.js will be edited
osc_add_hook('scripts_loaded''marktplaats_scripts_loaded');
osc_register_script('marktplaats'osc_plugin_url('marktplaats/js/marktplaats.js') . 'marktplaats.js', array('jquery'));
osc_enqueue_script('marktplaats');    

// ORIGINAL: this function will use the javascript with function name 'watchlist' and use items id (= item_id)
// Okay so this is used to add an item to the watchlist, we don't need this function since we are not adding stuff
//function watchlist() {
// echo '<a href="javascript://" class="watchlist" id="' . osc_item_id() . '" title="' . __('add to watchlist', 'watchlist') . '" rel="nofollow"><i //class="fa fa-heart"></i></a>';
//}

// ORIGINAL: Show config button text for use on user profile menu   link to item page in user-profile
// In our case we rename this function to marktplaats_user_menu and the watchlist.php page will become marktplaats.php 
// user-config page where the user can administer personal marktplaats settings. (that is the idea at least)
    
function marktplaats_user_menu() {
        echo 
'<li><a href="' osc_render_file_url(osc_plugin_folder(__FILE__) . 'marktplaats.php') . '" >' __('marktplaats''marktplaats') . '</a></li>';
    }

// After installing plugin: add DB changes based on "struct.sql"
// struct.sql is adjusted to keep user settings for marktplaats plugin 
// We create a table that contains the following:
// (primary key, foreign key of user, enabled setting, nr. of ads to show and the marktplaats_user_id)
    
function marktplaats_call_after_install() {
        
$conn getConnection();
        
$path osc_plugin_resource('marktplaats/struct.sql');
        
$sql  file_get_contents($path);
        
$conn->osc_dbImportSQL($sql);
    }

// ORIGINAL: After deleting plugin: remove DB table t_mp_settings instead of watchlist _item table
// We simply rename the watchlist table to the new table we created in struct.sql for this plugin
    
function marktplaats_call_after_uninstall() {
        
$conn getConnection();
        
$conn->osc_dbExec('DROP TABLE %st_mp_settings'DB_TABLE_PREFIX);
    }

// Put javascript in footer (we create it for version OSC > 3.11 so we get rid of code for older Osclass versions)
// This javascript is used to add a function call to the config menu-button /link and show the config setting is user-profile ??
// We will leave this function but make it work for our plugin (hopefully!)
// This function is called by the osc_add_hook('scripts_loaded'.....
    
function marktplaats_scripts_loaded() {
        echo 
'<!-- marktplaats js -->';
        echo 
'<script type="text/javascript">';
        echo 
'var marktplaats_url = "' osc_ajax_plugin_url('marktplaats/ajax_marktplaats.php') . '";';
        echo 
'</script>';
        echo 
'<!-- marktplaats js end -->';
    }

// Delete all items from table _item_marktplaats based on item id. this function is used by added hook 'delete_item'
// We will use this not for items in our table but to remove user settings from table if user gets removed
// I rename this function to marktplaats_delete_user_settings, old name was marktplaats_delete_item
// After rename all instances will be renamed using notepad   ctrl H rename in all open files(!)
// Also renamed the $item param to $userid to make it more in sync
function marktplaats_delete_user_settings($userid) {
        
$conn getConnection();
        
$conn->osc_dbExec("DELETE FROM %st_mp_settings WHERE fk_user_id = '$userid'"DB_TABLE_PREFIX);
    }

// Function that calls the help page
// This function can stay the same, we will change help.php content for our own plugin
    
function marktplaats_help() {
        
osc_admin_render_plugin(osc_plugin_path(dirname(__FILE__)) . '/help.php');
    }

//Okay, so I changed all filenames and watchlist references, looked at their functions and changed 1 really that deletes
//user settings if user gets deleted for which I found the Hook in the Hooks documentation.
//I also changed the struct.sql so a table will be created that suits my needs, you can look up MySQL
//documentation here: http://dev.mysql.com/doc/refman/5.5/en/index.html
//Osclass has some needed documentation about their hooks here: http://doc.osclass.org/Hooks
//
//So, index.php should be okay (for now) as well as the JS file and the struct.sql meaning I have to look
//at a page that enables the user to administer their marktplaats settings (marktplaats.php most likely)
//Also I need to look at the ajax_marktplaats.php and change the help.php
//I don't know yet how to show the ads based on the RSS feed, want it to be similar to watchlist but before
//I can I will have to figure out how to add the 'works' that read the RSS feed based on a standard url and the
//marktplaats_user_id as well as a decent way to return an array or something. Actually the marktplaats Logic
//need to be copied from the WP_Marktplaats plugin I found. SO first see if the plugin installs like this and
//uninstalls :) If so, we will check the user profile menu for the extra config button and then continue the REAL work!
?>


This is the new 'struct.sql' (haven't tested it yet so there might be errors)
Code: [Select]
CREATE TABLE /*TABLE_PREFIX*/t_mp_settings (
    id int(10) unsigned NOT NULL AUTO_INCREMENT,
    fk_user_id INT(10) UNSIGNED,
    mp_user_id varchar(10),
show_max tinyint(2) UNSIGNED,
enabled_b_mp BOOLEAN,

        PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET 'UTF8' COLLATE 'UTF8_GENERAL_CI';

And last but not least the new JS file:
Code: [Select]
//So I changed the name of this JS file and the watchlist here into marktplaats, keeping the function the same
jQuery(document).ready(function($) {
    $(".marktplaats").click(function() {
        var id = $(this).attr("id");
        var dataString = 'id='  id ;
        var parent = $(this);

        $(this).fadeOut(300);
        $.ajax({
            type: "POST",
            url: marktplaats_url,
            data: dataString,
            cache: false,

            success: function(html) {
            parent.html(html);
            parent.fadeIn(300);
            }
        });
    });
});

I will start testing, make fixes where needed and start looking at the next steps....the hard work will start here I guess, figuring out how to get the logic built into this plugin, how to enable the display, how to create the 'cache' mechanism....well you get the point :)

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2519
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #4 on: December 29, 2014, 04:17:28 am »
TESTED & SUCCESS!  8)

So far so good, with my current modifications I am able to install the plugin, deinstall the plugin, I see the new table created and removed, I can click on 'config' in plugin menu and I get to see the Help page and I see the marktplaats config button in user profile menu....
A true first time right so far which is GREAT!  ;D

However, as mentioned, the hard part is yet to come.... it might be a couple of days before you see me posting new stuff here but there's no hurry. Very happy so far with what I have accomplished and even more so with the fact there are no errors and everything works as I had visioned. Ofcourse no testing has been done yet with actual data in the new table, that will be later on, first I want to create a page where the user can actually enter his personal marktplaats id, the nr of ads they want to show on their marktpaats profile and whether they want it to set to enabled or disabled.

P.s. I might not need the javascript since I disabled/removed the Add to Watchlist function so we'll see if this one stays in.

L8ers...
« Last Edit: December 29, 2014, 04:34:50 am by SmaRTeY »

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2519
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #5 on: January 01, 2015, 08:00:08 pm »
Okay, so a small update, I have done a LOT of analysis and found out that watch list is good for understanding how the hooks work and especially regarding the places I want to make use of them for my plugin.

However, the watchlist is not good for further use with what I want to achieve, instead I will copy some logic that is used in another great free osclass plugin made by russia, the 'print_rss' plugin. This plugin teaches me how to read a rss file, create a cache mechanism as well as shows me a way I can actually make use of the feed in a custom page.
The 'loop' used in this plugin will be a good sample and base for what I want to achieve not in a seperate page but in a page similar to the Bender item-loop so I can let it blend in in the user public profile.

This means that users of this plugin, once finished, will have to do some work themselves as well.  8)

Anyway, it took me some time to understand how watchlist works as for the displaying of the ads and to figure out how I want to progress. Also I have created several functions inside a class 'form' however I think, looking at the print_rss plugin, that I will convert it to just an include file (as we see in the themes) where several functions will be created to enable the feed download/save/read/etc... Not so much because a class would not work but mostly because I am not yet enough comfortable with the way classes work in php web environment work/behave. To get results as soon as possible I will go what is already done thinking I can always make things prettier/better afterwards. ;)

My next step is combining script from bender theme with print_rss logic and hopefully come up with a 'page' that can act as a subpage on the user-public-profile.

Keep you posted!

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2519
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #6 on: January 03, 2015, 02:12:20 am »
Well, finally I figured out the form with which I am able to add/save/load settings in the new table, I got it working and even make use of the osclass msg system to display success or failure depending on input. I had to learn how php / webdev works to be honest. I can read code from others and make changes but it is a different ball game if you are on your own with a lack of webdev knowledge and only so much osclass knowledge. Never the less, pretty happy with what I have achieved so far, I'll upload a picture here so you can actually see the result so far.

One downside, I am using my own theme (Boxer modded) and once it is done you HAVE to make some serious CSS changes but the up side is that this would mainly concern two pages, one where the ads from the rss feed are shown and the user settings page. I should probably add a CSS folder so that would make it easier to make changes to the pages concerned but not sure how to do that (yet).

NB. I will make the last two fields selections so I have more control over the values entered there AND I have to do some XSS prevention yet on the marktplaats uid field but that's peanuts  ;D

Okay, next is working on the actual RSS Feed logic as well as the page to display the ads in similar style as my theme is.
« Last Edit: January 03, 2015, 02:31:35 am by SmaRTeY »

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2519
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #7 on: January 10, 2015, 04:24:02 am »
Okay, so it did take me more time than originally planned but I think most of the plugin is done now!  8)

I managed to create a download, save it to disk as xml file and manipulate it in such a way that it shows the ads the way the regular theme does making it look good! So I added another image here so you can see what t looks like on the user's personal profile page.
Unfortunately it is not possible to use these ads in the search queries but for me that's okay, I wanted to enable users to display their personal Marktplaats ads on my site as well as enable them to be able to create the settings and save them.

If you want to have a look at the user's profile page, here's a link:
http://93.157.1.143/spulleboel/winkels/Verkoper

I'll post the plugin here soon once I have done more testing.
There'll be no support and no guarantees or anything since this is just a learning experience which might help others to get inspired. I use a lot of comments in code so it should be easier to understand what's happening and why.


Regards,
Eric
« Last Edit: January 10, 2015, 04:31:47 am by SmaRTeY »

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2519
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #8 on: January 11, 2015, 11:26:47 pm »
Not sure if others are interested since it's mainly targeted towards the dutch market but it will hopefully give others a better insight in how a plugin can be created. I mean if I can get this far in less than two weeks YOU can do this as well!  ;D

I found it hard to use the forum as a way to 'teach' the coding of a plugin, instead I decided to go big on the comments in the code and a readme.txt with most important stuff to help you get it working within your own theme.

Things I miss in this plugin:
- a way to add a Google/Bot nofollow construct (only) for the user_settings page in the header

There is also one "snag", I created the plugin for my own needs so it is based on the Boxer 2.5 theme I am using.
AND since my main goal is getting others on plugin track as well I decided NOT to convert it to default Bender theme meaning if you want to use the plugin you WILL have to dig into Bender css and code.  :P

Also, feel free to ask questions, not sure if I have all the answers and please do not expect me to give personal support as I only have so much time and I am still learning myself.  8)

Hope someone finds it usefull and get's interested in creating plugins themselves.

Regards,
Eric

mrwright

  • Newbie
  • *
  • Posts: 19
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #9 on: February 23, 2015, 02:22:28 am »
Do you think this could be used for craigslist rss feed?

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2519
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #10 on: February 23, 2015, 02:40:11 pm »
Pretty sure but it would most likely need code changes and what is more important is that you play by the rules unless you do not mind being chased down and brought up to court :)

It is a very simple principle, you need to know the rss url for starters, download the rss and save the file on the server
Second is the bit where ads are extracted from the downloaded rss file
third is making it visible within your theme

This plugin does not save any ads and therefor does not enable searching for these ads.

All these steps are in this plugin so you should be able to play with the code and adjust it to your needs.

But as mentioned use at own risk, I know that if you would start 'stealing' ads from other classified site and saving them in your own DB as if they are yours you are looking for trouble that can be very costly.

Regards,
Eric

mrwright

  • Newbie
  • *
  • Posts: 19
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #11 on: February 24, 2015, 09:00:13 pm »
Oh ok, thanks...  I actually plan to link to the actual craigslist ad; if possible. I just would like for it to be searched as well.  Thank you, you're the man @SmaRTey

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2519
Re: [TUTORIAL] Howto: Create a plugin myself
« Reply #12 on: June 27, 2015, 03:04:48 am »
Coincidence or not, unfortunately Marktplaats disabled their RSS feed functionality shortly after my plugin was finished.  :-X
So no use in downloading this plugin any longer besides a nice learning experience maybe.