{"id":735,"date":"2016-09-28T14:45:19","date_gmt":"2016-09-28T14:45:19","guid":{"rendered":"http:\/\/myprojects.advchaweb.com\/?p=735"},"modified":"2016-09-28T14:45:19","modified_gmt":"2016-09-28T14:45:19","slug":"osclass-first-plugin-tutorial","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2016\/09\/28\/osclass-first-plugin-tutorial\/","title":{"rendered":"OSClass First Plugin Tutorial"},"content":{"rendered":"<p>OSCLASS PLUGIN TUTORIAL<br \/>\n1. Create a new directory in any directory. name it &#8216;my_firstplugin&#8217;.<br \/>\n2. Create a new file in the directory. name it &#8216;index.php&#8217;.<br \/>\n3. open the file and add these lines in it :<br \/>\n\/*<br \/>\nPlugin Name: My First Plugin<br \/>\nShort Name: my_firstplugin<br \/>\nPlugin URI: &#8211;<br \/>\nDescription: A first osclass plugin for tutorial purpopse.<br \/>\nVersion: 1.00<br \/>\nAuthor: Satria<br \/>\nAuthor URI: &#8211;<br \/>\n*\/<br \/>\nabove is &#8216;plugin info&#8217;<br \/>\n4. to make the plugin installable, add this function : osc_register_plugin. so<\/p>\n<p>\/**<br \/>\n* (hook: install) Make installation operations<br \/>\n* \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0It creates the database schema and sets some preferences.<br \/>\n* @returns void.<br \/>\n*\/<br \/>\nfunction my_firstplugin_install() {<\/p>\n<p>}<br \/>\nosc_register_plugin(osc_plugin_path(__FILE__), &#8216;my_firstplugin_install&#8217;);<\/p>\n<p>then add two hooks to initialize (load\/init) the plugin and to uninstall the plugin, respectively :<\/p>\n<p>\/**<br \/>\n* (hook: init) Registers scripts and styles.<br \/>\n* @returns void.<br \/>\n*\/<br \/>\nfunction my_firstplugin_load() {<\/p>\n<p>}<br \/>\nosc_add_hook(&#8216;init&#8217;, &#8216;my_firstplugin_load&#8217;);<\/p>\n<p>\/**<br \/>\n* (hook: uninstall) Make un-installation operations<br \/>\n* \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0It destroys the database schema and unsets some preferences.<br \/>\n* @returns void.<br \/>\n*\/<br \/>\nfunction my_firstplugin_uninstall() {<\/p>\n<p>}<br \/>\nosc_add_hook(osc_plugin_path(__FILE__) . &#8216;_uninstall&#8217;, &#8216;my_firstplugin_uninstall&#8217;);<\/p>\n<p>5. those are the minimum requirement (install, uninstall and load) to create an osclass plugin. Zip the directory (so it&#8217;ll be my_firstplugin.zip) then open the oc-admin page and add the plugin from the zip file.<br \/>\nif there is an error &#8220;There was a problem adding the plugin&#8221; when installing the plugin then just copy and paste the plugin directory in oc-content\/plugins\/ directory<\/p>\n<p>6. From admin page, we can install\/uninstall and enable\/disable our plugin but this plugin dont have any functionality yet. we will add some scripts so we can display the links\/menu in admin page to configure the plugin. In the index.php file, add this hook &#8216;admin_menu&#8217; so it&#8217;ll be :<br \/>\n\/**<br \/>\n* (hook: admin_menu) Display the admin menu<br \/>\n* @returns html.<br \/>\n*\/<br \/>\nfunction my_firstplugin_admin_menu() {<br \/>\necho &#8216;&lt;h3&gt;&lt;a href=&#8221;#&#8221;&gt;My First Plugin&lt;\/a&gt;&lt;\/h3&gt;<br \/>\n&lt;ul&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;&#8216; . osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . &#8216;\/admin\/link1.php&#8217;) . &#8216;&#8221;&gt;&amp;raquo; &#8216; . __(&#8216;Link 1&#8217;, &#8216;my_firstplugin&#8217;) . &#8216;&lt;\/a&gt;&lt;li&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;&#8216; . osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . &#8216;\/admin\/link2.php&#8217;) . &#8216;&#8221;&gt;&amp;raquo; &#8216; . __(&#8216;Link 2&#8217;, &#8216;my_firstplugin&#8217;) . &#8216;&lt;\/a&gt;&lt;\/li&gt;<br \/>\n&lt;\/ul&gt;&#8217;;<br \/>\n}<br \/>\nosc_add_hook(&#8216;admin_menu&#8217;, &#8216;my_firstplugin_admin_menu&#8217;);<\/p>\n<p>EDIT : if we have osclass version higher than 320 then we better to edit the above function like this :<br \/>\n\/**<br \/>\n* (hook: admin_menu) Display the admin menu<br \/>\n* @returns html.<br \/>\n*\/<br \/>\nfunction my_firstplugin_admin_menu() {<br \/>\nif(osc_version()&lt;320) {<br \/>\necho &#8216;&lt;h3&gt;&lt;a href=&#8221;#&#8221;&gt;My First Plugin&lt;\/a&gt;&lt;\/h3&gt;<br \/>\n&lt;ul&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;&#8216; . osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . &#8216;\/admin\/link1.php&#8217;) . &#8216;&#8221;&gt;&amp;raquo; &#8216; . __(&#8216;Link 1&#8217;, &#8216;my_firstplugin&#8217;) . &#8216;&lt;\/a&gt;&lt;li&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;&#8216; . osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . &#8216;\/admin\/link2.php&#8217;) . &#8216;&#8221;&gt;&amp;raquo; &#8216; . __(&#8216;Link 2&#8217;, &#8216;my_firstplugin&#8217;) . &#8216;&lt;\/a&gt;&lt;\/li&gt;<br \/>\n&lt;\/ul&gt;&#8217;;<br \/>\n}else{<br \/>\nosc_add_admin_submenu_divider(&#8216;plugins&#8217;, __(&#8216;My First Plugin&#8217;, &#8216;my_firstplugin&#8217;), &#8216;my_firstplugin_divider&#8217;, &#8216;administrator&#8217;);<br \/>\nosc_add_admin_submenu_page(&#8216;plugins&#8217;, &#8216;&amp;raquo; &#8216;.__(&#8216;Link 1&#8217;, &#8216;my_firstplugin&#8217;), osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . &#8216;\/admin\/link1.php&#8217;), &#8216;my_firstplugin_link1&#8217;, &#8216;administrator&#8217;);<br \/>\nosc_add_admin_submenu_page(&#8216;plugins&#8217;, &#8216;&amp;raquo; &#8216;.__(&#8216;Link 2&#8217;, &#8216;my_firstplugin&#8217;), osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . &#8216;\/admin\/link2.php&#8217;), &#8216;my_firstplugin_link2&#8217;, &#8216;administrator&#8217;);<br \/>\n}<br \/>\n}<br \/>\n\/\/osc_add_hook(&#8216;admin_menu&#8217;, &#8216;my_firstplugin_admin_menu&#8217;);<br \/>\nif(osc_version()&lt;320) {<br \/>\nosc_add_hook(&#8216;admin_menu&#8217;, &#8216;my_firstplugin_admin_menu&#8217;);<br \/>\n} else {<br \/>\nosc_add_hook(&#8216;admin_menu_init&#8217;, &#8216;my_firstplugin_admin_menu&#8217;);<br \/>\n}<\/p>\n<p>dont forget to add a new directory &#8216;admin&#8217; in the plugin. then add two new files in it : link1.php and link2.php. the files will display the html content. you can add any html file in its.<\/p>\n<p>7. Ok. great. then how to add any content on the front page by our plugin? Without any plugin we can add any content in oc-content\/themes\/your-theme\/main.php. &#8216;your-theme&#8217; is curent activated theme, it could be &#8216;bender&#8217; (default theme). we can insert any html\/php code in main.php file like &lt;p&gt;TEST&lt;\/p&gt; or &lt;?php echo &#8220;Great&#8221;; ?&gt;. If we want to make it better, we can include any php\/html file and add this code &lt;?php osc_current_web_theme_path(&#8216;your_file.php&#8217;) ; ?&gt; dont forget to add the new file your_file.php and fill it with any html\/php code.<\/p>\n<p>8. Ok. now we want to connect to database and pull some data to be displayed in the admin page.<br \/>\nin index.php file below the plugin info, add a line :<br \/>\nrequire_once &#8216;model\/myfirstplugin_model.php&#8217;;<br \/>\nthen add a new directory &#8216;model&#8217; and a file &#8216;myfirstplugin_model.php&#8217; in it.<br \/>\nwe want to display all city in a table t_city on link1.php file. so add these lines in myfirstplugin_model.php file :<br \/>\n&lt;?php<\/p>\n<p>\/**<br \/>\n* Data Access Object (DAO) for messages.<br \/>\n*\u00a0\u00a0 \u00a0Performs operations on database.<br \/>\n* @author Satria<br \/>\n* @package my_firstplugin<br \/>\n* @subpackage Model<br \/>\n* @since 1.00<br \/>\n*\/<br \/>\nclass MyFirstPluginModel extends DAO {<\/p>\n<p>function __construct() {<br \/>\nparent::__construct();<br \/>\n}<\/p>\n<p>\/**<br \/>\n* Singleton.<br \/>\n*\/<br \/>\nprivate static $instance;<\/p>\n<p>\/**<br \/>\n* Singleton constructor.<br \/>\n* @return an MadhouseMessengerDAO object.<br \/>\n*\/<br \/>\npublic static function newInstance() {<br \/>\nif(!self::$instance instanceof self) {<br \/>\nself::$instance = new self;<br \/>\n}<br \/>\nreturn self::$instance;<br \/>\n}<\/p>\n<p>\/**<br \/>\n* Return table name city<br \/>\n* @return string<br \/>\n*\/<br \/>\npublic function getTable_City()<br \/>\n{<br \/>\nreturn DB_TABLE_PREFIX.&#8217;t_city&#8217;;<br \/>\n}<\/p>\n<p>\/**<br \/>\n* Get all city<br \/>\n*<br \/>\n* @return array<br \/>\n*\/<br \/>\npublic function getAllCity()<br \/>\n{<br \/>\n$this-&gt;dao-&gt;select();<br \/>\n$this-&gt;dao-&gt;from( $this-&gt;getTable_City() ) ;<br \/>\n$this-&gt;dao-&gt;orderBy(&#8216;s_name&#8217;, &#8216;ASC&#8217;) ;<\/p>\n<p>$results = $this-&gt;dao-&gt;get();<br \/>\nif( !$results ) {<br \/>\nreturn array() ;<br \/>\n}<\/p>\n<p>return $results-&gt;result();<br \/>\n}<br \/>\n}<\/p>\n<p>then edit link1.php file so it&#8217;ll be :<\/p>\n<p>&lt;div id=&#8221;settings_form&#8221; style=&#8221;border: 1px solid #ccc; background: #eee; &#8220;&gt;<br \/>\n&lt;div style=&#8221;padding: 20px;&#8221;&gt;<br \/>\n&lt;div&gt;<br \/>\n&lt;fieldset&gt;<br \/>\n&lt;legend&gt;&lt;?php _e(&#8216;My First Plugin Link 1 : All City Name&#8217;); ?&gt;&lt;\/legend&gt;<br \/>\n&lt;!&#8211;p&gt;<br \/>\n&lt;?php _e(&#8216;Congratulation. This is a link 1 content&#8217;, &#8216;my_firstplugin&#8217;); ?&gt;<br \/>\n&lt;\/p&#8211;&gt;<br \/>\n&lt;?php<br \/>\n$city = MyFirstPluginModel::newInstance()-&gt;getAllCity();<br \/>\nforeach($city as $c){<br \/>\necho $c[&#8216;s_name&#8217;].'&lt;br&gt;&#8217;;<br \/>\n}<br \/>\n?&gt;<br \/>\n&lt;p&gt;<br \/>\n&lt;?php _e(&#8216;Created by Satria Faestha. Osclass version : &#8216;.osc_version(), &#8216;my_firstplugin &#8216;); ?&gt;<br \/>\n&lt;\/p&gt;<br \/>\n&lt;\/fieldset&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/div&gt;<\/p>\n<p>then open link1.php on admin page. All city name will be displayed on the page.<br \/>\nYou can play with the query like just show 5 top city, etc.<\/p>\n<p>9. Now we want to add our own table so when we install the plugin, our table will be created and ready to be used. otherwise if we want to uninstall the plugin, the tables will be deleted.<br \/>\nAdd these two sql file in sql directory (create a new directory &#8216;sql&#8217;). they are myfirstplugin.sql and add_data.sql<br \/>\nin myfirstplugin.sql :<br \/>\nCREATE TABLE \/*TABLE_PREFIX*\/t_my_firstplugin (<br \/>\npk_i_id INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,<br \/>\ns_name VARCHAR(255),<\/p>\n<p>PRIMARY KEY (pk_i_id)<br \/>\n) ENGINE=InnoDB DEFAULT CHARACTER SET &#8216;UTF8&#8217; COLLATE &#8216;UTF8_GENERAL_CI&#8217;;<\/p>\n<p>in add_data.sql :<br \/>\nINSERT INTO \/*TABLE_PREFIX*\/t_my_firstplugin (pk_i_id, s_name) VALUES (1,&#8217;Satria Faestha&#8217;);<br \/>\nINSERT INTO \/*TABLE_PREFIX*\/t_my_firstplugin (pk_i_id, s_name) VALUES (2,&#8217;Ermillya Roza&#8217;);<br \/>\nINSERT INTO \/*TABLE_PREFIX*\/t_my_firstplugin (pk_i_id, s_name) VALUES (3,&#8217;Sarah Almahyrah&#8217;);<br \/>\nINSERT INTO \/*TABLE_PREFIX*\/t_my_firstplugin (pk_i_id, s_name) VALUES (4,&#8217;Alim Alhikmah&#8217;);<\/p>\n<p>in myfirstplugin_model.php add these lines :<br \/>\n\/**<br \/>\n* Import sql file<br \/>\n* @param type $file<br \/>\n*\/<br \/>\npublic function import($file)<br \/>\n{<br \/>\n$path = osc_plugin_resource($file) ;<br \/>\n$sql = file_get_contents($path);<\/p>\n<p>if(! $this-&gt;dao-&gt;importSQL($sql) ){<br \/>\nthrow new Exception( &#8220;Error importSQL::MyFirstPluginModel&lt;br&gt;&#8221;.$file ) ;<br \/>\n}<br \/>\n}<br \/>\n\/**<br \/>\n* Remove data and tables related to the plugin.<br \/>\n*\/<br \/>\npublic function uninstall()<br \/>\n{<br \/>\n$this-&gt;dao-&gt;query(sprintf(&#8216;DROP TABLE &#8216;.DB_TABLE_PREFIX.&#8217;t_my_firstplugin&#8217;) ) ;<br \/>\n}<\/p>\n<p>then in index.php file edit these functions :<br \/>\nfunction my_firstplugin_install() {<br \/>\nMyFirstPluginModel::newInstance()-&gt;import(&#8216;my_firstplugin\/sql\/myfirstplugin.sql&#8217;);<br \/>\nMyFirstPluginModel::newInstance()-&gt;import(&#8216;my_firstplugin\/sql\/add_data.sql&#8217;);<br \/>\n}<br \/>\nosc_register_plugin(osc_plugin_path(__FILE__), &#8216;my_firstplugin_install&#8217;);<\/p>\n<p>and<\/p>\n<p>function my_firstplugin_uninstall() {<br \/>\nMyFirstPluginModel::newInstance()-&gt;uninstall();<br \/>\n}<br \/>\nosc_add_hook(osc_plugin_path(__FILE__) . &#8216;_uninstall&#8217;, &#8216;my_firstplugin_uninstall&#8217;);<\/p>\n<p>then do uninstall (if our plugin still installed) and install the plugin. you can see the new table t_my_firstplugin in our database. you can add new function in the model file to pull the content :<br \/>\npublic function getMyFirstPluginData()<br \/>\n{<br \/>\n$this-&gt;dao-&gt;select();<br \/>\n$this-&gt;dao-&gt;from( $this-&gt;getTable_MyFirstPlugin() ) ;<br \/>\n\/\/$this-&gt;dao-&gt;orderBy(&#8216;s_name&#8217;, &#8216;ASC&#8217;) ;<br \/>\n$this-&gt;dao-&gt;orderBy(&#8216;pk_i_id&#8217;, &#8216;ASC&#8217;) ;<\/p>\n<p>$results = $this-&gt;dao-&gt;get();<br \/>\nif( !$results ) {<br \/>\nreturn array() ;<br \/>\n}<\/p>\n<p>return $results-&gt;result();<br \/>\n}<\/p>\n<p>then in link2.php file, edit the content so it&#8217;ll be :<br \/>\n&lt;div id=&#8221;settings_form&#8221; style=&#8221;border: 1px solid #ccc; background: #eee; &#8220;&gt;<br \/>\n&lt;div style=&#8221;padding: 20px;&#8221;&gt;<br \/>\n&lt;div&gt;<br \/>\n&lt;fieldset&gt;<br \/>\n&lt;legend&gt;&lt;?php _e(&#8216;My First Plugin Link 2&#8217;); ?&gt;&lt;\/legend&gt;<br \/>\n&lt;p&gt;<br \/>\n&lt;?php<br \/>\n$data = MyFirstPluginModel::newInstance()-&gt;getMyFirstPluginData();<br \/>\nforeach($data as $d){<br \/>\necho $d[&#8216;s_name&#8217;].'&lt;br&gt;&#8217;;<br \/>\n}<br \/>\n?&gt;<br \/>\n&lt;\/p&gt;<br \/>\n&lt;\/fieldset&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/div&gt;<\/p>\n<p>10. We also want the data in our table can be added, edited and\/or deleted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OSCLASS PLUGIN TUTORIAL 1. Create a new directory in any directory. name it &#8216;my_firstplugin&#8217;. 2. Create a new file in the directory. name it &#8216;index.php&#8217;. 3. open the file and add these lines in it : \/* Plugin Name: My First Plugin Short Name: my_firstplugin Plugin URI: &#8211; Description: A first osclass plugin for tutorial &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2016\/09\/28\/osclass-first-plugin-tutorial\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;OSClass First Plugin Tutorial&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36,13],"tags":[],"class_list":["post-735","post","type-post","status-publish","format-standard","hentry","category-osclass","category-tutorial"],"_links":{"self":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=735"}],"version-history":[{"count":1,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/735\/revisions"}],"predecessor-version":[{"id":736,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/735\/revisions\/736"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}