CM Glossary Tooltip Integration Question

Hi all,

Now that we’ve figured out the filtering custom post types problem we were having, we’ve proceeded to do some further testing with the CM Glossary Tooltip plugin along side of Pressbooks. So far, the glossary term creation works as expected, and Pressbooks now allows for the automatic creation of a dynamic glossary index page, and child pages for each individual glossary entry, which is all good. See: https://wisc-dev.pb.unizin.org/portuguesparaprincipiantes/glossary/, for a very minimal example.

Here’s the issue I’m running into now: This glossary index is published at rooturl/glossary-2 and doesn’t appear in the Pressbooks ToC. I’m wondering whether there’d be a way for us to automatically set a glossary index page produced via this plugin as a back matter -> glossary ‘chapter’ and have a link to it automatically added to the book’s ToC? @ned do you have any thoughts on this?

Another thing that occurs to me–we’ll also want to ensure that these glossary entries are included in the WXR exports for the books, so that glossaries (when produced) are included in cloned or imported copies of books. I believe the CM Tooltip plugin offers some support for importing/exporting glossaries, but I’ll have to check on this tomorrow.

See: https://wisc-dev.pb.unizin.org/portuguesparaprincipiantes/glossary/4,

That’s pretty cool.

we’ll also want to ensure that these glossary entries are included in the WXR exports for the books

That data will be in the exported WXR (everything gets exported). Assuming the post has HTML it’s a matter of tweaking the importer not the exporter. The current importer uses the same pb_import_custom_post_types filter so it should be fine?

doesn’t appear in the Pressbooks ToC.

Currently, Toc is very hardcoded to PB types. It also informs navigation. I don’t think this is easy to change.

I don’t want to be the “back to the drawing board” person but Pressbooks back-matter has a “Glossary” type. Could you not generate your glossary into that kind of post?

Dac–thanks. This all makes good sense. I’ll check to see what happens when we export/import a book with glossary entries shortly. As for your last question–yes, I think that’s exactly what we want to do. Generating the plugin’s glossary index as a PB ‘Back Matter’ glossary type would be perfect. I just don’t know how we’d go about doing that …

Something like?

$id = 0; // Set to zero for insert, or some existing post id to update
$html = "<p>Insert your glossary html code here.</p>";
$data = [
	'post_title' => 'Glossary',
	'post_content' => $html,
	'post_type' => 'back-matter',
];
$post_id = wp_insert_post( $data );
wp_set_object_terms( $post_id, 'glossary', 'back-matter-type' );

More info:

1 Like

Thanks @dac.chartrand! I’ll play with this a bit next week when I’m back in the office and try to post an update shortly.

Ok, @dac.chartrand I’m getting post of this, but am still a little bit confused. Here’s what the plugin does to create its glossary page:

 /**
    * Create the actual glossary
    * @param type $content
    * @return string
    */
    public static function cmtt_glossary_createList( $content ) {
            $currentPost    = get_post();
            $glossaryPageID = get_option( 'cmtt_glossaryID' );
            if ( is_numeric( $glossaryPageID ) && is_page( $glossaryPageID ) && $glossaryPageID > 0 && $currentPost && $currentPost->ID == $glossaryPageID ) {
                $content                              = self::cmtt_glossaryShowList( $content );
                $removeGlossaryIndexFilterAfterOutput = (get_option( 'cmtt_removeGlossaryCreateListFilter', 0 ) == 1);
                if ( $removeGlossaryIndexFilterAfterOutput ) {
                    remove_filter( 'the_content', array( self::$calledClassName, 'cmtt_glossary_createList' ), 9998 );
                }
            }
            return $content;
        }
/**
 * Displays the main glossary index
*
* @global type $removeLinksToTerms
* @global boolean $isMainGlossaryPage
* @global type $glossary_RequiredJSData
* @param type $content
* @param type $shortcode
* @return string $content
 */
    public static function cmtt_glossaryShowList( $content = '', $shortcode = false ) {
        global $removeLinksToTerms, $isMainGlossaryPage, $glossary_RequiredJSData, $post;

        /*
         * Store the value if it's the main Glossary Index
         */
        $isMainGlossaryPage = true;

        $glossary_list_id = 'glossaryList' . ($shortcode || isset( $_POST[ 'isshortcode' ] ) ? '_' . $_POST[ "gcat_id" ] : '');

        $content .= '<div id="' . $glossary_list_id . '-nav" class="listNav" role="tablist"></div>';
        $content .= '<div class="glossary-container">';

        $args = array(
            'post_type'      => 'glossary',
            'post_status'    => 'publish',
            'orderby'        => 'title',
            'order'          => 'ASC',
            'posts_per_page' => 500
        );

        ob_start();
        ?>
        <div class="glossary_top_filter">
            <?php echo apply_filters( 'cmtt_glossary_index_additional_filters_html', '' ); ?>
        </div>
        <?php
        $content .= ob_get_clean();

        $glossary_query = new WP_Query( $args );
        $glossary_index = $glossary_query->get_posts();

        if ( empty( $glossary_index ) ) {
            $content.= '<span class="error">Nothing found. Please change search parameters.</span>';
        }

        if ( $glossary_index ) {
            $glossary_RequiredJSData[ 'list_id' ] = $glossary_list_id;

            $content .= '<ul class="glossaryList" role="tabpanel" id="' . $glossary_list_id . '">';

            /*
             * Style links based on option
             */
            $glossary_style = (get_option( 'cmtt_glossaryDiffLinkClass' ) == 1) ? 'glossaryLinkMain' : 'glossaryLink';
            if ( $removeLinksToTerms ) {
                $tag = 'span';
            } else {
                $tag = 'a';
            }

            foreach ( $glossary_index as $glossary_item ) {
                $glossaryItemContentBase = (get_option( 'cmtt_glossaryExcerptHover' ) && $glossary_item->post_excerpt) ? $glossary_item->post_excerpt : $glossary_item->post_content;
                $glossaryItemContent     = self::cmtt_glossary_filterTooltipContent( $glossaryItemContentBase, get_permalink( $glossary_item ) );

                $glossaryItemDesc = '';

                if ( $removeLinksToTerms ) {
                    $href = '';
                } else {
                    $href = 'href="' . get_permalink( $glossary_item ) . '"';
                }

                /*
                 * Add filter to change the tooltip content on both Glossary Index and Post/Pages
                 */
                $glossaryItemContent = apply_filters( 'cmtt_tooltip_content_add', $glossaryItemContent, $glossary_item );

                /*
                 * Add filter to change the tooltip content only on the Glossary Index page
                 */
                $glossaryItemContent = apply_filters( 'cmtt_glossary_index_tooltip_content_add', $glossaryItemContent, $glossary_item );

                $preItemTitleContent  = '';
                $postItemTitleContent = '';

                $preItemTitleContent .= '<li>';

                /*
                 * Start the internal tag: span or a
                 */
                $preItemTitleContent .= '<' . $tag . ' class="' . $glossary_style . '" ' . $href . ' ';

                /*
                 * Add tooltip if needed (general setting enabled and page not excluded from plugin)
                 */
                $preItemTitleContent .= (get_option( 'cmtt_glossaryTooltip' ) == 1) ? 'data-cmtooltip="' . $glossaryItemContent . '"' : '';
                $preItemTitleContent .= '>';

                /*
                 * Add filter to change the content of what's before the glossary item title on the list
                 */
                $preItemTitleContent = apply_filters( 'cmtt_glossaryPreItemTitleContent_add', $preItemTitleContent, $glossary_item );

                /*
                 * Insert post title here later on
                 */
                $postItemTitleContent .= '</' . $tag . '>';
                /*
                 * Add description if needed
                 */
                $postItemTitleContent .= $glossaryItemDesc;
                $postItemTitleContent .= '</li>';

                $content .= $preItemTitleContent . $glossary_item->post_title . $postItemTitleContent;
            }
            $content .= '</ul>';
        }

        if ( get_option( 'cmtt_glossaryListTiles' ) == 1 ) {
            $content = '<div class="tiles">' . $content . '<p class="clear"></p></div>';
        }

        $content .= '</div>';

        $content = apply_filters( 'cmtt_glossary_index_after_content', $content );

        $authorUrl = do_shortcode( '[cminds_free_author id="cmtt"]' );
        $content .= (get_option( 'cmtt_glossaryReferral' ) == 1 && get_option( 'cmtt_glossaryAffiliateCode' )) ? self::cmtt_getReferralSnippet() : $authorUrl;
        return $content;
    }

I think what I want to do is to use wp_set_object_terms to apply ‘glossary’ from the ‘back-matter-type’ taxonomy to the $content variable instantiated and set by the cmtt_glossary_createList() function, right? What else would I need to do to help this glossary act/behave like a PB back matter glossary type?

Both these function return HTML strings. Assuming this plugin is loaded, you could just do:

$html = CLASS::cmtt_glossaryShowList();

Where CLASS is the name of the class not included in your snippet. Then use $html like in my example above.

This is way beyond modifying pb_import_custom_post_types though. These are big code dumps from a plugin that does it’s own thing, that I have never used. There will be a lot of work to do to get this working. The main idea is that the Pressbooks TOC requires PB post types and I am brainstorming around that.

Hope this helps.