Skip to content

Commit 34e306c

Browse files
committed
Framework ready
1 parent f36a910 commit 34e306c

14 files changed

Lines changed: 358 additions & 216 deletions

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ php:
88
- "5.6"
99
- "5.5"
1010
- "5.4"
11-
- "5.3"
1211

1312
# Declare the WordPress requirements
1413
env:

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22
[![Build Status](https://travis-ci.org/nirjharlo/wp-plugin-framework.svg?branch=master)](https://travis-ci.org/nirjharlo/wp-plugin-framework)
33

44
A WordPress plugin framework base to build any standard WP plugin on top of it.
5-
It contains various items, such as Settings pages, Data tables, Widgets, Metaboxes, Custom Post Types, Shortcodes along with infrastructure for DB operations and CRON jobs.
5+
It contains various items, such as Settings pages, Data tables, Widgets, Metaboxes, Custom Post Types, Shortcodes along with infrastructure for DB operations.
66

7-
There are extra classes for API integration and AJAX.
7+
There are extra classes for API integration, AJAX, File upload and Cron jobs.
8+
9+
NOTE: Requires PHP 5.4 and up.
810

911
## Usage
1012

13+
1. Change the class names, file names and declarations in `framework.php` according to need.
14+
15+
2. In `autoload.php` the `PLUGIN_BUILD` class includes all the files in process and decares the classes inside them. Modify them according to need.
16+
17+
3. Go through the files in `/lib/` and `/src/`. First one contains classes for extra features, while the src is using essential features.
18+
19+
4. In `autoload.php` the installation and uninstallation classes contain possible situations. Modify them as per need.

autoload.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,32 @@ public function installation() {
2727
);
2828
$install->do();
2929
}
30+
31+
$this->corn();
3032
*
3133
*/
3234
}
3335

3436

3537

38+
//Custom corn class, register it while activation
39+
public function corn() {
40+
41+
if ( class_exists( 'PLUGIN_CRON' ) ) {
42+
$cron = new PLUGIN_CRON();
43+
$schedule = $cron->schedule_task(
44+
array(
45+
'timestamp' => current_time('timestamp'),
46+
'recurrence' => 'amaz_24_hrs',
47+
// Use custom_corn_hook to hook into any cron process, anywhere in the plugin.
48+
'hook' => 'custom_corn_hook'
49+
) );
50+
}
51+
52+
}
53+
54+
55+
3656
public function db_install() {
3757

3858
/**
@@ -158,6 +178,12 @@ public function helpers() {
158178
require_once ('lib/table.php');
159179
require_once ('lib/ajax.php');
160180
require_once ('lib/upload.php');
181+
182+
/**
183+
* Available classes:
184+
*
185+
*
186+
*/
161187
}
162188

163189

lib/ajax.php

Lines changed: 25 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,63 @@
11
<?php
2+
/**
3+
* Doing AJAX the WordPress way.
4+
* Use this class in admin or user side
5+
*/
26
if ( ! defined( 'ABSPATH' ) ) exit;
37

48
//AJAX helper class
5-
if ( ! class_exists( 'INTERNAL_LINK_MASTER_AJAX' ) ) {
6-
7-
final class INTERNAL_LINK_MASTER_AJAX {
9+
if ( ! class_exists( 'PLUGIN_AJAX' ) ) {
810

11+
final class PLUGIN_AJAX {
912

1013

1114
// Add basic actions
1215
public function __construct() {
1316

14-
$this->table = 'internal_link_master';
15-
1617
//Adding the AJAX
17-
add_action( 'admin_footer', array( $this, 'addLink_js' ) );
18-
add_action( 'wp_ajax_addLink', array( $this, 'addLink' ) );
19-
add_action( 'wp_ajax_nopriv_addLink', array( $this, 'addLink' ) );
18+
add_action( 'admin_footer', array( $this, 'customName_js' ) );
19+
add_action( 'wp_ajax_customName', array( $this, 'customName' ) );
20+
add_action( 'wp_ajax_nopriv_customName', array( $this, 'customName' ) );
2021
}
2122

2223

2324

2425
//Output the form
2526
public function form() { ?>
2627

27-
<form method="POST" action="">
28-
<table class="widefat">
29-
<tr>
30-
<td style="width: 35%">
31-
<input type="url" name="hasUrl" class="regular-text" placeholder="<?php _e( 'Enter Has URL', 'InLinkMaster' ); ?>">
32-
</td>
33-
<td style="width: 35%">
34-
<input type="url" name="needUrl" class="regular-text" placeholder="<?php _e( 'Enter Need URL', 'InLinkMaster' ); ?>">
35-
</td>
36-
<td style="width: 20%">
37-
<input id="addByAjaxSubmit" type="submit" name="submit" value="Add URL" class="button-primary">
38-
</td>
39-
<td style="width: 10%">
40-
<span id="addByAjaxNotice"></span>
41-
</td>
42-
</tr>
43-
</table>
44-
</form>
28+
<form id="addByAjax" method="POST" action="">
29+
<input type="text" name="textName" placeholder="<?php _e( 'Text', 'textdomain' ); ?>">
30+
<input id="AjaxSubmit" type="submit" name="submit" value="Submit">
31+
</form>
4532
<?php
4633
}
4734

4835

4936

5037
//The javascript
51-
public function addLink_js() { ?>
38+
public function customName_js() { ?>
5239

5340
<script type="text/javascript">
5441
jQuery(document).ready(function() {
5542

56-
jQuery("#addByAjax").hide();
57-
jQuery("#addNewUrlSet").click(function(){
58-
jQuery("#addByAjax").toggle();
59-
});
60-
61-
jQuery("#addByAjaxNotice").text("");
62-
6343
jQuery("#addByAjax form").submit(function() {
6444

65-
jQuery("#addByAjaxNotice").text("");
66-
6745
event.preventDefault();
6846

69-
var hasUrl = jQuery("input[name='hasUrl']").val();
70-
var needUrl = jQuery("input[name='needUrl']").val();
71-
72-
if ( hasUrl != '' && needUrl != '' ) {
73-
jQuery("#addByAjaxSubmit").val("Adding ...").attr("disabled", "disabled");
47+
var val = jQuery("input[name='textName']").val();
7448

7549
jQuery.post(
7650
<?php if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { ?>
7751
'<?php echo admin_url("admin-ajax.php", "https"); ?>',
7852
<?php } else { ?>
7953
'<?php echo admin_url("admin-ajax.php"); ?>',
8054
<?php } ?>
81-
{ 'action': 'addLink', 'hasUrl': hasUrl, 'needUrl': needUrl },
55+
{ 'action': 'customName', 'val': val },
8256
function(response) {
8357
if ( response != '' && response != false && response != undefined ) {
84-
var data = JSON.parse(response);
85-
if (data.ping == 'true') {
86-
var newTableRow = '<tr><th scope="row" class="check-column"><input type="checkbox" name="bulk-select[]" value="' + data.id + '"></th><td>' + data.has_url + '</td><td>' + data.has_pa + '</td><td>' + data.need_url + '</td><td>' + data.need_pa + '</td></tr>';
87-
jQuery("#the-list .no-items").remove();
88-
jQuery("#the-list").append(newTableRow);
89-
jQuery("#addByAjaxSubmit").val("Add Another").removeAttr("disabled");
90-
jQuery("input[name='hasUrl'], input[name='needUrl']").val('');
91-
jQuery("#addByAjaxNotice").text("Added");
92-
} else if (data.ping == 'false') {
93-
jQuery("#addByAjaxSubmit").val("Try Again").removeAttr("disabled");
94-
jQuery("#addByAjaxNotice").text("Can't Add");
95-
}
9658

59+
var data = JSON.parse(response);
60+
// Do some stuff
9761
}
9862
}
9963
);
@@ -107,47 +71,15 @@ function(response) {
10771

10872

10973
//The data processor
110-
public function addLink() {
111-
112-
$hasUrl = esc_url( $_POST['hasUrl'] );
113-
$needUrl = esc_url( $_POST['needUrl'] );
114-
115-
$hasUrlPa = $this->APICall($hasUrl);
116-
$needUrlPa = $this->APICall($needUrl);
117-
118-
global $wpdb;
119-
$wpdb->suppress_errors = true;
120-
$value = $wpdb->insert( $wpdb->prefix. $this->table, array( 'has_pa_url' => $hasUrl, 'has_pa' => $hasUrlPa, 'need_pa_url' => $needUrl, 'need_pa' => $needUrlPa ), array( '%s', '%s', '%s', '%s' ) );
121-
122-
if ($value) {
123-
$hasUrlParsed = parse_url($hasUrl);
124-
$needUrlParsed = parse_url($needUrl);
125-
echo json_encode(
126-
array(
127-
'ping' => 'true',
128-
'id' => $value,
129-
'has_url' => (array_key_exists('path', $hasUrlParsed) ? $hasUrlParsed['path'] : '/'),
130-
'has_pa' => $hasUrlPa,
131-
'need_url' => (array_key_exists('path', $needUrlParsed) ? $needUrlParsed['path'] : '/'),
132-
'need_pa' => $needUrlPa
133-
));
134-
} else {
135-
echo json_encode(array('ping' => 'false'));
136-
}
137-
wp_die();
138-
}
74+
public function customName() {
13975

76+
$val = $_POST['val'];
14077

141-
142-
//API call
143-
public function APICall($url) {
144-
145-
$api = new INTERNAL_LINK_MASTER_API();
146-
$api->objectURL = $url;
147-
$api->prepare();
148-
$data = $api->call();
149-
$parsed = $api->parse($data);
150-
return (array_key_exists('upa', $parsed) ? round($parsed['upa'], 2) : 0);
78+
// DO some stuff
79+
80+
$response = array( 'val' => $value );
81+
echo json_encode( $response );
82+
wp_die();
15183
}
15284
}
15385
} ?>

lib/api.php

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,43 @@
11
<?php
2-
if ( ! defined( 'ABSPATH' ) ) exit;
3-
4-
//Implimentation of MOZ API
5-
if ( ! class_exists( 'INTERNAL_LINK_MASTER_API' ) ) {
6-
7-
final class INTERNAL_LINK_MASTER_API {
8-
9-
10-
11-
public static $host = 'https://lsapi.seomoz.com/linkscape';
12-
public static $endpoint = '/url-metrics';
13-
public $expires;
14-
public $accessID;
15-
public $secretKey;
16-
public $objectURL; //Set it before calling
17-
public $requestUrl; //Set it after $objectURL and before calling prepare() method
18-
public static $call_type = 'POST';
19-
20-
21-
22-
//Refer to https://github.com/seomoz/SEOmozAPISamples/blob/master/php/signed_authentication_sample.php
23-
public function prepare() {
24-
25-
$this->expires = time() + 300;
26-
27-
$this->accessID = get_option('InternalLinkApiKeyField');
28-
$this->secretKey = get_option('InternalLinkApiPassField');
29-
30-
$stringToSign = $this->accessID."\n".$this->expires;
31-
$binarySignature = hash_hmac('sha1', $stringToSign, $this->secretKey, true);
32-
$urlSafeSignature = urlencode(base64_encode($binarySignature));
33-
$cols = "34359738368";
34-
35-
$this->requestUrl = self::$host . self::$endpoint .'/'. urlencode($this->objectURL) . '?Cols=' . $cols . '&AccessID=' . $this->accessID . '&Expires=' . $this->expires . '&Signature=' . $urlSafeSignature;
36-
}
37-
2+
/**
3+
* Implimentation of WordPress inbuilt API class
4+
*
5+
* Usage:
6+
*
7+
* $api = new PLUGIN_API();
8+
* $api->endpoint = 'endpoint_url'
9+
* $api->header = array( "key: $val" )
10+
* $api->data_type = 'xml' or 'json'
11+
* $api->call_type = 'GET' or 'POST'
12+
* $api->call();
13+
* $data = $api->parse();
14+
*
15+
*/
16+
if ( ! class_exists( 'PLUGIN_API' ) ) {
17+
18+
class PLUGIN_API {
19+
20+
21+
public $endpoint;
22+
public $header;
23+
public $data_type;
24+
public $call_type;
3825

3926

4027
//Define the necessary database tables
4128
public function build() {
4229

4330
$args = array(
44-
CURLOPT_URL => $this->requestUrl,
45-
CURLOPT_RETURNTRANSFER => true
46-
);
31+
CURLOPT_URL => $this->endpoint,
32+
CURLOPT_RETURNTRANSFER => true,
33+
CURLOPT_ENCODING => "",
34+
CURLOPT_MAXREDIRS => 10,
35+
CURLOPT_TIMEOUT => 30,
36+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
37+
CURLOPT_CUSTOMREQUEST => $this->call_type,
38+
CURLOPT_HTTPHEADER => $this->header,
39+
);
40+
4741
return $args;
4842
}
4943

@@ -53,13 +47,18 @@ public function build() {
5347
public function call() {
5448

5549
$curl = curl_init();
50+
5651
curl_setopt_array( $curl, $this->build() );
52+
5753
$result = curl_exec($curl);
5854
$err = curl_error($curl);
55+
5956
curl_close($curl);
57+
6058
if ($err) {
6159
$result = "cURL Error #:" . $err;
6260
}
61+
6362
return $result;
6463
}
6564

@@ -68,6 +67,30 @@ public function call() {
6867
//Check options and tables and output the info to check if db install is successful
6968
public function parse($data) {
7069

70+
call_user_func( array( $this, $this->data_type ), $data );
71+
}
72+
73+
74+
75+
//Parse XML data type
76+
public function xml($data) {
77+
78+
libxml_use_internal_errors(true);
79+
$parsed = ( ! $data || $data == '' ? false : simplexml_load_string( $data ) );
80+
81+
if ( ! $parsed ) {
82+
return false;
83+
libxml_clear_errors();
84+
} else {
85+
return $parsed;
86+
}
87+
}
88+
89+
90+
91+
//Parse JSON data type
92+
public function json($data) {
93+
7194
$parsed = ( ! $data || $data == '' ? false : json_decode( $data, 1 ) );
7295
return $parsed;
7396
}

0 commit comments

Comments
 (0)