1 | <?php
|
---|
2 | /**
|
---|
3 | * Copyright 2006, 2007, 2008, 2009 Eric D. Hough (http://ehough.com)
|
---|
4 | *
|
---|
5 | * This file is part of TubePress (http://tubepress.org)
|
---|
6 | *
|
---|
7 | * TubePress is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * TubePress is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with TubePress. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | function_exists('tubepress_load_classes')
|
---|
23 | || require(dirname(__FILE__) . '/../../../tubepress_classloader.php');
|
---|
24 | tubepress_load_classes(array('org_tubepress_shortcode_ShortcodeService',
|
---|
25 | 'org_tubepress_options_category_Advanced',
|
---|
26 | 'org_tubepress_options_manager_OptionsManager'));
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Handles some tasks related to the query string
|
---|
30 | */
|
---|
31 | class org_tubepress_shortcode_SimpleShortcodeService implements org_tubepress_shortcode_ShortcodeService
|
---|
32 | {
|
---|
33 | /**
|
---|
34 | * This function is used to parse a shortcode into options that TubePress can use.
|
---|
35 | *
|
---|
36 | * @param string $content The haystack in which to search
|
---|
37 | * @param org_tubepress_options_manager_OptionsManager &$tpom The TubePress options manager
|
---|
38 | *
|
---|
39 | * @return void
|
---|
40 | */
|
---|
41 | public function parse($content, org_tubepress_options_manager_OptionsManager $tpom, $mergeWithExistingOptions = false)
|
---|
42 | {
|
---|
43 | /* what trigger word are we using? */
|
---|
44 | $keyword = $tpom->get(org_tubepress_options_category_Advanced::KEYWORD);
|
---|
45 |
|
---|
46 | if (!$this->somethingToParse($content, $keyword)) {
|
---|
47 | return;
|
---|
48 | }
|
---|
49 |
|
---|
50 | $customOptions = array();
|
---|
51 |
|
---|
52 | /* Match everything in square brackets after the trigger */
|
---|
53 | $regexp = "\[$keyword(.*)\]";
|
---|
54 | preg_match("/$regexp/", $content, $matches);
|
---|
55 |
|
---|
56 | $tpom->setShortcode($matches[0]);
|
---|
57 |
|
---|
58 | /* Anything matched? */
|
---|
59 | if (!isset($matches[1]) || $matches[1] == "") {
|
---|
60 | $this->_applyOptions($tpom, $customOptions, $mergeWithExistingOptions);
|
---|
61 | return;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /* Break up the options by comma */
|
---|
65 | $pairs = explode(",", $matches[1]);
|
---|
66 |
|
---|
67 | $optionsArray = array();
|
---|
68 | foreach ($pairs as $pair) {
|
---|
69 | $pieces = explode("=", $pair);
|
---|
70 | $pieces[0] = org_tubepress_shortcode_SimpleShortcodeService::_cleanupTagValue($pieces[0]);
|
---|
71 | $pieces[1] = org_tubepress_shortcode_SimpleShortcodeService::_cleanupTagValue($pieces[1]);
|
---|
72 | $customOptions[$pieces[0]] = $pieces[1];
|
---|
73 | }
|
---|
74 |
|
---|
75 | $this->_applyOptions($tpom, $customOptions, $mergeWithExistingOptions);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public function somethingToParse($content, $trigger = "tubepress")
|
---|
79 | {
|
---|
80 | return strpos($content, '[' . $trigger) !== false;
|
---|
81 | }
|
---|
82 |
|
---|
83 | private function _applyOptions($tpom, $opts, $merge)
|
---|
84 | {
|
---|
85 | if ($merge) {
|
---|
86 | $tpom->mergeCustomOptions($opts);
|
---|
87 | } else {
|
---|
88 | $tpom->setCustomOptions($opts);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Tries to strip out any quotes from a tag option name or option value. This
|
---|
94 | * is ugly, ugly, ugly, and it still doesn't work as well as I'd like it to
|
---|
95 | *
|
---|
96 | * @param string &$nameOrValue The raw option name or value
|
---|
97 | *
|
---|
98 | * @return string The cleaned up option name or value
|
---|
99 | */
|
---|
100 | private static function _cleanupTagValue(&$nameOrValue)
|
---|
101 | {
|
---|
102 | $nameOrValue =
|
---|
103 | trim(str_replace(array("“",
|
---|
104 | "”", "’", "‘",
|
---|
105 | "′", "″", """, "'", "\""),"",
|
---|
106 | trim($nameOrValue)));
|
---|
107 |
|
---|
108 | if ($nameOrValue == "true") {
|
---|
109 | return true;
|
---|
110 | }
|
---|
111 | if ($nameOrValue == "false") {
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 | return $nameOrValue;
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|