*/ function pepvote_get_embed_pages() { return array( 'voting' => array( 'label' => __( 'Main voting page', 'pepvote' ), 'path' => '/voting', 'title' => __( 'Pepvote voting page', 'pepvote' ), ), 'candidate-registration' => array( 'label' => __( 'Candidate registration', 'pepvote' ), 'path' => '/candidate/register', 'title' => __( 'Pepvote candidate registration', 'pepvote' ), ), 'voter-registration' => array( 'label' => __( 'Voter registration (elections only)', 'pepvote' ), 'path' => '/voter/register', 'title' => __( 'Pepvote voter registration', 'pepvote' ), ), 'ballot' => array( 'label' => __( 'Ballot preview', 'pepvote' ), 'path' => '/vote/ballot', 'title' => __( 'Pepvote ballot preview', 'pepvote' ), ), 'cast' => array( 'label' => __( 'Cast votes', 'pepvote' ), 'path' => '/vote/cast', 'title' => __( 'Pepvote cast votes', 'pepvote' ), ), 'confirm' => array( 'label' => __( 'Confirm votes', 'pepvote' ), 'path' => '/vote/confirm', 'title' => __( 'Pepvote confirm votes', 'pepvote' ), ), 'result' => array( 'label' => __( 'Results', 'pepvote' ), 'path' => '/vote/result', 'title' => __( 'Pepvote results', 'pepvote' ), ), ); } /** * Turns a voting event code or a pasted Pepvote link into a clean code. * * Accepts values such as "ENWPH8799", " enwph8799 ", or * "https://pepvote.com/voting?votingEventCode=ENWPH8799". * * @param mixed $value Raw code or link. * @return string The code, or an empty string when the value is not usable. */ function pepvote_sanitize_event_code( $value ) { if ( ! is_scalar( $value ) ) { return ''; } $value = trim( (string) $value ); if ( false !== strpos( $value, 'votingEventCode=' ) ) { $query = wp_parse_url( $value, PHP_URL_QUERY ); $args = array(); wp_parse_str( is_string( $query ) ? $query : '', $args ); $value = isset( $args['votingEventCode'] ) && is_string( $args['votingEventCode'] ) ? $args['votingEventCode'] : ''; } $code = strtoupper( (string) preg_replace( '/[^A-Za-z0-9]/', '', $value ) ); $length = strlen( $code ); // Generated codes have 9 characters. The range leaves room for custom codes. return ( $length >= 4 && $length <= 32 ) ? $code : ''; } /** * Returns a known page key, or the main voting page for unknown values. * * @param mixed $page Raw page key. * @return string */ function pepvote_sanitize_page( $page ) { $page = is_string( $page ) ? sanitize_key( $page ) : ''; $pages = pepvote_get_embed_pages(); return isset( $pages[ $page ] ) ? $page : 'voting'; } /** * Clamps a height value to the allowed range. * * @param mixed $height Raw height. * @return int */ function pepvote_sanitize_height( $height ) { $height = absint( $height ); if ( 0 === $height ) { return PEPVOTE_DEFAULT_HEIGHT; } return min( PEPVOTE_MAX_HEIGHT, max( PEPVOTE_MIN_HEIGHT, $height ) ); } /** * Builds the public Pepvote URL for a voting event page. * * @param string $code Sanitized voting event code. * @param string $page Sanitized page key. * @return string */ function pepvote_get_embed_url( $code, $page ) { $pages = pepvote_get_embed_pages(); $page = pepvote_sanitize_page( $page ); return add_query_arg( array( 'votingEventCode' => rawurlencode( $code ) ), pepvote_get_base_url() . $pages[ $page ]['path'] ); } /** * Registers the small front-end script that resizes embeds to fit their content. */ function pepvote_register_scripts() { wp_register_script( 'pepvote-embed', plugins_url( 'assets/js/embed.js', PEPVOTE_PLUGIN_FILE ), array(), PEPVOTE_VERSION, array( 'in_footer' => true, 'strategy' => 'defer', ) ); } add_action( 'init', 'pepvote_register_scripts' ); /** * Renders the iframe embed. * * @param array $args { * Embed options. * * @type string $code Voting event code or Pepvote link. * @type string $page Page key from pepvote_get_embed_pages(). * @type int $height Iframe height in pixels. Used as the starting height with auto height. * @type string $title Accessible iframe title. Defaults to the page title. * @type bool $auto_height Whether the iframe grows and shrinks to fit the Pepvote page. * } * @param string $wrapper_attributes Optional HTML attributes for the wrapper, already escaped. * @return string */ function pepvote_render_embed( $args, $wrapper_attributes = '' ) { $args = wp_parse_args( $args, array( 'code' => '', 'page' => 'voting', 'height' => PEPVOTE_DEFAULT_HEIGHT, 'title' => '', 'auto_height' => true, ) ); $code = pepvote_sanitize_event_code( $args['code'] ); if ( '' === $code ) { if ( ! current_user_can( 'edit_posts' ) ) { return ''; } return sprintf( '

%s

', esc_html__( 'Pepvote: add a valid voting event code to show this embed. Only editors can see this message.', 'pepvote' ) ); } $pages = pepvote_get_embed_pages(); $page = pepvote_sanitize_page( $args['page'] ); $height = pepvote_sanitize_height( $args['height'] ); $title = trim( sanitize_text_field( (string) $args['title'] ) ); $title = '' !== $title ? $title : $pages[ $page ]['title']; $auto_height = filter_var( $args['auto_height'], FILTER_VALIDATE_BOOLEAN ); if ( $auto_height ) { wp_enqueue_script( 'pepvote-embed' ); } if ( '' === $wrapper_attributes ) { $wrapper_attributes = 'class="pepvote-embed"'; } return sprintf( '
', $wrapper_attributes, esc_url( pepvote_get_embed_url( $code, $page ) ), esc_attr( $title ), $height, $auto_height ? ' data-pepvote-auto-height' : '' ); }