Skip to content

Commit dd2e43b

Browse files
add schema support for dataset
1 parent b014b8b commit dd2e43b

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

classes/Visualizer/Module/Frontend.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,54 @@ public function renderChart( $atts ) {
344344
}
345345

346346
// return placeholder div
347-
return $actions_div . '<div id="' . $id . '"' . $class . '></div>';
347+
return $actions_div . '<div id="' . $id . '"' . $class . '></div>' . $this->addSchema( $chart->ID );
348+
}
349+
350+
/**
351+
* Adds the schema corresponding to the dataset.
352+
*
353+
* @since 3.3.2
354+
*
355+
* @access private
356+
*/
357+
private function addSchema( $id ) {
358+
$settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true );
359+
$title = '';
360+
if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) {
361+
$title = $settings['title'];
362+
if ( is_array( $title ) ) {
363+
$title = $settings['title']['text'];
364+
}
365+
}
366+
$title = apply_filters( 'visualizer_schema_name', $title, $id );
367+
if ( empty( $title ) ) {
368+
return '';
369+
}
370+
371+
$desc = '';
372+
if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) {
373+
$desc = $settings['description'];
374+
}
375+
$desc = apply_filters( 'visualizer_schema_description', $desc, $id );
376+
if ( empty( $desc ) || strlen( $desc ) < 50 ) {
377+
return '';
378+
}
379+
380+
$schema = apply_filters(
381+
'visualizer_schema',
382+
'{
383+
"@context":"https://schema.org/",
384+
"@type":"Dataset",
385+
"name":"' . esc_html( $title ) . '",
386+
"description":"' . esc_html( $desc ) . '"
387+
}',
388+
$id
389+
);
390+
391+
if ( empty( $schema ) ) {
392+
return '';
393+
}
394+
395+
return '<script type="application/ld+json">' . $schema . '</script>';
348396
}
349397
}

classes/Visualizer/Render/Sidebar/ChartJS.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ protected function _renderChartTitleSettings() {
161161
'#000'
162162
);
163163

164+
echo '<div class="viz-section-delimiter"></div>';
165+
166+
self::_renderTextAreaItem(
167+
esc_html__( 'Chart Description', 'visualizer' ),
168+
'description',
169+
$this->description,
170+
sprintf( esc_html__( 'Description to display in the structured data schema as explained %1$shere%2$s', 'visualizer' ), '<a href="https://developers.google.com/search/docs/data-types/dataset#dataset" target="_blank">', '</a>' )
171+
);
172+
164173
}
165174

166175
/**

classes/Visualizer/Render/Sidebar/Graph.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ protected function _renderChartTitleSettings() {
122122
$this->_positions,
123123
esc_html__( 'Determines where to place the axis titles, compared to the chart area.', 'visualizer' )
124124
);
125+
126+
echo '<div class="viz-section-delimiter"></div>';
127+
128+
self::_renderTextAreaItem(
129+
esc_html__( 'Chart Description', 'visualizer' ),
130+
'description',
131+
$this->description,
132+
sprintf( esc_html__( 'Description to display in the structured data schema as explained %1$shere%2$s', 'visualizer' ), '<a href="https://developers.google.com/search/docs/data-types/dataset#dataset" target="_blank">', '</a>' )
133+
);
134+
125135
}
126136

127137
/**

classes/Visualizer/Render/Sidebar/Type/DataTable/DataTable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ protected function _renderGeneralSettings() {
136136
$this->title,
137137
esc_html__( 'Text to display in the back-end admin area.', 'visualizer' )
138138
);
139+
140+
echo '<div class="viz-section-delimiter"></div>';
141+
142+
self::_renderTextAreaItem(
143+
esc_html__( 'Chart Description', 'visualizer' ),
144+
'description',
145+
$this->description,
146+
sprintf( esc_html__( 'Description to display in the structured data schema as explained %1$shere%2$s', 'visualizer' ), '<a href="https://developers.google.com/search/docs/data-types/dataset#dataset" target="_blank">', '</a>' )
147+
);
148+
139149
self::_renderSectionEnd();
140150
self::_renderGroupEnd();
141151
}

0 commit comments

Comments
 (0)