Skip to content

Commit a70b0f8

Browse files
Merge pull request #548 from contactashish13/issue-399
add schema support for dataset
2 parents b014b8b + 70761c2 commit a70b0f8

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

classes/Visualizer/Module/Frontend.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,55 @@ 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+
// descriptions below 50 chars are not allowed.
377+
if ( empty( $desc ) || strlen( $desc ) < 50 ) {
378+
return '';
379+
}
380+
381+
$schema = apply_filters(
382+
'visualizer_schema',
383+
'{
384+
"@context":"https://schema.org/",
385+
"@type":"Dataset",
386+
"name":"' . esc_html( $title ) . '",
387+
"description":"' . esc_html( $desc ) . '"
388+
}',
389+
$id
390+
);
391+
392+
if ( empty( $schema ) ) {
393+
return '';
394+
}
395+
396+
return '<script type="application/ld+json">' . $schema . '</script>';
348397
}
349398
}

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)