Skip to content

Commit 22b48bb

Browse files
authored
Merge pull request #27 from INN/22-23-custom-classnames
22 23 custom classnames
2 parents 7763d3f + 2c351d5 commit 22b48bb

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
## All options:
1111

1212
```
13-
[pym src="" pymsrc="" pymoptions="" id="" ]
13+
[pym src="" pymsrc="" pymoptions="" id="" class="" ]
1414
```
1515

1616
`src` is the URL of the page that is to be embedded.
@@ -34,5 +34,15 @@ To do the same thing with this Pym shortcode, you would write:
3434
For example, the shortcode `[pym src="http://blog.apps.npr.org/pym.js/examples/graphic/child.html" id="extremely_specific_id"]` results in the following output:
3535

3636
```html
37-
<div id="extremely_specific_example"></div><script src="http://example.org/wp-content/plugins/pym-shortcode/js/pym.v1.min.js"></script><script>var pym_0 = new pym.Parent('extremely_specific_example', 'http://blog.apps.npr.org/pym.js/examples/graphic/child.html', {})</script>
37+
<div id="extremely_specific_example" class="pym"></div><script src="http://example.org/wp-content/plugins/pym-shortcode/js/pym.v1.min.js"></script><script>var pym_0 = new pym.Parent('extremely_specific_example', 'http://blog.apps.npr.org/pym.js/examples/graphic/child.html', {})</script>
3838
```
39+
40+
`class` is optional; this should be a valid HTML class name. It will be added to the element's default class, `'pym'`. You would want to use this if, for example, you wanted to [use a size-based class name to determine the size of the embed on your site](https://github.com/INN/pym-shortcode/issues/23). The class `'pym'` will always be output on container elements created by the Pym Shortcode. This class was introduced in version 1.2.2.
41+
42+
For example, the shortcode `[pym src="http://blog.apps.npr.org/pym.js/examples/graphic/child.html" class="one two three four float-left mw_50"]` results in the following output:
43+
44+
```html
45+
<div id="pym_0" class="pym one two three four float-left mw_50"></div><script src="http://insideenergy.dev/wp-content/plugins/pym-shortcode/js/pym.v1.min.js"></script><script>var pym_0 = new pym.Parent('pym_0', 'http://blog.apps.npr.org/pym.js/examples/graphic/child.html', {})</script>
46+
```
47+
48+
If you do not want the class `'pym'` output on container elements, [add a filter](https://codex.wordpress.org/Plugin_API/Filter_Reference) to the hook `pym_shortcode_default_class` that returns an empty string.

pym-shortcode.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,68 @@
1717

1818
/**
1919
* A shortcode to simplify the process of embedding articles using pym.js
20+
*
21+
* @param Array $atts the attributes passed in the shortcode.
22+
* @param String $content the enclosed content; should be empty for this shortcode.
23+
* @param String $tag the shortcode tag.
2024
*/
21-
function pym_shortcode( $atts, $context, $tag ) {
25+
function pym_shortcode( $atts, $content, $tag ) {
2226

23-
// generate an ID for this embed, necessary to prevent conflicts
27+
// generate an ID for this embed; necessary to prevent conflicts.
2428
global $pym_id;
2529
if ( ! isset( $pym_id ) ) {
2630
$pym_id = 0;
2731
} else {
2832
++$pym_id;
2933
}
3034

35+
// Set us up the vars.
3136
$pymsrc = empty( $atts['pymsrc'] ) ? plugins_url( '/js/pym.v1.min.js', __FILE__ ) : $atts['pymsrc'];
3237
$pymoptions = empty( $atts['pymoptions'] ) ? '' : $atts['pymoptions'];
3338
$id = empty( $atts['id'] ) ? '' : esc_attr( $atts['id'] );
3439
$actual_id = empty( $id ) ? 'pym_' . $pym_id : $id;
3540

41+
/**
42+
* Filter pym_shortcode_default_class allows setting the default class on embeds
43+
*
44+
* @param String $default
45+
* @return String the default class name
46+
*/
47+
$default_class = apply_filters( 'pym_shortcode_default_class', 'pym' );
48+
$class = empty( $atts['class'] ) ? '' : esc_attr( $atts['class'] );
49+
$actual_classes = $default_class . ' ' . $class;
50+
3651
$src = $atts['src'];
3752

3853
ob_start();
3954

4055
printf(
41-
'<div id="%1$s"></div>',
42-
$actual_id
56+
'<div id="%1$s" class="%2$s"></div>',
57+
esc_attr( $actual_id ),
58+
esc_attr( $actual_classes )
4359
);
4460

4561
// If this is the first one on the page, output the pym src
4662
// or if the pymsrc is set, output that.
4763
if ( 0 === $pym_id || ! empty( $atts['pymsrc'] ) ) {
4864
echo sprintf(
4965
'<script src="%s"></script>',
50-
$pymsrc
66+
esc_attr( $pymsrc )
5167
);
5268
}
5369

54-
// Output the parent's scripts
70+
// Output the parent's scripts.
5571
echo '<script>';
5672
echo sprintf(
5773
'var pym_%1$s = new pym.Parent(\'%2$s\', \'%3$s\', {%4$s})',
58-
$pym_id,
59-
$actual_id,
60-
$src,
74+
esc_js( $pym_id ),
75+
esc_js( $actual_id ),
76+
esc_js( $src ),
6177
$pymoptions
6278
);
6379
echo '</script>';
6480

65-
// What is output to the page
81+
// What is output to the page:
6682
$ret = ob_get_clean();
6783
return $ret;
6884
}

readme.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Mobile view of the WordPress post with the NPR embed using Pym Shortcode:
4949
* Update to pym.js version 1.2.2: https://github.com/nprapps/pym.js/releases/tag/v1.2.2 (Changelog at https://github.com/nprapps/pym.js/blob/master/CHANGELOG )
5050
* (we skipped pym.js version 1.2.1: https://github.com/nprapps/pym.js/releases/tag/v1.2.1 )
5151
* Add `id=""` attribute to allow setting custom IDs on embeds. [#21](https://github.com/INN/pym-shortcode/issues/21)
52+
* Add `class=""` attribute to allow setting custom classes on embeds. [#22](https://github.com/INN/pym-shortcode/issues/22) and [#23](https://github.com/INN/pym-shortcode/issues/23).
53+
* Add a default class name `pym` to all embed-containing div elements output by this plugin, and a filter 'pym_shortcode_default_class' to allow changing it.
5254

5355
= 1.2.0.2 =
5456

0 commit comments

Comments
 (0)