forked from petenelson/bacon-ipsum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gga-bacon-ipsum-api.php
74 lines (51 loc) · 1.68 KB
/
gga-bacon-ipsum-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/*
Plugin Name: Bacon Ipsum - API
Description: Handles incoming API requests
Plugin URI: https://github.com/petenelson/bacon-ipsum
Version: 2.1.4
Author: Pete Nelson (@GunGeekATX)
Author URI: http://petenelson.com
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class GGA_Bacon_Ipsum_API {
function __construct() {
add_action('posts_selection', array(&$this, 'api'));
}
function api() {
if (is_page('api') && isset($_REQUEST['type'])) {
require_once 'gga-BaconIpsumGenerator.php';
$generator = new BaconIpsumGenerator();
$number_of_sentences = 0;
$number_of_paragraphs = 5;
if (isset($_REQUEST["paras"]))
$number_of_paragraphs = intval($_REQUEST["paras"]);
if (isset($_REQUEST["sentences"]))
$number_of_sentences = intval($_REQUEST["sentences"]);
$output = '';
if ($number_of_paragraphs < 1)
$number_of_paragraphs = 1;
if ($number_of_paragraphs > 100)
$number_of_paragraphs = 100;
if ($number_of_sentences > 100)
$number_of_sentences = 100;
$start_with_lorem = isset($_REQUEST["start-with-lorem"]) && $_REQUEST["start-with-lorem"] == "1";
$paras = $generator->Make_Some_Meaty_Filler(
filter_var($_REQUEST["type"], FILTER_SANITIZE_STRING),
$number_of_paragraphs,
$start_with_lorem,
$number_of_sentences);
header('Access-Control-Allow-Origin: *');
if (isset($_REQUEST["callback"])) {
header("Content-Type: application/javascript");
echo $_GET['callback'] . '(' . json_encode($paras) . ')';
}
else {
header("Content-Type: application/json; charset=utf-8");
echo json_encode($paras);
}
exit;
}
}
}
new GGA_Bacon_Ipsum_API();