/** * Pre-process footnote shortcode * * @param array $atts * @param string $content * * @return string */ function shortcodeHandler($atts, $content = '') { global $id; $a = shortcode_atts(array('numbered' => 'yes', 'symbol' => '*', 'suptext' => ' '), $atts); if (!$content) { return ''; } if (!isset($this->footnotes[$id])) { $this->footnotes[$id] = array(); if ($a['numbered'] == 'no') { $this->numbered[$id] = false; } else { $this->numbered[$id] = true; } } $this->footnotes[$id][] = $content; $footnotes = $this->footnotes[$id]; $num = count($footnotes); $numlabel = "{$id}-{$num}"; $retval = '<a class="footnote" title="' . \PressBooks\Sanitize\sanitize_xml_attribute(wp_strip_all_tags($content)) . '" id="return-footnote-' . $numlabel . '" href="#footnote-' . $numlabel . '"><sup class="footnote">['; if ($this->numbered[$id]) { $retval .= $num; } else { $retval .= $a['symbol']; } if (trim($a['suptext'])) { if ($this->numbered[$id]) { $retval .= '. '; } $retval .= $a['suptext']; } $retval .= ']</sup></a>'; return $retval; }
/** * @covers \PressBooks\Sanitize\sanitize_xml_attribute */ public function test_sanitize_xml_attribute() { $var = 'Hello-World!'; $this->assertEquals($var, \PressBooks\Sanitize\sanitize_xml_attribute($var)); $var = "\t <Hello&World!> "; $this->assertEquals('<Hello&World!>', \PressBooks\Sanitize\sanitize_xml_attribute($var)); $var = " te\fst"; $this->assertEquals('test', \PressBooks\Sanitize\sanitize_xml_attribute($var)); }
/** * @param $book_contents * * @return mixed */ protected function preProcessBookContents($book_contents) { // We need to change global $id for shortcodes, the_content, ... global $id; $old_id = $id; // Do root level structures first. foreach ($book_contents as $type => $struct) { if (preg_match('/^__/', $type)) { continue; } // Skip __magic keys foreach ($struct as $i => $val) { if (isset($val['post_content'])) { $id = $val['ID']; $book_contents[$type][$i]['post_content'] = $this->preProcessPostContent($val['post_content']); } if (isset($val['post_title'])) { $book_contents[$type][$i]['post_title'] = \PressBooks\Sanitize\sanitize_xml_attribute($val['post_title']); } if (isset($val['post_name'])) { $book_contents[$type][$i]['post_name'] = $this->preProcessPostName($val['post_name']); } if ('part' == $type) { // Do chapters, which are embedded in part structure foreach ($book_contents[$type][$i]['chapters'] as $j => $val2) { if (isset($val2['post_content'])) { $id = $val2['ID']; $book_contents[$type][$i]['chapters'][$j]['post_content'] = $this->preProcessPostContent($val2['post_content']); } if (isset($val2['post_title'])) { $book_contents[$type][$i]['chapters'][$j]['post_title'] = \PressBooks\Sanitize\sanitize_xml_attribute($val2['post_title']); } if (isset($val2['post_name'])) { $book_contents[$type][$i]['chapters'][$j]['post_name'] = $this->preProcessPostName($val2['post_name']); } } } } } $id = $old_id; return $book_contents; }